Using Enums
This chapter will describe the use of flip::Enum with the Flip framework.
In the following, we will show the step by step construction of an hypothetic MyClass class.
This chapter will assume that your company is called ACME making the application “Product”.
Why Use an Enum ?
The Flip framework offers an flip::Enum template class which instantiated type is made over of an enum in the client code.
The flip::Enum type itselfs just holds an integer value, but offers in practice the following additionnal features when compared to flip::Int64 :
- It checks for out of bound values. Any out of bounds value is handled at the Flip level as a structural validation failure and therefore does not need to be implemented in server logical validation code
- Each value of the original enum is mapped to a character string used when the back-end writes the document. This makes the reading of the document a lot easier, in particular for enum that contains a lot of values
Using Enums
In the same way as for Flip classes, Flip enums must be declared. The code itself is very similar to the use of ClassDescription and ClassDescManager and the two classes to use are EnumDescription and EnumDescManager .
Important:An enum must be declared before the concrete type of the enum can be used as a class member
The following listing shows the declaration of the MyClass with an enum
#include "ohm/flip/Object.h" |
#include "ohm/flip/Enum.h" |
class MyClass |
: public ohm::flip::Object |
{ |
public: |
enum MyEnum |
{ |
MyEnum_FOO = 0, |
MyEnum_BAR, |
MyEnum_NBR_ELT |
} |
static void declare (); |
MyClass (ohm::flip::DocumentBase & document); |
virtual ~MyClass () {} |
private: |
ohm::flip::Enum <MyEnum, MyEnum_FOO, MyEnum_NBR_ELT> |
_my_enum; |
}; |
- The first template parameter for Enum is the enum itself
- The second template parameter, MIN , is the minimum value of the enum
- The third template parameter, NBR , indicates the number of value in the enum
When testing the internal integer value for correct range, the following test is made
MIN <= value < MIN + NBR |
That is the value must be in the [MIN; MIN + NBR) range.
The following listing shows the declaration of the Enum to the Flip framework.
void MyClass::declare () |
{ |
using namespace ohm::flip; |
EnumDescription <MyEnum>::use ().set_name ("acme.product.MyClass.MyEnum"); |
EnumDescription <MyEnum>::use ().push_enum_desc (MyEnum_FOO, "MyEnum_FOO"); |
EnumDescription <MyEnum>::use ().push_enum_desc (MyEnum_BAR, "MyEnum_BAR"); |
EnumDescManager::declare (EnumDescription <MyEnum>::use ()); |
} |
After the enum has been declared, the members of the Enum of its type can be declared.