From cf81aa75b61ab8fab93471ca274f85e6798908aa Mon Sep 17 00:00:00 2001 From: alex Date: Fri, 29 Sep 2023 09:45:55 +0200 Subject: [PATCH 01/21] using allocator for bson --- single_include/nlohmann/json.hpp | 27 ++++++++++++++++++--------- 1 file changed, 18 insertions(+), 9 deletions(-) diff --git a/single_include/nlohmann/json.hpp b/single_include/nlohmann/json.hpp index ac4a1d08b..580c70ef2 100644 --- a/single_include/nlohmann/json.hpp +++ b/single_include/nlohmann/json.hpp @@ -6903,7 +6903,10 @@ class json_sax_dom_parser /// the parsed JSON value BasicJsonType& root; /// stack to model hierarchy of values - std::vector ref_stack {}; + + + using allocator_type = typename BasicJsonType::allocator_type; + std::vector ref_stack {}; /// helper to hold the reference for the next object element BasicJsonType* object_element = nullptr; /// whether a syntax error occurred @@ -8915,8 +8918,10 @@ scan_number_done: position_t position {}; /// raw input token string (for error messages) - std::vector token_string {}; + using allocator_type = typename BasicJsonType::allocator_type; + + std::vector token_string {}; /// buffer for variable-length tokens (numbers, strings) string_t token_buffer {}; @@ -11791,7 +11796,7 @@ class binary_reader case token_type::value_unsigned: return sax->number_unsigned(number_lexer.get_number_unsigned()); case token_type::value_float: - return sax->number_float(number_lexer.get_number_float(), std::move(number_string)); + return sax->number_float(number_lexer.get_number_float(), std::move(static_cast(number_string))); case token_type::uninitialized: case token_type::literal_true: case token_type::literal_false: @@ -12165,7 +12170,7 @@ class parser using string_t = typename BasicJsonType::string_t; using lexer_t = lexer; using token_type = typename lexer_t::token_type; - + using allocator_type = typename BasicJsonType::string_t::allocator_type; public: /// a parser reading from an input adapter explicit parser(InputAdapterType&& adapter, @@ -12279,9 +12284,10 @@ class parser JSON_HEDLEY_NON_NULL(2) bool sax_parse_internal(SAX* sax) { + using allocator_type = typename BasicJsonType::allocator_type; // stack to remember the hierarchy of structured values we are parsing // true = array; false = object - std::vector states; + std::vector states; // value to avoid a goto (see comment where set to true) bool skip_to_state_evaluation = false; @@ -14950,7 +14956,10 @@ class output_adapter public: template> output_adapter(std::vector& vec) - : oa(std::make_shared>(vec)) {} + { + AllocatorType alloc; + oa = std::allocate_shared>(alloc ,vec); + } #ifndef JSON_NO_IO output_adapter(std::basic_ostream& s) @@ -16061,7 +16070,7 @@ class binary_writer const std::size_t embedded_document_size = std::accumulate(std::begin(value), std::end(value), static_cast(0), [&array_index](std::size_t result, const typename BasicJsonType::array_t::value_type & el) { - return result + calc_bson_element_size(std::to_string(array_index++), el); + return result + calc_bson_element_size(static_cast(std::to_string(array_index++)), el); }); return sizeof(std::int32_t) + embedded_document_size + 1ul; @@ -16088,7 +16097,7 @@ class binary_writer for (const auto& el : value) { - write_bson_element(std::to_string(array_index++), el); + write_bson_element(static_cast(std::to_string(array_index++)), el); } oa->write_character(to_char_type(0x00)); @@ -19780,7 +19789,7 @@ class basic_json // NOLINT(cppcoreguidelines-special-member-functions,hicpp-spec if (t == value_t::array || t == value_t::object) { // flatten the current json_value to a heap-allocated stack - std::vector stack; + std::vector> stack; // move the top-level items to stack if (t == value_t::array) From cec4a9f2ebdb8a801a4c947f8368adb1291b257d Mon Sep 17 00:00:00 2001 From: Alex Fishgait Date: Wed, 4 Oct 2023 07:38:50 +0200 Subject: [PATCH 02/21] allocator in ouput adapter --- include/nlohmann/detail/output/binary_writer.hpp | 4 ++-- include/nlohmann/detail/output/output_adapters.hpp | 5 ++++- 2 files changed, 6 insertions(+), 3 deletions(-) diff --git a/include/nlohmann/detail/output/binary_writer.hpp b/include/nlohmann/detail/output/binary_writer.hpp index d3495a4bb..232e97d5c 100644 --- a/include/nlohmann/detail/output/binary_writer.hpp +++ b/include/nlohmann/detail/output/binary_writer.hpp @@ -1110,7 +1110,7 @@ class binary_writer const std::size_t embedded_document_size = std::accumulate(std::begin(value), std::end(value), static_cast(0), [&array_index](std::size_t result, const typename BasicJsonType::array_t::value_type & el) { - return result + calc_bson_element_size(std::to_string(array_index++), el); + return result + calc_bson_element_size(static_cast(std::to_string(array_index++)), el); }); return sizeof(std::int32_t) + embedded_document_size + 1ul; @@ -1137,7 +1137,7 @@ class binary_writer for (const auto& el : value) { - write_bson_element(std::to_string(array_index++), el); + write_bson_element(static_cast(std::to_string(array_index++)), el); } oa->write_character(to_char_type(0x00)); diff --git a/include/nlohmann/detail/output/output_adapters.hpp b/include/nlohmann/detail/output/output_adapters.hpp index 630bd8f73..0f5934035 100644 --- a/include/nlohmann/detail/output/output_adapters.hpp +++ b/include/nlohmann/detail/output/output_adapters.hpp @@ -124,7 +124,10 @@ class output_adapter public: template> output_adapter(std::vector& vec) - : oa(std::make_shared>(vec)) {} + { + AllocatorType alloc; + oa = std::allocate_shared>(alloc, vec); + } #ifndef JSON_NO_IO output_adapter(std::basic_ostream& s) From 05b495928b2b0ebf5ea2919208ead2d6e12684c2 Mon Sep 17 00:00:00 2001 From: Alex Fishgait Date: Fri, 6 Oct 2023 10:00:52 +0200 Subject: [PATCH 03/21] different types of allocators --- .../nlohmann/detail/input/binary_reader.hpp | 11 ++-- include/nlohmann/detail/input/json_sax.hpp | 4 +- include/nlohmann/detail/input/lexer.hpp | 4 +- include/nlohmann/detail/input/parser.hpp | 8 +-- include/nlohmann/json.hpp | 60 +++++++++---------- 5 files changed, 44 insertions(+), 43 deletions(-) diff --git a/include/nlohmann/detail/input/binary_reader.hpp b/include/nlohmann/detail/input/binary_reader.hpp index 263fdb525..bb6d2edbd 100644 --- a/include/nlohmann/detail/input/binary_reader.hpp +++ b/include/nlohmann/detail/input/binary_reader.hpp @@ -62,7 +62,8 @@ static inline bool little_endianness(int num = 1) noexcept /*! @brief deserialization of CBOR, MessagePack, and UBJSON values */ -template> +template + , typename SAX = json_sax_dom_parser> class binary_reader { using number_integer_t = typename BasicJsonType::number_integer_t; @@ -2694,7 +2695,7 @@ class binary_reader // parse number string using ia_type = decltype(detail::input_adapter(number_vector)); - auto number_lexer = detail::lexer(detail::input_adapter(number_vector), false); + auto number_lexer = detail::lexer(detail::input_adapter(number_vector), false); const auto result_number = number_lexer.scan(); const auto number_string = number_lexer.get_token_string(); const auto result_remainder = number_lexer.scan(); @@ -2714,7 +2715,7 @@ class binary_reader case token_type::value_unsigned: return sax->number_unsigned(number_lexer.get_number_unsigned()); case token_type::value_float: - return sax->number_float(number_lexer.get_number_float(), std::move(number_string)); + return sax->number_float(number_lexer.get_number_float(), std::move(static_cast(number_string))); case token_type::uninitialized: case token_type::literal_true: case token_type::literal_false: @@ -3001,8 +3002,8 @@ class binary_reader }; #ifndef JSON_HAS_CPP_17 - template - constexpr std::size_t binary_reader::npos; + template + constexpr std::size_t binary_reader::npos; #endif } // namespace detail diff --git a/include/nlohmann/detail/input/json_sax.hpp b/include/nlohmann/detail/input/json_sax.hpp index ce1c66065..7b5ae5687 100644 --- a/include/nlohmann/detail/input/json_sax.hpp +++ b/include/nlohmann/detail/input/json_sax.hpp @@ -157,7 +157,7 @@ constructor contains the parsed value. @tparam BasicJsonType the JSON type */ -template +template> class json_sax_dom_parser { public: @@ -331,7 +331,7 @@ class json_sax_dom_parser /// the parsed JSON value BasicJsonType& root; /// stack to model hierarchy of values - std::vector ref_stack {}; + std::vector ref_stack {}; /// helper to hold the reference for the next object element BasicJsonType* object_element = nullptr; /// whether a syntax error occurred diff --git a/include/nlohmann/detail/input/lexer.hpp b/include/nlohmann/detail/input/lexer.hpp index 50fc9df59..fda9531b7 100644 --- a/include/nlohmann/detail/input/lexer.hpp +++ b/include/nlohmann/detail/input/lexer.hpp @@ -107,7 +107,7 @@ class lexer_base This class organizes the lexical analysis during JSON deserialization. */ -template +template> class lexer : public lexer_base { using number_integer_t = typename BasicJsonType::number_integer_t; @@ -1611,7 +1611,7 @@ scan_number_done: position_t position {}; /// raw input token string (for error messages) - std::vector token_string {}; + std::vector token_string {}; /// buffer for variable-length tokens (numbers, strings) string_t token_buffer {}; diff --git a/include/nlohmann/detail/input/parser.hpp b/include/nlohmann/detail/input/parser.hpp index 8acbd4fca..dfa80ff1c 100644 --- a/include/nlohmann/detail/input/parser.hpp +++ b/include/nlohmann/detail/input/parser.hpp @@ -56,14 +56,14 @@ using parser_callback_t = This class implements a recursive descent parser. */ -template +template class parser { using number_integer_t = typename BasicJsonType::number_integer_t; using number_unsigned_t = typename BasicJsonType::number_unsigned_t; using number_float_t = typename BasicJsonType::number_float_t; using string_t = typename BasicJsonType::string_t; - using lexer_t = lexer; + using lexer_t = lexer; using token_type = typename lexer_t::token_type; public: @@ -122,7 +122,7 @@ class parser } else { - json_sax_dom_parser sdp(result, allow_exceptions); + json_sax_dom_parser sdp(result, allow_exceptions); sax_parse_internal(&sdp); // in strict mode, input must be completely read @@ -181,7 +181,7 @@ class parser { // stack to remember the hierarchy of structured values we are parsing // true = array; false = object - std::vector states; + std::vector states; // value to avoid a goto (see comment where set to true) bool skip_to_state_evaluation = false; diff --git a/include/nlohmann/json.hpp b/include/nlohmann/json.hpp index f7fac1fb4..61ba350c1 100644 --- a/include/nlohmann/json.hpp +++ b/include/nlohmann/json.hpp @@ -105,16 +105,16 @@ class basic_json // NOLINT(cppcoreguidelines-special-member-functions,hicpp-spec // can be restored when json_pointer backwards compatibility is removed // friend ::nlohmann::json_pointer; - template + template friend class ::nlohmann::detail::parser; friend ::nlohmann::detail::serializer; template friend class ::nlohmann::detail::iter_impl; template friend class ::nlohmann::detail::binary_writer; - template + template friend class ::nlohmann::detail::binary_reader; - template + template friend class ::nlohmann::detail::json_sax_dom_parser; template friend class ::nlohmann::detail::json_sax_dom_callback_parser; @@ -129,14 +129,14 @@ class basic_json // NOLINT(cppcoreguidelines-special-member-functions,hicpp-spec using lexer = ::nlohmann::detail::lexer_base; template - static ::nlohmann::detail::parser parser( - InputAdapterType adapter, - detail::parser_callback_tcb = nullptr, - const bool allow_exceptions = true, - const bool ignore_comments = false - ) + static ::nlohmann::detail::parser, AllocatorType> parser( //alex + InputAdapterType adapter, + detail::parser_callback_tcb = nullptr, + const bool allow_exceptions = true, + const bool ignore_comments = false + ) { - return ::nlohmann::detail::parser(std::move(adapter), + return ::nlohmann::detail::parser, AllocatorType>(std::move(adapter), std::move(cb), allow_exceptions, ignore_comments); } @@ -154,7 +154,7 @@ class basic_json // NOLINT(cppcoreguidelines-special-member-functions,hicpp-spec using output_adapter_t = ::nlohmann::detail::output_adapter_t; template - using binary_reader = ::nlohmann::detail::binary_reader; + using binary_reader = ::nlohmann::detail::binary_reader>; template using binary_writer = ::nlohmann::detail::binary_writer; JSON_PRIVATE_UNLESS_TESTED: @@ -218,7 +218,7 @@ class basic_json // NOLINT(cppcoreguidelines-special-member-functions,hicpp-spec /// the allocator type using allocator_type = AllocatorType; - + using allocator_type_ptr = AllocatorType; /// the type of an element pointer using pointer = typename std::allocator_traits::pointer; /// the type of an element const pointer @@ -566,7 +566,7 @@ class basic_json // NOLINT(cppcoreguidelines-special-member-functions,hicpp-spec if (t == value_t::array || t == value_t::object) { // flatten the current json_value to a heap-allocated stack - std::vector stack; + std::vector> stack; // move the top-level items to stack if (t == value_t::array) @@ -4087,7 +4087,7 @@ class basic_json // NOLINT(cppcoreguidelines-special-member-functions,hicpp-spec auto ia = detail::input_adapter(std::forward(i)); return format == input_format_t::json ? parser(std::move(ia), nullptr, true, ignore_comments).sax_parse(sax, strict) - : detail::binary_reader(std::move(ia), format).sax_parse(format, sax, strict); + : detail::binary_reader(std::move(ia), format).sax_parse(format, sax, strict); } /// @brief generate SAX events @@ -4102,7 +4102,7 @@ class basic_json // NOLINT(cppcoreguidelines-special-member-functions,hicpp-spec auto ia = detail::input_adapter(std::move(first), std::move(last)); return format == input_format_t::json ? parser(std::move(ia), nullptr, true, ignore_comments).sax_parse(sax, strict) - : detail::binary_reader(std::move(ia), format).sax_parse(format, sax, strict); + : detail::binary_reader(std::move(ia), format).sax_parse(format, sax, strict); } /// @brief generate SAX events @@ -4123,7 +4123,7 @@ class basic_json // NOLINT(cppcoreguidelines-special-member-functions,hicpp-spec // NOLINTNEXTLINE(hicpp-move-const-arg,performance-move-const-arg) ? parser(std::move(ia), nullptr, true, ignore_comments).sax_parse(sax, strict) // NOLINTNEXTLINE(hicpp-move-const-arg,performance-move-const-arg) - : detail::binary_reader(std::move(ia), format).sax_parse(format, sax, strict); + : detail::binary_reader(std::move(ia), format).sax_parse(format, sax, strict); } #ifndef JSON_NO_IO /// @brief deserialize from stream @@ -4365,7 +4365,7 @@ class basic_json // NOLINT(cppcoreguidelines-special-member-functions,hicpp-spec const cbor_tag_handler_t tag_handler = cbor_tag_handler_t::error) { basic_json result; - detail::json_sax_dom_parser sdp(result, allow_exceptions); + detail::json_sax_dom_parser sdp(result, allow_exceptions); auto ia = detail::input_adapter(std::forward(i)); const bool res = binary_reader(std::move(ia), input_format_t::cbor).sax_parse(input_format_t::cbor, &sdp, strict, tag_handler); return res ? result : basic_json(value_t::discarded); @@ -4381,7 +4381,7 @@ class basic_json // NOLINT(cppcoreguidelines-special-member-functions,hicpp-spec const cbor_tag_handler_t tag_handler = cbor_tag_handler_t::error) { basic_json result; - detail::json_sax_dom_parser sdp(result, allow_exceptions); + detail::json_sax_dom_parser sdp(result, allow_exceptions); auto ia = detail::input_adapter(std::move(first), std::move(last)); const bool res = binary_reader(std::move(ia), input_format_t::cbor).sax_parse(input_format_t::cbor, &sdp, strict, tag_handler); return res ? result : basic_json(value_t::discarded); @@ -4406,7 +4406,7 @@ class basic_json // NOLINT(cppcoreguidelines-special-member-functions,hicpp-spec const cbor_tag_handler_t tag_handler = cbor_tag_handler_t::error) { basic_json result; - detail::json_sax_dom_parser sdp(result, allow_exceptions); + detail::json_sax_dom_parser sdp(result, allow_exceptions); auto ia = i.get(); // NOLINTNEXTLINE(hicpp-move-const-arg,performance-move-const-arg) const bool res = binary_reader(std::move(ia), input_format_t::cbor).sax_parse(input_format_t::cbor, &sdp, strict, tag_handler); @@ -4422,7 +4422,7 @@ class basic_json // NOLINT(cppcoreguidelines-special-member-functions,hicpp-spec const bool allow_exceptions = true) { basic_json result; - detail::json_sax_dom_parser sdp(result, allow_exceptions); + detail::json_sax_dom_parser sdp(result, allow_exceptions); auto ia = detail::input_adapter(std::forward(i)); const bool res = binary_reader(std::move(ia), input_format_t::msgpack).sax_parse(input_format_t::msgpack, &sdp, strict); return res ? result : basic_json(value_t::discarded); @@ -4437,7 +4437,7 @@ class basic_json // NOLINT(cppcoreguidelines-special-member-functions,hicpp-spec const bool allow_exceptions = true) { basic_json result; - detail::json_sax_dom_parser sdp(result, allow_exceptions); + detail::json_sax_dom_parser sdp(result, allow_exceptions); auto ia = detail::input_adapter(std::move(first), std::move(last)); const bool res = binary_reader(std::move(ia), input_format_t::msgpack).sax_parse(input_format_t::msgpack, &sdp, strict); return res ? result : basic_json(value_t::discarded); @@ -4460,7 +4460,7 @@ class basic_json // NOLINT(cppcoreguidelines-special-member-functions,hicpp-spec const bool allow_exceptions = true) { basic_json result; - detail::json_sax_dom_parser sdp(result, allow_exceptions); + detail::json_sax_dom_parser sdp(result, allow_exceptions); auto ia = i.get(); // NOLINTNEXTLINE(hicpp-move-const-arg,performance-move-const-arg) const bool res = binary_reader(std::move(ia), input_format_t::msgpack).sax_parse(input_format_t::msgpack, &sdp, strict); @@ -4476,7 +4476,7 @@ class basic_json // NOLINT(cppcoreguidelines-special-member-functions,hicpp-spec const bool allow_exceptions = true) { basic_json result; - detail::json_sax_dom_parser sdp(result, allow_exceptions); + detail::json_sax_dom_parser sdp(result, allow_exceptions); auto ia = detail::input_adapter(std::forward(i)); const bool res = binary_reader(std::move(ia), input_format_t::ubjson).sax_parse(input_format_t::ubjson, &sdp, strict); return res ? result : basic_json(value_t::discarded); @@ -4491,7 +4491,7 @@ class basic_json // NOLINT(cppcoreguidelines-special-member-functions,hicpp-spec const bool allow_exceptions = true) { basic_json result; - detail::json_sax_dom_parser sdp(result, allow_exceptions); + detail::json_sax_dom_parser sdp(result, allow_exceptions); auto ia = detail::input_adapter(std::move(first), std::move(last)); const bool res = binary_reader(std::move(ia), input_format_t::ubjson).sax_parse(input_format_t::ubjson, &sdp, strict); return res ? result : basic_json(value_t::discarded); @@ -4514,7 +4514,7 @@ class basic_json // NOLINT(cppcoreguidelines-special-member-functions,hicpp-spec const bool allow_exceptions = true) { basic_json result; - detail::json_sax_dom_parser sdp(result, allow_exceptions); + detail::json_sax_dom_parser sdp(result, allow_exceptions); auto ia = i.get(); // NOLINTNEXTLINE(hicpp-move-const-arg,performance-move-const-arg) const bool res = binary_reader(std::move(ia), input_format_t::ubjson).sax_parse(input_format_t::ubjson, &sdp, strict); @@ -4530,7 +4530,7 @@ class basic_json // NOLINT(cppcoreguidelines-special-member-functions,hicpp-spec const bool allow_exceptions = true) { basic_json result; - detail::json_sax_dom_parser sdp(result, allow_exceptions); + detail::json_sax_dom_parser sdp(result, allow_exceptions); auto ia = detail::input_adapter(std::forward(i)); const bool res = binary_reader(std::move(ia), input_format_t::bjdata).sax_parse(input_format_t::bjdata, &sdp, strict); return res ? result : basic_json(value_t::discarded); @@ -4545,7 +4545,7 @@ class basic_json // NOLINT(cppcoreguidelines-special-member-functions,hicpp-spec const bool allow_exceptions = true) { basic_json result; - detail::json_sax_dom_parser sdp(result, allow_exceptions); + detail::json_sax_dom_parser sdp(result, allow_exceptions); auto ia = detail::input_adapter(std::move(first), std::move(last)); const bool res = binary_reader(std::move(ia), input_format_t::bjdata).sax_parse(input_format_t::bjdata, &sdp, strict); return res ? result : basic_json(value_t::discarded); @@ -4560,7 +4560,7 @@ class basic_json // NOLINT(cppcoreguidelines-special-member-functions,hicpp-spec const bool allow_exceptions = true) { basic_json result; - detail::json_sax_dom_parser sdp(result, allow_exceptions); + detail::json_sax_dom_parser sdp(result, allow_exceptions); auto ia = detail::input_adapter(std::forward(i)); const bool res = binary_reader(std::move(ia), input_format_t::bson).sax_parse(input_format_t::bson, &sdp, strict); return res ? result : basic_json(value_t::discarded); @@ -4575,7 +4575,7 @@ class basic_json // NOLINT(cppcoreguidelines-special-member-functions,hicpp-spec const bool allow_exceptions = true) { basic_json result; - detail::json_sax_dom_parser sdp(result, allow_exceptions); + detail::json_sax_dom_parser sdp(result, allow_exceptions); auto ia = detail::input_adapter(std::move(first), std::move(last)); const bool res = binary_reader(std::move(ia), input_format_t::bson).sax_parse(input_format_t::bson, &sdp, strict); return res ? result : basic_json(value_t::discarded); @@ -4598,7 +4598,7 @@ class basic_json // NOLINT(cppcoreguidelines-special-member-functions,hicpp-spec const bool allow_exceptions = true) { basic_json result; - detail::json_sax_dom_parser sdp(result, allow_exceptions); + detail::json_sax_dom_parser sdp(result, allow_exceptions); auto ia = i.get(); // NOLINTNEXTLINE(hicpp-move-const-arg,performance-move-const-arg) const bool res = binary_reader(std::move(ia), input_format_t::bson).sax_parse(input_format_t::bson, &sdp, strict); From 791534ac98112091f67ad465f1eea6f590a6cca1 Mon Sep 17 00:00:00 2001 From: Alex Fishgait Date: Fri, 6 Oct 2023 10:57:01 +0200 Subject: [PATCH 04/21] define allocator_type_ptr --- include/nlohmann/detail/input/binary_reader.hpp | 2 +- include/nlohmann/json.hpp | 6 +++--- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/include/nlohmann/detail/input/binary_reader.hpp b/include/nlohmann/detail/input/binary_reader.hpp index bb6d2edbd..a3b95e9d6 100644 --- a/include/nlohmann/detail/input/binary_reader.hpp +++ b/include/nlohmann/detail/input/binary_reader.hpp @@ -62,7 +62,7 @@ static inline bool little_endianness(int num = 1) noexcept /*! @brief deserialization of CBOR, MessagePack, and UBJSON values */ -template +template , typename SAX = json_sax_dom_parser> class binary_reader { diff --git a/include/nlohmann/json.hpp b/include/nlohmann/json.hpp index 61ba350c1..b04e94206 100644 --- a/include/nlohmann/json.hpp +++ b/include/nlohmann/json.hpp @@ -4087,7 +4087,7 @@ class basic_json // NOLINT(cppcoreguidelines-special-member-functions,hicpp-spec auto ia = detail::input_adapter(std::forward(i)); return format == input_format_t::json ? parser(std::move(ia), nullptr, true, ignore_comments).sax_parse(sax, strict) - : detail::binary_reader(std::move(ia), format).sax_parse(format, sax, strict); + : detail::binary_reader(std::move(ia), format).sax_parse(format, sax, strict); } /// @brief generate SAX events @@ -4102,7 +4102,7 @@ class basic_json // NOLINT(cppcoreguidelines-special-member-functions,hicpp-spec auto ia = detail::input_adapter(std::move(first), std::move(last)); return format == input_format_t::json ? parser(std::move(ia), nullptr, true, ignore_comments).sax_parse(sax, strict) - : detail::binary_reader(std::move(ia), format).sax_parse(format, sax, strict); + : detail::binary_reader(std::move(ia), format).sax_parse(format, sax, strict); } /// @brief generate SAX events @@ -4123,7 +4123,7 @@ class basic_json // NOLINT(cppcoreguidelines-special-member-functions,hicpp-spec // NOLINTNEXTLINE(hicpp-move-const-arg,performance-move-const-arg) ? parser(std::move(ia), nullptr, true, ignore_comments).sax_parse(sax, strict) // NOLINTNEXTLINE(hicpp-move-const-arg,performance-move-const-arg) - : detail::binary_reader(std::move(ia), format).sax_parse(format, sax, strict); + : detail::binary_reader(std::move(ia), format).sax_parse(format, sax, strict); } #ifndef JSON_NO_IO /// @brief deserialize from stream From ba52e6d51d33c39a2ed248dd8b67e92ac40ef6d9 Mon Sep 17 00:00:00 2001 From: Alex Fishgait Date: Mon, 9 Oct 2023 07:58:14 +0200 Subject: [PATCH 05/21] allocator in binary_reader class --- include/nlohmann/detail/input/binary_reader.hpp | 10 +++++----- include/nlohmann/json.hpp | 13 +++++++------ 2 files changed, 12 insertions(+), 11 deletions(-) diff --git a/include/nlohmann/detail/input/binary_reader.hpp b/include/nlohmann/detail/input/binary_reader.hpp index a3b95e9d6..f37f04a72 100644 --- a/include/nlohmann/detail/input/binary_reader.hpp +++ b/include/nlohmann/detail/input/binary_reader.hpp @@ -62,8 +62,8 @@ static inline bool little_endianness(int num = 1) noexcept /*! @brief deserialization of CBOR, MessagePack, and UBJSON values */ -template - , typename SAX = json_sax_dom_parser> +template, typename AllocatorChar = std::allocator< typename InputAdapterType::char_type> + , typename SAX = json_sax_dom_parser> class binary_reader { using number_integer_t = typename BasicJsonType::number_integer_t; @@ -2695,7 +2695,7 @@ class binary_reader // parse number string using ia_type = decltype(detail::input_adapter(number_vector)); - auto number_lexer = detail::lexer(detail::input_adapter(number_vector), false); + auto number_lexer = detail::lexer(detail::input_adapter(number_vector), false); const auto result_number = number_lexer.scan(); const auto number_string = number_lexer.get_token_string(); const auto result_remainder = number_lexer.scan(); @@ -3002,8 +3002,8 @@ class binary_reader }; #ifndef JSON_HAS_CPP_17 - template - constexpr std::size_t binary_reader::npos; + template + constexpr std::size_t binary_reader::npos; #endif } // namespace detail diff --git a/include/nlohmann/json.hpp b/include/nlohmann/json.hpp index b04e94206..6ffa8b867 100644 --- a/include/nlohmann/json.hpp +++ b/include/nlohmann/json.hpp @@ -112,7 +112,7 @@ class basic_json // NOLINT(cppcoreguidelines-special-member-functions,hicpp-spec friend class ::nlohmann::detail::iter_impl; template friend class ::nlohmann::detail::binary_writer; - template + template friend class ::nlohmann::detail::binary_reader; template friend class ::nlohmann::detail::json_sax_dom_parser; @@ -153,8 +153,8 @@ class basic_json // NOLINT(cppcoreguidelines-special-member-functions,hicpp-spec template using output_adapter_t = ::nlohmann::detail::output_adapter_t; - template - using binary_reader = ::nlohmann::detail::binary_reader>; + template + using binary_reader = ::nlohmann::detail::binary_reader, AllocatorType>; template using binary_writer = ::nlohmann::detail::binary_writer; JSON_PRIVATE_UNLESS_TESTED: @@ -219,6 +219,7 @@ class basic_json // NOLINT(cppcoreguidelines-special-member-functions,hicpp-spec /// the allocator type using allocator_type = AllocatorType; using allocator_type_ptr = AllocatorType; + using allocator_type_char = AllocatorType; /// the type of an element pointer using pointer = typename std::allocator_traits::pointer; /// the type of an element const pointer @@ -4087,7 +4088,7 @@ class basic_json // NOLINT(cppcoreguidelines-special-member-functions,hicpp-spec auto ia = detail::input_adapter(std::forward(i)); return format == input_format_t::json ? parser(std::move(ia), nullptr, true, ignore_comments).sax_parse(sax, strict) - : detail::binary_reader(std::move(ia), format).sax_parse(format, sax, strict); + : detail::binary_reader(std::move(ia), format).sax_parse(format, sax, strict); } /// @brief generate SAX events @@ -4102,7 +4103,7 @@ class basic_json // NOLINT(cppcoreguidelines-special-member-functions,hicpp-spec auto ia = detail::input_adapter(std::move(first), std::move(last)); return format == input_format_t::json ? parser(std::move(ia), nullptr, true, ignore_comments).sax_parse(sax, strict) - : detail::binary_reader(std::move(ia), format).sax_parse(format, sax, strict); + : detail::binary_reader(std::move(ia), format).sax_parse(format, sax, strict); } /// @brief generate SAX events @@ -4123,7 +4124,7 @@ class basic_json // NOLINT(cppcoreguidelines-special-member-functions,hicpp-spec // NOLINTNEXTLINE(hicpp-move-const-arg,performance-move-const-arg) ? parser(std::move(ia), nullptr, true, ignore_comments).sax_parse(sax, strict) // NOLINTNEXTLINE(hicpp-move-const-arg,performance-move-const-arg) - : detail::binary_reader(std::move(ia), format).sax_parse(format, sax, strict); + : detail::binary_reader(std::move(ia), format).sax_parse(format, sax, strict); } #ifndef JSON_NO_IO /// @brief deserialize from stream From c911eb342bf87ef96c4d58f69b126dbb794920f1 Mon Sep 17 00:00:00 2001 From: Alex Fishgait Date: Mon, 9 Oct 2023 10:42:09 +0200 Subject: [PATCH 06/21] adding unit test for rtstring with allocator --- tests/src/unit-allocator.cpp | 160 +++++++++++++++++++++++++++++++++++ 1 file changed, 160 insertions(+) diff --git a/tests/src/unit-allocator.cpp b/tests/src/unit-allocator.cpp index f9a25b24f..2553a4bed 100644 --- a/tests/src/unit-allocator.cpp +++ b/tests/src/unit-allocator.cpp @@ -261,3 +261,163 @@ TEST_CASE("bad my_allocator::construct") j["test"].push_back("should not leak"); } } + +namespace{ + template + struct NAlloc { + // Aliases and inner types + using value_type = T; + using size_type = std::size_t; + using difference_type = ptrdiff_t; + using reference = value_type&; + using const_reference = const value_type&; + using pointer = value_type*; + using const_pointer = const value_type*; + + template + struct rebind { + using other = NAlloc; + }; + + NAlloc() : + alloc() { + + }; + + virtual ~NAlloc() { } + + pointer allocate(std::size_t n) { + #ifdef _DEBUG + std::cout << "NAlloc allocating " << n << " bytes\n"; + #endif + + return static_cast(alloc.allocate(n)); // get memory from pool + } + void deallocate(pointer p, [[maybe_unused]] std::size_t n) { + #ifdef _DEBUG + std::cout << "NAlloc deallocating " << n * sizeof *p << " bytes\n"; + #endif + + alloc.deallocate(static_cast(p), 1); // return memory to pool + } + + + bool operator!=(const NAlloc& other) const { + return !(*this == other); + } + bool operator==(const NAlloc& other) const { + return alloc == other.alloc; + } + + my_allocator alloc; + }; + + + using OstringStream = std::basic_ostringstream, NAlloc>; + using IstringStream = std::basic_istringstream, NAlloc>; + typedef std::basic_string, NAlloc> RtString; + + +} + +TEST_CASE("controlled bad_alloc_rt_string") +{ + // create JSON type using the throwing allocator + using my_json = nlohmann::basic_json; + + + + SECTION("class json_value") + { + + SECTION("json_value(value_t)") + { + SECTION("object") + { + next_construct_fails = false; + auto t = my_json::value_t::object; + CHECK_NOTHROW(my_allocator_clean_up(my_json::json_value(t).object)); + next_construct_fails = true; + CHECK_THROWS_AS(my_json::json_value(t), std::bad_alloc&); + next_construct_fails = false; + } + SECTION("array") + { + next_construct_fails = false; + auto t = my_json::value_t::array; + CHECK_NOTHROW(my_allocator_clean_up(my_json::json_value(t).array)); + next_construct_fails = true; + CHECK_THROWS_AS(my_json::json_value(t), std::bad_alloc&); + next_construct_fails = false; + } + SECTION("string") + { + next_construct_fails = false; + auto t = my_json::value_t::string; + CHECK_NOTHROW(my_allocator_clean_up(my_json::json_value(t).string)); + next_construct_fails = true; + CHECK_THROWS_AS(my_json::json_value(t), std::bad_alloc&); + next_construct_fails = false; + } + } + + SECTION("json_value(const string_t&)") + { + next_construct_fails = false; + const my_json::string_t v("foo"); + CHECK_NOTHROW(my_allocator_clean_up(my_json::json_value(v).string)); + next_construct_fails = true; + CHECK_THROWS_AS(my_json::json_value(v), std::bad_alloc&); + next_construct_fails = false; + } + } + + SECTION("class basic_json_rt_string") + { + SECTION("basic_json(const CompatibleObjectType&)") + { + next_construct_fails = false; + const std::map v {{"foo", "bar"}}; + CHECK_NOTHROW(my_json(v)); + next_construct_fails = true; + CHECK_THROWS_AS(my_json(v), std::bad_alloc&); + next_construct_fails = false; + } + + SECTION("basic_json(const CompatibleArrayType&)") + { + next_construct_fails = false; + const std::vector v {"foo", "bar", "baz"}; + CHECK_NOTHROW(my_json(v)); + next_construct_fails = true; + CHECK_THROWS_AS(my_json(v), std::bad_alloc&); + next_construct_fails = false; + } + + SECTION("basic_json(const typename string_t::value_type*)") + { + next_construct_fails = false; + CHECK_NOTHROW(my_json("foo")); + next_construct_fails = true; + CHECK_THROWS_AS(my_json("foo"), std::bad_alloc&); + next_construct_fails = false; + } + + SECTION("basic_json(const typename string_t::value_type*)") + { + next_construct_fails = false; + const RtString s("foo"); + CHECK_NOTHROW(my_json(s)); + next_construct_fails = true; + CHECK_THROWS_AS(my_json(s), std::bad_alloc&); + next_construct_fails = false; + } + } +} \ No newline at end of file From 87776b37816193f593cbe813d4f62646749c3025 Mon Sep 17 00:00:00 2001 From: Alex Fishgait Date: Mon, 9 Oct 2023 11:32:16 +0200 Subject: [PATCH 07/21] json after amalgamate --- single_include/nlohmann/json.hpp | 88 +++++++++++++++----------------- 1 file changed, 42 insertions(+), 46 deletions(-) diff --git a/single_include/nlohmann/json.hpp b/single_include/nlohmann/json.hpp index 580c70ef2..de0380586 100644 --- a/single_include/nlohmann/json.hpp +++ b/single_include/nlohmann/json.hpp @@ -6729,7 +6729,7 @@ constructor contains the parsed value. @tparam BasicJsonType the JSON type */ -template +template> class json_sax_dom_parser { public: @@ -6903,10 +6903,7 @@ class json_sax_dom_parser /// the parsed JSON value BasicJsonType& root; /// stack to model hierarchy of values - - - using allocator_type = typename BasicJsonType::allocator_type; - std::vector ref_stack {}; + std::vector ref_stack {}; /// helper to hold the reference for the next object element BasicJsonType* object_element = nullptr; /// whether a syntax error occurred @@ -7414,7 +7411,7 @@ class lexer_base This class organizes the lexical analysis during JSON deserialization. */ -template +template> class lexer : public lexer_base { using number_integer_t = typename BasicJsonType::number_integer_t; @@ -8918,10 +8915,8 @@ scan_number_done: position_t position {}; /// raw input token string (for error messages) + std::vector token_string {}; - using allocator_type = typename BasicJsonType::allocator_type; - - std::vector token_string {}; /// buffer for variable-length tokens (numbers, strings) string_t token_buffer {}; @@ -9144,7 +9139,8 @@ static inline bool little_endianness(int num = 1) noexcept /*! @brief deserialization of CBOR, MessagePack, and UBJSON values */ -template> +template, typename AllocatorChar = std::allocator< typename InputAdapterType::char_type> + , typename SAX = json_sax_dom_parser> class binary_reader { using number_integer_t = typename BasicJsonType::number_integer_t; @@ -11776,7 +11772,7 @@ class binary_reader // parse number string using ia_type = decltype(detail::input_adapter(number_vector)); - auto number_lexer = detail::lexer(detail::input_adapter(number_vector), false); + auto number_lexer = detail::lexer(detail::input_adapter(number_vector), false); const auto result_number = number_lexer.scan(); const auto number_string = number_lexer.get_token_string(); const auto result_remainder = number_lexer.scan(); @@ -12083,8 +12079,8 @@ class binary_reader }; #ifndef JSON_HAS_CPP_17 - template - constexpr std::size_t binary_reader::npos; + template + constexpr std::size_t binary_reader::npos; #endif } // namespace detail @@ -12161,16 +12157,16 @@ using parser_callback_t = This class implements a recursive descent parser. */ -template +template class parser { using number_integer_t = typename BasicJsonType::number_integer_t; using number_unsigned_t = typename BasicJsonType::number_unsigned_t; using number_float_t = typename BasicJsonType::number_float_t; using string_t = typename BasicJsonType::string_t; - using lexer_t = lexer; + using lexer_t = lexer; using token_type = typename lexer_t::token_type; - using allocator_type = typename BasicJsonType::string_t::allocator_type; + public: /// a parser reading from an input adapter explicit parser(InputAdapterType&& adapter, @@ -12227,7 +12223,7 @@ class parser } else { - json_sax_dom_parser sdp(result, allow_exceptions); + json_sax_dom_parser sdp(result, allow_exceptions); sax_parse_internal(&sdp); // in strict mode, input must be completely read @@ -12284,10 +12280,9 @@ class parser JSON_HEDLEY_NON_NULL(2) bool sax_parse_internal(SAX* sax) { - using allocator_type = typename BasicJsonType::allocator_type; // stack to remember the hierarchy of structured values we are parsing // true = array; false = object - std::vector states; + std::vector states; // value to avoid a goto (see comment where set to true) bool skip_to_state_evaluation = false; @@ -14958,7 +14953,7 @@ class output_adapter output_adapter(std::vector& vec) { AllocatorType alloc; - oa = std::allocate_shared>(alloc ,vec); + oa = std::allocate_shared>(alloc, vec); } #ifndef JSON_NO_IO @@ -19328,16 +19323,16 @@ class basic_json // NOLINT(cppcoreguidelines-special-member-functions,hicpp-spec // can be restored when json_pointer backwards compatibility is removed // friend ::nlohmann::json_pointer; - template + template friend class ::nlohmann::detail::parser; friend ::nlohmann::detail::serializer; template friend class ::nlohmann::detail::iter_impl; template friend class ::nlohmann::detail::binary_writer; - template + template friend class ::nlohmann::detail::binary_reader; - template + template friend class ::nlohmann::detail::json_sax_dom_parser; template friend class ::nlohmann::detail::json_sax_dom_callback_parser; @@ -19352,14 +19347,14 @@ class basic_json // NOLINT(cppcoreguidelines-special-member-functions,hicpp-spec using lexer = ::nlohmann::detail::lexer_base; template - static ::nlohmann::detail::parser parser( + static ::nlohmann::detail::parser, AllocatorType> parser( //alex InputAdapterType adapter, detail::parser_callback_tcb = nullptr, const bool allow_exceptions = true, const bool ignore_comments = false ) { - return ::nlohmann::detail::parser(std::move(adapter), + return ::nlohmann::detail::parser, AllocatorType>(std::move(adapter), std::move(cb), allow_exceptions, ignore_comments); } @@ -19376,8 +19371,8 @@ class basic_json // NOLINT(cppcoreguidelines-special-member-functions,hicpp-spec template using output_adapter_t = ::nlohmann::detail::output_adapter_t; - template - using binary_reader = ::nlohmann::detail::binary_reader; + template + using binary_reader = ::nlohmann::detail::binary_reader, AllocatorType>; template using binary_writer = ::nlohmann::detail::binary_writer; JSON_PRIVATE_UNLESS_TESTED: @@ -19441,7 +19436,8 @@ class basic_json // NOLINT(cppcoreguidelines-special-member-functions,hicpp-spec /// the allocator type using allocator_type = AllocatorType; - + using allocator_type_ptr = AllocatorType; + using allocator_type_char = AllocatorType; /// the type of an element pointer using pointer = typename std::allocator_traits::pointer; /// the type of an element const pointer @@ -19789,7 +19785,7 @@ class basic_json // NOLINT(cppcoreguidelines-special-member-functions,hicpp-spec if (t == value_t::array || t == value_t::object) { // flatten the current json_value to a heap-allocated stack - std::vector> stack; + std::vector> stack; // move the top-level items to stack if (t == value_t::array) @@ -23310,7 +23306,7 @@ class basic_json // NOLINT(cppcoreguidelines-special-member-functions,hicpp-spec auto ia = detail::input_adapter(std::forward(i)); return format == input_format_t::json ? parser(std::move(ia), nullptr, true, ignore_comments).sax_parse(sax, strict) - : detail::binary_reader(std::move(ia), format).sax_parse(format, sax, strict); + : detail::binary_reader(std::move(ia), format).sax_parse(format, sax, strict); } /// @brief generate SAX events @@ -23325,7 +23321,7 @@ class basic_json // NOLINT(cppcoreguidelines-special-member-functions,hicpp-spec auto ia = detail::input_adapter(std::move(first), std::move(last)); return format == input_format_t::json ? parser(std::move(ia), nullptr, true, ignore_comments).sax_parse(sax, strict) - : detail::binary_reader(std::move(ia), format).sax_parse(format, sax, strict); + : detail::binary_reader(std::move(ia), format).sax_parse(format, sax, strict); } /// @brief generate SAX events @@ -23346,7 +23342,7 @@ class basic_json // NOLINT(cppcoreguidelines-special-member-functions,hicpp-spec // NOLINTNEXTLINE(hicpp-move-const-arg,performance-move-const-arg) ? parser(std::move(ia), nullptr, true, ignore_comments).sax_parse(sax, strict) // NOLINTNEXTLINE(hicpp-move-const-arg,performance-move-const-arg) - : detail::binary_reader(std::move(ia), format).sax_parse(format, sax, strict); + : detail::binary_reader(std::move(ia), format).sax_parse(format, sax, strict); } #ifndef JSON_NO_IO /// @brief deserialize from stream @@ -23588,7 +23584,7 @@ class basic_json // NOLINT(cppcoreguidelines-special-member-functions,hicpp-spec const cbor_tag_handler_t tag_handler = cbor_tag_handler_t::error) { basic_json result; - detail::json_sax_dom_parser sdp(result, allow_exceptions); + detail::json_sax_dom_parser sdp(result, allow_exceptions); auto ia = detail::input_adapter(std::forward(i)); const bool res = binary_reader(std::move(ia), input_format_t::cbor).sax_parse(input_format_t::cbor, &sdp, strict, tag_handler); return res ? result : basic_json(value_t::discarded); @@ -23604,7 +23600,7 @@ class basic_json // NOLINT(cppcoreguidelines-special-member-functions,hicpp-spec const cbor_tag_handler_t tag_handler = cbor_tag_handler_t::error) { basic_json result; - detail::json_sax_dom_parser sdp(result, allow_exceptions); + detail::json_sax_dom_parser sdp(result, allow_exceptions); auto ia = detail::input_adapter(std::move(first), std::move(last)); const bool res = binary_reader(std::move(ia), input_format_t::cbor).sax_parse(input_format_t::cbor, &sdp, strict, tag_handler); return res ? result : basic_json(value_t::discarded); @@ -23629,7 +23625,7 @@ class basic_json // NOLINT(cppcoreguidelines-special-member-functions,hicpp-spec const cbor_tag_handler_t tag_handler = cbor_tag_handler_t::error) { basic_json result; - detail::json_sax_dom_parser sdp(result, allow_exceptions); + detail::json_sax_dom_parser sdp(result, allow_exceptions); auto ia = i.get(); // NOLINTNEXTLINE(hicpp-move-const-arg,performance-move-const-arg) const bool res = binary_reader(std::move(ia), input_format_t::cbor).sax_parse(input_format_t::cbor, &sdp, strict, tag_handler); @@ -23645,7 +23641,7 @@ class basic_json // NOLINT(cppcoreguidelines-special-member-functions,hicpp-spec const bool allow_exceptions = true) { basic_json result; - detail::json_sax_dom_parser sdp(result, allow_exceptions); + detail::json_sax_dom_parser sdp(result, allow_exceptions); auto ia = detail::input_adapter(std::forward(i)); const bool res = binary_reader(std::move(ia), input_format_t::msgpack).sax_parse(input_format_t::msgpack, &sdp, strict); return res ? result : basic_json(value_t::discarded); @@ -23660,7 +23656,7 @@ class basic_json // NOLINT(cppcoreguidelines-special-member-functions,hicpp-spec const bool allow_exceptions = true) { basic_json result; - detail::json_sax_dom_parser sdp(result, allow_exceptions); + detail::json_sax_dom_parser sdp(result, allow_exceptions); auto ia = detail::input_adapter(std::move(first), std::move(last)); const bool res = binary_reader(std::move(ia), input_format_t::msgpack).sax_parse(input_format_t::msgpack, &sdp, strict); return res ? result : basic_json(value_t::discarded); @@ -23683,7 +23679,7 @@ class basic_json // NOLINT(cppcoreguidelines-special-member-functions,hicpp-spec const bool allow_exceptions = true) { basic_json result; - detail::json_sax_dom_parser sdp(result, allow_exceptions); + detail::json_sax_dom_parser sdp(result, allow_exceptions); auto ia = i.get(); // NOLINTNEXTLINE(hicpp-move-const-arg,performance-move-const-arg) const bool res = binary_reader(std::move(ia), input_format_t::msgpack).sax_parse(input_format_t::msgpack, &sdp, strict); @@ -23699,7 +23695,7 @@ class basic_json // NOLINT(cppcoreguidelines-special-member-functions,hicpp-spec const bool allow_exceptions = true) { basic_json result; - detail::json_sax_dom_parser sdp(result, allow_exceptions); + detail::json_sax_dom_parser sdp(result, allow_exceptions); auto ia = detail::input_adapter(std::forward(i)); const bool res = binary_reader(std::move(ia), input_format_t::ubjson).sax_parse(input_format_t::ubjson, &sdp, strict); return res ? result : basic_json(value_t::discarded); @@ -23714,7 +23710,7 @@ class basic_json // NOLINT(cppcoreguidelines-special-member-functions,hicpp-spec const bool allow_exceptions = true) { basic_json result; - detail::json_sax_dom_parser sdp(result, allow_exceptions); + detail::json_sax_dom_parser sdp(result, allow_exceptions); auto ia = detail::input_adapter(std::move(first), std::move(last)); const bool res = binary_reader(std::move(ia), input_format_t::ubjson).sax_parse(input_format_t::ubjson, &sdp, strict); return res ? result : basic_json(value_t::discarded); @@ -23737,7 +23733,7 @@ class basic_json // NOLINT(cppcoreguidelines-special-member-functions,hicpp-spec const bool allow_exceptions = true) { basic_json result; - detail::json_sax_dom_parser sdp(result, allow_exceptions); + detail::json_sax_dom_parser sdp(result, allow_exceptions); auto ia = i.get(); // NOLINTNEXTLINE(hicpp-move-const-arg,performance-move-const-arg) const bool res = binary_reader(std::move(ia), input_format_t::ubjson).sax_parse(input_format_t::ubjson, &sdp, strict); @@ -23753,7 +23749,7 @@ class basic_json // NOLINT(cppcoreguidelines-special-member-functions,hicpp-spec const bool allow_exceptions = true) { basic_json result; - detail::json_sax_dom_parser sdp(result, allow_exceptions); + detail::json_sax_dom_parser sdp(result, allow_exceptions); auto ia = detail::input_adapter(std::forward(i)); const bool res = binary_reader(std::move(ia), input_format_t::bjdata).sax_parse(input_format_t::bjdata, &sdp, strict); return res ? result : basic_json(value_t::discarded); @@ -23768,7 +23764,7 @@ class basic_json // NOLINT(cppcoreguidelines-special-member-functions,hicpp-spec const bool allow_exceptions = true) { basic_json result; - detail::json_sax_dom_parser sdp(result, allow_exceptions); + detail::json_sax_dom_parser sdp(result, allow_exceptions); auto ia = detail::input_adapter(std::move(first), std::move(last)); const bool res = binary_reader(std::move(ia), input_format_t::bjdata).sax_parse(input_format_t::bjdata, &sdp, strict); return res ? result : basic_json(value_t::discarded); @@ -23783,7 +23779,7 @@ class basic_json // NOLINT(cppcoreguidelines-special-member-functions,hicpp-spec const bool allow_exceptions = true) { basic_json result; - detail::json_sax_dom_parser sdp(result, allow_exceptions); + detail::json_sax_dom_parser sdp(result, allow_exceptions); auto ia = detail::input_adapter(std::forward(i)); const bool res = binary_reader(std::move(ia), input_format_t::bson).sax_parse(input_format_t::bson, &sdp, strict); return res ? result : basic_json(value_t::discarded); @@ -23798,7 +23794,7 @@ class basic_json // NOLINT(cppcoreguidelines-special-member-functions,hicpp-spec const bool allow_exceptions = true) { basic_json result; - detail::json_sax_dom_parser sdp(result, allow_exceptions); + detail::json_sax_dom_parser sdp(result, allow_exceptions); auto ia = detail::input_adapter(std::move(first), std::move(last)); const bool res = binary_reader(std::move(ia), input_format_t::bson).sax_parse(input_format_t::bson, &sdp, strict); return res ? result : basic_json(value_t::discarded); @@ -23821,7 +23817,7 @@ class basic_json // NOLINT(cppcoreguidelines-special-member-functions,hicpp-spec const bool allow_exceptions = true) { basic_json result; - detail::json_sax_dom_parser sdp(result, allow_exceptions); + detail::json_sax_dom_parser sdp(result, allow_exceptions); auto ia = i.get(); // NOLINTNEXTLINE(hicpp-move-const-arg,performance-move-const-arg) const bool res = binary_reader(std::move(ia), input_format_t::bson).sax_parse(input_format_t::bson, &sdp, strict); From 0f35c88a37e24851a8169cbca90162d9637facc9 Mon Sep 17 00:00:00 2001 From: Alex Fishgait Date: Mon, 9 Oct 2023 11:35:05 +0200 Subject: [PATCH 08/21] remove comment --- include/nlohmann/json.hpp | 2 +- single_include/nlohmann/json.hpp | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/include/nlohmann/json.hpp b/include/nlohmann/json.hpp index 6ffa8b867..7a1c8ac71 100644 --- a/include/nlohmann/json.hpp +++ b/include/nlohmann/json.hpp @@ -129,7 +129,7 @@ class basic_json // NOLINT(cppcoreguidelines-special-member-functions,hicpp-spec using lexer = ::nlohmann::detail::lexer_base; template - static ::nlohmann::detail::parser, AllocatorType> parser( //alex + static ::nlohmann::detail::parser, AllocatorType> parser( InputAdapterType adapter, detail::parser_callback_tcb = nullptr, const bool allow_exceptions = true, diff --git a/single_include/nlohmann/json.hpp b/single_include/nlohmann/json.hpp index de0380586..aad80d440 100644 --- a/single_include/nlohmann/json.hpp +++ b/single_include/nlohmann/json.hpp @@ -19347,7 +19347,7 @@ class basic_json // NOLINT(cppcoreguidelines-special-member-functions,hicpp-spec using lexer = ::nlohmann::detail::lexer_base; template - static ::nlohmann::detail::parser, AllocatorType> parser( //alex + static ::nlohmann::detail::parser, AllocatorType> parser( InputAdapterType adapter, detail::parser_callback_tcb = nullptr, const bool allow_exceptions = true, From 4a08806397955aadc3918e77279f87c66b804046 Mon Sep 17 00:00:00 2001 From: Alex Fishgait Date: Mon, 9 Oct 2023 11:35:27 +0200 Subject: [PATCH 09/21] remove comment 2 --- include/nlohmann/json.hpp | 2 +- single_include/nlohmann/json.hpp | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/include/nlohmann/json.hpp b/include/nlohmann/json.hpp index 7a1c8ac71..504117bf1 100644 --- a/include/nlohmann/json.hpp +++ b/include/nlohmann/json.hpp @@ -129,7 +129,7 @@ class basic_json // NOLINT(cppcoreguidelines-special-member-functions,hicpp-spec using lexer = ::nlohmann::detail::lexer_base; template - static ::nlohmann::detail::parser, AllocatorType> parser( + static ::nlohmann::detail::parser, AllocatorType> parser( InputAdapterType adapter, detail::parser_callback_tcb = nullptr, const bool allow_exceptions = true, diff --git a/single_include/nlohmann/json.hpp b/single_include/nlohmann/json.hpp index aad80d440..7caa58635 100644 --- a/single_include/nlohmann/json.hpp +++ b/single_include/nlohmann/json.hpp @@ -19347,7 +19347,7 @@ class basic_json // NOLINT(cppcoreguidelines-special-member-functions,hicpp-spec using lexer = ::nlohmann::detail::lexer_base; template - static ::nlohmann::detail::parser, AllocatorType> parser( + static ::nlohmann::detail::parser, AllocatorType> parser( InputAdapterType adapter, detail::parser_callback_tcb = nullptr, const bool allow_exceptions = true, From 8bbece686df806c0bdb898d011e34594753fae81 Mon Sep 17 00:00:00 2001 From: Alex Fishgait Date: Mon, 9 Oct 2023 13:29:28 +0200 Subject: [PATCH 10/21] remove std::cout --- tests/src/unit-allocator.cpp | 108 ++++++++++++++++++----------------- 1 file changed, 56 insertions(+), 52 deletions(-) diff --git a/tests/src/unit-allocator.cpp b/tests/src/unit-allocator.cpp index 2553a4bed..6de79871c 100644 --- a/tests/src/unit-allocator.cpp +++ b/tests/src/unit-allocator.cpp @@ -262,60 +262,64 @@ TEST_CASE("bad my_allocator::construct") } } -namespace{ - template - struct NAlloc { - // Aliases and inner types - using value_type = T; - using size_type = std::size_t; - using difference_type = ptrdiff_t; - using reference = value_type&; - using const_reference = const value_type&; - using pointer = value_type*; - using const_pointer = const value_type*; +namespace +{ +template +struct NAlloc +{ + // Aliases and inner types + using value_type = T; + using size_type = std::size_t; + using difference_type = ptrdiff_t; + using reference = value_type&; + using const_reference = const value_type&; + using pointer = value_type*; + using const_pointer = const value_type*; - template - struct rebind { - using other = NAlloc; - }; - - NAlloc() : - alloc() { - - }; - - virtual ~NAlloc() { } - - pointer allocate(std::size_t n) { - #ifdef _DEBUG - std::cout << "NAlloc allocating " << n << " bytes\n"; - #endif - - return static_cast(alloc.allocate(n)); // get memory from pool - } - void deallocate(pointer p, [[maybe_unused]] std::size_t n) { - #ifdef _DEBUG - std::cout << "NAlloc deallocating " << n * sizeof *p << " bytes\n"; - #endif - - alloc.deallocate(static_cast(p), 1); // return memory to pool - } - - - bool operator!=(const NAlloc& other) const { - return !(*this == other); - } - bool operator==(const NAlloc& other) const { - return alloc == other.alloc; - } - - my_allocator alloc; + template + struct rebind + { + using other = NAlloc; }; + NAlloc() : + alloc() + { - using OstringStream = std::basic_ostringstream, NAlloc>; - using IstringStream = std::basic_istringstream, NAlloc>; - typedef std::basic_string, NAlloc> RtString; + }; + + virtual ~NAlloc() { } + + pointer allocate(std::size_t n) + { + + + return static_cast(alloc.allocate(n)); // get memory from pool + } + void deallocate(pointer p, [[maybe_unused]] std::size_t n) + { + + + alloc.deallocate(static_cast(p), 1); // return memory to pool + } + + + bool operator!=(const NAlloc& other) const + { + return !(*this == other); + } + bool operator==(const NAlloc& other) const + { + return alloc == other.alloc; + } + + my_allocator alloc; +}; + + +using OstringStream = std::basic_ostringstream, NAlloc>; +using IstringStream = std::basic_istringstream, NAlloc>; +typedef std::basic_string, NAlloc> RtString; } @@ -332,11 +336,11 @@ TEST_CASE("controlled bad_alloc_rt_string") double, my_allocator>; - + SECTION("class json_value") { - + SECTION("json_value(value_t)") { SECTION("object") From 199422d577ca04df2407205114bc45904d1d0c26 Mon Sep 17 00:00:00 2001 From: Alex Fishgait Date: Mon, 9 Oct 2023 14:04:21 +0200 Subject: [PATCH 11/21] end of line in unit test --- tests/src/unit-allocator.cpp | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/tests/src/unit-allocator.cpp b/tests/src/unit-allocator.cpp index 6de79871c..e0cdc13d1 100644 --- a/tests/src/unit-allocator.cpp +++ b/tests/src/unit-allocator.cpp @@ -286,7 +286,7 @@ struct NAlloc alloc() { - }; + } virtual ~NAlloc() { } @@ -424,4 +424,4 @@ TEST_CASE("controlled bad_alloc_rt_string") next_construct_fails = false; } } -} \ No newline at end of file +} From 04a5ec12f9cbdb24522f812b7c15d681c9ae49ae Mon Sep 17 00:00:00 2001 From: Alex Fishgait Date: Mon, 9 Oct 2023 14:10:24 +0200 Subject: [PATCH 12/21] solve maybe unused for c++11 --- tests/src/unit-allocator.cpp | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/tests/src/unit-allocator.cpp b/tests/src/unit-allocator.cpp index e0cdc13d1..d67966b22 100644 --- a/tests/src/unit-allocator.cpp +++ b/tests/src/unit-allocator.cpp @@ -283,7 +283,7 @@ struct NAlloc }; NAlloc() : - alloc() + alloc(),m_alloc_size(0) { } @@ -293,7 +293,7 @@ struct NAlloc pointer allocate(std::size_t n) { - + m_alloc_size = n; return static_cast(alloc.allocate(n)); // get memory from pool } void deallocate(pointer p, [[maybe_unused]] std::size_t n) @@ -314,6 +314,7 @@ struct NAlloc } my_allocator alloc; + std::size_t m_alloc_size; }; From 0a3b42fdf91a671412d8fa835e9ba57d52e6b449 Mon Sep 17 00:00:00 2001 From: Alex Fishgait Date: Mon, 9 Oct 2023 14:54:07 +0200 Subject: [PATCH 13/21] fixinf maybe_unused in unit test --- tests/src/unit-allocator.cpp | 16 +++------------- 1 file changed, 3 insertions(+), 13 deletions(-) diff --git a/tests/src/unit-allocator.cpp b/tests/src/unit-allocator.cpp index d67966b22..7ec323289 100644 --- a/tests/src/unit-allocator.cpp +++ b/tests/src/unit-allocator.cpp @@ -292,14 +292,12 @@ struct NAlloc pointer allocate(std::size_t n) { - - m_alloc_size = n; + return static_cast(alloc.allocate(n)); // get memory from pool } void deallocate(pointer p, [[maybe_unused]] std::size_t n) { - - + m_alloc_size = n; alloc.deallocate(static_cast(p), 1); // return memory to pool } @@ -344,15 +342,7 @@ TEST_CASE("controlled bad_alloc_rt_string") SECTION("json_value(value_t)") { - SECTION("object") - { - next_construct_fails = false; - auto t = my_json::value_t::object; - CHECK_NOTHROW(my_allocator_clean_up(my_json::json_value(t).object)); - next_construct_fails = true; - CHECK_THROWS_AS(my_json::json_value(t), std::bad_alloc&); - next_construct_fails = false; - } + SECTION("array") { next_construct_fails = false; From df0fc3e046b8d8220f419a3aa2269ea58f4337e7 Mon Sep 17 00:00:00 2001 From: Alex Fishgait Date: Mon, 9 Oct 2023 15:35:32 +0200 Subject: [PATCH 14/21] removing maybe_unused for win build --- tests/src/unit-allocator.cpp | 13 ++----------- 1 file changed, 2 insertions(+), 11 deletions(-) diff --git a/tests/src/unit-allocator.cpp b/tests/src/unit-allocator.cpp index 7ec323289..8f016c89d 100644 --- a/tests/src/unit-allocator.cpp +++ b/tests/src/unit-allocator.cpp @@ -295,7 +295,7 @@ struct NAlloc return static_cast(alloc.allocate(n)); // get memory from pool } - void deallocate(pointer p, [[maybe_unused]] std::size_t n) + void deallocate(pointer p, std::size_t n) { m_alloc_size = n; alloc.deallocate(static_cast(p), 1); // return memory to pool @@ -342,16 +342,7 @@ TEST_CASE("controlled bad_alloc_rt_string") SECTION("json_value(value_t)") { - - SECTION("array") - { - next_construct_fails = false; - auto t = my_json::value_t::array; - CHECK_NOTHROW(my_allocator_clean_up(my_json::json_value(t).array)); - next_construct_fails = true; - CHECK_THROWS_AS(my_json::json_value(t), std::bad_alloc&); - next_construct_fails = false; - } + SECTION("string") { next_construct_fails = false; From 519f972fc49ad936f022c9daf01b48f89ec8552e Mon Sep 17 00:00:00 2001 From: Alex Fishgait Date: Tue, 10 Oct 2023 09:32:57 +0200 Subject: [PATCH 15/21] include memory --- include/nlohmann/detail/input/binary_reader.hpp | 1 + include/nlohmann/detail/input/json_sax.hpp | 1 + include/nlohmann/detail/input/lexer.hpp | 1 + tests/src/unit-allocator.cpp | 8 ++++---- 4 files changed, 7 insertions(+), 4 deletions(-) diff --git a/include/nlohmann/detail/input/binary_reader.hpp b/include/nlohmann/detail/input/binary_reader.hpp index f37f04a72..2f0906705 100644 --- a/include/nlohmann/detail/input/binary_reader.hpp +++ b/include/nlohmann/detail/input/binary_reader.hpp @@ -20,6 +20,7 @@ #include // char_traits, string #include // make_pair, move #include // vector +#include #include #include diff --git a/include/nlohmann/detail/input/json_sax.hpp b/include/nlohmann/detail/input/json_sax.hpp index 7b5ae5687..03fcc19e4 100644 --- a/include/nlohmann/detail/input/json_sax.hpp +++ b/include/nlohmann/detail/input/json_sax.hpp @@ -12,6 +12,7 @@ #include // string #include // move #include // vector +#include #include #include diff --git a/include/nlohmann/detail/input/lexer.hpp b/include/nlohmann/detail/input/lexer.hpp index fda9531b7..be2ae048e 100644 --- a/include/nlohmann/detail/input/lexer.hpp +++ b/include/nlohmann/detail/input/lexer.hpp @@ -17,6 +17,7 @@ #include // char_traits, string #include // move #include // vector +#include #include #include diff --git a/tests/src/unit-allocator.cpp b/tests/src/unit-allocator.cpp index 8f016c89d..e86609103 100644 --- a/tests/src/unit-allocator.cpp +++ b/tests/src/unit-allocator.cpp @@ -288,7 +288,7 @@ struct NAlloc } - virtual ~NAlloc() { } + virtual ~NAlloc() = default; pointer allocate(std::size_t n) { @@ -312,16 +312,16 @@ struct NAlloc } my_allocator alloc; - std::size_t m_alloc_size; + std::size_t m_alloc_size {}; }; using OstringStream = std::basic_ostringstream, NAlloc>; using IstringStream = std::basic_istringstream, NAlloc>; -typedef std::basic_string, NAlloc> RtString; +using RtString = std::basic_string, NAlloc>; -} +} //namespace TEST_CASE("controlled bad_alloc_rt_string") { From 453c8c24d7042babe3c4c60ebee4545eafaa1953 Mon Sep 17 00:00:00 2001 From: Alex Fishgait Date: Tue, 10 Oct 2023 11:36:23 +0200 Subject: [PATCH 16/21] single include changes --- single_include/nlohmann/json.hpp | 3 +++ tests/src/unit-allocator.cpp | 6 +++--- 2 files changed, 6 insertions(+), 3 deletions(-) diff --git a/single_include/nlohmann/json.hpp b/single_include/nlohmann/json.hpp index 7caa58635..5a3812841 100644 --- a/single_include/nlohmann/json.hpp +++ b/single_include/nlohmann/json.hpp @@ -6068,6 +6068,7 @@ NLOHMANN_JSON_NAMESPACE_END #include // char_traits, string #include // make_pair, move #include // vector +#include // #include @@ -6581,6 +6582,7 @@ NLOHMANN_JSON_NAMESPACE_END #include // string #include // move #include // vector +#include // #include @@ -7318,6 +7320,7 @@ NLOHMANN_JSON_NAMESPACE_END #include // char_traits, string #include // move #include // vector +#include // #include diff --git a/tests/src/unit-allocator.cpp b/tests/src/unit-allocator.cpp index e86609103..c22f343ff 100644 --- a/tests/src/unit-allocator.cpp +++ b/tests/src/unit-allocator.cpp @@ -283,7 +283,7 @@ struct NAlloc }; NAlloc() : - alloc(),m_alloc_size(0) + alloc(), m_alloc_size(0) { } @@ -292,7 +292,7 @@ struct NAlloc pointer allocate(std::size_t n) { - + return static_cast(alloc.allocate(n)); // get memory from pool } void deallocate(pointer p, std::size_t n) @@ -342,7 +342,7 @@ TEST_CASE("controlled bad_alloc_rt_string") SECTION("json_value(value_t)") { - + SECTION("string") { next_construct_fails = false; From 71a7831c2d362fb5d084c7e25a3c9100adb6109a Mon Sep 17 00:00:00 2001 From: Alex Fishgait Date: Tue, 10 Oct 2023 13:23:47 +0200 Subject: [PATCH 17/21] define copy constructor for NAlloc --- tests/src/unit-allocator.cpp | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/tests/src/unit-allocator.cpp b/tests/src/unit-allocator.cpp index c22f343ff..3b15cfc8c 100644 --- a/tests/src/unit-allocator.cpp +++ b/tests/src/unit-allocator.cpp @@ -283,11 +283,16 @@ struct NAlloc }; NAlloc() : - alloc(), m_alloc_size(0) + alloc() { } + NAlloc(const NAlloc&) = default; // Copy constructor + NAlloc(NAlloc&&) = default; // Move constructor + NAlloc& operator=(const NAlloc& other) = default; // Copy assignment operator + NAlloc& operator=(NAlloc&&) = default; // Move assignment operator + virtual ~NAlloc() = default; pointer allocate(std::size_t n) From 1fc3a4fe6c5c9bc2a55456ee8a4f8a87e7e98317 Mon Sep 17 00:00:00 2001 From: Alex Fishgait Date: Tue, 10 Oct 2023 13:59:45 +0200 Subject: [PATCH 18/21] allocator for vector --- include/nlohmann/detail/input/parser.hpp | 5 +++-- include/nlohmann/json.hpp | 6 +++--- single_include/nlohmann/json.hpp | 11 ++++++----- 3 files changed, 12 insertions(+), 10 deletions(-) diff --git a/include/nlohmann/detail/input/parser.hpp b/include/nlohmann/detail/input/parser.hpp index dfa80ff1c..f6a4a3ea9 100644 --- a/include/nlohmann/detail/input/parser.hpp +++ b/include/nlohmann/detail/input/parser.hpp @@ -56,7 +56,7 @@ using parser_callback_t = This class implements a recursive descent parser. */ -template +template class parser { using number_integer_t = typename BasicJsonType::number_integer_t; @@ -181,7 +181,8 @@ class parser { // stack to remember the hierarchy of structured values we are parsing // true = array; false = object - std::vector states; + std::vector states; + // value to avoid a goto (see comment where set to true) bool skip_to_state_evaluation = false; diff --git a/include/nlohmann/json.hpp b/include/nlohmann/json.hpp index 504117bf1..62c8c123a 100644 --- a/include/nlohmann/json.hpp +++ b/include/nlohmann/json.hpp @@ -105,7 +105,7 @@ class basic_json // NOLINT(cppcoreguidelines-special-member-functions,hicpp-spec // can be restored when json_pointer backwards compatibility is removed // friend ::nlohmann::json_pointer; - template + template friend class ::nlohmann::detail::parser; friend ::nlohmann::detail::serializer; template @@ -129,14 +129,14 @@ class basic_json // NOLINT(cppcoreguidelines-special-member-functions,hicpp-spec using lexer = ::nlohmann::detail::lexer_base; template - static ::nlohmann::detail::parser, AllocatorType> parser( + static ::nlohmann::detail::parser, AllocatorType, AllocatorType> parser( InputAdapterType adapter, detail::parser_callback_tcb = nullptr, const bool allow_exceptions = true, const bool ignore_comments = false ) { - return ::nlohmann::detail::parser, AllocatorType>(std::move(adapter), + return ::nlohmann::detail::parser, AllocatorType, AllocatorType>(std::move(adapter), std::move(cb), allow_exceptions, ignore_comments); } diff --git a/single_include/nlohmann/json.hpp b/single_include/nlohmann/json.hpp index 5a3812841..992a75d70 100644 --- a/single_include/nlohmann/json.hpp +++ b/single_include/nlohmann/json.hpp @@ -12160,7 +12160,7 @@ using parser_callback_t = This class implements a recursive descent parser. */ -template +template class parser { using number_integer_t = typename BasicJsonType::number_integer_t; @@ -12285,7 +12285,8 @@ class parser { // stack to remember the hierarchy of structured values we are parsing // true = array; false = object - std::vector states; + std::vector states; + // value to avoid a goto (see comment where set to true) bool skip_to_state_evaluation = false; @@ -19326,7 +19327,7 @@ class basic_json // NOLINT(cppcoreguidelines-special-member-functions,hicpp-spec // can be restored when json_pointer backwards compatibility is removed // friend ::nlohmann::json_pointer; - template + template friend class ::nlohmann::detail::parser; friend ::nlohmann::detail::serializer; template @@ -19350,14 +19351,14 @@ class basic_json // NOLINT(cppcoreguidelines-special-member-functions,hicpp-spec using lexer = ::nlohmann::detail::lexer_base; template - static ::nlohmann::detail::parser, AllocatorType> parser( + static ::nlohmann::detail::parser, AllocatorType, AllocatorType> parser( InputAdapterType adapter, detail::parser_callback_tcb = nullptr, const bool allow_exceptions = true, const bool ignore_comments = false ) { - return ::nlohmann::detail::parser, AllocatorType>(std::move(adapter), + return ::nlohmann::detail::parser, AllocatorType, AllocatorType>(std::move(adapter), std::move(cb), allow_exceptions, ignore_comments); } From e2a77ba3c10f58150e93b24b17f6cce6c2fdb618 Mon Sep 17 00:00:00 2001 From: Alex Fishgait Date: Thu, 12 Oct 2023 07:50:09 +0200 Subject: [PATCH 19/21] adjust unit test for rtstring --- tests/src/unit-allocator.cpp | 103 ++++++++++++++++++----------------- 1 file changed, 52 insertions(+), 51 deletions(-) diff --git a/tests/src/unit-allocator.cpp b/tests/src/unit-allocator.cpp index 3b15cfc8c..9efabf83c 100644 --- a/tests/src/unit-allocator.cpp +++ b/tests/src/unit-allocator.cpp @@ -262,74 +262,75 @@ TEST_CASE("bad my_allocator::construct") } } -namespace -{ -template -struct NAlloc -{ - // Aliases and inner types - using value_type = T; - using size_type = std::size_t; - using difference_type = ptrdiff_t; - using reference = value_type&; - using const_reference = const value_type&; - using pointer = value_type*; - using const_pointer = const value_type*; +// namespace +// { +// template +// struct NAlloc +// { +// // Aliases and inner types +// using value_type = T; +// using size_type = std::size_t; +// using difference_type = ptrdiff_t; +// using reference = value_type&; +// using const_reference = const value_type&; +// using pointer = value_type*; +// using const_pointer = const value_type*; - template - struct rebind - { - using other = NAlloc; - }; +// template +// struct rebind +// { +// using other = NAlloc; +// }; - NAlloc() : - alloc() - { +// NAlloc() : +// alloc() +// { - } +// } - NAlloc(const NAlloc&) = default; // Copy constructor - NAlloc(NAlloc&&) = default; // Move constructor - NAlloc& operator=(const NAlloc& other) = default; // Copy assignment operator - NAlloc& operator=(NAlloc&&) = default; // Move assignment operator +// NAlloc(const NAlloc&) = default; // Copy constructor +// NAlloc(NAlloc&&) = default; // Move constructor +// NAlloc& operator=(const NAlloc& other) = default; // Copy assignment operator +// NAlloc& operator=(NAlloc&&) = default; // Move assignment operator - virtual ~NAlloc() = default; +// virtual ~NAlloc() = default; - pointer allocate(std::size_t n) - { +// pointer allocate(std::size_t n) +// { - return static_cast(alloc.allocate(n)); // get memory from pool - } - void deallocate(pointer p, std::size_t n) - { - m_alloc_size = n; - alloc.deallocate(static_cast(p), 1); // return memory to pool - } +// return static_cast(alloc.allocate(n)); // get memory from pool +// } +// void deallocate(pointer p, std::size_t n) +// { +// m_alloc_size = n; +// alloc.deallocate(static_cast(p), 1); // return memory to pool +// } - bool operator!=(const NAlloc& other) const - { - return !(*this == other); - } - bool operator==(const NAlloc& other) const - { - return alloc == other.alloc; - } +// bool operator!=(const NAlloc& other) const +// { +// return !(*this == other); +// } +// bool operator==(const NAlloc& other) const +// { +// return alloc == other.alloc; +// } - my_allocator alloc; - std::size_t m_alloc_size {}; -}; +// my_allocator alloc; +// std::size_t m_alloc_size {}; +// }; -using OstringStream = std::basic_ostringstream, NAlloc>; -using IstringStream = std::basic_istringstream, NAlloc>; -using RtString = std::basic_string, NAlloc>; +// using OstringStream = std::basic_ostringstream, NAlloc>; +// using IstringStream = std::basic_istringstream, NAlloc>; +// using RtString = std::basic_string, NAlloc>; -} //namespace +// } //namespace TEST_CASE("controlled bad_alloc_rt_string") { + using RtString = std::basic_string, my_allocator>; // create JSON type using the throwing allocator using my_json = nlohmann::basic_json Date: Mon, 16 Oct 2023 08:46:27 +0200 Subject: [PATCH 20/21] remove unused code --- tests/src/unit-allocator.cpp | 64 ------------------------------------ 1 file changed, 64 deletions(-) diff --git a/tests/src/unit-allocator.cpp b/tests/src/unit-allocator.cpp index 9efabf83c..e04a217b9 100644 --- a/tests/src/unit-allocator.cpp +++ b/tests/src/unit-allocator.cpp @@ -262,71 +262,7 @@ TEST_CASE("bad my_allocator::construct") } } -// namespace -// { -// template -// struct NAlloc -// { -// // Aliases and inner types -// using value_type = T; -// using size_type = std::size_t; -// using difference_type = ptrdiff_t; -// using reference = value_type&; -// using const_reference = const value_type&; -// using pointer = value_type*; -// using const_pointer = const value_type*; -// template -// struct rebind -// { -// using other = NAlloc; -// }; - -// NAlloc() : -// alloc() -// { - -// } - -// NAlloc(const NAlloc&) = default; // Copy constructor -// NAlloc(NAlloc&&) = default; // Move constructor -// NAlloc& operator=(const NAlloc& other) = default; // Copy assignment operator -// NAlloc& operator=(NAlloc&&) = default; // Move assignment operator - -// virtual ~NAlloc() = default; - -// pointer allocate(std::size_t n) -// { - -// return static_cast(alloc.allocate(n)); // get memory from pool -// } -// void deallocate(pointer p, std::size_t n) -// { -// m_alloc_size = n; -// alloc.deallocate(static_cast(p), 1); // return memory to pool -// } - - -// bool operator!=(const NAlloc& other) const -// { -// return !(*this == other); -// } -// bool operator==(const NAlloc& other) const -// { -// return alloc == other.alloc; -// } - -// my_allocator alloc; -// std::size_t m_alloc_size {}; -// }; - - -// using OstringStream = std::basic_ostringstream, NAlloc>; -// using IstringStream = std::basic_istringstream, NAlloc>; -// using RtString = std::basic_string, NAlloc>; - - -// } //namespace TEST_CASE("controlled bad_alloc_rt_string") { From 8f6eb81aaffe4b7bc7b2ffad8a7f170c8aca6975 Mon Sep 17 00:00:00 2001 From: Alex Fishgait Date: Mon, 16 Oct 2023 11:22:02 +0200 Subject: [PATCH 21/21] remove unused code 2 --- tests/src/unit-allocator.cpp | 1 - 1 file changed, 1 deletion(-) diff --git a/tests/src/unit-allocator.cpp b/tests/src/unit-allocator.cpp index e04a217b9..7ea44f01b 100644 --- a/tests/src/unit-allocator.cpp +++ b/tests/src/unit-allocator.cpp @@ -263,7 +263,6 @@ TEST_CASE("bad my_allocator::construct") } - TEST_CASE("controlled bad_alloc_rt_string") { using RtString = std::basic_string, my_allocator>;