Managing Pseudo Const Members
This chapter will describe how to make a member (pseudo) const .
In the following, we will show the step by step construction of an hypothetic MyClass class.
Concepts
The Flip framework does not support const members inherently, that is declaring, for example, a ohm::flip::Int64 member as const won’t let you chance to assign a value. The member would always be 0 .
To circumvant this limitation, it was introduced the concept of post contruction stage . The post construction stage is called just after a Flip object is created, and before the object can be manipulated by the client. This stage allows to manage pseudo const members in Flip.
Typically pseudo const members have the same access API as normal members, except that they have no setter. To set the actual value of the member, the post construction stage is used, and will be called once by Flip when the object is created.
WARNING:The following technics should only be used for pseudo const members as the use of flip::Args is a common source of bugs, in particular when refactoring code, as no static syntaxic check can be done on the type and number of parameters at compilation time.
Transforming the Code
This section will show how to transform the code for a regular member with its setter/getter/changed members to a pseudo const member.
Declarative Code
The following listing shows the original declarative code, before it is modified to make the member _my_float a pseudo const member.
ListingOriginal declarative source code showing a normal member
#include "ohm/flip/Object.h" |
#include "ohm/flip/Float64.h" |
class MyClass |
: public ohm::flip::Object |
{ |
public: |
static void declare (); |
MyClass (ohm::flip::DocumentBase & document); |
virtual ~MyClass () {} |
void ext_set_my_float (float val); |
float get_my_float () const; |
bool my_float_changed () const; |
private: |
ohm::flip::Float64 _my_float; |
}; |
The following listing shows the modified declarative code, after it is modified to make the member _my_float a pseudo const member : ext_set_my_float was replaced by the virtual method ctor .
ListingModified declarative source code showing a pseudo const member
#include "ohm/flip/Object.h" |
#include "ohm/flip/Float64.h" |
class MyClass |
: public ohm::flip::Object |
{ |
public: |
static void declare (); |
MyClass (ohm::flip::DocumentBase & document); |
virtual ~MyClass () {} |
virtual void ctor (flip::Args & args); |
float get_my_float () const; |
bool my_float_changed () const; |
private: |
ohm::flip::Float64 _my_float; |
}; |
Implementation Code
The following listing shows the original implementation code, before it is modified to make the member _my_float a pseudo const member.
ListingOriginal implementation source code showing a normal member
void MyClass::ext_set_my_float (float val) |
{ |
_my_float = val; |
} |
float MyClass::get_my_float () const |
{ |
return _my_float; |
} |
bool MyClass::my_float_changed () const |
{ |
return _my_float.did_value_change (); |
} |
The following listing shows the modified implementation code, after it is modified to make the member _my_float a pseudo const member : ext_set_my_float was replaced by the virtual method ctor .
ListingModified implementation source code showing a pseudo const member
void MyClass::ctor (flip::Args & args) |
{ |
_my_float.ctor (args); |
} |
float MyClass::get_my_float () const |
{ |
return _my_float; |
} |
bool MyClass::my_float_changed () const |
{ |
return _my_float.did_value_change (); |
} |
Calling Code
The following listing shows the original calling code, before it is modified to make the member _my_float a pseudo const member. The class is always created by the mean of insert into a container.
ListingOriginal calling source code
MyClassColl::iterator MySuperClass::ext_add_element (float val) |
{ |
MyClassColl::iterator it = _class_coll.insert (); |
MyClassColl & my_class = *it; |
my_class.ext_set_my_float (val); |
return it; |
} |
The following listing shows the modified calling code, after it is modified to make the member _my_float a pseudo const member : ext_set_my_float was replaced by the use of a flip::Args object.
ListingModified calling source code
MyClassColl::iterator MySuperClass::ext_add_element (float val) |
{ |
MyClassColl::iterator it = _class_coll.insert ( |
flip::Args ().push (val) |
); |
return it; |
} |
The class flip::Args will behave like a parameter list holder, and acts like a FIFO queue. The exact same number of pushed values must be popped.
Note:flip::Args will assert if the number of popped values from ctor does not exactly match the number and type of the original pushed values.
If the Flip class has one or more base class, the Flip framework code will automatically call the base classes with the same rule as for the constructor of C++ classes.
WARNING:You cannot modify a container in a ctor call.