Example C++ API Core Cursor Application
Schema Compilation
The schema compile process creates generated source files necessary for your application program to access the database defined by the schema.
Generating C++ program interface files:
rdm-compile --cxx-api cpp01Example.sdl
The C++ API interface generates three files, cpp01Example_structs.h, which contains the definition of the row in the INFO table as a C-struct as well as the other identifiers necessary to communicate with this database via the Core Cursor C++ API. It also generates cpp01Example_gen.cpp, cpp01Example_gen_api.h and cpp01Example_cat.h, which define the C++ class, methods and catalog for accessing the database defined by the schema.
Include Files Needed
The necessary files that need to be included for this application are:
Source for cpp01Example.cpp (helloworld)
The complete source for this example HelloWorld application can found in shared/RDM/GettingStarted/cpp/cpp01Example directory.
/*
* HELLO WORLD CPP
* ---------------
*
* This document describes the process to create a simple database,
* insert a record containing a text field, read the text field from
* database and print it out.
*/
#include <iostream>
#include <cstring>
#include <cstdlib>
#include "cpp-tfs.h" // The RDM TFS class
#include "cpp-exception.h" // The RDM exception class
#include "cpp-transaction.h" // The RDM transaction class
#include "cpp01Example_gen_api.h" // The cpp01Example database API.
#include "rdmstartupapi.h"
using namespace RDM_CPP; // RDM Class
using namespace RDM_CPP::CPP01EXAMPLE; // RDM cpp01Example Db Class
static void hello_worldCPP (void)
{
TFS tfs;
Db_cpp01Example db;
RDM_TABLE_ID tables[] = {TABLE_INFO};
try
{
// Allocate RDM_TFS handle
tfs = TFS::Alloc ("");
// Allocate RDM_DB handle
db = tfs.AllocDatabase ();
// Open database
db.Open ("cpp01");
} catch (rdm_exception)
{
std::cerr << "I can't open the cpp01Example database." << std::endl;
throw;
}
/* Start an update transaction and lock the table */
db.StartUpdate (tables, RDM_LEN (tables));
try
{
/* Remove all of the rows from the database */
db.DeleteAllRows ();
/* Commit the transaction */
db.End ();
} catch (rdm_exception)
{
/* Rollback the transaction */
db.EndRollback ();
throw;
}
/* Start an update transaction and lock the table */
db.StartUpdate (tables, RDM_LEN (tables));
try
{
INFO infoInserted;
/* Insert a row into the table */
strcpy (
infoInserted.mychar, "Hello World CPP! - using the embedded TFS");
db.Insert_info_Row (infoInserted);
/* Commit a transaction */
db.End ();
} catch (rdm_exception)
{
/* Rollback the transaction */
db.EndRollback ();
throw;
}
/* Start a read transaction and lock the table */
db.StartRead (tables, RDM_LEN (tables));
try
{
Cursor_info cursor;
INFO infoRead;
/* Fill a cursor with all the rows in a table */
cursor = db.Get_info_Rows ();
/* Move to the first row in the info table */
cursor.MoveToFirst ();
/* Read the full content of the current record */
cursor.ReadRow (infoRead);
std::cout << infoRead.mychar << std::endl;
/* Free the read lock on the table */
db.End ();
} catch (rdm_exception)
{
/* Rollback the transaction */
db.EndRollback ();
throw;
}
}
RDM_C_EXPORT int32_t main_cpp01Example (
int32_t /*argc*/,
const char *const * /*argv*/)
{
try
{
hello_worldCPP ();
} catch (const rdm_exception &e)
{
std::cerr << "There was an error in this Tutorial (" << e.GetEnum ()
<< ", " << e.GetDescription () << ")" << std::endl;
return EXIT_FAILURE;
}
return EXIT_SUCCESS;
}
RDM_STARTUP_EXAMPLE (cpp01Example)