Commit Graph

4 Commits

Author SHA1 Message Date
Asias He
88e7dcfa86 Remove redundant const in static constexpr const
From http://en.cppreference.com/w/cpp/language/constexpr:

   A constexpr specifier used in an object declaration implies const.

However, We can not change from
   static constexpr const char* TIME_FORMAT = "%a %b %d %I:%M:%S %Z %Y";
to
   static constexpr char* TIME_FORMAT = "%a %b %d %I:%M:%S %Z %Y";

The compiler complains:

   In file included from json/formatter.cc:22:0:
   json/formatter.hh:132:42: error: deprecated conversion from string
   constant to ‘char*’ [-Werror=write-strings]
        static constexpr char* TIME_FORMAT = "%a %b %d %I:%M:%S %Z %Y";

Since, unlike const, constexpr does not modify a type. It just applies
to an object (or function), and incidentally implies const to the
top-level type.
2015-05-25 11:57:19 +03:00
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
Amnon Heiman
b93dcf3dbd json adding the swagger code generation
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>
2015-03-30 15:38:41 +03:00
Amnon Heiman
1866091b3d Adding basic json support
This is a migration of the basic json support taken from the
osv/httpserver.

The routes uses the json object to return errors in a json format.

This also adds json_exception which has a constructor from an exception.

Signed-off-by: Amnon Heiman <amnon@cloudius-systems.com>
2015-03-08 21:55:57 +02:00