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;
gc["two"] = true;
gc["3"] = 1.4;
gc["four"] = "pippo";
gc["five"].set_vec_int(10);
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
compile and link with GenericContainerInterface_lua.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
compile and link with GenericContainerInterface_matlab.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 "mex.h"
#include <sstream>
extern "C"
void
mexFunction( int nlhs, mxArray *plhs[],
int nrhs, mxArray const *prhs[] ) {
try {
GenericContainer gc;
}
catch ( std::exception & exc ) {
mexPrintf("Error: %s\n", exc.what() );
}
catch (...) {
mexPrintf("Unknown error\n");
}
}
}
Definition GenericContainer.cc:70
void mexPrint(GenericContainer const &gc)
void mxArray_to_GenericContainer(mxArray const *mx, GenericContainer &gc)
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 GenericContainerInterface_matlab.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 ]