The code generation generate an enum_wrapper for the enum that defined
in the swagger definition file.
This patch adds more functionality to the enum_wrapper:
It is now possible to cast the enum to other enum if wrapped enum is a
subset of the target enum, this is a reverse functionality of
constructing an enum from another enum.
The wrapper now support comparison operator, begin, end method and the
++ operator, with that the boost::irange is used to return a range
object so the the enum values can be iterate using for range loop.
This is an example of the added methods, After code generation:
template<class T>
operator T() const {
switch(v) {
case my_object_enum_var::VAL1: return T::VAL1;
case my_object_enum_var::VAL2: return T::VAL2;
case my_object_enum_var::VAL3: return T::VAL3;
default: return T::VAL1;
}
}
typedef typename std::underlying_type<my_object_enum_var>::type
pos_type;
enum_var_wrapper& operator++() {
v = static_cast<my_object_enum_var>(static_cast<pos_type>(v) + 1);
return *this;
}
enum_var_wrapper& operator++(int) {
return ++(*this);
}
bool operator==(const enum_var_wrapper& c) const {
return v == c.v;
}
bool operator!=(const enum_var_wrapper& c) const {
return v != c.v;
}
bool operator<=(const enum_var_wrapper& c) const {
return static_cast<pos_type>(v) <= static_cast<pos_type>(c.v);
}
static enum_var_wrapper begin() {
return enum_var_wrapper(my_object_enum_var::VAL1);
}
static enum_var_wrapper end() {
return enum_var_wrapper(my_object_enum_var::NUM_ITEMS);
}
static boost::integer_range<enum_var_wrapper> all_items() {
return boost::irange(begin(), end());
}
This generated code code be found after compilation at:
./build/release/gen/apps/httpd/demo.json.hh
Signed-off-by: Amnon Heiman <amnon@cloudius-systems.com>
This takes the json2code perl script from the osv and pass it with small
adaptation.
It takes a swagger definition file and creates a hh file from it with
the following code support.
Api opperations are translated to path_description with a name that
determine by their nick name.
Moduls are defined as json object with the same name.
Enums are defined as enum class, a string to enum function is defined
for any query enum parameters.
For enums that are part of a json object a conversion function is
defined so enum of a different type can be assigned to the target enum
as long as it has the same enum values.
Signed-off-by: Amnon Heiman <amnon@cloudius-systems.com>