From 301bd76a2eb27148d35c239dc68f707d8accc604 Mon Sep 17 00:00:00 2001 From: f0reachARR Date: Thu, 26 Sep 2024 14:15:36 +0900 Subject: [PATCH] Rabbit allocator wip --- .../external/rabbit.hpp | 1830 ++++++++++------- .../src/syntax/act.cpp | 2 +- .../src/syntax/condition.cpp | 4 +- .../src/syntax/condition_group.cpp | 2 +- .../src/syntax/event.cpp | 2 +- .../src/syntax/open_scenario.cpp | 2 +- .../src/syntax/private.cpp | 4 +- .../src/syntax/storyboard.cpp | 3 +- 8 files changed, 1062 insertions(+), 787 deletions(-) diff --git a/openscenario/openscenario_interpreter/include/openscenario_interpreter/external/rabbit.hpp b/openscenario/openscenario_interpreter/include/openscenario_interpreter/external/rabbit.hpp index 77245ef4820..87708a12e30 100644 --- a/openscenario/openscenario_interpreter/include/openscenario_interpreter/external/rabbit.hpp +++ b/openscenario/openscenario_interpreter/include/openscenario_interpreter/external/rabbit.hpp @@ -29,25 +29,27 @@ #define RABBIT_NAMESPACE rabbit #endif -#include -#include -#include -#include -#include -#include #include -#include -#include #include +#include +#include + +#include +#include +#include +#include +#include +#include -namespace RABBIT_NAMESPACE { +namespace RABBIT_NAMESPACE +{ -#define RABBIT_TAG_DEF(name, id) \ - struct name \ - { \ +#define RABBIT_TAG_DEF(name, id) \ + struct name \ + { \ static const rapidjson::Type native_value = id; \ - static const int value = id; \ - }; \ + static const int value = id; \ + }; \ /**/ RABBIT_TAG_DEF(null_tag, rapidjson::kNullType) RABBIT_TAG_DEF(false_tag, rapidjson::kFalseType) @@ -61,9 +63,7 @@ RABBIT_TAG_DEF(number_tag, rapidjson::kNumberType) class type_mismatch : public std::runtime_error { public: - type_mismatch(const std::string& msg) - : std::runtime_error(msg) - {} + type_mismatch(const std::string & msg) : std::runtime_error(msg) {} }; typedef rapidjson::ParseErrorCode parse_error_code; @@ -75,543 +75,794 @@ class parse_error : public std::runtime_error public: parse_error(parse_error_code code) - : std::runtime_error(rapidjson::GetParseError_En(code)) - , code_(code) - {} + : std::runtime_error(rapidjson::GetParseError_En(code)), code_(code) + { + } parse_error_code code() const { return code_; } }; // fwd -template class basic_value_ref; -template class basic_value; -template class basic_object; -template class basic_array; +template +class basic_value_ref; +template +class basic_value; +template +class basic_object; +template +class basic_array; -namespace details { +namespace details +{ - template - struct enable_if_c - { - typedef T type; - }; +template +struct enable_if_c +{ + typedef T type; +}; - template - struct enable_if_c - {}; +template +struct enable_if_c +{ +}; - template - struct enable_if : enable_if_c - {}; +template +struct enable_if : enable_if_c +{ +}; - template - struct disable_if : enable_if_c - {}; +template +struct disable_if : enable_if_c +{ +}; +template +struct bool_ +{ + static const bool value = C; +}; - template - struct bool_ - { - static const bool value = C; - }; +typedef bool_ true_; +typedef bool_ false_; + +template +struct remove_reference +{ + typedef T type; +}; +template +struct remove_reference +{ + typedef T type; +}; + +template +struct remove_const +{ + typedef T type; +}; +template +struct remove_const +{ + typedef T type; +}; + +template > +class basic_string_ref +{ +public: + typedef Char value_type; + typedef std::size_t size_type; - typedef bool_ true_; - typedef bool_ false_; +private: + const value_type * data_; + size_type length_; +public: + basic_string_ref() : data_(0), length_(0) {} - template struct remove_reference { typedef T type; }; - template struct remove_reference { typedef T type; }; + basic_string_ref(const basic_string_ref & other) : data_(other.data_), length_(other.length_) {} - template struct remove_const { typedef T type; }; - template struct remove_const { typedef T type; }; + basic_string_ref(const value_type * str) : data_(str), length_(Traits::length(str)) {} + basic_string_ref(const value_type * str, size_type length) : data_(str), length_(length) {} - template > - class basic_string_ref + template + basic_string_ref(const std::basic_string & other) + : data_(other.data()), length_(other.length()) { - public: - typedef Char value_type; - typedef std::size_t size_type; + } - private: - const value_type* data_; - size_type length_; + size_type size() const { return length_; } + size_type length() const { return length_; } + size_type max_size() const { return length_; } + bool empty() const { return length_ == 0; } - public: - basic_string_ref() - : data_(0) - , length_(0) - {} - - basic_string_ref(const basic_string_ref& other) - : data_(other.data_) - , length_(other.length_) - {} - - basic_string_ref(const value_type* str) - : data_(str) - , length_(Traits::length(str)) - {} - - basic_string_ref(const value_type* str, size_type length) - : data_(str) - , length_(length) - {} - - template - basic_string_ref(const std::basic_string& other) - : data_(other.data()) - , length_(other.length()) - {} - - size_type size() const { return length_; } - size_type length() const { return length_; } - size_type max_size() const { return length_; } - bool empty() const { return length_ == 0; } - - const value_type* data() const { return data_; } - }; + const value_type * data() const { return data_; } +}; +// type traits +template +struct is_tag : false_ +{ +}; +template <> +struct is_tag : true_ +{ +}; +template <> +struct is_tag : true_ +{ +}; +template <> +struct is_tag : true_ +{ +}; +template <> +struct is_tag : true_ +{ +}; +template <> +struct is_tag : true_ +{ +}; +template <> +struct is_tag : true_ +{ +}; +template <> +struct is_tag : true_ +{ +}; - // type traits - template struct is_tag : false_ {}; - template <> struct is_tag : true_ {}; - template <> struct is_tag : true_ {}; - template <> struct is_tag : true_ {}; - template <> struct is_tag : true_ {}; - template <> struct is_tag : true_ {}; - template <> struct is_tag : true_ {}; - template <> struct is_tag : true_ {}; - - template struct is_null : false_ {}; - template <> struct is_null : true_ {}; - - template struct is_false : false_ {}; - template <> struct is_false : true_ {}; - - template struct is_true : false_ {}; - template <> struct is_true : true_ {}; - - template struct is_object : false_ {}; - template <> struct is_object : true_ {}; - template struct is_object< basic_object > : true_ {}; - - template struct is_array : false_ {}; - template <> struct is_array : true_ {}; - template struct is_array< basic_array > : true_ {}; - - template struct is_string : false_ {}; - template <> struct is_string : true_ {}; - template struct is_string< std::basic_string > : true_ {}; - template struct is_string< basic_string_ref > : true_ {}; - - template struct is_cstr_ptr : false_ {}; - template <> struct is_cstr_ptr< char * > : true_ {}; - template <> struct is_cstr_ptr< const char * > : true_ {}; - template <> struct is_cstr_ptr< wchar_t * > : true_ {}; - template <> struct is_cstr_ptr< const wchar_t * > : true_ {}; - - template struct is_number : false_ {}; - template <> struct is_number : true_ {}; - - template struct is_bool : false_ {}; - template <> struct is_bool : true_ {}; - - template struct is_int : false_ {}; - template <> struct is_int : true_ {}; - - template struct is_uint : false_ {}; - template <> struct is_uint : true_ {}; - - template struct is_int64 : false_ {}; - template <> struct is_int64 : true_ {}; - - template struct is_uint64 : false_ {}; - template <> struct is_uint64 : true_ {}; - - template struct is_double : false_ {}; - template <> struct is_double : true_ {}; - - template struct is_value_ref : false_ {}; - template struct is_value_ref< basic_value_ref > : true_ {}; - template struct is_value_ref< basic_value > : true_ {}; - template struct is_value_ref< basic_object > : true_ {}; - template struct is_value_ref< basic_array > : true_ {}; - - // type name - template const char* type_name(typename enable_if< is_null >::type* = 0) { return "null"; } - template const char* type_name(typename enable_if< is_false >::type* = 0) { return "false"; } - template const char* type_name(typename enable_if< is_true >::type* = 0) { return "true"; } - template const char* type_name(typename enable_if< is_object >::type* = 0) { return "object"; } - template const char* type_name(typename enable_if< is_array >::type* = 0) { return "array"; } - template const char* type_name(typename enable_if< is_string >::type* = 0) { return "string"; } - template const char* type_name(typename enable_if< is_number >::type* = 0) { return "number"; } - template const char* type_name(typename enable_if< is_bool >::type* = 0) { return "bool"; } - template const char* type_name(typename enable_if< is_int >::type* = 0) { return "int"; } - template const char* type_name(typename enable_if< is_uint >::type* = 0) { return "uint"; } - template const char* type_name(typename enable_if< is_int64 >::type* = 0) { return "int64"; } - template const char* type_name(typename enable_if< is_uint64 >::type* = 0) { return "uint64"; } - template const char* type_name(typename enable_if< is_double >::type* = 0) { return "double"; } - template const char* type_name(typename enable_if< is_value_ref >::type* = 0) { return "value_ref"; } - - - template - struct operator_arrow_proxy - { - mutable typename remove_const::type value_; - operator_arrow_proxy(const PseudoReference& value) : value_(value) {} - PseudoReference* operator->() const { return &value_; } - }; +template +struct is_null : false_ +{ +}; +template <> +struct is_null : true_ +{ +}; - template - struct operator_arrow_proxy - { - T& value_; - operator_arrow_proxy(T& value) : value_(value) {} - T* operator->() const { return &value_; } - }; +template +struct is_false : false_ +{ +}; +template <> +struct is_false : true_ +{ +}; +template +struct is_true : false_ +{ +}; +template <> +struct is_true : true_ +{ +}; - template - class transform_iterator - { - typedef std::iterator_traits traits_type; +template +struct is_object : false_ +{ +}; +template <> +struct is_object : true_ +{ +}; +template +struct is_object > : true_ +{ +}; - public: - typedef transform_iterator this_type; - typedef Function function_type; +template +struct is_array : false_ +{ +}; +template <> +struct is_array : true_ +{ +}; +template +struct is_array > : true_ +{ +}; - typedef Iterator iterator_type; - typedef typename traits_type::iterator_category iterator_category; - typedef typename traits_type::difference_type difference_type; +template +struct is_string : false_ +{ +}; +template <> +struct is_string : true_ +{ +}; +template +struct is_string > : true_ +{ +}; +template +struct is_string > : true_ +{ +}; - typedef typename Function::result_type result_type; - typedef typename remove_reference::type value_type; - typedef operator_arrow_proxy operator_arrow_proxy_type; - typedef operator_arrow_proxy_type pointer; - typedef result_type reference; +template +struct is_cstr_ptr : false_ +{ +}; +template <> +struct is_cstr_ptr : true_ +{ +}; +template <> +struct is_cstr_ptr : true_ +{ +}; +template <> +struct is_cstr_ptr : true_ +{ +}; +template <> +struct is_cstr_ptr : true_ +{ +}; - private: - iterator_type it_; - function_type func_; +template +struct is_number : false_ +{ +}; +template <> +struct is_number : true_ +{ +}; - public: - transform_iterator() - : it_() - , func_() - {} +template +struct is_bool : false_ +{ +}; +template <> +struct is_bool : true_ +{ +}; - explicit transform_iterator(const iterator_type& it) - : it_(it) - , func_() - {} +template +struct is_int : false_ +{ +}; +template <> +struct is_int : true_ +{ +}; - transform_iterator(const iterator_type& it, const function_type& func) - : it_(it) - , func_(func) - {} +template +struct is_uint : false_ +{ +}; +template <> +struct is_uint : true_ +{ +}; - template - transform_iterator(const transform_iterator& other) - : it_(other.base()) - , func_(other.functor()) - {} +template +struct is_int64 : false_ +{ +}; +template <> +struct is_int64 : true_ +{ +}; - iterator_type& base() { return it_; } - const iterator_type& base() const { return it_; } +template +struct is_uint64 : false_ +{ +}; +template <> +struct is_uint64 : true_ +{ +}; - function_type& functor() { return func_; } - const function_type& functor() const { return func_; } +template +struct is_double : false_ +{ +}; +template <> +struct is_double : true_ +{ +}; - result_type dereference() const { return func_(*it_); } +template +struct is_value_ref : false_ +{ +}; +template +struct is_value_ref > : true_ +{ +}; +template +struct is_value_ref > : true_ +{ +}; +template +struct is_value_ref > : true_ +{ +}; +template +struct is_value_ref > : true_ +{ +}; - result_type operator*() const { return dereference(); } - operator_arrow_proxy_type operator->() const { return operator_arrow_proxy_type(dereference()); } +// type name +template +const char * type_name(typename enable_if >::type * = 0) +{ + return "null"; +} +template +const char * type_name(typename enable_if >::type * = 0) +{ + return "false"; +} +template +const char * type_name(typename enable_if >::type * = 0) +{ + return "true"; +} +template +const char * type_name(typename enable_if >::type * = 0) +{ + return "object"; +} +template +const char * type_name(typename enable_if >::type * = 0) +{ + return "array"; +} +template +const char * type_name(typename enable_if >::type * = 0) +{ + return "string"; +} +template +const char * type_name(typename enable_if >::type * = 0) +{ + return "number"; +} +template +const char * type_name(typename enable_if >::type * = 0) +{ + return "bool"; +} +template +const char * type_name(typename enable_if >::type * = 0) +{ + return "int"; +} +template +const char * type_name(typename enable_if >::type * = 0) +{ + return "uint"; +} +template +const char * type_name(typename enable_if >::type * = 0) +{ + return "int64"; +} +template +const char * type_name(typename enable_if >::type * = 0) +{ + return "uint64"; +} +template +const char * type_name(typename enable_if >::type * = 0) +{ + return "double"; +} +template +const char * type_name(typename enable_if >::type * = 0) +{ + return "value_ref"; +} + +template +struct operator_arrow_proxy +{ + mutable typename remove_const::type value_; + operator_arrow_proxy(const PseudoReference & value) : value_(value) {} + PseudoReference * operator->() const { return &value_; } +}; - this_type& operator++() { ++it_; return *this; } - this_type operator++(int) { return this_type(it_++, func_); } - this_type& operator--() { --it_; return *this; } - this_type operator--(int) { return this_type(it_--, func_); } +template +struct operator_arrow_proxy +{ + T & value_; + operator_arrow_proxy(T & value) : value_(value) {} + T * operator->() const { return &value_; } +}; - this_type operator+(difference_type n) const { return this_type(it_ + n, func_); } - this_type& operator+=(difference_type n) { it_ += n; return *this; } - this_type operator-(difference_type n) const { return this_type(it_ - n, func_); } - this_type& operator-=(difference_type n) { it_ -= n; return *this; } +template +class transform_iterator +{ + typedef std::iterator_traits traits_type; - result_type operator[](difference_type n) const { return func_(it_[n]); } +public: + typedef transform_iterator this_type; + typedef Function function_type; - template - bool operator==(const transform_iterator& other) const { return base() == other.base(); } + typedef Iterator iterator_type; + typedef typename traits_type::iterator_category iterator_category; + typedef typename traits_type::difference_type difference_type; - template - bool operator!=(const transform_iterator& other) const { return base() != other.base(); } + typedef typename Function::result_type result_type; + typedef typename remove_reference::type value_type; + typedef operator_arrow_proxy operator_arrow_proxy_type; + typedef operator_arrow_proxy_type pointer; + typedef result_type reference; - template - bool operator<(const transform_iterator& other) const { return base() < other.base(); } +private: + iterator_type it_; + function_type func_; - template - bool operator>(const transform_iterator& other) const { return base() > other.base(); } +public: + transform_iterator() : it_(), func_() {} - template - bool operator<=(const transform_iterator& other) const { return base() <= other.base(); } + explicit transform_iterator(const iterator_type & it) : it_(it), func_() {} - template - bool operator>=(const transform_iterator& other) const { return base() >= other.base(); } - }; + transform_iterator(const iterator_type & it, const function_type & func) : it_(it), func_(func) {} - template - transform_iterator make_transform_iterator(Iterator it, Function func = Function()) + template + transform_iterator(const transform_iterator & other) + : it_(other.base()), func_(other.functor()) { - return transform_iterator(it, func); } + iterator_type & base() { return it_; } + const iterator_type & base() const { return it_; } - template - class member_wrapper - { - public: - typedef Member wrapped_type; - typedef ValueRef value_ref_type; - typedef typename ValueRef::string_type string_type; - typedef typename ValueRef::allocator_type allocator_type; + function_type & functor() { return func_; } + const function_type & functor() const { return func_; } - class proxy - { - wrapped_type& member_; - allocator_type* alloc_; + result_type dereference() const { return func_(*it_); } - public: - proxy(wrapped_type& member, allocator_type* alloc) - : member_(member) - , alloc_(alloc) - {} + result_type operator*() const { return dereference(); } + operator_arrow_proxy_type operator->() const { return operator_arrow_proxy_type(dereference()); } - string_type name() const { return value_ref_type(&(member_.name), alloc_).as_string(); } - value_ref_type value() const { return value_ref_type(&(member_.value), alloc_); } - }; + this_type & operator++() + { + ++it_; + return *this; + } + this_type operator++(int) { return this_type(it_++, func_); } + this_type & operator--() + { + --it_; + return *this; + } + this_type operator--(int) { return this_type(it_--, func_); } - private: - allocator_type* alloc_; + this_type operator+(difference_type n) const { return this_type(it_ + n, func_); } + this_type & operator+=(difference_type n) + { + it_ += n; + return *this; + } + this_type operator-(difference_type n) const { return this_type(it_ - n, func_); } + this_type & operator-=(difference_type n) + { + it_ -= n; + return *this; + } - public: - member_wrapper(allocator_type* alloc) - : alloc_(alloc) - {} + result_type operator[](difference_type n) const { return func_(it_[n]); } - template - member_wrapper(const member_wrapper& other) - : alloc_(other.get_allocator_pointer()) - {} + template + bool operator==(const transform_iterator & other) const + { + return base() == other.base(); + } - typedef proxy result_type; - result_type operator()(wrapped_type& member) const - { - return result_type(member, alloc_); - } + template + bool operator!=(const transform_iterator & other) const + { + return base() != other.base(); + } - allocator_type* get_allocator_pointer() const { return alloc_; } - }; + template + bool operator<(const transform_iterator & other) const + { + return base() < other.base(); + } - template - class value_wrapper + template + bool operator>(const transform_iterator & other) const { - public: - typedef Value wrapped_type; - typedef ValueRef value_ref_type; - typedef typename ValueRef::string_type string_type; - typedef typename ValueRef::allocator_type allocator_type; + return base() > other.base(); + } + + template + bool operator<=(const transform_iterator & other) const + { + return base() <= other.base(); + } - private: - allocator_type* alloc_; + template + bool operator>=(const transform_iterator & other) const + { + return base() >= other.base(); + } +}; - public: - value_wrapper(allocator_type* alloc) - : alloc_(alloc) - {} +template +transform_iterator make_transform_iterator( + Iterator it, Function func = Function()) +{ + return transform_iterator(it, func); +} - template - value_wrapper(const value_wrapper& other) - : alloc_(other.get_allocator_pointer()) - {} +template +class global_allocator +{ + RAllocator allocator_; - typedef value_ref_type result_type; - result_type operator()(wrapped_type& value) const - { - return result_type(&value, alloc_); - } +public: + global_allocator() : allocator_() {} - allocator_type* get_allocator_pointer() const { return alloc_; } - }; + RAllocator & get() { return allocator_; } - template struct value_ref_traits; - template struct const_value_ref_traits; + RAllocator * get_pointer() { return &allocator_; } - template - struct value_ref_traits + static RAllocator * instance() { - typedef Encoding encoding_type; - typedef rapidjson::Type native_type; - typedef rapidjson::GenericDocument native_document_type; - typedef rapidjson::GenericValue native_value_type; - typedef typename native_document_type::AllocatorType native_allocator_type; - typedef const_value_ref_traits const_traits; + static global_allocator instance_; + return instance_.get_pointer(); + } +}; - template - static void set(ValueRef& ref, Tag tag = Tag()) - { - ref.set(tag); - } - }; +template +class member_wrapper +{ +public: + typedef Member wrapped_type; + typedef ValueRef value_ref_type; + typedef typename ValueRef::string_type string_type; + typedef typename ValueRef::allocator_type allocator_type; - template - struct const_value_ref_traits + class proxy { - typedef Encoding encoding_type; - typedef const rapidjson::Type native_type; - typedef const rapidjson::GenericDocument native_document_type; - typedef const rapidjson::GenericValue native_value_type; - typedef const typename native_document_type::AllocatorType native_allocator_type; - typedef const_value_ref_traits const_traits; + wrapped_type & member_; + allocator_type * alloc_; + + public: + proxy(wrapped_type & member, allocator_type * alloc) : member_(member), alloc_(alloc) {} - template - static void set(const ValueRef& ref, Tag tag = Tag()) - {} + string_type name() const { return value_ref_type(&(member_.name), alloc_).as_string(); } + value_ref_type value() const { return value_ref_type(&(member_.value), alloc_); } }; +private: + allocator_type * alloc_; - template - class scoped_ptr +public: + member_wrapper(allocator_type * alloc) : alloc_(alloc) {} + + template + member_wrapper(const member_wrapper & other) + : alloc_(other.get_allocator_pointer()) { - private: - T* p_; + } - private: - scoped_ptr(const scoped_ptr& other); - scoped_ptr& operator=(const scoped_ptr& other); + typedef proxy result_type; + result_type operator()(wrapped_type & member) const { return result_type(member, alloc_); } - public: - explicit scoped_ptr(T* p = 0) - : p_(p) - {} + allocator_type * get_allocator_pointer() const { return alloc_; } +}; - ~scoped_ptr() - { - delete p_; - } +template +class value_wrapper +{ +public: + typedef Value wrapped_type; + typedef ValueRef value_ref_type; + typedef typename ValueRef::string_type string_type; + typedef typename ValueRef::allocator_type allocator_type; - T* operator->() { return p_; } - const T* operator->() const { return p_; } +private: + allocator_type * alloc_; - T& operator*() { return *p_; } - const T& operator*() const { return *p_; } +public: + value_wrapper(allocator_type * alloc) : alloc_(alloc) {} - T* get() { return p_; } - const T* get() const { return p_; } + template + value_wrapper(const value_wrapper & other) + : alloc_(other.get_allocator_pointer()) + { + } - void swap(scoped_ptr& other) throw() - { - std::swap(p_, other.p_); - } - }; + typedef value_ref_type result_type; + result_type operator()(wrapped_type & value) const { return result_type(&value, alloc_); } + + allocator_type * get_allocator_pointer() const { return alloc_; } +}; + +template +struct value_ref_traits; +template +struct const_value_ref_traits; -} // details +template +struct value_ref_traits +{ + typedef Encoding encoding_type; + typedef rapidjson::Type native_type; + typedef rapidjson::GenericDocument native_document_type; + typedef rapidjson::GenericValue native_value_type; + typedef typename native_document_type::AllocatorType native_allocator_type; + typedef const_value_ref_traits const_traits; + + template + static void set(ValueRef & ref, Tag tag = Tag()) + { + ref.set(tag); + } +}; + +template +struct const_value_ref_traits +{ + typedef Encoding encoding_type; + typedef const rapidjson::Type native_type; + typedef const rapidjson::GenericDocument native_document_type; + typedef const rapidjson::GenericValue native_value_type; + typedef const typename native_document_type::AllocatorType native_allocator_type; + typedef const_value_ref_traits const_traits; + + template + static void set(const ValueRef & ref, Tag tag = Tag()) + { + } +}; + +template +class scoped_ptr +{ +private: + T * p_; + +private: + scoped_ptr(const scoped_ptr & other); + scoped_ptr & operator=(const scoped_ptr & other); + +public: + explicit scoped_ptr(T * p = 0) : p_(p) {} + + ~scoped_ptr() { delete p_; } + + T * operator->() { return p_; } + const T * operator->() const { return p_; } + + T & operator*() { return *p_; } + const T & operator*() const { return *p_; } + + T * get() { return p_; } + const T * get() const { return p_; } + + void swap(scoped_ptr & other) throw() { std::swap(p_, other.p_); } +}; + +} // namespace details template class basic_value_ref { public: - typedef Traits traits; - typedef typename Traits::const_traits const_traits; - - typedef typename traits::encoding_type encoding_type; - typedef typename traits::native_type native_type; - typedef typename traits::native_document_type native_document_type; - typedef typename traits::native_value_type native_value_type; - typedef typename traits::native_allocator_type native_allocator_type; - - typedef basic_value_ref value_ref_type; - typedef const basic_value_ref const_value_ref_type; - typedef typename encoding_type::Ch char_type; - typedef std::basic_string string_type; - typedef details::basic_string_ref string_ref_type; - typedef native_allocator_type allocator_type; + typedef Traits traits; + typedef typename Traits::const_traits const_traits; + + typedef typename traits::encoding_type encoding_type; + typedef typename traits::native_type native_type; + typedef typename traits::native_document_type native_document_type; + typedef typename traits::native_value_type native_value_type; + typedef typename traits::native_allocator_type native_allocator_type; + + typedef basic_value_ref value_ref_type; + typedef const basic_value_ref const_value_ref_type; + typedef typename encoding_type::Ch char_type; + typedef std::basic_string string_type; + typedef details::basic_string_ref string_ref_type; + typedef native_allocator_type allocator_type; private: - typedef details::member_wrapper< typename native_value_type::Member, value_ref_type> member_wrapper_type; - typedef details::member_wrapper const_member_wrapper_type; - typedef details::value_wrapper< native_value_type, value_ref_type> value_wrapper_type; - typedef details::value_wrapper const_value_wrapper_type; + typedef details::member_wrapper + member_wrapper_type; + typedef details::member_wrapper + const_member_wrapper_type; + typedef details::value_wrapper value_wrapper_type; + typedef details::value_wrapper + const_value_wrapper_type; public: - typedef details::transform_iterator< member_wrapper_type, typename native_value_type::MemberIterator> member_iterator; - typedef details::transform_iterator const_member_iterator; - typedef details::transform_iterator< value_wrapper_type, typename native_value_type::ValueIterator> value_iterator; - typedef details::transform_iterator const_value_iterator; + typedef details::transform_iterator< + member_wrapper_type, typename native_value_type::MemberIterator> + member_iterator; + typedef details::transform_iterator< + const_member_wrapper_type, typename native_value_type::ConstMemberIterator> + const_member_iterator; + typedef details::transform_iterator + value_iterator; + typedef details::transform_iterator< + const_value_wrapper_type, typename native_value_type::ConstValueIterator> + const_value_iterator; private: - native_value_type* value_; - allocator_type* alloc_; + native_value_type * value_; + allocator_type * alloc_; public: - basic_value_ref(native_value_type* value = 0, allocator_type* alloc = 0) - : value_(value) - , alloc_(alloc) - {} + basic_value_ref(native_value_type * value = 0, allocator_type * alloc = 0) + : value_(value), alloc_(alloc) + { + } template - basic_value_ref(const basic_value_ref& other) - : value_(other.get_native_value_pointer()) - , alloc_(other.get_allocator_pointer()) - {} - - native_value_type* get_native_value_pointer() const { return value_; } - allocator_type* get_allocator_pointer() const { return alloc_; } - allocator_type& get_allocator() const { return *alloc_; } - - void set(null_tag) { value_->SetNull(); } - void set(object_tag) { value_->SetObject(); } - void set(array_tag) { value_->SetArray(); } - void set(bool value) { value_->SetBool(value); } - void set(int value) { value_->SetInt(value); } - void set(unsigned value) { value_->SetUint(value); } - void set(int64_t value) { value_->SetInt64(value); } - void set(uint64_t value) { value_->SetUint64(value); } - void set(double value) { value_->SetDouble(value); } - void set(const char_type* value) { value_->SetString(value, *alloc_); } - void set(const string_type& value) { value_->SetString(value.data(), value.length(), *alloc_); } + basic_value_ref(const basic_value_ref & other) + : value_(other.get_native_value_pointer()), alloc_(other.get_allocator_pointer()) + { + } + + native_value_type * get_native_value_pointer() const { return value_; } + allocator_type * get_allocator_pointer() const { return alloc_; } + allocator_type & get_allocator() const { return *alloc_; } + + void set(null_tag) { value_->SetNull(); } + void set(object_tag) { value_->SetObject(); } + void set(array_tag) { value_->SetArray(); } + void set(bool value) { value_->SetBool(value); } + void set(int value) { value_->SetInt(value); } + void set(unsigned value) { value_->SetUint(value); } + void set(int64_t value) { value_->SetInt64(value); } + void set(uint64_t value) { value_->SetUint64(value); } + void set(double value) { value_->SetDouble(value); } + void set(const char_type * value) { value_->SetString(value, *alloc_); } + void set(const string_type & value) { value_->SetString(value.data(), value.length(), *alloc_); } template - void set(const T& value, typename details::enable_if< details::is_value_ref >::type* = 0) - { - if (value.is_null()) set(null_tag()); - else if (value.is_bool()) set(value.as_bool()); - else if (value.is_int()) set(value.as_int()); - else if (value.is_uint()) set(value.as_uint()); - else if (value.is_int64()) set(value.as_int64()); - else if (value.is_uint64()) set(value.as_uint64()); - else if (value.is_double()) set(value.as_double()); - else if (value.is_string()) set(value.as_string()); - else if (value.is_array()) throw std::runtime_error("can not assign array directly. please use insert"); - else if (value.is_object()) throw std::runtime_error("can not assign object directly. please use insert"); + void set(const T & value, typename details::enable_if >::type * = 0) + { + if (value.is_null()) + set(null_tag()); + else if (value.is_bool()) + set(value.as_bool()); + else if (value.is_int()) + set(value.as_int()); + else if (value.is_uint()) + set(value.as_uint()); + else if (value.is_int64()) + set(value.as_int64()); + else if (value.is_uint64()) + set(value.as_uint64()); + else if (value.is_double()) + set(value.as_double()); + else if (value.is_string()) + set(value.as_string()); + else if (value.is_array()) + throw std::runtime_error("can not assign array directly. please use insert"); + else if (value.is_object()) + throw std::runtime_error("can not assign object directly. please use insert"); } template - void deep_copy(const basic_value_ref& other) + void deep_copy(const basic_value_ref & other) { value_->CopyFrom(*other.get_native_value_pointer(), *alloc_); } - template - value_ref_type& operator=(const T& value) + value_ref_type & operator=(const T & value) { set(value); return *this; } - template - bool operator==(const basic_value_ref& other) const + bool operator==(const basic_value_ref & other) const { if (is_null() && other.is_null()) return true; if (is_bool() && other.is_bool() && as_bool() == other.as_bool()) return true; @@ -625,28 +876,21 @@ class basic_value_ref } template - bool operator!=(const basic_value_ref& other) const + bool operator!=(const basic_value_ref & other) const { return !(*this == other); } + int which() const { return static_cast(value_->GetType()); } - int which() const - { - return static_cast(value_->GetType()); - } - -#define RABBIT_IS_DEF(name, base_name) \ - template \ - bool is(typename details::enable_if< details::is_##name >::type* = 0) const \ - { \ - return value_->Is##base_name(); \ - } \ - bool is_##name() const \ - { \ - return value_->Is##base_name(); \ - } \ -/**/ +#define RABBIT_IS_DEF(name, base_name) \ + template \ + bool is(typename details::enable_if >::type * = 0) const \ + { \ + return value_->Is##base_name(); \ + } \ + bool is_##name() const { return value_->Is##base_name(); } \ + /**/ RABBIT_IS_DEF(null, Null) RABBIT_IS_DEF(false, False) RABBIT_IS_DEF(true, True) @@ -662,19 +906,19 @@ class basic_value_ref RABBIT_IS_DEF(string, String) #undef RABBIT_IS_DEF -#define RABBIT_AS_DEF(result_type, name, base_name) \ - template \ - T as(typename details::enable_if< details::is_##name >::type* = 0) const \ - { \ - type_check(); \ - return value_->Get##base_name(); \ - } \ - result_type as_##name() const \ - { \ - type_check(); \ - return value_->Get##base_name(); \ - } \ -/**/ +#define RABBIT_AS_DEF(result_type, name, base_name) \ + template \ + T as(typename details::enable_if >::type * = 0) const \ + { \ + type_check(); \ + return value_->Get##base_name(); \ + } \ + result_type as_##name() const \ + { \ + type_check(); \ + return value_->Get##base_name(); \ + } \ + /**/ RABBIT_AS_DEF(bool, bool, Bool) RABBIT_AS_DEF(int, int, Int) RABBIT_AS_DEF(unsigned, uint, Uint) @@ -684,7 +928,7 @@ class basic_value_ref #undef RABBIT_AS_DEF double as_double() const { - if(!is_number()){ + if (!is_number()) { std::stringstream ss; ss << "value is not "; ss << details::type_name(); @@ -695,134 +939,157 @@ class basic_value_ref } template - T as(typename details::enable_if< details::is_double >::type* = 0) const + T as(typename details::enable_if >::type * = 0) const { return as_double(); } - - - - private: struct as_t { - const value_ref_type& ref_; - as_t(const value_ref_type& ref) : ref_(ref) {} + const value_ref_type & ref_; + as_t(const value_ref_type & ref) : ref_(ref) {} template - operator Result() const { return ref_.as(); } + operator Result() const + { + return ref_.as(); + } }; public: as_t as() const { return as_t(*this); } - bool has(const string_ref_type& name) const + bool has(const string_ref_type & name) const { type_check(); return value_->HasMember(name.data()); } template - void insert(const string_ref_type& name, const T& value, const bool copy_name_string = true, typename details::disable_if< details::is_value_ref >::type* = 0, typename details::enable_if< details::is_string >::type * = 0) + void insert( + const string_ref_type & name, const T & value, const bool copy_name_string = true, + typename details::disable_if >::type * = 0, + typename details::enable_if >::type * = 0) { type_check(); native_value_type v(value.data(), value.length(), *alloc_); - if(copy_name_string){ + if (copy_name_string) { native_value_type copied_name(name.data(), name.length(), *alloc_); value_->AddMember(copied_name, v, *alloc_); - }else{ + } else { value_->AddMember(rapidjson::StringRef(name.data(), name.length()), v, *alloc_); } } template - void insert(const string_ref_type& name, const T& value, const bool copy_name_string = true, typename details::disable_if< details::is_value_ref >::type* = 0, typename details::enable_if< details::is_cstr_ptr >::type * = 0) + void insert( + const string_ref_type & name, const T & value, const bool copy_name_string = true, + typename details::disable_if >::type * = 0, + typename details::enable_if >::type * = 0) { type_check(); native_value_type v(value, *alloc_); - if(copy_name_string){ + if (copy_name_string) { native_value_type copied_name(name.data(), name.length(), *alloc_); value_->AddMember(copied_name, v, *alloc_); - }else{ + } else { value_->AddMember(rapidjson::StringRef(name.data(), name.length()), v, *alloc_); } } - template - void insert(const string_ref_type& name, const T& value, const bool copy_name_string = true, typename details::disable_if< details::is_value_ref >::type* = 0, typename details::disable_if< details::is_string >::type * = 0, typename details::disable_if< details::is_cstr_ptr >::type * = 0) + void insert( + const string_ref_type & name, const T & value, const bool copy_name_string = true, + typename details::disable_if >::type * = 0, + typename details::disable_if >::type * = 0, + typename details::disable_if >::type * = 0) { type_check(); native_value_type v(value); - if(copy_name_string){ + if (copy_name_string) { native_value_type copied_name(name.data(), name.length(), *alloc_); value_->AddMember(copied_name, v, *alloc_); - }else{ + } else { value_->AddMember(rapidjson::StringRef(name.data(), name.length()), v, *alloc_); } } template - void insert(const string_ref_type& name, const T& value, const bool copy_name_string = true, typename details::enable_if< details::is_value_ref >::type* = 0) + void insert( + const string_ref_type & name, const T & value, const bool copy_name_string = true, + typename details::enable_if >::type * = 0) { type_check(); - if(copy_name_string){ + if (copy_name_string) { native_value_type copied_name(name.data(), name.length(), *alloc_); value_->AddMember(copied_name, *value.get_native_value_pointer(), *alloc_); - }else{ - value_->AddMember(rapidjson::StringRef(name.data(), name.length()), *value.get_native_value_pointer(), *alloc_); + } else { + value_->AddMember( + rapidjson::StringRef(name.data(), name.length()), *value.get_native_value_pointer(), + *alloc_); } - } - bool erase(const string_ref_type& name) + bool erase(const string_ref_type & name) { type_check(); return value_->RemoveMember(name.data()); } - void reserve(const size_t reserve_size){ + void reserve(const size_t reserve_size) + { type_check(); value_->Reserve(reserve_size, *alloc_); } - const_member_iterator erase(const const_member_iterator& itr){ + const_member_iterator erase(const const_member_iterator & itr) + { type_check(); - return details::make_transform_iterator(value_->EraseMember(itr.base()), const_member_wrapper_type(alloc_)); + return details::make_transform_iterator( + value_->EraseMember(itr.base()), const_member_wrapper_type(alloc_)); } - member_iterator erase(const member_iterator& itr){ + member_iterator erase(const member_iterator & itr) + { type_check(); - return details::make_transform_iterator(value_->EraseMember(itr.base()), member_wrapper_type(alloc_)); + return details::make_transform_iterator( + value_->EraseMember(itr.base()), member_wrapper_type(alloc_)); } - const_value_iterator erase(const const_value_iterator& itr){ + const_value_iterator erase(const const_value_iterator & itr) + { type_check(); - return details::make_transform_iterator(value_->Erase(itr.base()), const_value_wrapper_type(alloc_)); + return details::make_transform_iterator( + value_->Erase(itr.base()), const_value_wrapper_type(alloc_)); } - const_value_iterator erase(const const_value_iterator& beginItr, const const_value_iterator& endItr){ + const_value_iterator erase( + const const_value_iterator & beginItr, const const_value_iterator & endItr) + { type_check(); - return details::make_transform_iterator(value_->Erase(beginItr.base(), endItr.base()), const_value_wrapper_type(alloc_)); + return details::make_transform_iterator( + value_->Erase(beginItr.base(), endItr.base()), const_value_wrapper_type(alloc_)); } - value_iterator erase(const value_iterator& itr){ + value_iterator erase(const value_iterator & itr) + { type_check(); return details::make_transform_iterator(value_->Erase(itr.base()), value_wrapper_type(alloc_)); } - value_iterator erase(const value_iterator& beginItr, const value_iterator& endItr){ + value_iterator erase(const value_iterator & beginItr, const value_iterator & endItr) + { type_check(); - return details::make_transform_iterator(value_->Erase(beginItr.base(), endItr.base()), value_wrapper_type(alloc_)); + return details::make_transform_iterator( + value_->Erase(beginItr.base(), endItr.base()), value_wrapper_type(alloc_)); } - value_ref_type at(const string_ref_type& name) + value_ref_type at(const string_ref_type & name) { type_check(); - if (!has(name)) - { + if (!has(name)) { native_value_type null; native_value_type copied_name(name.data(), name.length(), *alloc_); value_->AddMember(copied_name, null, *alloc_); @@ -831,7 +1098,7 @@ class basic_value_ref return value_ref_type(&((*value_)[name.data()]), alloc_); } - const_value_ref_type at(const string_ref_type& name) const + const_value_ref_type at(const string_ref_type & name) const { type_check(); if (!has(name)) @@ -839,8 +1106,8 @@ class basic_value_ref return const_value_ref_type(&((*value_)[name.data()]), alloc_); } - value_ref_type operator[](const string_ref_type& name) { return at(name); } - const_value_ref_type operator[](const string_ref_type& name) const { return at(name); } + value_ref_type operator[](const string_ref_type & name) { return at(name); } + const_value_ref_type operator[](const string_ref_type & name) const { return at(name); } member_iterator member_begin() { @@ -857,7 +1124,8 @@ class basic_value_ref const_member_iterator member_begin() const { type_check(); - return details::make_transform_iterator(value_->MemberBegin(), const_member_wrapper_type(alloc_)); + return details::make_transform_iterator( + value_->MemberBegin(), const_member_wrapper_type(alloc_)); } const_member_iterator member_end() const @@ -909,7 +1177,9 @@ class basic_value_ref const_value_ref_type operator[](std::size_t index) const { return at(index); } template - void push_back(const T& value, typename details::disable_if< details::is_value_ref >::type* = 0, typename details::enable_if< details::is_string >::type * = 0) + void push_back( + const T & value, typename details::disable_if >::type * = 0, + typename details::enable_if >::type * = 0) { type_check(); native_value_type v(value.data(), value.length(), *alloc_); @@ -917,7 +1187,9 @@ class basic_value_ref } template - void push_back(const T& value, typename details::disable_if< details::is_value_ref >::type* = 0, typename details::enable_if< details::is_cstr_ptr >::type * = 0) + void push_back( + const T & value, typename details::disable_if >::type * = 0, + typename details::enable_if >::type * = 0) { type_check(); native_value_type v(value, *alloc_); @@ -925,7 +1197,10 @@ class basic_value_ref } template - void push_back(const T& value, typename details::disable_if< details::is_value_ref >::type* = 0, typename details::disable_if< details::is_string >::type * = 0, typename details::disable_if< details::is_cstr_ptr >::type * = 0) + void push_back( + const T & value, typename details::disable_if >::type * = 0, + typename details::disable_if >::type * = 0, + typename details::disable_if >::type * = 0) { type_check(); native_value_type v(value); @@ -933,7 +1208,8 @@ class basic_value_ref } template - void push_back(const T& value, typename details::enable_if< details::is_value_ref >::type* = 0) + void push_back( + const T & value, typename details::enable_if >::type * = 0) { type_check(); value_->PushBack(*value.get_native_value_pointer(), *alloc_); @@ -972,8 +1248,7 @@ class basic_value_ref const_value_iterator value_cbegin() const { return value_begin(); } const_value_iterator value_cend() const { return value_end(); } - - void swap(value_ref_type& other) throw() + void swap(value_ref_type & other) throw() { std::swap(value_, other.value_); std::swap(alloc_, other.alloc_); @@ -981,33 +1256,35 @@ class basic_value_ref string_type str() const { - switch (which()) - { - case null_tag::value: - return "null"; + switch (which()) { + case null_tag::value: + return "null"; - case false_tag::value: - return "false"; + case false_tag::value: + return "false"; - case true_tag::value: - return "true"; + case true_tag::value: + return "true"; - case string_tag::value: - return as_string(); + case string_tag::value: + return as_string(); - case number_tag::value: - { + case number_tag::value: { std::basic_stringstream ss; - if (is_int()) ss << as_int(); - else if (is_uint()) ss << as_uint(); - else if (is_int64()) ss << as_int64(); - else if (is_uint64()) ss << as_uint64(); - else if (is_double()) ss << as_double(); + if (is_int()) + ss << as_int(); + else if (is_uint()) + ss << as_uint(); + else if (is_int64()) + ss << as_int64(); + else if (is_uint64()) + ss << as_uint64(); + else if (is_double()) + ss << as_double(); return ss.str(); } - default: - { + default: { typedef rapidjson::GenericStringBuffer buffer_t; typedef rapidjson::Writer writer_t; buffer_t buffer; @@ -1022,8 +1299,7 @@ class basic_value_ref template void type_check() const { - if (!is()) - { + if (!is()) { std::stringstream ss; ss << "value is not "; ss << details::type_name(); @@ -1034,8 +1310,7 @@ class basic_value_ref void range_check(std::size_t index) const { - if (index >= size()) - { + if (index >= size()) { std::stringstream ss; ss << "index (which is " << index << ") >= size() (which is " << size() << ")"; throw std::out_of_range(ss.str()); @@ -1043,107 +1318,127 @@ class basic_value_ref } }; - template struct basic_value_base { - typedef basic_value_ref base_type; - typedef typename base_type::native_value_type native_value_type; - typedef typename base_type::allocator_type allocator_type; + typedef basic_value_ref base_type; + typedef typename base_type::native_value_type native_value_type; + typedef typename base_type::allocator_type allocator_type; details::scoped_ptr value_impl_; - details::scoped_ptr alloc_impl_; + allocator_type * alloc_impl_; - explicit basic_value_base(native_value_type* value = 0, allocator_type* alloc = 0) - : value_impl_(value) - , alloc_impl_(alloc) - {} + explicit basic_value_base(native_value_type * value = 0, allocator_type * alloc = 0) + : value_impl_(value), alloc_impl_(alloc) + { + } }; template -class basic_value - : private basic_value_base - , public basic_value_ref +class basic_value : private basic_value_base, public basic_value_ref { public: - typedef Traits traits; - typedef typename Traits::const_traits const_traits; - - typedef basic_value_base member_type; - typedef basic_value_ref base_type; - - typedef typename base_type::encoding_type encoding_type; - typedef typename base_type::native_type native_type; - typedef typename base_type::native_document_type native_document_type; - typedef typename base_type::native_value_type native_value_type; - typedef typename base_type::native_allocator_type native_allocator_type; - - typedef typename base_type::value_ref_type value_ref_type; - typedef typename base_type::const_value_ref_type const_value_ref_type; - typedef typename base_type::char_type char_type; - typedef typename base_type::string_type string_type; - typedef typename base_type::string_ref_type string_ref_type; - typedef typename base_type::allocator_type allocator_type; - - typedef typename base_type::member_iterator member_iterator; - typedef typename base_type::const_member_iterator const_member_iterator; - typedef typename base_type::value_iterator value_iterator; - typedef typename base_type::const_value_iterator const_value_iterator; + typedef Traits traits; + typedef typename Traits::const_traits const_traits; + + typedef basic_value_base member_type; + typedef basic_value_ref base_type; + + typedef typename base_type::encoding_type encoding_type; + typedef typename base_type::native_type native_type; + typedef typename base_type::native_document_type native_document_type; + typedef typename base_type::native_value_type native_value_type; + typedef typename base_type::native_allocator_type native_allocator_type; + + typedef typename base_type::value_ref_type value_ref_type; + typedef typename base_type::const_value_ref_type const_value_ref_type; + typedef typename base_type::char_type char_type; + typedef typename base_type::string_type string_type; + typedef typename base_type::string_ref_type string_ref_type; + typedef typename base_type::allocator_type allocator_type; + + typedef typename base_type::member_iterator member_iterator; + typedef typename base_type::const_member_iterator const_member_iterator; + typedef typename base_type::value_iterator value_iterator; + typedef typename base_type::const_value_iterator const_value_iterator; private: typedef DefaultTag default_tag; public: basic_value() - : member_type(new native_value_type(DefaultTag::native_value), new allocator_type()) - , base_type(member_type::value_impl_.get(), member_type::alloc_impl_.get()) - {} - - basic_value(allocator_type& alloc) - : member_type(new native_value_type(DefaultTag::native_value)) - , base_type(member_type::value_impl_.get(), &alloc) - {} + : member_type( + new native_value_type(DefaultTag::native_value), + details::global_allocator::instance()), + base_type(member_type::value_impl_.get(), member_type::alloc_impl_) + { + } + basic_value(allocator_type & alloc) + : member_type(new native_value_type(DefaultTag::native_value)), + base_type(member_type::value_impl_.get(), &alloc) + { + } /* * Tag based constructors */ template - basic_value(Tag tag, typename details::enable_if< details::is_tag >::type* = 0) - : member_type(new native_value_type(Tag::native_value), new allocator_type()) - , base_type(member_type::value_impl_.get(), member_type::alloc_impl_.get()) - {} + basic_value(Tag tag, typename details::enable_if >::type * = 0) + : member_type( + new native_value_type(Tag::native_value), + details::global_allocator::instance()), + base_type(member_type::value_impl_.get(), member_type::alloc_impl_) + { + } template - basic_value(Tag tag, allocator_type& alloc, typename details::enable_if< details::is_tag >::type* = 0) - : member_type(new native_value_type(Tag::native_value)) - , base_type(member_type::value_impl_.get(), &alloc) - {} - + basic_value( + Tag tag, allocator_type & alloc, typename details::enable_if >::type * = 0) + : member_type(new native_value_type(Tag::native_value)), + base_type(member_type::value_impl_.get(), &alloc) + { + } /* * Value based constructors */ template - basic_value(const T& value, typename details::disable_if< details::is_value_ref >::type* = 0, typename details::disable_if< details::is_tag >::type* = 0, typename details::disable_if< details::is_string >::type* = 0, typename details::disable_if< details::is_cstr_ptr >::type* = 0) - : member_type(new native_value_type(value), new allocator_type()) - , base_type(member_type::value_impl_.get(), member_type::alloc_impl_.get()) - {} + basic_value( + const T & value, typename details::disable_if >::type * = 0, + typename details::disable_if >::type * = 0, + typename details::disable_if >::type * = 0, + typename details::disable_if >::type * = 0) + : member_type( + new native_value_type(value), details::global_allocator::instance()), + base_type(member_type::value_impl_.get(), member_type::alloc_impl_) + { + } //Special handling for string types because to copy a string type we need to provide an allocator, but we don't have an allocator till we are part way through construction template - basic_value(const T& value, typename details::disable_if< details::is_value_ref >::type* = 0, typename details::disable_if< details::is_tag >::type* = 0, typename details::enable_if< details::is_string >::type* = 0) - : member_type(new native_value_type(null_tag::native_value), new allocator_type()) - , base_type(member_type::value_impl_.get(), member_type::alloc_impl_.get()) + basic_value( + const T & value, typename details::disable_if >::type * = 0, + typename details::disable_if >::type * = 0, + typename details::enable_if >::type * = 0) + : member_type( + new native_value_type(null_tag::native_value), + details::global_allocator::instance()), + base_type(member_type::value_impl_.get(), member_type::alloc_impl_) { base_type::set(value); } //Special handling for string types because to copy a string type we need to provide an allocator, but we don't have an allocator till we are part way through construction template - basic_value(const T& value, typename details::disable_if< details::is_value_ref >::type* = 0, typename details::disable_if< details::is_tag >::type* = 0, typename details::enable_if >::type* = 0) - : member_type(new native_value_type(null_tag::native_value), new allocator_type()) - , base_type(member_type::value_impl_.get(), member_type::alloc_impl_.get()) + basic_value( + const T & value, typename details::disable_if >::type * = 0, + typename details::disable_if >::type * = 0, + typename details::enable_if >::type * = 0) + : member_type( + new native_value_type(null_tag::native_value), + details::global_allocator::instance()), + base_type(member_type::value_impl_.get(), member_type::alloc_impl_) { base_type::set(value); } @@ -1152,83 +1447,85 @@ class basic_value * Value based constructors WITH an external allocator */ template - basic_value(const T& value, allocator_type& alloc, typename details::disable_if< details::is_value_ref >::type* = 0, typename details::disable_if< details::is_tag >::type* = 0, typename details::disable_if< details::is_string >::type* = 0, typename details::disable_if< details::is_cstr_ptr >::type* = 0) - : member_type(new native_value_type(value)) - , base_type(member_type::value_impl_.get(), &alloc) - {} + basic_value( + const T & value, allocator_type & alloc, + typename details::disable_if >::type * = 0, + typename details::disable_if >::type * = 0, + typename details::disable_if >::type * = 0, + typename details::disable_if >::type * = 0) + : member_type(new native_value_type(value)), base_type(member_type::value_impl_.get(), &alloc) + { + } //Special handling to make sure we copy strings with the given allocator template - basic_value(const T& value, allocator_type& alloc, typename details::disable_if< details::is_value_ref >::type* = 0, typename details::disable_if< details::is_tag >::type* = 0, typename details::enable_if< details::is_string >::type* = 0) - : member_type(new native_value_type(value, alloc)) - , base_type(member_type::value_impl_.get(), &alloc) - {} + basic_value( + const T & value, allocator_type & alloc, + typename details::disable_if >::type * = 0, + typename details::disable_if >::type * = 0, + typename details::enable_if >::type * = 0) + : member_type(new native_value_type(value, alloc)), + base_type(member_type::value_impl_.get(), &alloc) + { + } //Special handling to make sure we copy strings with the given allocator template - basic_value(const T& value, allocator_type& alloc, typename details::disable_if< details::is_value_ref >::type* = 0, typename details::disable_if< details::is_tag >::type* = 0, typename details::enable_if< details::is_cstr_ptr >::type* = 0) - : member_type(new native_value_type(value, alloc)) - , base_type(member_type::value_impl_.get(), &alloc) - {} - + basic_value( + const T & value, allocator_type & alloc, + typename details::disable_if >::type * = 0, + typename details::disable_if >::type * = 0, + typename details::enable_if >::type * = 0) + : member_type(new native_value_type(value, alloc)), + base_type(member_type::value_impl_.get(), &alloc) + { + } - basic_value(const basic_value& other) - : member_type() - , base_type(other) + basic_value(const basic_value & other) : member_type(), base_type(other) { - if (other.is_root_value()) - throw std::runtime_error("can not copy root value"); + if (other.is_root_value()) throw std::runtime_error("can not copy root value"); } - basic_value& operator=(const basic_value& other) + basic_value & operator=(const basic_value & other) { - if (other.is_root_value()) - throw std::runtime_error("can not copy root value"); + if (other.is_root_value()) throw std::runtime_error("can not copy root value"); basic_value(other).swap(*this); return *this; } template - basic_value(const basic_value_ref& other) - : member_type() - , base_type(other) + basic_value(const basic_value_ref & other) : member_type(), base_type(other) { - if (base_type::is_null()) - traits::set(*this, default_tag()); + if (base_type::is_null()) traits::set(*this, default_tag()); } template - basic_value& operator=(const basic_value_ref& other) + basic_value & operator=(const basic_value_ref & other) { basic_value(other).swap(*this); return *this; } - template - basic_value& operator=(const T& value) + basic_value & operator=(const T & value) { base_type::set(value); return *this; } - void clear() - { - base_type::set(default_tag()); - } + void clear() { base_type::set(default_tag()); } - void swap(basic_value& other) throw() + void swap(basic_value & other) throw() { base_type::swap(other); member_type::value_impl_.swap(other.value_impl_); - member_type::alloc_impl_.swap(other.alloc_impl_); + member_type::alloc_impl_ = other.alloc_impl_; } private: bool is_root_value() const { - return member_type::value_impl_.get() != 0 - || member_type::alloc_impl_.get() != 0; + return member_type::value_impl_.get() != 0 || member_type::alloc_impl_ != 0; } }; @@ -1236,157 +1533,138 @@ template class basic_object : public basic_value { public: - typedef basic_value base_type; + typedef basic_value base_type; - typedef typename base_type::encoding_type encoding_type; - typedef typename base_type::native_type native_type; - typedef typename base_type::native_document_type native_document_type; - typedef typename base_type::native_value_type native_value_type; - typedef typename base_type::native_allocator_type native_allocator_type; + typedef typename base_type::encoding_type encoding_type; + typedef typename base_type::native_type native_type; + typedef typename base_type::native_document_type native_document_type; + typedef typename base_type::native_value_type native_value_type; + typedef typename base_type::native_allocator_type native_allocator_type; - typedef typename base_type::value_ref_type value_ref_type; - typedef typename base_type::const_value_ref_type const_value_ref_type; - typedef typename base_type::char_type char_type; - typedef typename base_type::string_type string_type; - typedef typename base_type::string_ref_type string_ref_type; - typedef typename base_type::allocator_type allocator_type; + typedef typename base_type::value_ref_type value_ref_type; + typedef typename base_type::const_value_ref_type const_value_ref_type; + typedef typename base_type::char_type char_type; + typedef typename base_type::string_type string_type; + typedef typename base_type::string_ref_type string_ref_type; + typedef typename base_type::allocator_type allocator_type; - typedef typename base_type::member_iterator iterator; - typedef typename base_type::const_member_iterator const_iterator; + typedef typename base_type::member_iterator iterator; + typedef typename base_type::const_member_iterator const_iterator; public: - basic_object() - : base_type() - {} + basic_object() : base_type() {} - basic_object(allocator_type& alloc) - : base_type(alloc) - {} + basic_object(allocator_type & alloc) : base_type(alloc) {} - basic_object(const basic_object& other) - : base_type(other) - {} + basic_object(const basic_object & other) : base_type(other) {} template - basic_object(const basic_value_ref& other) - : base_type(other) - {} + basic_object(const basic_value_ref & other) : base_type(other) + { + } - iterator begin() { return base_type::member_begin(); } - iterator end() { return base_type::member_end(); } - const_iterator begin() const { return base_type::member_begin(); } - const_iterator end() const { return base_type::member_end(); } - const_iterator cbegin() const { return base_type::member_begin(); } - const_iterator cend() const { return base_type::member_end(); } + iterator begin() { return base_type::member_begin(); } + iterator end() { return base_type::member_end(); } + const_iterator begin() const { return base_type::member_begin(); } + const_iterator end() const { return base_type::member_end(); } + const_iterator cbegin() const { return base_type::member_begin(); } + const_iterator cend() const { return base_type::member_end(); } }; template class basic_array : public basic_value { public: - typedef basic_value base_type; + typedef basic_value base_type; - typedef typename base_type::encoding_type encoding_type; - typedef typename base_type::native_type native_type; - typedef typename base_type::native_document_type native_document_type; - typedef typename base_type::native_value_type native_value_type; - typedef typename base_type::native_allocator_type native_allocator_type; + typedef typename base_type::encoding_type encoding_type; + typedef typename base_type::native_type native_type; + typedef typename base_type::native_document_type native_document_type; + typedef typename base_type::native_value_type native_value_type; + typedef typename base_type::native_allocator_type native_allocator_type; - typedef typename base_type::value_ref_type value_ref_type; - typedef typename base_type::const_value_ref_type const_value_ref_type; - typedef typename base_type::char_type char_type; - typedef typename base_type::string_type string_type; - typedef typename base_type::string_ref_type string_ref_type; - typedef typename base_type::allocator_type allocator_type; + typedef typename base_type::value_ref_type value_ref_type; + typedef typename base_type::const_value_ref_type const_value_ref_type; + typedef typename base_type::char_type char_type; + typedef typename base_type::string_type string_type; + typedef typename base_type::string_ref_type string_ref_type; + typedef typename base_type::allocator_type allocator_type; - typedef typename base_type::value_iterator iterator; - typedef typename base_type::const_value_iterator const_iterator; + typedef typename base_type::value_iterator iterator; + typedef typename base_type::const_value_iterator const_iterator; public: - basic_array() - : base_type() - {} + basic_array() : base_type() {} - basic_array(allocator_type& alloc) - : base_type(alloc) - {} + basic_array(allocator_type & alloc) : base_type(alloc) {} - basic_array(const basic_array& other) - : base_type(other) - {} + basic_array(const basic_array & other) : base_type(other) {} template - basic_array(const basic_value_ref& other) - : base_type(other) - {} + basic_array(const basic_value_ref & other) : base_type(other) + { + } - iterator begin() { return base_type::value_begin(); } - iterator end() { return base_type::value_end(); } - const_iterator begin() const { return base_type::value_begin(); } - const_iterator end() const { return base_type::value_end(); } - const_iterator cbegin() const { return base_type::value_begin(); } - const_iterator cend() const { return base_type::value_end(); } + iterator begin() { return base_type::value_begin(); } + iterator end() { return base_type::value_end(); } + const_iterator begin() const { return base_type::value_begin(); } + const_iterator end() const { return base_type::value_end(); } + const_iterator cbegin() const { return base_type::value_begin(); } + const_iterator cend() const { return base_type::value_end(); } }; - template struct basic_document_base { - typedef basic_value_ref base_type; - typedef typename base_type::native_document_type native_document_type; + typedef basic_value_ref base_type; + typedef typename base_type::native_document_type native_document_type; details::scoped_ptr document_impl_; - explicit basic_document_base(native_document_type* document = 0) - : document_impl_(document) - {} + explicit basic_document_base(native_document_type * document = 0) : document_impl_(document) {} }; template -class basic_document - : private basic_document_base - , public basic_value_ref +class basic_document : private basic_document_base, public basic_value_ref { public: - typedef basic_document_base member_type; - typedef basic_value_ref base_type; - - typedef typename base_type::encoding_type encoding_type; - typedef typename base_type::native_type native_type; - typedef typename base_type::native_document_type native_document_type; - typedef typename base_type::native_value_type native_value_type; - typedef typename base_type::native_allocator_type native_allocator_type; - - typedef typename base_type::value_ref_type value_ref_type; - typedef typename base_type::const_value_ref_type const_value_ref_type; - typedef typename base_type::char_type char_type; - typedef typename base_type::string_type string_type; - typedef typename base_type::string_ref_type string_ref_type; - typedef typename base_type::allocator_type allocator_type; + typedef basic_document_base member_type; + typedef basic_value_ref base_type; + + typedef typename base_type::encoding_type encoding_type; + typedef typename base_type::native_type native_type; + typedef typename base_type::native_document_type native_document_type; + typedef typename base_type::native_value_type native_value_type; + typedef typename base_type::native_allocator_type native_allocator_type; + + typedef typename base_type::value_ref_type value_ref_type; + typedef typename base_type::const_value_ref_type const_value_ref_type; + typedef typename base_type::char_type char_type; + typedef typename base_type::string_type string_type; + typedef typename base_type::string_ref_type string_ref_type; + typedef typename base_type::allocator_type allocator_type; private: - basic_document(const basic_document&); - basic_document& operator=(const basic_document&); + basic_document(const basic_document &); + basic_document & operator=(const basic_document &); public: basic_document() - : member_type(new native_document_type()) - , base_type(member_type::document_impl_.get(), &(member_type::document_impl_->GetAllocator())) - {} + : member_type(new native_document_type()), + base_type(member_type::document_impl_.get(), &(member_type::document_impl_->GetAllocator())) + { + } - void swap(basic_document& other) + void swap(basic_document & other) { base_type::swap(other); member_type::document_impl_.swap(other.document_impl_); } - void parse(const string_ref_type& str) - { - parse<0>(str); - } + void parse(const string_ref_type & str) { parse<0>(str); } template - void parse(const string_ref_type& str) + void parse(const string_ref_type & str) { member_type::document_impl_->template Parse(str.data()); if (member_type::document_impl_->HasParseError()) @@ -1394,85 +1672,83 @@ class basic_document } }; - template -void swap(basic_value_ref& a, basic_value_ref& b) +void swap(basic_value_ref & a, basic_value_ref & b) { a.swap(b); } template -void swap(basic_value& a, basic_value& b) +void swap(basic_value & a, basic_value & b) { a.swap(b); } template -void swap(basic_object& a, basic_object& b) +void swap(basic_object & a, basic_object & b) { a.swap(b); } template -void swap(basic_array& a, basic_array& b) +void swap(basic_array & a, basic_array & b) { a.swap(b); } template -void swap(basic_document& a, basic_document& b) +void swap(basic_document & a, basic_document & b) { a.swap(b); } - template -typename basic_value_ref::string_type str(const basic_value_ref& value) +typename basic_value_ref::string_type str(const basic_value_ref & value) { return value.str(); } template -std::basic_ostream& operator<<(std::basic_ostream& os, const basic_value_ref& value) +std::basic_ostream & operator<<( + std::basic_ostream & os, const basic_value_ref & value) { os << str(value); return os; } - typedef rapidjson::UTF8<> default_encoding; template struct types { - typedef details::value_ref_traits traits; - typedef details::const_value_ref_traits const_traits; - - typedef basic_value_ref value_ref; - typedef const basic_value_ref const_value_ref; - typedef basic_value value; - typedef const basic_value const_value; - typedef basic_object object; - typedef const basic_object const_object; - typedef basic_array array; - typedef const basic_array const_array; - typedef basic_document document; - typedef const basic_document const_document; - typedef typename document::allocator_type allocator; -}; - -typedef types<>::value_ref value_ref; -typedef types<>::const_value_ref const_value_ref; -typedef types<>::value value; -typedef types<>::const_value const_value; -typedef types<>::object object; -typedef types<>::const_object const_object; -typedef types<>::array array; -typedef types<>::const_array const_array; -typedef types<>::document document; -typedef types<>::const_document const_document; -typedef types<>::allocator allocator; + typedef details::value_ref_traits traits; + typedef details::const_value_ref_traits const_traits; + + typedef basic_value_ref value_ref; + typedef const basic_value_ref const_value_ref; + typedef basic_value value; + typedef const basic_value const_value; + typedef basic_object object; + typedef const basic_object const_object; + typedef basic_array array; + typedef const basic_array const_array; + typedef basic_document document; + typedef const basic_document const_document; + typedef typename document::allocator_type allocator; +}; -} +typedef types<>::value_ref value_ref; +typedef types<>::const_value_ref const_value_ref; +typedef types<>::value value; +typedef types<>::const_value const_value; +typedef types<>::object object; +typedef types<>::const_object const_object; +typedef types<>::array array; +typedef types<>::const_array const_array; +typedef types<>::document document; +typedef types<>::const_document const_document; +typedef types<>::allocator allocator; + +} // namespace RABBIT_NAMESPACE #endif \ No newline at end of file diff --git a/openscenario/openscenario_interpreter/src/syntax/act.cpp b/openscenario/openscenario_interpreter/src/syntax/act.cpp index d15606c2e20..3a9065b1806 100644 --- a/openscenario/openscenario_interpreter/src/syntax/act.cpp +++ b/openscenario/openscenario_interpreter/src/syntax/act.cpp @@ -55,7 +55,7 @@ auto operator<<(rabbit::object & json, const Act & datum) -> rabbit::object & json.insert("ManeuverGroup", rabbit::array()); for (auto && maneuver_group : datum.elements) { - rabbit::object act; + rabbit::object act(json.get_allocator()); act << maneuver_group.as(); json["ManeuverGroup"].push_back(std::move(act)); } diff --git a/openscenario/openscenario_interpreter/src/syntax/condition.cpp b/openscenario/openscenario_interpreter/src/syntax/condition.cpp index d07be392125..c719efeec6e 100644 --- a/openscenario/openscenario_interpreter/src/syntax/condition.cpp +++ b/openscenario/openscenario_interpreter/src/syntax/condition.cpp @@ -70,11 +70,11 @@ auto Condition::evaluate() -> Object auto operator<<(rabbit::object & json, const Condition & datum) -> rabbit::object & { - // json["currentEvaluation"] = std::movedatum.description(); + json["currentEvaluation"] = datum.description(); json["currentValue"] = boost::lexical_cast(Boolean(datum.current_value)); - json["name"] = std::move(datum.name); + json["name"] = datum.name; // clang-format off static const std::unordered_map< diff --git a/openscenario/openscenario_interpreter/src/syntax/condition_group.cpp b/openscenario/openscenario_interpreter/src/syntax/condition_group.cpp index 1fa1d0ff446..af53e3527a9 100644 --- a/openscenario/openscenario_interpreter/src/syntax/condition_group.cpp +++ b/openscenario/openscenario_interpreter/src/syntax/condition_group.cpp @@ -42,7 +42,7 @@ auto operator<<(rabbit::object & json, const ConditionGroup & datum) -> rabbit:: json.insert("Condition", rabbit::array()); for (const auto & each : datum) { - rabbit::object condition; + rabbit::object condition(json.get_allocator()); condition << each; json["Condition"].push_back(std::move(condition)); } diff --git a/openscenario/openscenario_interpreter/src/syntax/event.cpp b/openscenario/openscenario_interpreter/src/syntax/event.cpp index 7e55ce66f5b..ff236289bab 100644 --- a/openscenario/openscenario_interpreter/src/syntax/event.cpp +++ b/openscenario/openscenario_interpreter/src/syntax/event.cpp @@ -71,7 +71,7 @@ auto Event::evaluate() -> Object auto operator<<(rabbit::object & json, const Event & datum) -> rabbit::object & { - json["name"] = std::move(datum.name); + json["name"] = datum.name; json["currentState"] = boost::lexical_cast(datum.state()); diff --git a/openscenario/openscenario_interpreter/src/syntax/open_scenario.cpp b/openscenario/openscenario_interpreter/src/syntax/open_scenario.cpp index eac56e16422..9396ffbf250 100644 --- a/openscenario/openscenario_interpreter/src/syntax/open_scenario.cpp +++ b/openscenario/openscenario_interpreter/src/syntax/open_scenario.cpp @@ -63,7 +63,7 @@ auto operator<<(rabbit::object & json, const OpenScenario & datum) -> rabbit::ob if (datum.category.is()) { rabbit::object scenario_definition; scenario_definition << datum.category.as(); - json["OpenSCENARIO"].swap(scenario_definition); + json.insert("OpenSCENARIO", scenario_definition); } return json; diff --git a/openscenario/openscenario_interpreter/src/syntax/private.cpp b/openscenario/openscenario_interpreter/src/syntax/private.cpp index 36d66906f52..71647e4b2e3 100644 --- a/openscenario/openscenario_interpreter/src/syntax/private.cpp +++ b/openscenario/openscenario_interpreter/src/syntax/private.cpp @@ -92,8 +92,8 @@ auto operator<<(rabbit::object & json, const Private & datum) -> rabbit::object json.insert("PrivateAction", rabbit::array()); for (const auto & private_action : datum.private_actions) { - rabbit::object action; - action["type"] = makeTypename(private_action.type()); + rabbit::object action(json.get_allocator()); + action["type"] = std::move(makeTypename(private_action.type())); json["PrivateAction"].push_back(std::move(action)); } diff --git a/openscenario/openscenario_interpreter/src/syntax/storyboard.cpp b/openscenario/openscenario_interpreter/src/syntax/storyboard.cpp index 8f71bd6bdd6..5b07afed74e 100644 --- a/openscenario/openscenario_interpreter/src/syntax/storyboard.cpp +++ b/openscenario/openscenario_interpreter/src/syntax/storyboard.cpp @@ -62,9 +62,8 @@ auto operator<<(rabbit::object & json, const Storyboard & datum) -> rabbit::obje { json["currentState"].set(boost::lexical_cast(datum.state())); - rabbit::object json_init; + rabbit::object json_init = json["Init"]; json_init << datum.init; - json["Init"].swap(json_init); json.insert("Story", rabbit::array());