Skip to content

Commit

Permalink
Merge pull request #1950 from FrancoisChabot/issues/1457
Browse files Browse the repository at this point in the history
templated input adapters
  • Loading branch information
nlohmann authored May 14, 2020
2 parents d7b032f + a4266bb commit 0857140
Show file tree
Hide file tree
Showing 8 changed files with 698 additions and 409 deletions.
35 changes: 18 additions & 17 deletions include/nlohmann/detail/input/binary_reader.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -24,14 +24,28 @@ namespace nlohmann
{
namespace detail
{

/*!
@brief determine system byte order
@return true if and only if system's byte order is little endian
@note from https://stackoverflow.com/a/1001328/266378
*/
static bool little_endianess(int num = 1) noexcept
{
return *reinterpret_cast<char*>(&num) == 1;
}


///////////////////
// binary reader //
///////////////////

/*!
@brief deserialization of CBOR, MessagePack, and UBJSON values
*/
template<typename BasicJsonType, typename SAX = json_sax_dom_parser<BasicJsonType>>
template<typename BasicJsonType, typename InputAdapterType, typename SAX = json_sax_dom_parser<BasicJsonType>>
class binary_reader
{
using number_integer_t = typename BasicJsonType::number_integer_t;
Expand All @@ -47,10 +61,9 @@ class binary_reader
@param[in] adapter input adapter to read from
*/
explicit binary_reader(input_adapter_t adapter) : ia(std::move(adapter))
explicit binary_reader(InputAdapterType&& adapter) : ia(std::move(adapter))
{
(void)detail::is_sax_static_asserts<SAX, BasicJsonType> {};
assert(ia);
}

// make class move-only
Expand Down Expand Up @@ -119,18 +132,6 @@ class binary_reader
return result;
}

/*!
@brief determine system byte order
@return true if and only if system's byte order is little endian
@note from https://stackoverflow.com/a/1001328/266378
*/
static constexpr bool little_endianess(int num = 1) noexcept
{
return *reinterpret_cast<char*>(&num) == 1;
}

private:
//////////
// BSON //
Expand Down Expand Up @@ -2085,7 +2086,7 @@ class binary_reader
int get()
{
++chars_read;
return current = ia->get_character();
return current = ia.get_character();
}

/*!
Expand Down Expand Up @@ -2273,7 +2274,7 @@ class binary_reader

private:
/// input adapter
input_adapter_t ia = nullptr;
InputAdapterType ia;

/// the current character
int current = std::char_traits<char>::eof();
Expand Down
Loading

0 comments on commit 0857140

Please sign in to comment.