Taking the example (static),
class nested_typ : public complex_var
{
public:
COMPLEXCONST(nested_typ)
generic_var<float> f1;
generic_var<double> d1;
};
class var_test : public rm_var
{
public:
RMCONST(TEST_VAR,1)
generic_var<int> header;
nested_typ value;
generic_var<char> c1;
generic_var<char> c2;
generic_var<int> a[15];
};
var_test test;
The code to dynamically constuct a test object is as follows,
rm_var *test = new rm_var("TEST_VAR",1);
new generic_var<int>;
new complex_var;
new generic_var<float>;
new generic_var<double>;
mpt_var::pop();
new generic_var<char>;
new generic_var<char>;
new generic_var<int>[15];
mpt_var::pop();
We can assign a value to test.value.d1, for example,
test.value.d1 = 30.5;
We can also do this using reflection, which would work for both static and dynamic forms,
mpt_var *root = &test; /* Use this for static example */
mpt_var *root = test; /* Use this for dynamic example */
/* We cast root to a complex_var */
complex_var *cptr = static_cast<complex_var *>(root);
/* We get the value element, which is the 2nd element */
mpt_var *mptr = (*cptr)[1];
/* We cast it to a complex_var since we know it is really a nested_typ */
cptr = static_cast<complex_var *>(mptr);
/* We get the d1 element, which is the 2nd element */
mptr = (*cptr)[1];
/* We cast it to a generic_var<double> since we know that is what it is */
*static_cast<generic_var<double>*>(mptr) = 30.5;
The Data Monitor will need to allow the user to navigate the tree of test--->value--->d1, and work out the dynamic access mechanism from the XML.
Bearing in mind that the construction of an object is in the following order,
(1) Constructor of parent class is called
(2) Construction of each member variable
(3) Constructor of the class is called
Therefore, in the static case, during construction of nested_typ,
(1) The constructor for complex_var is called, note that it push().
(2) Member variables are constructed, and since the pointer of the object is at the top of the composite stack – createlist, it will be added as a child
(3) The macros COMPLEXCONST and RMCONST all pop().
push and pop must be matched. At the end of the construction of all variables, the stack – createlist – must be empty. Otherwise, the entire reflection mechanism will breakdown.