C++ Demo

This sample demonstrates the use of the -hpp schema compiler option to generate the C++ interface to a simple "Parts" database.

The project makefile, or the custom build step in Visual Studio, demonstrates the use of -hpp option on the mcocomp command line:

 
    \host\bin\mcocomp.exe  -hpp cppdemo.mco
     

This causes the schema compiler to generate the file cppdemo.hpp containing the implementation of classes Part, MyPart and index classes ByCode and ByType.

How to Run

See instructions here on how to build this and other samples on your development platform. Once built, the sample can be run from the \target\bin directory:

 
    sequences_api_cpp
     

But it is more instructive to run it in your development environment debugger, stepping through the code.

Pertinent code

The schema is shown below with snippets of the application code written to take advantage of the generated class interface implemented in cppdemo.hpp:

 
    Schema snippet:
     
    struct Dimensions
    {
        float width, length, height;
    };
     
    class Part
    {
        string      type;
        string      name;
        float       price;
        unsigned<4> code;
        Dimensions  dim;
         
        hash< code >        ByCode [ 10000 ];
        tree< type, name >  ByType; // ordered by type, name
    };
     
    Application snippet:
     
    class MyPart: public Part
    {
        public:
        MCO_RET stype_put(const char* s)
        {
            return type_put(s, (uint2)strlen(s) + 1);
        }
         
        MCO_RET sname_put(const char* s)
        {
            return name_put(s, (uint2)strlen(s) + 1);
        }
         
        void print(mco_trans_h t);
         
        const char* name()
        {
            static char bf[200];
            uint2 len;
            name_get(bf, sizeof(bf), len);
            bf[len] = 0;
            return bf;
        }
         
        const char* type()
        {
            static char bf[200];
            uint2 len;
            type_get(bf, sizeof(bf), len);
            bf[len] = 0;
            return bf;
        }
    };
     
    void MyPart::print(mco_trans_h t)
    {
        if (!is_valid())
        {
            return ;
        }
 
        uint4 code;
        code_get(code);
        printf("\n[%d] %s : %s\n", code, name(), type());
     
        Dimensions dim;
        dim_read(dim);
        float w, h, l;
        dim.width_get(w);
        dim.length_get(l);
        dim.height_get(h);
        printf("%g x %g x %g \n", l, w, h);
    }
     
    ...
 
    MCO_RET populate_db(mco_db_h db)
    {
        mco_trans_h t = 0;
        MCO_RET rc = MCO_S_OK;
        int j;
         
        for (j = 0; j < 1000 && MCO_S_OK == rc; j++)
        {
            MyPart part;
            Dimensions dim;
            char temp[200];
             
            rc = mco_trans_start(db, MCO_READ_WRITE, MCO_TRANS_FOREGROUND, &t);
            if ( MCO_S_OK == rc ) {
                part.create(t);
                part.stype_put(i2s(j % 100));
                sprintf(temp, "part # %d", j);
                part.sname_put(temp);
                part.price_put((float)(2.0 + j % 100 / 200.0));
                part.code_put(1000000+j);
                part.dim_write(dim);
                dim.height_put((float)rand2(20, 40));
                dim.width_put((float)rand2(20, 50));
                dim.length_put((float)rand2(35, 90));
                rc = mco_trans_commit(t);
            }
        }
         
        printf("Inserted %d parts to the database\n", j);
        return rc;
    }