Generic Container

GenericContainer is a C++ class that permit to store eterogeneous data:

Supported types

pointer

boolean

integer

long integer

floating point

complex floating point

string

vector of pointer

vector of boolean

vector of integer

vector of floating point

vector of complex floating point

vector of string

matrix of floating point

matrix of complex floating point

in addition to this data type the following two container data are avaiable

  • vector of GenericContainer

  • map of GenericContainer

this permits to build complex recursive data. The main usage of the class is in interchange data with scripting language like Ruby, Lua, MATLAB.

Simple Usage

The usage is simple, for example it can be used as an associative array with eterogenous data

GenericContainer gc;
gc["one"]  = 1;             // store integer
gc["two"]  = true;          // store a boolean
gc["3"]    = 1.4;           // store floating point number
gc["four"] = "pippo";       // store a string
gc["five"].set_vec_int(10); // store a vector of integer of 10 elements

and to retrieve elements

cout << gc["one"].get_int()     << '\n';
cout << gc["two"].get_bool()    << '\n';
cout << gc["3"].get_real()      << '\n';
cout << gc["four"].get_string() << '\n';
GenericContainer::vec_int_type & v = gc["five"].get_vec_int();
cout << v[1] << '\n';

For more complex emxamples and recursive data see example test files in the distribution.

Compile and tests

Edit makefile file to match compiler of your OS and do:

make # using make (only linux/OSX or mingw)

rake build_osx   # compile for OSX
rake build_linux # compile for linux
rake build_win   # compile for WINDOWS

To run the test

make run

rake run       # for linux/OSX
rake run_win   # for windows

Lua Support

GenericContainer has an interfacing to exchange data with Lua. To use the interface include

#include "GenericContainerLuaInterface.hh"

compile and link with GenericContainerLuaInterface.cc. The interface contains a set of functions to convert from GenericContainer to Lua global variables and the other way around.

Matlab Support

GenericContainer has an interfacing to exchange data with matlab. To use the interface include

#include "GenericContainerMatlabInterface.hh"

compile and link with GenericContainerMatlabInterface.cc. The interface contains a set of functions to convert from GenericContainer to mxArray and the other way around.

The following code stored in mex_print_recursive.cc

#include "GenericContainer.hh"
#include "GenericContainerMatlabInterface.hh"
#include "mex.h"

#include <sstream>

namespace GC_namespace {

  extern "C"
  void
  mexFunction( int nlhs, mxArray       *plhs[],
               int nrhs, mxArray const *prhs[] ) {
    try {
      GenericContainer gc;
      mxArray_to_GenericContainer( prhs[0], gc );
      mexPrint(gc);
    }
    catch ( std::exception & exc ) {
      mexPrintf("Error: %s\n", exc.what() );
    }
    catch (...) {
      mexPrintf("Unknown error\n");
    }
  }
}

Implement a mex command that print a Matlab structure recursively on the console after the conversion to a mxArray_to_GenericContainer. After the compilation

> mex mex_print_recursive.cc GenericContainerMatlabInterface.cc -output print_recursive

Produce the Matlab command print_recursive so that the following MATLAB code:

S         = [ 1 0 2 9; 0 0 2 3; 2 0 0 0; 1 0 -2 -2 ];
S1        = [ 1 0 2 9; 0 0 2 3; 2+1i 0 0 0; 1 0 -2 -2 ];
A.vector  = [1,2,3,4];
A.string  = 'pippo';
A.strings = { 'pippo', 'pluto', 'paperino' };
A.struct1  = { 'paperino', [1 2], [1 2; 3 5] };
A.struct2  = { B, sparse(S), sparse(S1) };

print_recursive( A );

has the following output:

string: "pippo"
strings:
    0: "pippo"
    1: "pluto"
    2: "paperino"
struct1:
    0: "paperino"
    1: [ 1 2 ]
    2:
       1        2
       3        5
struct2:
    0:
        fieldA:
            0: 1
            1: 2
            2: 3
            3: "pippo"
        fieldB:
            [ 1 1 2 ]
        fieldC: "stringa"
    1:
        ir:
            [ 0 2 3 0 1 3 0 1 3 ]
        jc:
            [ 0 3 3 6 9 ]
        values:
            [ 1 2 1 2 2 -2 9 3 -2 ]
    2:
        ir:
            [ 0 2 3 0 1 3 0 1 3 ]
        jc:
            [ 0 3 3 6 9 ]
        values:
            [ (1, 0 ) (2, 1 ) (1, 0 ) (2, 0 ) (2, 0 ) (-2, 0 ) (9, 0 ) (3, 0 ) (-2, 0 ) ]
vector:
    [ 1 2 3 4 ]

Interfaces

Author

Enrico Bertolazzi
Dipartimento di Ingegneria Industriale
Università degli Studi di Trento
email:

License

BSD 2-Clause License

Copyright (c) 2016, Enrico Bertolazzi
All rights reserved.

Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions are met:

1. Redistributions of source code must retain the above copyright notice, this
   list of conditions and the following disclaimer.

2. Redistributions in binary form must reproduce the above copyright notice,
   this list of conditions and the following disclaimer in the documentation
   and/or other materials provided with the distribution.

THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.