Files
scylladb/json/json2code.py
Amnon Heiman c552fdb8b3 json2code Enhence the enum_wrapper
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>
2015-05-19 10:08:12 +03:00

18 KiB
Executable File