X-Git-Url: http://review.tizen.org/git/?a=blobdiff_plain;f=dali-scene-loader%2Finternal%2Fjson-reader.h;h=4fb6f7a7f299943dadf89e940b20731b8f54a9a4;hb=f26914807b617e08646c399209c0b42c3d99baba;hp=2f769a7f5b0c30f7518c1e5b02a0d48c0e2e696d;hpb=7a315fe9869206bf6ad3195931f27d88fe9c128e;p=platform%2Fcore%2Fuifw%2Fdali-toolkit.git diff --git a/dali-scene-loader/internal/json-reader.h b/dali-scene-loader/internal/json-reader.h index 2f769a7..4fb6f7a 100644 --- a/dali-scene-loader/internal/json-reader.h +++ b/dali-scene-loader/internal/json-reader.h @@ -1,7 +1,7 @@ #ifndef DALI_SCENE_LOADER_JSON_READER_H_ #define DALI_SCENE_LOADER_JSON_READER_H_ /* - * Copyright (c) 2020 Samsung Electronics Co., Ltd. + * Copyright (c) 2021 Samsung Electronics Co., Ltd. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -21,17 +21,16 @@ #include "dali-scene-loader/third-party/json.h" // EXTERNAL INCLUDES -#include "dali/public-api/common/vector-wrapper.h" #include -#include -#include -#include #include +#include #include +#include +#include +#include "dali/public-api/common/vector-wrapper.h" namespace json { - /** * @brief Helper for freeing the memory allocated by json_parse() */ @@ -60,9 +59,8 @@ int StrCmp(const json_string_s& js, const std::string& s); * @brief Convenience function to compare json_string_s and other supported string type, * in swapped order. */ -template -inline -int StrCmp(String& s, const json_string_s& js) +template +inline int StrCmp(String& s, const json_string_s& js) { return -StrCmp(js, s); } @@ -77,28 +75,33 @@ namespace detail /** * @brief Compile-time type-enum mapping. */ -template +template struct Type2Enum -{}; - -#define TYPE2ENUM(x) template<> struct Type2Enum\ -{\ - enum { VALUE = json_type_## x };\ +{ }; +#define TYPE2ENUM(x) \ + template<> \ + struct Type2Enum \ + { \ + enum \ + { \ + VALUE = json_type_##x \ + }; \ + }; + TYPE2ENUM(object) TYPE2ENUM(array) TYPE2ENUM(string) TYPE2ENUM(number) #undef TYPE2ENUM -} +} // namespace detail /** * @brief Casts the payload of a json_value_s to the given type. */ -template -inline -const Out& Cast(const json_value_s& j) +template +inline const Out& Cast(const json_value_s& j) { Validate(j, static_cast(detail::Type2Enum::type>::VALUE)); return *static_cast(j.payload); @@ -108,9 +111,8 @@ const Out& Cast(const json_value_s& j) * @brief Casts the payload of a json_value_s to the given type. * @note std::runtime_error is thrown if the value is not the given type. */ -template -inline -Out& Cast(json_value_s& j) +template +inline Out& Cast(json_value_s& j) { Validate(j, static_cast(detail::Type2Enum::type>::VALUE)); return *static_cast(j.payload); @@ -129,11 +131,11 @@ struct Read { static bool Boolean(const json_value_s& j) { - if (j.type == json_type_true) + if(j.type == json_type_true) { return true; } - else if (j.type == json_type_false) + else if(j.type == json_type_false) { return false; } @@ -143,25 +145,25 @@ struct Read } } - template + template static T Number(const json_value_s& j) { - auto& jn = Cast(j); + auto& jn = Cast(j); std::stringstream ss; - for (auto i0 = jn.number, i1 = i0 + jn.number_size; i0 != i1; ++i0) + for(auto i0 = jn.number, i1 = i0 + jn.number_size; i0 != i1; ++i0) { ss.put(*i0); } T result; - if (ss >> result) + if(ss >> result) { return result; } throw std::runtime_error("Failed to convert value to number"); } - template + template static E Enum(const json_value_s& j) { size_t number = Number(j); @@ -180,14 +182,14 @@ struct Read return std::string(js.string, js.string_size); } - template + template static std::vector Array(const json_value_s& j) { - auto& ja = Cast(j); + auto& ja = Cast(j); std::vector result; result.reserve(ja.length); auto i = ja.start; - while (i) + while(i) { result.push_back(std::move(readElement(*i->value))); i = i->next; @@ -204,9 +206,13 @@ struct PropertyCore protected: explicit PropertyCore(const std::string& key) : mKey(key) - {} + { + } - const std::string& GetKey() const { return mKey; } + const std::string& GetKey() const + { + return mKey; + } private: std::string mKey; @@ -215,17 +221,19 @@ private: /** * @brief Base class for the properties of a type T. */ -template +template struct PropertyBase : PropertyCore { using PropertyCore::GetKey; explicit PropertyBase(const std::string& key) : PropertyCore(key) - {} + { + } virtual ~PropertyBase() - {} + { + } virtual void Read(const json_value_s& j, T& obj) = 0; }; @@ -233,28 +241,31 @@ struct PropertyBase : PropertyCore /** * @brief Concrete property of an object to read into from JSON with a given function. */ -template +template struct Property : PropertyBase { - using ReadFn = U(*)(const json_value_s&); - using MemberPtr = U T::*; + using ReadFn = U (*)(const json_value_s&); + using MemberPtr = U T::*; using SetterArgType = typename std::conditional::type; - using Setter = void (T::*)(SetterArgType); + using Setter = void (T::*)(SetterArgType); Property(const std::string& key, ReadFn read, MemberPtr ptr) : PropertyBase(key), mRead(read), mAccessor(new DirectAccessor(ptr)) - {} + { + } Property(const std::string& key, ReadFn read, Setter setter) : PropertyBase(key), mRead(read), mAccessor(new SetterAccessor(setter)) - {} + { + } ~Property() - {} + { + } void Read(const json_value_s& j, T& obj) override { @@ -265,7 +276,8 @@ private: struct AccessorBase { virtual ~AccessorBase() - {} + { + } virtual void Set(U value, T& obj) const = 0; }; @@ -274,7 +286,8 @@ private: { DirectAccessor(MemberPtr ptr) : mPointer(ptr) - {} + { + } void Set(U value, T& obj) const override { @@ -288,7 +301,8 @@ private: { SetterAccessor(Setter setter) : mSetter(setter) - {} + { + } void Set(U value, T& obj) const override { @@ -298,16 +312,15 @@ private: Setter mSetter; }; - ReadFn mRead; + ReadFn mRead; std::unique_ptr mAccessor; }; /** * @brief Helper function to make a Property for a member of type U, of object of type T. */ -template -Property* MakeProperty(const std::string& key, typename Property::ReadFn readFn, - U T::* ptr) +template +Property* MakeProperty(const std::string& key, typename Property::ReadFn readFn, U T::*ptr) { return new Property(key, readFn, ptr); } @@ -333,7 +346,7 @@ protected: * @brief Object Reader template for reading into an object of a given type, * with properties registered for the various members. */ -template +template class Reader : protected ReaderCore { public: @@ -347,7 +360,7 @@ public: ~Reader() { - for (auto& p : mProperties) + for(auto& p : mProperties) { delete Cast(p); } @@ -355,9 +368,8 @@ public: Reader& Register(PropertyBase& prop) { - auto iInsert = std::lower_bound(mProperties.begin(), mProperties.end(), &prop, - SortPredicate); - if (iInsert == mProperties.end() || Cast(*iInsert)->GetKey() != prop.GetKey()) + auto iInsert = std::lower_bound(mProperties.begin(), mProperties.end(), &prop, SortPredicate); + if(iInsert == mProperties.end() || Cast(*iInsert)->GetKey() != prop.GetKey()) { mProperties.insert(iInsert, &prop); } @@ -372,14 +384,13 @@ public: void Read(const json_object_s& jo, T& obj) const { auto i = jo.start; - while (i) + while(i) { - auto iFind = std::lower_bound(mProperties.begin(), mProperties.end(), - *i->name, FindPredicate); - if (iFind != mProperties.end()) + auto iFind = std::lower_bound(mProperties.begin(), mProperties.end(), *i->name, FindPredicate); + if(iFind != mProperties.end()) { auto prop = Cast(*iFind); - if (0 == StrCmp(*i->name, prop->GetKey())) + if(0 == StrCmp(*i->name, prop->GetKey())) { prop->Read(*i->value, obj); } @@ -411,29 +422,29 @@ private: * @brief Wraps a Reader in a function usable as a Property::ReadFn, i.e. to facilitate * deserializing structures of nested objects. */ -template +template struct ObjectReader { static const Reader* sReader; static T Read(const json_value_s& j) { - T result; + T result; auto& jo = Cast(j); sReader->Read(jo, result); return result; } }; -template +template const Reader* ObjectReader::sReader = nullptr; -template +template void SetObjectReader(const Reader& r) { ObjectReader::sReader = &r; } -} // json +} // namespace json #endif //DALI_SCENE_LOADER_JSON_READER_H_