diff options
author | rtk0c <[email protected]> | 2022-05-24 21:47:55 -0700 |
---|---|---|
committer | rtk0c <[email protected]> | 2022-05-24 21:47:55 -0700 |
commit | d18a28a9659092952aef70a30a47726e7c16d31a (patch) | |
tree | b1d6304e631961a797225912ed1ef7ee55864f86 /source/RapidJsonHelper.hpp | |
parent | 123f741e3f5374b93ac39887b62bfa0d66762ae2 (diff) |
Changeset: 38 Branch comment: [] Add infrastructure for codegen
Diffstat (limited to 'source/RapidJsonHelper.hpp')
-rw-r--r-- | source/RapidJsonHelper.hpp | 110 |
1 files changed, 0 insertions, 110 deletions
diff --git a/source/RapidJsonHelper.hpp b/source/RapidJsonHelper.hpp deleted file mode 100644 index 75cd93a..0000000 --- a/source/RapidJsonHelper.hpp +++ /dev/null @@ -1,110 +0,0 @@ -#pragma once - -#include <rapidjson/document.h> -#include <cstring> -#include <string> -#include <string_view> - -#define BRUSSEL_JSON_GET(object, name, type, out, failAction) \ - { \ - auto it = (object).FindMember(name); \ - if (it == (object).MemberEnd()) failAction; \ - auto& value = it->value; \ - if (!value.Is<type>()) failAction; \ - (out) = value.Get<type>(); \ - } - -#define BRUSSEL_JSON_GET_DEFAULT(object, name, type, out, theDefault) \ - do { \ - auto it = (object).FindMember(name); \ - if (it == (object).MemberEnd()) { \ - (out) = theDefault; \ - break; \ - } \ - auto& value = it->value; \ - if (!value.Is<type>()) { \ - (out) = theDefault; \ - break; \ - } \ - (out) = value.Get<type>(); \ - } while (0); - -namespace rapidjson { - -inline const Value* GetProperty(const Value& value, std::string_view name) { - for (auto it = value.MemberBegin(); it != value.MemberEnd(); ++it) { - if (it->name.GetStringLength() != name.size()) continue; - if (std::memcmp(it->name.GetString(), name.data(), name.size())) continue; - - return &it->value; - } - return nullptr; -} - -inline const Value* GetProperty(const Value& value, Type type, std::string_view name) { - for (auto it = value.MemberBegin(); it != value.MemberEnd(); ++it) { - if (it->name.GetStringLength() != name.size()) continue; - if (it->value.GetType() != type) continue; - if (std::memcmp(it->name.GetString(), name.data(), name.size())) continue; - - return &it->value; - } - return nullptr; -} - -inline std::string_view AsStringView(const Value& value) { - return std::string_view(value.GetString(), value.GetStringLength()); -} - -inline std::string_view AsStringView(const GenericStringRef<char>& strRef) { - return std::string_view(strRef.s, strRef.length); -} - -inline std::string AsString(const Value& value) { - return std::string(value.GetString(), value.GetStringLength()); -} - -inline std::string AsString(const GenericStringRef<char>& strRef) { - return std::string(strRef.s, strRef.length); -} - -// RapidJson itself already provides std::string and const char* overloads -inline GenericStringRef<char> StringRef(std::string_view str) { - return GenericStringRef<char>( - str.data() ? str.data() : "", - str.size()); -} - -template <class TIter, class TSentienl> -rapidjson::Value WriteVectorPrimitives(rapidjson::Document& root, TIter begin, TSentienl end) { - using TElement = typename TIter::value_type; - - rapidjson::Value list; - while (begin != end) { - if constexpr (std::is_same_v<TElement, std::string>) { - auto& elm = *begin; - list.PushBack(rapidjson::Value(elm.c_str(), elm.size()), root.GetAllocator()); - } else { - list.PushBack(*begin, root.GetAllocator()); - } - ++begin; - } - return list; -} - -template <class TContainer> -bool ReadVectorPrimitives(const rapidjson::Value& value, TContainer& list) { - using TElement = typename TContainer::value_type; - - if (!value.IsArray()) return false; - - list.reserve(value.Size()); - for (auto& elm : value.GetArray()) { - if (!elm.Is<TElement>()) return {}; - list.push_back(elm.Get<TElement>()); - } - - return true; -} - -} // namespace rapidjson |