Skip to content

Commit

Permalink
🏗️ overworking input/output adapters
Browse files Browse the repository at this point in the history
  • Loading branch information
nlohmann committed Mar 23, 2019
1 parent 3755cbf commit 4141e39
Show file tree
Hide file tree
Showing 8 changed files with 643 additions and 782 deletions.
239 changes: 117 additions & 122 deletions include/nlohmann/detail/input/input_adapters.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -337,128 +337,6 @@ class wide_string_input_adapter : public input_adapter_protocol
std::size_t utf8_bytes_filled = 0;
};

/// input adapter for C FILE p
input_adapter_t make_input_adapter(std::FILE* file)
{
return std::make_shared<file_input_adapter>(file);
}

/// input adapter for input stream
input_adapter_t make_input_adapter(std::istream& i)
{
return std::make_shared<input_stream_adapter>(i);
}

/// input adapter for input stream
input_adapter_t make_input_adapter(std::istream&& i)
{
return std::make_shared<input_stream_adapter>(i);
}

/// input adapter for wide string
input_adapter_t make_input_adapter(const std::wstring& ws)
{
return std::make_shared<wide_string_input_adapter<std::wstring>>(ws);
}

/// input adapter for UTF-16 string
input_adapter_t make_input_adapter(const std::u16string& ws)
{
return std::make_shared<wide_string_input_adapter<std::u16string>>(ws);
}

/// input adapter for UTF-32 string
input_adapter_t make_input_adapter(const std::u32string& ws)
{
return std::make_shared<wide_string_input_adapter<std::u32string>>(ws);
}

/// input adapter for character buffer
template<typename CharT,
typename std::enable_if<
std::is_pointer<CharT>::value and
std::is_integral<typename std::remove_pointer<CharT>::type>::value and
sizeof(typename std::remove_pointer<CharT>::type) == 1,
int>::type = 0>
input_adapter_t make_input_adapter(CharT b, std::size_t l)
{
return std::make_shared<input_buffer_adapter>(reinterpret_cast<const char*>(b), l);
}

/// input adapter for string literal
template<typename CharT,
typename std::enable_if<
std::is_pointer<CharT>::value and
std::is_integral<typename std::remove_pointer<CharT>::type>::value and
sizeof(typename std::remove_pointer<CharT>::type) == 1,
int>::type = 0>
input_adapter_t make_input_adapter(CharT b)
{
return make_input_adapter(reinterpret_cast<const char*>(b),
std::strlen(reinterpret_cast<const char*>(b)));
}

/// input adapter for iterator range with contiguous storage
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>
input_adapter_t make_input_adapter(IteratorType first, IteratorType last)
{
#ifndef NDEBUG
// assertion to check that the iterator range is indeed contiguous,
// see http://stackoverflow.com/a/35008842/266378 for more discussion
const auto is_contiguous = std::accumulate(
first, last, std::pair<bool, int>(true, 0),
[&first](std::pair<bool, int> res, decltype(*first) val)
{
res.first &= (val == *(std::next(std::addressof(*first), res.second++)));
return res;
}).first;
assert(is_contiguous);
#endif

// assertion to check that each element is 1 byte long
static_assert(
sizeof(typename iterator_traits<IteratorType>::value_type) == 1,
"each element in the iterator range must have the size of 1 byte");

const auto len = static_cast<size_t>(std::distance(first, last));
if (JSON_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);
}
else
{
// the address of first cannot be used: use nullptr
return std::make_shared<input_buffer_adapter>(nullptr, len);
}
}

/// input adapter for character array
template<class T, std::size_t N>
input_adapter_t make_input_adapter(T (&array)[N])
{
return make_input_adapter(std::begin(array), std::end(array));
}

/// input adapter for contiguous container
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>
input_adapter_t make_input_adapter(const ContiguousContainer& c)
{
return make_input_adapter(std::begin(c), std::end(c));
}

/// extension point for user-defined input adapters
input_adapter_t make_input_adapter(input_adapter_t ia)
{
return ia;
}

/*!
@brief Convenience wrapper around @ref input_adapter_protocol.
*/
Expand Down Expand Up @@ -494,6 +372,123 @@ class input_adapter
return ia;
}

/// input adapter for C FILE p
static input_adapter_t make_input_adapter(std::FILE* file)
{
return std::make_shared<file_input_adapter>(file);
}

/// input adapter for input stream
static input_adapter_t make_input_adapter(std::istream& i)
{
return std::make_shared<input_stream_adapter>(i);
}

/// input adapter for input stream
static input_adapter_t make_input_adapter(std::istream&& i)
{
return std::make_shared<input_stream_adapter>(i);
}

/// input adapter for wide string
static input_adapter_t make_input_adapter(const std::wstring& ws)
{
return std::make_shared<wide_string_input_adapter<std::wstring>>(ws);
}

/// input adapter for UTF-16 string
static input_adapter_t make_input_adapter(const std::u16string& ws)
{
return std::make_shared<wide_string_input_adapter<std::u16string>>(ws);
}

/// input adapter for UTF-32 string
static input_adapter_t make_input_adapter(const std::u32string& ws)
{
return std::make_shared<wide_string_input_adapter<std::u32string>>(ws);
}

/// input adapter for character buffer
template<typename CharT,
typename std::enable_if<
std::is_pointer<CharT>::value and
std::is_integral<typename std::remove_pointer<CharT>::type>::value and
sizeof(typename std::remove_pointer<CharT>::type) == 1,
int>::type = 0>
static input_adapter_t make_input_adapter(CharT b, std::size_t l)
{
return std::make_shared<input_buffer_adapter>(reinterpret_cast<const char*>(b), l);
}

/// input adapter for string literal
template<typename CharT,
typename std::enable_if<
std::is_pointer<CharT>::value and
std::is_integral<typename std::remove_pointer<CharT>::type>::value and
sizeof(typename std::remove_pointer<CharT>::type) == 1,
int>::type = 0>
static input_adapter_t make_input_adapter(CharT b)
{
return make_input_adapter(reinterpret_cast<const char*>(b),
std::strlen(reinterpret_cast<const char*>(b)));
}

/// input adapter for iterator range with contiguous storage
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>
static input_adapter_t make_input_adapter(IteratorType first, IteratorType last)
{
#ifndef NDEBUG
// assertion to check that the iterator range is indeed contiguous,
// see http://stackoverflow.com/a/35008842/266378 for more discussion
const auto is_contiguous = std::accumulate(
first, last, std::pair<bool, int>(true, 0),
[&first](std::pair<bool, int> res, decltype(*first) val)
{
res.first &= (val == *(std::next(std::addressof(*first), res.second++)));
return res;
}).first;
assert(is_contiguous);
#endif

// assertion to check that each element is 1 byte long
static_assert(
sizeof(typename iterator_traits<IteratorType>::value_type) == 1,
"each element in the iterator range must have the size of 1 byte");

const auto len = static_cast<size_t>(std::distance(first, last));
return JSON_LIKELY(len > 0)
// there is at least one element: use the address of first
? std::make_shared<input_buffer_adapter>(reinterpret_cast<const char*>(&(*first)), len)
// the address of first cannot be used: use nullptr
: std::make_shared<input_buffer_adapter>(nullptr, len);
}

/// input adapter for character array
template<class T, std::size_t N>
static input_adapter_t make_input_adapter(T (&array)[N])
{
return make_input_adapter(std::begin(array), std::end(array));
}

/// input adapter for contiguous container
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>
static input_adapter_t make_input_adapter(const ContiguousContainer& c)
{
return make_input_adapter(std::begin(c), std::end(c));
}

/// extension point for user-defined input adapters
static input_adapter_t make_input_adapter(input_adapter_t ia)
{
return ia;
}

private:
/// the actual adapter
input_adapter_t ia = nullptr;
Expand Down
Loading

0 comments on commit 4141e39

Please sign in to comment.