Horizon
output_adapters.hpp
1 #pragma once
2 
3 #include <algorithm> // copy
4 #include <cstddef> // size_t
5 #include <ios> // streamsize
6 #include <iterator> // back_inserter
7 #include <memory> // shared_ptr, make_shared
8 #include <ostream> // basic_ostream
9 #include <string> // basic_string
10 #include <vector> // vector
11 
12 namespace nlohmann
13 {
14 namespace detail
15 {
17 template<typename CharType> struct output_adapter_protocol
18 {
19  virtual void write_character(CharType c) = 0;
20  virtual void write_characters(const CharType* s, std::size_t length) = 0;
21  virtual ~output_adapter_protocol() = default;
22 };
23 
25 template<typename CharType>
26 using output_adapter_t = std::shared_ptr<output_adapter_protocol<CharType>>;
27 
29 template<typename CharType>
31 {
32  public:
33  explicit output_vector_adapter(std::vector<CharType>& vec) : v(vec) {}
34 
35  void write_character(CharType c) override
36  {
37  v.push_back(c);
38  }
39 
40  void write_characters(const CharType* s, std::size_t length) override
41  {
42  std::copy(s, s + length, std::back_inserter(v));
43  }
44 
45  private:
46  std::vector<CharType>& v;
47 };
48 
50 template<typename CharType>
52 {
53  public:
54  explicit output_stream_adapter(std::basic_ostream<CharType>& s) : stream(s) {}
55 
56  void write_character(CharType c) override
57  {
58  stream.put(c);
59  }
60 
61  void write_characters(const CharType* s, std::size_t length) override
62  {
63  stream.write(s, static_cast<std::streamsize>(length));
64  }
65 
66  private:
67  std::basic_ostream<CharType>& stream;
68 };
69 
71 template<typename CharType>
73 {
74  public:
75  explicit output_string_adapter(std::basic_string<CharType>& s) : str(s) {}
76 
77  void write_character(CharType c) override
78  {
79  str.push_back(c);
80  }
81 
82  void write_characters(const CharType* s, std::size_t length) override
83  {
84  str.append(s, length);
85  }
86 
87  private:
88  std::basic_string<CharType>& str;
89 };
90 
91 template<typename CharType>
93 {
94  public:
95  output_adapter(std::vector<CharType>& vec)
96  : oa(std::make_shared<output_vector_adapter<CharType>>(vec)) {}
97 
98  output_adapter(std::basic_ostream<CharType>& s)
99  : oa(std::make_shared<output_stream_adapter<CharType>>(s)) {}
100 
101  output_adapter(std::basic_string<CharType>& s)
102  : oa(std::make_shared<output_string_adapter<CharType>>(s)) {}
103 
104  operator output_adapter_t<CharType>()
105  {
106  return oa;
107  }
108 
109  private:
110  output_adapter_t<CharType> oa = nullptr;
111 };
112 }
113 }
nlohmann
namespace for Niels Lohmann
Definition: adl_serializer.hpp:8
nlohmann::detail::output_vector_adapter
output adapter for byte vectors
Definition: output_adapters.hpp:30
nlohmann::detail::output_adapter_t
std::shared_ptr< output_adapter_protocol< CharType > > output_adapter_t
a type to simplify interfaces
Definition: output_adapters.hpp:26
nlohmann::detail::output_adapter_protocol
abstract output adapter interface
Definition: output_adapters.hpp:17
nlohmann::detail::output_stream_adapter
output adapter for output streams
Definition: output_adapters.hpp:51
nlohmann::detail::output_string_adapter
output adapter for basic_string
Definition: output_adapters.hpp:72
nlohmann::detail::output_adapter
Definition: output_adapters.hpp:92