Support for uints 57/36957/2
authorJan Olszak <j.olszak@samsung.com>
Tue, 17 Mar 2015 14:09:45 +0000 (15:09 +0100)
committerJan Olszak <j.olszak@samsung.com>
Tue, 17 Mar 2015 14:40:04 +0000 (15:40 +0100)
[Bug/Feature]   N/A
[Cause]         N/A
[Solution]      N/A
[Verification]  Run tests under valgrind

Change-Id: I40b0ea4e61d98d4b41c1bc2dff4a0e1c5aa89b07

src/config/from-gvariant-visitor.hpp
src/config/from-json-visitor.hpp
src/config/from-kvjson-visitor.hpp
src/config/to-gvariant-visitor.hpp
src/config/to-json-visitor.hpp

index 4a0330a..0992a68 100644 (file)
@@ -84,7 +84,7 @@ private:
         }
     }
 
-    static void fromGVariant(GVariant* object, int& value)
+    static void fromGVariant(GVariant* object, std::int32_t& value)
     {
         checkType(object, G_VARIANT_TYPE_INT32);
         value = g_variant_get_int32(object);
@@ -96,6 +96,18 @@ private:
         value = g_variant_get_int64(object);
     }
 
+    static void fromGVariant(GVariant* object, std::uint32_t& value)
+    {
+        checkType(object, G_VARIANT_TYPE_UINT32);
+        value = g_variant_get_uint32(object);
+    }
+
+    static void fromGVariant(GVariant* object, std::uint64_t& value)
+    {
+        checkType(object, G_VARIANT_TYPE_UINT64);
+        value = g_variant_get_uint64(object);
+    }
+
     static void fromGVariant(GVariant* object, bool& value)
     {
         checkType(object, G_VARIANT_TYPE_BOOLEAN);
index 41b09cd..d07cbc2 100644 (file)
@@ -83,14 +83,24 @@ private:
         }
     }
 
-    static void fromJsonObject(json_object* object, int& value)
+    static void fromJsonObject(json_object* object, std::int32_t& value)
     {
         checkType(object, json_type_int);
         std::int64_t value64 = json_object_get_int64(object);
         if (value64 > INT32_MAX || value64 < INT32_MIN) {
             throw ConfigException("Value out of range");
         }
-        value = static_cast<int>(value64);
+        value = static_cast<std::int32_t>(value64);
+    }
+
+    static void fromJsonObject(json_object* object, std::uint32_t& value)
+    {
+        checkType(object, json_type_int);
+        std::int64_t value64 = json_object_get_int64(object);
+        if (value64 > UINT32_MAX || value64 < 0) {
+            throw ConfigException("Value out of range");
+        }
+        value = static_cast<std::uint32_t>(value64);
     }
 
     static void fromJsonObject(json_object* object, std::int64_t& value)
@@ -99,6 +109,16 @@ private:
         value = json_object_get_int64(object);
     }
 
+    static void fromJsonObject(json_object* object, std::uint64_t& value)
+    {
+        checkType(object, json_type_int);
+        std::int64_t value64 = json_object_get_int64(object);
+        if (value64 < 0) {
+            throw ConfigException("Value out of range");
+        }
+        value = static_cast<std::uint64_t>(value64);
+    }
+
     static void fromJsonObject(json_object* object, bool& value)
     {
         checkType(object, json_type_boolean);
index 6b2f6a6..fa68b4b 100644 (file)
@@ -235,6 +235,26 @@ private:
         value = json_object_get_int64(object);
     }
 
+    static void fromJsonObject(json_object* object, std::uint32_t& value)
+    {
+        checkType(object, json_type_int);
+        std::int64_t value64 = json_object_get_int64(object);
+        if (value64 > UINT32_MAX || value64 < 0) {
+            throw ConfigException("Value out of range");
+        }
+        value = static_cast<std::uint32_t>(value64);
+    }
+
+    static void fromJsonObject(json_object* object, std::uint64_t& value)
+    {
+        checkType(object, json_type_int);
+        std::int64_t value64 = json_object_get_int64(object);
+        if (value64 < 0) {
+            throw ConfigException("Value out of range");
+        }
+        value = static_cast<std::uint64_t>(value64);
+    }
+
     static void fromJsonObject(json_object* object, bool& value)
     {
         checkType(object, json_type_boolean);
index e8476fc..d495711 100644 (file)
@@ -77,6 +77,8 @@ private:
 
     void writeInternal(std::int32_t value) { add("i", value); };
     void writeInternal(std::int64_t value) { add("x", value); };
+    void writeInternal(std::uint32_t value) { add("u", value); };
+    void writeInternal(std::uint64_t value) { add("t", value); };
     void writeInternal(bool value) { add("b", value); };
     void writeInternal(double value) { add("d", value); };
     void writeInternal(const std::string& value) { add("s", value.c_str()); };
index 85f8a16..70f619b 100644 (file)
@@ -26,6 +26,7 @@
 #define CONFIG_TO_JSON_VISITOR_HPP
 
 #include "config/is-visitable.hpp"
+#include "config/exception.hpp"
 
 #include <json/json.h>
 #include <string>
@@ -74,7 +75,7 @@ private:
         return ret;
     }
 
-    static json_object* toJsonObject(int value)
+    static json_object* toJsonObject(std::int32_t value)
     {
         return json_object_new_int(value);
     }
@@ -84,6 +85,22 @@ private:
         return json_object_new_int64(value);
     }
 
+    static json_object* toJsonObject(std::uint32_t value)
+    {
+        if (value > INT32_MAX) {
+            throw ConfigException("Value out of range");
+        }
+        return json_object_new_int(value);
+    }
+
+    static json_object* toJsonObject(std::uint64_t value)
+    {
+        if (value > INT64_MAX) {
+            throw ConfigException("Value out of range");
+        }
+        return json_object_new_int64(value);
+    }
+
     static json_object* toJsonObject(bool value)
     {
         return json_object_new_boolean(value);