From 97f1dc57cf4e0af0b877d9c1bf0cd47055ee0ae6 Mon Sep 17 00:00:00 2001 From: Martchus Date: Fri, 27 Oct 2017 18:26:36 +0200 Subject: [PATCH] Make use of reserve() and size() if possible --- lib/CMakeLists.txt | 2 +- lib/jsonreflector.h | 61 +++++++++++++++++++++++++++++++++++++-------- 2 files changed, 52 insertions(+), 11 deletions(-) diff --git a/lib/CMakeLists.txt b/lib/CMakeLists.txt index a79198c..d035267 100644 --- a/lib/CMakeLists.txt +++ b/lib/CMakeLists.txt @@ -26,7 +26,7 @@ set(DOC_FILES ) # find c++utilities -find_package(c++utilities 4.6.0 REQUIRED) +find_package(c++utilities 4.11.0 REQUIRED) use_cpp_utilities() # include modules to apply configuration diff --git a/lib/jsonreflector.h b/lib/jsonreflector.h index dde9a77..a27b3ff 100644 --- a/lib/jsonreflector.h +++ b/lib/jsonreflector.h @@ -136,7 +136,22 @@ inline void push(const char *const &reflectable, const char value.AddMember(RAPIDJSON_NAMESPACE::StringRef(name), RAPIDJSON_NAMESPACE::StringRef(reflectable), allocator); } -template , Traits::Not>>...> +template , Traits::Not>, + Traits::Not>>...> +void push( + const Type &reflectable, const char *name, RAPIDJSON_NAMESPACE::Value::Object &value, RAPIDJSON_NAMESPACE::Document::AllocatorType &allocator) +{ + RAPIDJSON_NAMESPACE::Value arrayValue(RAPIDJSON_NAMESPACE::kArrayType); + RAPIDJSON_NAMESPACE::Value::Array array(arrayValue.GetArray()); + for (const auto &item : reflectable) { + push(item, array, allocator); + } + value.AddMember(RAPIDJSON_NAMESPACE::StringRef(name), array, allocator); +} + +template , Traits::HasSize, Traits::Not>>...> void push( const Type &reflectable, const char *name, RAPIDJSON_NAMESPACE::Value::Object &value, RAPIDJSON_NAMESPACE::Document::AllocatorType &allocator) { @@ -206,25 +221,51 @@ template <> inline void pull(std::string &reflectable, const RAPIDJ } template , Traits::Not>>...> +void pull(Type &reflectable, rapidjson::GenericValue>::ConstArray array); + +template , Traits::Not>, + Traits::Not>>...> void pull(Type &reflectable, rapidjson::GenericValue>::ValueIterator &value) { - // clear previous contents of the array (TODO: make this configurable) - reflectable.clear(); - // pull all array elements of the specified value - for (const auto &item : value->GetArray()) { - reflectable.emplace_back(); - pull(reflectable.back(), item); - } + pull(reflectable, value->GetArray()); ++value; } -template , Traits::Not>>...> +template , Traits::IsReservable, Traits::Not>>...> +void pull(Type &reflectable, rapidjson::GenericValue>::ValueIterator &value) +{ + auto array = value->GetArray(); + reflectable.reserve(array.Size()); + pull(reflectable, array); + ++value; +} + +template , Traits::Not>, + Traits::Not>>...> void pull(Type &reflectable, const rapidjson::GenericValue> &value) +{ + pull(reflectable, value.GetArray()); +} + +template , Traits::IsReservable, Traits::Not>>...> +void pull(Type &reflectable, const rapidjson::GenericValue> &value) +{ + auto array = value.GetArray(); + reflectable.reserve(array.Size()); + pull(reflectable, array); +} + +template , Traits::Not>>...> +void pull(Type &reflectable, rapidjson::GenericValue>::ConstArray array) { // clear previous contents of the array (TODO: make this configurable) reflectable.clear(); // pull all array elements of the specified value - for (const auto &item : value.GetArray()) { + for (const rapidjson::GenericValue> &item : array) { reflectable.emplace_back(); pull(reflectable.back(), item); }