diff Resources/CodeGeneration/template.in.h.j2 @ 628:84af39146e76 am-dev

CodeGeneration: support default values
author Alain Mazy <alain@mazy.be>
date Wed, 08 May 2019 16:32:57 +0200
parents 8432926e9db9
children 5dd496343fad
line wrap: on
line diff
--- a/Resources/CodeGeneration/template.in.h.j2	Wed May 08 10:51:41 2019 +0200
+++ b/Resources/CodeGeneration/template.in.h.j2	Wed May 08 16:32:57 2019 +0200
@@ -29,7 +29,10 @@
   /** Throws in case of problem */
   inline void _StoneDeserializeValue(int32_t& destValue, const Json::Value& jsonValue)
   {
-    destValue = jsonValue.asInt();
+    if (!jsonValue.isNull())
+    {
+      destValue = jsonValue.asInt();
+    }
   }
 
   inline Json::Value _StoneSerializeValue(int32_t value)
@@ -40,7 +43,10 @@
 
   inline void _StoneDeserializeValue(int64_t& destValue, const Json::Value& jsonValue)
   {
-    destValue = jsonValue.asInt64();
+    if (!jsonValue.isNull())
+    {
+      destValue = jsonValue.asInt64();
+    }
   }
 
   inline Json::Value _StoneSerializeValue(int64_t value)
@@ -51,7 +57,10 @@
 
   inline void _StoneDeserializeValue(uint32_t& destValue, const Json::Value& jsonValue)
   {
-    destValue = jsonValue.asUInt();
+    if (!jsonValue.isNull())
+    {
+      destValue = jsonValue.asUInt();
+    }
   }
 
   inline Json::Value _StoneSerializeValue(uint32_t value)
@@ -62,7 +71,10 @@
 
   inline void _StoneDeserializeValue(uint64_t& destValue, const Json::Value& jsonValue)
   {
-    destValue = jsonValue.asUInt64();
+    if (!jsonValue.isNull())
+    {
+      destValue = jsonValue.asUInt64();
+    }
   }
 
   inline Json::Value _StoneSerializeValue(uint64_t value)
@@ -84,7 +96,10 @@
   /** Throws in case of problem */
   inline void _StoneDeserializeValue(double& destValue, const Json::Value& jsonValue)
   {
-    destValue = jsonValue.asDouble();
+    if (!jsonValue.isNull())
+    {
+      destValue = jsonValue.asDouble();
+    }
   }
 
   inline Json::Value _StoneSerializeValue(double value)
@@ -96,7 +111,10 @@
   /** Throws in case of problem */
   inline void _StoneDeserializeValue(bool& destValue, const Json::Value& jsonValue)
   {
-    destValue = jsonValue.asBool();
+    if (!jsonValue.isNull())
+    {
+      destValue = jsonValue.asBool();
+    }
   }
 
   inline Json::Value _StoneSerializeValue(bool value)
@@ -110,7 +128,10 @@
        std::string& destValue
      , const Json::Value& jsonValue)
   {
-    destValue = jsonValue.asString();
+    if (!jsonValue.isNull())
+    {
+      destValue = jsonValue.asString();
+    }
   }
 
   inline Json::Value _StoneSerializeValue(const std::string& value)
@@ -151,19 +172,22 @@
   void _StoneDeserializeValue(
     std::map<std::string, T>& destValue, const Json::Value& jsonValue)
   {
-    destValue.clear();
-    for (
-      Json::Value::const_iterator itr = jsonValue.begin();
-      itr != jsonValue.end();
-      itr++)
+    if (!jsonValue.isNull())
     {
-      std::string key;
-      _StoneDeserializeValue(key, itr.key());
+      destValue.clear();
+      for (
+        Json::Value::const_iterator itr = jsonValue.begin();
+        itr != jsonValue.end();
+        itr++)
+      {
+        std::string key;
+        _StoneDeserializeValue(key, itr.key());
 
-      T innerDestValue;
-      _StoneDeserializeValue(innerDestValue, *itr);
+        T innerDestValue;
+        _StoneDeserializeValue(innerDestValue, *itr);
 
-      destValue[key] = innerDestValue;
+        destValue[key] = innerDestValue;
+      }
     }
   }
 
@@ -200,13 +224,16 @@
   void _StoneDeserializeValue(
     std::vector<T>& destValue, const Json::Value& jsonValue)
   {
-    destValue.clear();
-    destValue.reserve(jsonValue.size());
-    for (Json::Value::ArrayIndex i = 0; i != jsonValue.size(); i++)
+    if (!jsonValue.isNull())
     {
-      T innerDestValue;
-      _StoneDeserializeValue(innerDestValue, jsonValue[i]);
-      destValue.push_back(innerDestValue);
+      destValue.clear();
+      destValue.reserve(jsonValue.size());
+      for (Json::Value::ArrayIndex i = 0; i != jsonValue.size(); i++)
+      {
+        T innerDestValue;
+        _StoneDeserializeValue(innerDestValue, jsonValue[i]);
+        destValue.push_back(innerDestValue);
+      }
     }
   }
 
@@ -238,12 +265,15 @@
   void _StoneDeserializeValue(
     std::set<T>& destValue, const Json::Value& jsonValue)
   {
-    destValue.clear();
-    for (Json::Value::ArrayIndex i = 0; i != jsonValue.size(); i++)
+    if (!jsonValue.isNull())
     {
-      T innerDestValue;
-      _StoneDeserializeValue(innerDestValue, jsonValue[i]);
-      destValue.insert(innerDestValue);
+      destValue.clear();
+      for (Json::Value::ArrayIndex i = 0; i != jsonValue.size(); i++)
+      {
+        T innerDestValue;
+        _StoneDeserializeValue(innerDestValue, jsonValue[i]);
+        destValue.insert(innerDestValue);
+      }
     }
   }
 
@@ -335,7 +365,10 @@
   inline void _StoneDeserializeValue(
     {{enum['name']}}& destValue, const Json::Value& jsonValue)
   {
-    FromString(destValue, jsonValue.asString());
+    if (!jsonValue.isNull())
+    {
+      FromString(destValue, jsonValue.asString());
+    }
   }
 
   inline Json::Value _StoneSerializeValue(const {{enum['name']}}& value)
@@ -361,9 +394,9 @@
 
   struct {{struct['name']}}
   {
-{% if struct %}{% if struct['fields'] %}{% for key in struct['fields']%}    {{CanonToCpp(struct['fields'][key])}} {{key}};
+{% if struct %}{% if struct['fields'] %}{% for key in struct['fields']%}    {{CanonToCpp(struct['fields'][key]['type'])}} {{key}};
 {% endfor %}{% endif %}{% endif %}
-    {{struct['name']}}({% if struct %}{% if struct['fields'] %}{% for key in struct['fields']%}{{CanonToCpp(struct['fields'][key])}} {{key}} = {{CanonToCpp(struct['fields'][key])}}(){{ ", " if not loop.last }}{% endfor %}{% endif %}{% endif %})
+    {{struct['name']}}({% if struct %}{% if struct['fields'] %}{% for key in struct['fields']%}{{CanonToCpp(struct['fields'][key]['type'])}} {{key}} = {% if struct['fields'][key]['defaultValue'] %}{{DefaultValueToCpp(rootName,enums,struct['fields'][key])}} {%else%} {{CanonToCpp(struct['fields'][key]['type'])}}() {%endif%} {{ ", " if not loop.last }}{% endfor %}{% endif %}{% endif %})
     {
 {% if struct %}{% if struct['fields'] %}{% for key in struct['fields']%}      this->{{key}} = {{key}};
 {% endfor %}{% endif %}{% endif %}    }
@@ -371,8 +404,11 @@
 
   inline void _StoneDeserializeValue({{struct['name']}}& destValue, const Json::Value& value)
   {
-{% if struct %}{% if struct['fields'] %}{% for key in struct['fields']%}    _StoneDeserializeValue(destValue.{{key}}, value["{{key}}"]);
+    if (!value.isNull())
+    {
+{% if struct %}{% if struct['fields'] %}{% for key in struct['fields']%}      _StoneDeserializeValue(destValue.{{key}}, value["{{key}}"]);
 {% endfor %}{% endif %}{% endif %}    }
+  }
 
   inline Json::Value _StoneSerializeValue(const {{struct['name']}}& value)
   {