From: Jan Olszak Date: Tue, 17 Mar 2015 14:09:45 +0000 (+0100) Subject: Support for uints X-Git-Url: http://review.tizen.org/git/?a=commitdiff_plain;h=b42b5de4531dc24829c3ca0215c3b2a13b484cbb;p=archive%2Fplatform%2Fcore%2Fsystem%2FlibConfig.git Support for uints [Bug/Feature] N/A [Cause] N/A [Solution] N/A [Verification] Run tests under valgrind Change-Id: I40b0ea4e61d98d4b41c1bc2dff4a0e1c5aa89b07 --- diff --git a/src/config/from-gvariant-visitor.hpp b/src/config/from-gvariant-visitor.hpp index 4a0330a..0992a68 100644 --- a/src/config/from-gvariant-visitor.hpp +++ b/src/config/from-gvariant-visitor.hpp @@ -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); diff --git a/src/config/from-json-visitor.hpp b/src/config/from-json-visitor.hpp index 41b09cd..d07cbc2 100644 --- a/src/config/from-json-visitor.hpp +++ b/src/config/from-json-visitor.hpp @@ -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(value64); + value = static_cast(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(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(value64); + } + static void fromJsonObject(json_object* object, bool& value) { checkType(object, json_type_boolean); diff --git a/src/config/from-kvjson-visitor.hpp b/src/config/from-kvjson-visitor.hpp index 6b2f6a6..fa68b4b 100644 --- a/src/config/from-kvjson-visitor.hpp +++ b/src/config/from-kvjson-visitor.hpp @@ -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(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(value64); + } + static void fromJsonObject(json_object* object, bool& value) { checkType(object, json_type_boolean); diff --git a/src/config/to-gvariant-visitor.hpp b/src/config/to-gvariant-visitor.hpp index e8476fc..d495711 100644 --- a/src/config/to-gvariant-visitor.hpp +++ b/src/config/to-gvariant-visitor.hpp @@ -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()); }; diff --git a/src/config/to-json-visitor.hpp b/src/config/to-json-visitor.hpp index 85f8a16..70f619b 100644 --- a/src/config/to-json-visitor.hpp +++ b/src/config/to-json-visitor.hpp @@ -26,6 +26,7 @@ #define CONFIG_TO_JSON_VISITOR_HPP #include "config/is-visitable.hpp" +#include "config/exception.hpp" #include #include @@ -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);