WPILibC++ 2024.3.2
output_adapters.h
Go to the documentation of this file.
1// __ _____ _____ _____
2// __| | __| | | | JSON for Modern C++
3// | | |__ | | | | | | version 3.11.2
4// |_____|_____|_____|_|___| https://github.com/nlohmann/json
5//
6// SPDX-FileCopyrightText: 2013-2022 Niels Lohmann <https://nlohmann.me>
7// SPDX-License-Identifier: MIT
8
9#pragma once
10
11#include <algorithm> // copy
12#include <cstddef> // size_t
13#include <iterator> // back_inserter
14#include <memory> // shared_ptr, make_shared
15#include <string> // basic_string
16#include <vector> // vector
17
18#ifndef JSON_NO_IO
19 #include <ios> // streamsize
20 #include <ostream> // basic_ostream
21#endif // JSON_NO_IO
22
24
25#include <wpi/raw_ostream.h>
26
28namespace detail
29{
30
31/// abstract output adapter interface
32template<typename CharType> struct output_adapter_protocol
33{
34 virtual void write_character(CharType c) = 0;
35 virtual void write_characters(const CharType* s, std::size_t length) = 0;
36 virtual ~output_adapter_protocol() = default;
37
42 output_adapter_protocol& operator=(output_adapter_protocol&&) noexcept = default;
43};
44
45/// a type to simplify interfaces
46template<typename CharType>
47using output_adapter_t = std::shared_ptr<output_adapter_protocol<CharType>>;
48
49/// output adapter for byte vectors
50template<typename CharType, typename AllocatorType = std::allocator<CharType>>
52{
53 public:
54 explicit output_vector_adapter(std::vector<CharType, AllocatorType>& vec) noexcept
55 : v(vec)
56 {}
57
58 void write_character(CharType c) override
59 {
60 v.push_back(c);
61 }
62
64 void write_characters(const CharType* s, std::size_t length) override
65 {
66 v.insert(v.end(), s, s + length);
67 }
68
69 private:
70 std::vector<CharType, AllocatorType>& v;
71};
72
73#ifndef JSON_NO_IO
74/// output adapter for output streams
75template<typename CharType>
77{
78 public:
79 explicit output_stream_adapter(std::basic_ostream<CharType>& s) noexcept
80 : stream(s)
81 {}
82
83 void write_character(CharType c) override
84 {
85 stream.put(c);
86 }
87
89 void write_characters(const CharType* s, std::size_t length) override
90 {
91 stream.write(s, static_cast<std::streamsize>(length));
92 }
93
94 private:
95 std::basic_ostream<CharType>& stream;
96};
97#endif // JSON_NO_IO
98
99/// output adapter for basic_string
100template<typename CharType, typename StringType = std::basic_string<CharType>>
102{
103 public:
104 explicit output_string_adapter(StringType& s) noexcept
105 : str(s)
106 {}
107
108 void write_character(CharType c) override
109 {
110 str.push_back(c);
111 }
112
114 void write_characters(const CharType* s, std::size_t length) override
115 {
116 str.append(s, length);
117 }
118
119 private:
120 StringType& str;
121};
122
123template<typename CharType>
125{
126 public:
127 explicit raw_ostream_adapter(raw_ostream& s) noexcept
128 : os(s) {}
129
130
131 void write_character(CharType c) override {
132 os << c;
133 }
134
136 void write_characters(const CharType* s, std::size_t length) override {
137 os.write(s, length);
138 }
139
140 private:
141 raw_ostream& os;
142};
143
144template<typename CharType, typename StringType = std::basic_string<CharType>>
146{
147 public:
148 template<typename AllocatorType = std::allocator<CharType>>
149 output_adapter(std::vector<CharType, AllocatorType>& vec)
150 : oa(std::make_shared<output_vector_adapter<CharType, AllocatorType>>(vec)) {}
151
152#ifndef JSON_NO_IO
153 output_adapter(std::basic_ostream<CharType>& s)
154 : oa(std::make_shared<output_stream_adapter<CharType>>(s)) {}
155#endif // JSON_NO_IO
156
157 output_adapter(StringType& s)
158 : oa(std::make_shared<output_string_adapter<CharType, StringType>>(s)) {}
159
160 output_adapter(raw_ostream& os)
161 : oa(std::make_shared<raw_ostream_adapter<CharType>>(os)) {}
162
164 {
165 return oa;
166 }
167
168 private:
169 output_adapter_t<CharType> oa = nullptr;
170};
171
172} // namespace detail
#define WPI_JSON_NAMESPACE_END
Definition: abi_macros.h:59
#define WPI_JSON_NAMESPACE_BEGIN
Definition: abi_macros.h:53
Definition: output_adapters.h:146
output_adapter(StringType &s)
Definition: output_adapters.h:157
output_adapter(raw_ostream &os)
Definition: output_adapters.h:160
output_adapter(std::basic_ostream< CharType > &s)
Definition: output_adapters.h:153
output_adapter(std::vector< CharType, AllocatorType > &vec)
Definition: output_adapters.h:149
output adapter for output streams
Definition: output_adapters.h:77
output_stream_adapter(std::basic_ostream< CharType > &s) noexcept
Definition: output_adapters.h:79
void write_character(CharType c) override
Definition: output_adapters.h:83
output adapter for basic_string
Definition: output_adapters.h:102
void write_character(CharType c) override
Definition: output_adapters.h:108
output_string_adapter(StringType &s) noexcept
Definition: output_adapters.h:104
output adapter for byte vectors
Definition: output_adapters.h:52
output_vector_adapter(std::vector< CharType, AllocatorType > &vec) noexcept
Definition: output_adapters.h:54
void write_character(CharType c) override
Definition: output_adapters.h:58
Definition: output_adapters.h:125
raw_ostream_adapter(raw_ostream &s) noexcept
Definition: output_adapters.h:127
void write_character(CharType c) override
Definition: output_adapters.h:131
#define JSON_HEDLEY_NON_NULL(...)
Definition: hedley.h:1288
detail namespace with internal helper functions
Definition: xchar.h:20
std::shared_ptr< output_adapter_protocol< CharType > > output_adapter_t
a type to simplify interfaces
Definition: output_adapters.h:47
Definition: array.h:89
static constexpr const velocity::meters_per_second_t c(299792458.0)
Speed of light in vacuum.
abstract output adapter interface
Definition: output_adapters.h:33
output_adapter_protocol(const output_adapter_protocol &)=default
virtual ~output_adapter_protocol()=default
virtual void write_character(CharType c)=0
output_adapter_protocol(output_adapter_protocol &&) noexcept=default
virtual void write_characters(const CharType *s, std::size_t length)=0