Advertisement
ishellstrike

Untitled

Apr 1st, 2015
245
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 7.22 KB | None | 0 0
  1. struct DeserializeHelper {
  2.     template <class T>
  3.     inline static std::pair<const char *, T> make_nvp(const char *name, T &&value) {
  4.         return{name, std::forward<T>(value)};
  5.     }
  6.  
  7. #define NVP(T) DeserializeHelper::make_nvp(#T, T)
  8. #define DESERIALIZE(...) DeserializeHelper::deserialize(val, __VA_ARGS__)
  9.  
  10.     static void deserialize(const rapidjson::Value &val) {
  11.     }
  12.  
  13.     template <typename Last>
  14.     static void deserialize(const rapidjson::Value &val, const Last &last) {
  15.         __deserialize(val, last.first, last.second);
  16.     }
  17.  
  18.     template <typename First, typename... Rest>
  19.     static void deserialize(const rapidjson::Value &val, const First &first, const Rest&... rest) {
  20.         __deserialize(val, first.first, first.second);
  21.         deserialize(val, rest...);
  22.     }
  23.  
  24. private:
  25.     static void __deserialize(const rapidjson::Value &val, const char *s, int &target)
  26.     {
  27.         if(val.HasMember(s))
  28.         {
  29.             if(!val[s].IsInt())
  30.                 throw std::invalid_argument(string_format("value %s is not a integer", s));
  31.             target = val[s].GetInt();
  32.         }
  33.     }
  34.  
  35.     static void __deserialize(const rapidjson::Value &val, const char *s, std::string &target)
  36.     {
  37.         if(val.HasMember(s))
  38.         {
  39.             if(!val[s].IsString())
  40.                 throw std::invalid_argument(string_format("value %s is not a string", s));
  41.             target = val[s].GetString();
  42.         }
  43.     }
  44.  
  45.     static void __deserialize(const rapidjson::Value &val, const char *s, float &target)
  46.     {
  47.         if(val.HasMember(s))
  48.         {
  49.             if(!val[s].IsNumber())
  50.                 throw std::invalid_argument(string_format("value %s is not a number", s));
  51.             target = static_cast<float>(val[s].GetDouble());
  52.         }
  53.     }
  54.  
  55.     static void __deserialize(const rapidjson::Value &val, const char *s, bool &target)
  56.     {
  57.         if(val.HasMember(s))
  58.         {
  59.             if(!val[s].IsBool_())
  60.                 throw std::invalid_argument(string_format("value %s is not a bool", s));
  61.             target = val[s].GetBool_();
  62.         }
  63.     }
  64.  
  65.     static void __deserialize(const rapidjson::Value &val, const char *s, glm::vec2 &target)
  66.     {
  67.         if(val.HasMember(s) && val[s].IsArray())
  68.         {
  69.             const rapidjson::Value &arr = val[s];
  70.             if(arr.Size() != 2)          throw std::invalid_argument(string_format("value %s is not vec2", s));
  71.             if(!arr.Begin()->IsNumber()) throw std::invalid_argument(string_format("value %s[0] is not a number", s));
  72.             if(!arr[1].IsNumber())       throw std::invalid_argument(string_format("value %s[1] is not a number", s));
  73.  
  74.             target.x = static_cast<float>(arr.Begin()->GetDouble());
  75.             target.y = static_cast<float>(arr[1].GetDouble());
  76.         }
  77.     }
  78.  
  79.     static void __deserialize(const rapidjson::Value &val, const char *s, glm::vec3 &target)
  80.     {
  81.         if(val.HasMember(s) && val[s].IsArray())
  82.         {
  83.             const rapidjson::Value &arr = val[s];
  84.             if(arr.Size() != 3)          throw std::invalid_argument("value is not vec3");
  85.             if(!arr.Begin()->IsNumber()) throw std::invalid_argument(string_format("value %s[0] is not a number", s));
  86.             if(!arr[1].IsNumber())       throw std::invalid_argument(string_format("value %s[1] is not a number", s));
  87.             if(!arr[2].IsNumber())       throw std::invalid_argument(string_format("value %s[2] is not a number", s));
  88.  
  89.             target.x = static_cast<float>(arr.Begin()->GetDouble());
  90.             target.y = static_cast<float>(arr[1].GetDouble());
  91.             target.z = static_cast<float>(arr[2].GetDouble());
  92.         }
  93.     }
  94.  
  95.     static void __deserialize(const rapidjson::Value &val, const char *s, glm::vec4 &target)
  96.     {
  97.         if(val.HasMember(s) && val[s].IsArray())
  98.         {
  99.             const rapidjson::Value &arr = val[s];
  100.             if(arr.Size() != 4)          throw std::invalid_argument("target variable is not vec4");
  101.             if(!arr.Begin()->IsNumber()) throw std::invalid_argument(string_format("value %s[0] is not a number", s));
  102.             if(!arr[1].IsNumber())       throw std::invalid_argument(string_format("value %s[1] is not a number", s));
  103.             if(!arr[2].IsNumber())       throw std::invalid_argument(string_format("value %s[2] is not a number", s));
  104.             if(!arr[3].IsNumber())       throw std::invalid_argument(string_format("value %s[3] is not a number", s));
  105.  
  106.             target.x = static_cast<float>(arr.Begin()->GetDouble());
  107.             target.y = static_cast<float>(arr[1].GetDouble());
  108.             target.z = static_cast<float>(arr[2].GetDouble());
  109.             target.w = static_cast<float>(arr[3].GetDouble());
  110.         }
  111.     }
  112.  
  113.     static void __deserialize(const rapidjson::Value &val, const char *s, std::vector<int> &target)
  114.     {
  115.         if(val.HasMember(s))
  116.         {
  117.             const rapidjson::Value &arr = val[s];
  118.             if(!arr.IsArray())
  119.                 throw std::invalid_argument("target variable is not array");
  120.             for(decltype(arr.Size()) i = 0; i < arr.Size(); i++)
  121.             {
  122.                 if(!arr[i].IsInt())
  123.                     throw std::invalid_argument(string_format("value %s[%d] is not a integer", s, i));
  124.                 target.push_back(arr[i].GetInt());
  125.             }
  126.         }
  127.     }
  128.  
  129.     static void __deserialize(const rapidjson::Value &val, const char *s, std::vector<std::string> &target)
  130.     {
  131.         if(val.HasMember(s) && val[s].IsArray())
  132.         {
  133.             const rapidjson::Value &arr = val[s];
  134.             if(!arr.IsArray())
  135.                 throw std::invalid_argument("target variable is not array");
  136.             for(decltype(arr.Size()) i = 0; i < arr.Size(); i++)
  137.             {
  138.                 if(!arr[i].IsString())
  139.                     throw std::invalid_argument(string_format("value %s[%d] is not a string", s, i));
  140.                 target.push_back(arr[i].GetString());
  141.             }
  142.         }
  143.     }
  144.  
  145.     static void __deserialize(const rapidjson::Value &val, const char *s, std::vector<bool> &target)
  146.     {
  147.         if(val.HasMember(s) && val[s].IsArray())
  148.         {
  149.             const rapidjson::Value &arr = val[s];
  150.             if(!arr.IsArray())
  151.                 throw std::invalid_argument("target variable is not array");
  152.             for(decltype(arr.Size()) i = 0; i < arr.Size(); i++)
  153.             {
  154.                 if(!arr[i].IsBool_())
  155.                     throw std::invalid_argument(string_format("value %s[%d] is not a bool", s, i));
  156.                 target.push_back(arr[i].GetBool_());
  157.             }
  158.         }
  159.     }
  160.  
  161.     static void __deserialize(const rapidjson::Value &val, const char *s, std::vector<float> &target)
  162.     {
  163.         if(val.HasMember(s) && val[s].IsArray())
  164.         {
  165.             const rapidjson::Value &arr = val[s];
  166.             if(!arr.IsArray()) throw std::invalid_argument("target variable is not array");
  167.             for(decltype(arr.Size()) i = 0; i < arr.Size(); i++)
  168.             {
  169.                 if(!arr[i].IsDouble())
  170.                     throw std::invalid_argument(string_format("value %s[%d] is not a number", s, i));
  171.                 target.push_back(static_cast<float>(arr[i].GetDouble()));
  172.             }
  173.         }
  174.     }
  175. };
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement