Params.h
Go to the documentation of this file.
00001 #ifndef PARAMS_2128H8YJ
00002 #define PARAMS_2128H8YJ
00003 
00004 #include <boost/lexical_cast.hpp>
00005 #include <map>
00006 #include <string>
00007 
00008 #include <bwi_tools/json/json.h>
00009 
00010 #define SET_FROM_JSON(type,jsonCmd) \
00011   inline void setFromJson(const Json::Value &opts, const char *key, type &val) { \
00012     const Json::Value &opt = opts[key]; \
00013     if (!opt.isNull()) \
00014       val = opt.jsonCmd(); \
00015   }
00016 
00017 #define SET_FROM_JSON_ENUM(ENUM) \
00018   inline void setFromJson(const Json::Value &opts, const char *key, ENUM::type &val) { \
00019     const Json::Value &opt = opts[key]; \
00020     if (!opt.isNull()) \
00021       val = ENUM::fromName(opt.asCString()); \
00022   }
00023 
00024 SET_FROM_JSON(bool,asBool)
00025 SET_FROM_JSON(int,asInt)
00026 SET_FROM_JSON(unsigned int,asUInt)
00027 SET_FROM_JSON(double,asDouble)
00028 SET_FROM_JSON(float,asDouble)
00029 SET_FROM_JSON(std::string,asString)
00030 
00031 #define PARAM_DECL(type,var,key,val) type var;
00032 #define PARAM_INIT(type,var,key,val) var = val;
00033 #define PARAM_SET(type,var,key,val) setFromJson(opts,#key,var);
00034 #define PARAM_OUT(type,var,key,val) os << #var << ": " << p.var << " ";
00035 #define PARAM_MAP(type,var,key,val) stringMap[#var] = boost::lexical_cast<std::string>(var);
00036 
00037 #define Params_STRUCT(params) \
00038   struct Params {\
00039     params(PARAM_DECL) \
00040     \
00041     Params() { \
00042       params(PARAM_INIT) \
00043     } \
00044     \
00045     void fromJson(const Json::Value &opts) { \
00046       (void)opts; /* to remove any compiler warnings if params is empty */ \
00047       params(PARAM_SET) \
00048     } \
00049     std::map<std::string, std::string> asMap() { \
00050       std::map<std::string, std::string> stringMap; \
00051       params(PARAM_MAP) \
00052       return stringMap; \
00053     } \
00054     friend std::ostream& operator<<(std::ostream &os, const Params &p) { \
00055       params(PARAM_OUT) \
00056       return os; \
00057     } \
00058   }; 
00059 
00060 #endif /* end of include guard: PARAMS_2128H8YJ */


bwi_tools
Author(s): Piyush Khandelwal
autogenerated on Fri Aug 28 2015 10:14:30