C++ Smartptr

This sample demonstrates the use of the -smartptr 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 the -smartptr option on the mcocomp command line:

 
    \host\bin\mcocomp.exe  -hpp -smartptr schema.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:

 
    20_languages_cpp_smartptr
     

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. Note the difference between the code in function populate_db() compared to sample 20_languages\cpp.

 
    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.name = (const char *)temp;
                part.price = (float)(2.0 + j % 100 / 200.0);
                part.code = 1000000+j;
                part.dim_write(dim);
                dim.height = (float)rand2(20, 40);
                dim.width = (float)rand2(20, 50);
                dim.length = (float)rand2(35, 90);
                std::vector<uint4> options_arr(Part_options_length);
                for (i = 0; i < Part_options_length; i++)
                {
                    options_arr[i] = rand2(0, 30);
                }
                part.options = options_arr;
            }
        }
         
        printf("Inserted %d parts to the database\n", j);
        return rc;
    }