Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

using allocator for bson #4167

Open
wants to merge 21 commits into
base: develop
Choose a base branch
from
Open
12 changes: 7 additions & 5 deletions include/nlohmann/detail/input/binary_reader.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
#include <string> // char_traits, string
#include <utility> // make_pair, move
#include <vector> // vector
#include <memory>

#include <nlohmann/detail/exceptions.hpp>
#include <nlohmann/detail/input/input_adapters.hpp>
Expand Down Expand Up @@ -62,7 +63,8 @@ static inline bool little_endianness(int num = 1) noexcept
/*!
@brief deserialization of CBOR, MessagePack, and UBJSON values
*/
template<typename BasicJsonType, typename InputAdapterType, typename SAX = json_sax_dom_parser<BasicJsonType>>
template<typename BasicJsonType, typename InputAdapterType, typename AllocatorJson = std::allocator< BasicJsonType*>, typename AllocatorChar = std::allocator< typename InputAdapterType::char_type>
, typename SAX = json_sax_dom_parser<BasicJsonType, AllocatorJson>>
class binary_reader
{
using number_integer_t = typename BasicJsonType::number_integer_t;
Expand Down Expand Up @@ -2694,7 +2696,7 @@ class binary_reader

// parse number string
using ia_type = decltype(detail::input_adapter(number_vector));
auto number_lexer = detail::lexer<BasicJsonType, ia_type>(detail::input_adapter(number_vector), false);
auto number_lexer = detail::lexer<BasicJsonType, ia_type, AllocatorChar>(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();
Expand All @@ -2714,7 +2716,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<string_t>(number_string)));
case token_type::uninitialized:
case token_type::literal_true:
case token_type::literal_false:
Expand Down Expand Up @@ -3001,8 +3003,8 @@ class binary_reader
};

#ifndef JSON_HAS_CPP_17
template<typename BasicJsonType, typename InputAdapterType, typename SAX>
constexpr std::size_t binary_reader<BasicJsonType, InputAdapterType, SAX>::npos;
template<typename BasicJsonType, typename InputAdapterType, typename AllocatorJson, typename AllocatorChar, typename SAX>
constexpr std::size_t binary_reader<BasicJsonType, InputAdapterType, AllocatorJson, AllocatorChar, SAX>::npos;
#endif

} // namespace detail
Expand Down
5 changes: 3 additions & 2 deletions include/nlohmann/detail/input/json_sax.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
#include <string> // string
#include <utility> // move
#include <vector> // vector
#include <memory>

#include <nlohmann/detail/exceptions.hpp>
#include <nlohmann/detail/macro_scope.hpp>
Expand Down Expand Up @@ -157,7 +158,7 @@ constructor contains the parsed value.

@tparam BasicJsonType the JSON type
*/
template<typename BasicJsonType>
template<typename BasicJsonType, typename Allocator = std::allocator<BasicJsonType*>>
class json_sax_dom_parser
{
public:
Expand Down Expand Up @@ -331,7 +332,7 @@ class json_sax_dom_parser
/// the parsed JSON value
BasicJsonType& root;
/// stack to model hierarchy of values
std::vector<BasicJsonType*> ref_stack {};
std::vector<BasicJsonType*, Allocator> ref_stack {};
/// helper to hold the reference for the next object element
BasicJsonType* object_element = nullptr;
/// whether a syntax error occurred
Expand Down
5 changes: 3 additions & 2 deletions include/nlohmann/detail/input/lexer.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
#include <string> // char_traits, string
#include <utility> // move
#include <vector> // vector
#include <memory>

#include <nlohmann/detail/input/input_adapters.hpp>
#include <nlohmann/detail/input/position_t.hpp>
Expand Down Expand Up @@ -107,7 +108,7 @@ class lexer_base

This class organizes the lexical analysis during JSON deserialization.
*/
template<typename BasicJsonType, typename InputAdapterType>
template<typename BasicJsonType, typename InputAdapterType, class Allocator = std::allocator<typename InputAdapterType::char_type>>
class lexer : public lexer_base<BasicJsonType>
{
using number_integer_t = typename BasicJsonType::number_integer_t;
Expand Down Expand Up @@ -1611,7 +1612,7 @@ class lexer : public lexer_base<BasicJsonType>
position_t position {};

/// raw input token string (for error messages)
std::vector<char_type> token_string {};
std::vector<char_type, Allocator> token_string {};

/// buffer for variable-length tokens (numbers, strings)
string_t token_buffer {};
Expand Down
9 changes: 5 additions & 4 deletions include/nlohmann/detail/input/parser.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -56,14 +56,14 @@ using parser_callback_t =

This class implements a recursive descent parser.
*/
template<typename BasicJsonType, typename InputAdapterType>
template<typename BasicJsonType, typename InputAdapterType, typename AllocatorJson, typename AllocatorChar, typename AllocatorBool>
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<BasicJsonType, InputAdapterType>;
using lexer_t = lexer<BasicJsonType, InputAdapterType, AllocatorChar>;
using token_type = typename lexer_t::token_type;

public:
Expand Down Expand Up @@ -122,7 +122,7 @@ class parser
}
else
{
json_sax_dom_parser<BasicJsonType> sdp(result, allow_exceptions);
json_sax_dom_parser<BasicJsonType, AllocatorJson> sdp(result, allow_exceptions);
sax_parse_internal(&sdp);

// in strict mode, input must be completely read
Expand Down Expand Up @@ -181,7 +181,8 @@ class parser
{
// stack to remember the hierarchy of structured values we are parsing
// true = array; false = object
std::vector<bool> states;
std::vector<bool, AllocatorBool> states;

// value to avoid a goto (see comment where set to true)
bool skip_to_state_evaluation = false;

Expand Down
4 changes: 2 additions & 2 deletions include/nlohmann/detail/output/binary_writer.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -1110,7 +1110,7 @@ class binary_writer

const std::size_t embedded_document_size = std::accumulate(std::begin(value), std::end(value), static_cast<std::size_t>(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<string_t>(std::to_string(array_index++)), el);
});

return sizeof(std::int32_t) + embedded_document_size + 1ul;
Expand All @@ -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<string_t>(std::to_string(array_index++)), el);
}

oa->write_character(to_char_type(0x00));
Expand Down
5 changes: 4 additions & 1 deletion include/nlohmann/detail/output/output_adapters.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -124,7 +124,10 @@ class output_adapter
public:
template<typename AllocatorType = std::allocator<CharType>>
output_adapter(std::vector<CharType, AllocatorType>& vec)
: oa(std::make_shared<output_vector_adapter<CharType, AllocatorType>>(vec)) {}
{
AllocatorType alloc;
oa = std::allocate_shared<output_vector_adapter<CharType, AllocatorType>>(alloc, vec);
}

#ifndef JSON_NO_IO
output_adapter(std::basic_ostream<CharType>& s)
Expand Down
Loading