duck-typed object input adapters

This commit is contained in:
Francois Chabot 2020-02-19 14:59:31 -05:00
parent a0c4fc945a
commit 2e2cf02cfd
8 changed files with 202 additions and 207 deletions

View file

@ -31,10 +31,9 @@ namespace detail
/*!
@brief deserialization of CBOR, MessagePack, and UBJSON values
*/
template<typename BasicJsonType, typename SAX = json_sax_dom_parser<BasicJsonType>, typename InputAdapterType = input_adapter_protocol>
template<typename BasicJsonType, typename InputAdapterType, typename SAX = json_sax_dom_parser<BasicJsonType>>
class binary_reader
{
using input_adapter_ptr_t = std::shared_ptr<InputAdapterType>;
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;
@ -47,10 +46,9 @@ class binary_reader
@param[in] adapter input adapter to read from
*/
explicit binary_reader(input_adapter_ptr_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
@ -1810,7 +1808,7 @@ class binary_reader
int get()
{
++chars_read;
return current = ia->get_character();
return current = ia.get_character();
}
/*!
@ -1966,7 +1964,7 @@ class binary_reader
private:
/// input adapter
input_adapter_ptr_t ia = nullptr;
InputAdapterType ia;
/// the current character
int current = std::char_traits<char>::eof();

View file

@ -27,29 +27,11 @@ enum class input_format_t { json, cbor, msgpack, ubjson, bson };
// input adapters //
////////////////////
/*!
@brief abstract input adapter interface
Produces a stream of std::char_traits<char>::int_type characters from a
std::istream, a buffer, or some other input type. Accepts the return of
exactly one non-EOF character for future input. The int_type characters
returned consist of all valid char values as positive values (typically
unsigned char), plus an EOF value outside that range, specified by the value
of the function std::char_traits<char>::eof(). This value is typically -1, but
could be any arbitrary value which is not a valid char value.
*/
struct input_adapter_protocol
{
/// get a character [0,255] or std::char_traits<char>::eof().
virtual std::char_traits<char>::int_type get_character() = 0;
virtual ~input_adapter_protocol() = default;
};
/*!
Input adapter for stdio file access. This adapter read only 1 byte and do not use any
buffer. This adapter is a very low level adapter.
*/
class file_input_adapter final : public input_adapter_protocol
class file_input_adapter
{
public:
JSON_HEDLEY_NON_NULL(2)
@ -62,9 +44,8 @@ class file_input_adapter final : public input_adapter_protocol
file_input_adapter(file_input_adapter&&) = default;
file_input_adapter& operator=(const file_input_adapter&) = delete;
file_input_adapter& operator=(file_input_adapter&&) = default;
~file_input_adapter() override = default;
std::char_traits<char>::int_type get_character() noexcept override
std::char_traits<char>::int_type get_character() noexcept
{
return std::fgetc(m_file);
}
@ -84,48 +65,68 @@ characters following those used in parsing the JSON input. Clears the
std::istream flags; any input errors (e.g., EOF) will be detected by the first
subsequent call for input from the std::istream.
*/
class input_stream_adapter final : public input_adapter_protocol
class input_stream_adapter
{
public:
~input_stream_adapter() override
~input_stream_adapter()
{
// clear stream flags; we use underlying streambuf I/O, do not
// maintain ifstream flags, except eof
is.clear(is.rdstate() & std::ios::eofbit);
if (is)
{
is->clear(is->rdstate() & std::ios::eofbit);
}
}
explicit input_stream_adapter(std::istream& i)
: is(i), sb(*i.rdbuf())
: is(&i), sb(i.rdbuf())
{}
// delete because of pointer members
input_stream_adapter(const input_stream_adapter&) = delete;
input_stream_adapter& operator=(input_stream_adapter&) = delete;
input_stream_adapter(input_stream_adapter&&) = delete;
input_stream_adapter& operator=(input_stream_adapter&&) = delete;
input_stream_adapter(input_stream_adapter&& rhs) : is(rhs.is), sb(rhs.sb)
{
rhs.is = nullptr;
rhs.sb = nullptr;
}
input_stream_adapter& operator=(input_stream_adapter&& rhs)
{
if (is)
{
is->clear(is->rdstate() & std::ios::eofbit);
}
is = rhs.is;
sb = rhs.sb;
rhs.is = nullptr;
rhs.sb = nullptr;
}
// std::istream/std::streambuf use std::char_traits<char>::to_int_type, to
// ensure that std::char_traits<char>::eof() and the character 0xFF do not
// end up as the same value, eg. 0xFFFFFFFF.
std::char_traits<char>::int_type get_character() override
std::char_traits<char>::int_type get_character()
{
auto res = sb.sbumpc();
auto res = sb->sbumpc();
// set eof manually, as we don't use the istream interface.
if (res == EOF)
{
is.clear(is.rdstate() | std::ios::eofbit);
is->clear(is->rdstate() | std::ios::eofbit);
}
return res;
}
private:
/// the associated input stream
std::istream& is;
std::streambuf& sb;
std::istream* is = nullptr;
std::streambuf* sb = nullptr;
};
/// input adapter for buffer input
class input_buffer_adapter final : public input_adapter_protocol
class input_buffer_adapter
{
public:
input_buffer_adapter(const char* b, const std::size_t l) noexcept
@ -135,11 +136,10 @@ class input_buffer_adapter final : public input_adapter_protocol
// delete because of pointer members
input_buffer_adapter(const input_buffer_adapter&) = delete;
input_buffer_adapter& operator=(input_buffer_adapter&) = delete;
input_buffer_adapter(input_buffer_adapter&&) = delete;
input_buffer_adapter& operator=(input_buffer_adapter&&) = delete;
~input_buffer_adapter() override = default;
input_buffer_adapter(input_buffer_adapter&&) = default;
input_buffer_adapter& operator=(input_buffer_adapter&&) = default;
std::char_traits<char>::int_type get_character() noexcept override
std::char_traits<char>::int_type get_character() noexcept
{
if (JSON_HEDLEY_LIKELY(cursor < limit))
{
@ -282,14 +282,14 @@ struct wide_string_input_helper<WideStringType, 2>
};
template<typename WideStringType>
class wide_string_input_adapter final : public input_adapter_protocol
class wide_string_input_adapter
{
public:
explicit wide_string_input_adapter(const WideStringType& w) noexcept
: str(w)
{}
std::char_traits<char>::int_type get_character() noexcept override
std::char_traits<char>::int_type get_character() noexcept
{
// check if buffer needs to be filled
if (utf8_bytes_index == utf8_bytes_filled)
@ -328,19 +328,19 @@ class wide_string_input_adapter final : public input_adapter_protocol
std::size_t utf8_bytes_filled = 0;
};
inline std::shared_ptr<file_input_adapter> input_adapter(std::FILE* file)
inline file_input_adapter input_adapter(std::FILE* file)
{
return std::make_shared<file_input_adapter>(file);
return file_input_adapter(file);
}
inline std::shared_ptr<input_stream_adapter> input_adapter(std::istream& stream)
inline input_stream_adapter input_adapter(std::istream& stream)
{
return std::make_shared<input_stream_adapter>(stream);
return input_stream_adapter(stream);
}
inline std::shared_ptr<input_stream_adapter> input_adapter(std::istream&& stream)
inline input_stream_adapter input_adapter(std::istream&& stream)
{
return std::make_shared<input_stream_adapter>(stream);
return input_stream_adapter(stream);
}
template<typename CharT,
@ -349,9 +349,9 @@ template<typename CharT,
std::is_integral<typename std::remove_pointer<CharT>::type>::value and
sizeof(typename std::remove_pointer<CharT>::type) == 1,
int>::type = 0>
std::shared_ptr<input_buffer_adapter> input_adapter(CharT b, std::size_t l)
input_buffer_adapter input_adapter(CharT b, std::size_t l)
{
return std::make_shared<input_buffer_adapter>(reinterpret_cast<const char*>(b), l);
return input_buffer_adapter(reinterpret_cast<const char*>(b), l);
}
template<typename CharT,
@ -360,7 +360,7 @@ template<typename CharT,
std::is_integral<typename std::remove_pointer<CharT>::type>::value and
sizeof(typename std::remove_pointer<CharT>::type) == 1,
int>::type = 0>
std::shared_ptr<input_buffer_adapter> input_adapter(CharT b)
input_buffer_adapter input_adapter(CharT b)
{
return input_adapter(reinterpret_cast<const char*>(b),
std::strlen(reinterpret_cast<const char*>(b)));
@ -370,7 +370,7 @@ template<class IteratorType,
typename std::enable_if<
std::is_same<typename iterator_traits<IteratorType>::iterator_category, std::random_access_iterator_tag>::value,
int>::type = 0>
std::shared_ptr<input_buffer_adapter> input_adapter(IteratorType first, IteratorType last)
input_buffer_adapter input_adapter(IteratorType first, IteratorType last)
{
#ifndef NDEBUG
// assertion to check that the iterator range is indeed contiguous,
@ -394,43 +394,43 @@ std::shared_ptr<input_buffer_adapter> input_adapter(IteratorType first, Iterator
if (JSON_HEDLEY_LIKELY(len > 0))
{
// there is at least one element: use the address of first
return std::make_shared<input_buffer_adapter>(reinterpret_cast<const char*>(&(*first)), len);
return input_buffer_adapter(reinterpret_cast<const char*>(&(*first)), len);
}
else
{
// the address of first cannot be used: use nullptr
return std::make_shared<input_buffer_adapter>(nullptr, len);
return input_buffer_adapter(nullptr, len);
}
}
inline std::shared_ptr<wide_string_input_adapter<std::wstring>> input_adapter(const std::wstring& ws)
inline wide_string_input_adapter<std::wstring> input_adapter(const std::wstring& ws)
{
return std::make_shared<wide_string_input_adapter<std::wstring>>(ws);
return wide_string_input_adapter<std::wstring>(ws);
}
inline std::shared_ptr<wide_string_input_adapter<std::u16string>> input_adapter(const std::u16string& ws)
inline wide_string_input_adapter<std::u16string> input_adapter(const std::u16string& ws)
{
return std::make_shared<wide_string_input_adapter<std::u16string>>(ws);
return wide_string_input_adapter<std::u16string>(ws);
}
inline std::shared_ptr<wide_string_input_adapter<std::u32string>> input_adapter(const std::u32string& ws)
inline wide_string_input_adapter<std::u32string> input_adapter(const std::u32string& ws)
{
return std::make_shared<wide_string_input_adapter<std::u32string>>(ws);
return wide_string_input_adapter<std::u32string>(ws);
}
template<class ContiguousContainer, typename
std::enable_if<not std::is_pointer<ContiguousContainer>::value and
std::is_base_of<std::random_access_iterator_tag, typename iterator_traits<decltype(std::begin(std::declval<ContiguousContainer const>()))>::iterator_category>::value,
int>::type = 0>
std::shared_ptr<input_buffer_adapter> input_adapter(const ContiguousContainer& c)
input_buffer_adapter input_adapter(const ContiguousContainer& c)
{
return input_adapter(std::begin(c), std::end(c));
}
template<class T, std::size_t N>
std::shared_ptr<input_buffer_adapter> input_adapter(T (&array)[N])
input_buffer_adapter input_adapter(T (&array)[N])
{
return input_adapter(std::begin(array), std::end(array));
}
@ -448,7 +448,7 @@ class span_input_adapter
sizeof(typename std::remove_pointer<CharT>::type) == 1,
int>::type = 0>
span_input_adapter(CharT b, std::size_t l)
: ia(std::make_shared<input_buffer_adapter>(reinterpret_cast<const char*>(b), l)) {}
: ia(reinterpret_cast<const char*>(b), l) {}
template<typename CharT,
typename std::enable_if<
@ -479,13 +479,13 @@ class span_input_adapter
span_input_adapter(const ContiguousContainer& c)
: span_input_adapter(std::begin(c), std::end(c)) {}
std::shared_ptr<input_buffer_adapter> get()
input_buffer_adapter&& get()
{
return ia;
return std::move(ia);
}
private:
std::shared_ptr<input_buffer_adapter> ia = nullptr;
input_buffer_adapter ia;
};
} // namespace detail
} // namespace nlohmann

View file

@ -99,11 +99,9 @@ class lexer_base
This class organizes the lexical analysis during JSON deserialization.
*/
template<typename BasicJsonType, typename InputAdapterType = input_adapter_protocol>
template<typename BasicJsonType, typename InputAdapterType>
class lexer : public lexer_base<BasicJsonType>
{
using input_adapter_ptr_t = std::shared_ptr<InputAdapterType>;
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;
@ -112,7 +110,7 @@ class lexer : public lexer_base<BasicJsonType>
public:
using token_type = typename lexer_base<BasicJsonType>::token_type;
explicit lexer(input_adapter_ptr_t&& adapter)
explicit lexer(InputAdapterType&& adapter)
: ia(std::move(adapter)), decimal_point_char(get_decimal_point()) {}
// delete because of pointer members
@ -1264,7 +1262,7 @@ scan_number_done:
}
else
{
current = ia->get_character();
current = ia.get_character();
}
if (JSON_HEDLEY_LIKELY(current != std::char_traits<char>::eof()))
@ -1488,7 +1486,7 @@ scan_number_done:
private:
/// input adapter
input_adapter_ptr_t ia = nullptr;
InputAdapterType ia;
/// the current character
std::char_traits<char>::int_type current = std::char_traits<char>::eof();

View file

@ -49,10 +49,9 @@ using parser_callback_t =
This class implements a recursive descent parser.
*/
template<typename BasicJsonType, typename InputAdapterType = input_adapter_protocol>
template<typename BasicJsonType, typename InputAdapterType>
class parser
{
using input_adapter_ptr_t = std::shared_ptr<InputAdapterType>;
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;
@ -62,7 +61,7 @@ class parser
public:
/// a parser reading from an input adapter
explicit parser(input_adapter_ptr_t&& adapter,
explicit parser(InputAdapterType&& adapter,
const parser_callback_t<BasicJsonType> cb = nullptr,
const bool allow_exceptions_ = true)
: callback(cb), m_lexer(std::move(adapter)), allow_exceptions(allow_exceptions_)

View file

@ -1326,7 +1326,7 @@ class binary_writer
private:
/// whether we can assume little endianess
const bool is_little_endian = binary_reader<BasicJsonType>::little_endianess();
const bool is_little_endian = binary_reader<BasicJsonType, detail::input_buffer_adapter>::little_endianess();
/// the output
output_adapter_t<CharType> oa = nullptr;