Ariles
Loading...
Searching...
No Matches
reader.cpp
Go to the documentation of this file.
1/**
2 @file
3 @author Alexander Sherikov
4
5 @copyright 2018-2026 Alexander Sherikov, Licensed under the Apache License, Version 2.0.
6 (see @ref LICENSE or http://www.apache.org/licenses/LICENSE-2.0)
7
8 @brief
9*/
10
11#include <boost/math/special_functions.hpp>
13#include "common.h"
14
15
16namespace ariles2
17{
18 namespace ns_nlohmann_json
19 {
20 namespace impl
21 {
22 class Reader : public ariles2::ns_nlohmann_json::ImplBase<const ::nlohmann::ordered_json>,
24 {
25 public:
26 std::vector<::nlohmann::ordered_json::const_iterator> iterator_stack_;
27
28 public:
29 Reader() = default;
30
31 template <class... t_Args>
32 explicit Reader(t_Args &&...args) : FileVisitorImplementation(std::forward<t_Args>(args)...)
33 {
34 initialize();
35 }
36
37
39 {
40 document_ = ::nlohmann::ordered_json::parse(*input_stream_);
41 }
42 };
43 } // namespace impl
44 } // namespace ns_nlohmann_json
45} // namespace ariles2
46
47
48
49namespace ariles2
50{
51 namespace ns_nlohmann_json
52 {
53 Reader::Reader(const std::string &file_name)
54 {
56 makeImplPtr(file_name);
57 }
58
59
60 Reader::Reader(std::istream &input_stream)
61 {
63 makeImplPtr(input_stream);
64 }
65
66
67 void Reader::constructFromString(const char *input_string)
68 {
71 impl_->document_ = ::nlohmann::ordered_json::parse(input_string);
72 }
73
74
75 void Reader::startMap(const SizeLimitEnforcementType limit_type, const std::size_t min, const std::size_t max)
76 {
78 checkSize(limit_type, impl_->getRawNode().size(), min, max);
79 }
80
81 bool Reader::startMapEntry(const std::string &child_name)
82 {
83 const ::nlohmann::ordered_json &obj = impl_->getRawNode();
84 const ::nlohmann::ordered_json::const_iterator child = obj.find(child_name);
85
86 if (obj.end() == child)
87 {
88 return (false);
89 }
90 impl_->emplace(&child.value());
91 return (true);
92 }
93
95 {
96 impl_->pop();
97 }
98
99
101 const SizeLimitEnforcementType limit_type,
102 const std::size_t min,
103 const std::size_t max)
104 {
106 const ::nlohmann::ordered_json &obj = impl_->getRawNode();
107 checkSize(limit_type, obj.size(), min, max);
108
109
110 if (impl_->getRawNode().is_object())
111 {
112 impl_->iterator_stack_.push_back(obj.begin());
113 return (true);
114 }
115 return (false);
116 }
117
118 bool Reader::startIteratedMapElement(std::string &entry_name)
119 {
120 const auto &current_iter = impl_->iterator_stack_.back();
121 if (current_iter != impl_->getRawNode().end())
122 {
123 impl_->emplace(&current_iter.value());
124 entry_name = current_iter.key();
125 return (true);
126 }
127 return (false);
128 }
129
131 {
132 ++impl_->iterator_stack_.back();
133 impl_->pop();
134 }
135
137 {
138 CPPUT_ASSERT(impl_->iterator_stack_.back() == impl_->getRawNode().end(), "End of iterated map has not been reached.");
139 impl_->iterator_stack_.pop_back();
140 }
141
142
143 std::size_t Reader::startArray()
144 {
145 const std::size_t size = impl_->getRawNode().size();
146 impl_->emplace(0, size); // index=0, size=size
147 return (size);
148 }
149
150
152 {
154 impl_->back().index_ < impl_->back().size_,
155 "Internal error: array has more elements than expected.");
156 }
157
158
160 {
161 impl_->shiftArray();
162 }
163
164
166 {
167 impl_->pop();
168 }
169
170
171 void Reader::readElement(std::string &element)
172 {
173 element = impl_->getRawNode().get<std::string>();
174 }
175
176
177 void Reader::readElement(bool &element)
178 {
179 element = impl_->getRawNode().get<bool>();
180 }
181
182
183 void Reader::readElement(float &element)
184 {
185 float tmp_value = 0.0;
186 if (impl_->getRawNode().is_string())
187 {
188 tmp_value = boost::lexical_cast<float>(impl_->getRawNode().get<std::string>());
189 if (boost::math::isnan(tmp_value))
190 {
191 element = std::numeric_limits<float>::signaling_NaN();
192 return;
193 }
194 if (boost::math::isinf(tmp_value))
195 {
196 element = tmp_value;
197 return;
198 }
199 }
200 else
201 {
202 tmp_value = static_cast<float>(impl_->getRawNode().get<double>());
203 }
205 tmp_value <= std::numeric_limits<float>::max() && tmp_value >= -std::numeric_limits<float>::max(),
206 "Value is out of range.");
207 element = tmp_value;
208 }
209
210
211 void Reader::readElement(double &element)
212 {
213 double tmp_value = 0.0;
214 if (impl_->getRawNode().is_string())
215 {
216 tmp_value = boost::lexical_cast<double>(impl_->getRawNode().get<std::string>());
217 if (boost::math::isnan(tmp_value))
218 {
219 element = std::numeric_limits<double>::signaling_NaN();
220 return;
221 }
222 if (boost::math::isinf(tmp_value))
223 {
224 element = tmp_value;
225 return;
226 }
227 }
228 else
229 {
230 tmp_value = impl_->getRawNode().get<double>();
231 }
233 tmp_value <= std::numeric_limits<double>::max() && tmp_value >= -std::numeric_limits<double>::max(),
234 "Value is out of range.");
235 element = tmp_value;
236 }
237
238
239#define ARILES2_BASIC_TYPE(type) \
240 void Reader::readElement(type &element) \
241 { \
242 const int64_t tmp_value = impl_->getRawNode().get<int64_t>(); \
243 CPPUT_ASSERT( \
244 tmp_value <= std::numeric_limits<type>::max() && tmp_value >= std::numeric_limits<type>::min(), \
245 "Value is out of range."); \
246 element = static_cast<type>(tmp_value); \
247 }
248
250
251#undef ARILES2_BASIC_TYPE
252
253
254#define ARILES2_BASIC_TYPE(type) \
255 void Reader::readElement(type &element) \
256 { \
257 const uint64_t tmp_value = impl_->getRawNode().get<uint64_t>(); \
258 CPPUT_ASSERT(tmp_value <= std::numeric_limits<type>::max(), "Value is too large."); \
259 element = static_cast<type>(tmp_value); \
260 }
261
263
264#undef ARILES2_BASIC_TYPE
265 } // namespace ns_nlohmann_json
266} // namespace ariles2
::nlohmann::ordered_json document_
instance of the JSON value
Definition common.h:29
bool startIteratedMapElement(std::string &entry_name)
Definition reader.cpp:118
bool startMapEntry(const std::string &child_name)
startMapEntry to the entry with the given name
Definition reader.cpp:81
bool startIteratedMap(const SizeLimitEnforcementType=SIZE_LIMIT_NONE, const std::size_t=0, const std::size_t=0)
Definition reader.cpp:100
void startMap(const SizeLimitEnforcementType limit_type=SIZE_LIMIT_NONE, const std::size_t min=0, const std::size_t max=0)
Definition reader.cpp:75
void endMapEntry()
endMapEntry from the current entry to its parent.
Definition reader.cpp:94
void constructFromString(const char *)
Definition reader.cpp:67
std::vector<::nlohmann::ordered_json::const_iterator > iterator_stack_
Definition reader.cpp:26
void checkSize(const SizeLimitEnforcementType limit_type, const std::size_t size=0, const std::size_t min=0, const std::size_t max=0) const
Definition read.h:47
void readElement(std::complex< t_Scalar > &entry)
Definition read.h:357
#define CPPUT_ASSERT(condition,...)
Definition exception.h:32
#define ARILES2_BASIC_UNSIGNED_INTEGER_TYPES_LIST
Definition helpers.h:50
#define ARILES2_BASIC_SIGNED_INTEGER_TYPES_LIST
Definition helpers.h:37
#define CPPUT_MACRO_SUBSTITUTE(macro)
Definition misc.h:21
#define CPPUT_TRACE_FUNCTION
Definition trace.h:126