Ariles
Loading...
Searching...
No Matches
writer.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
13
14#include "common.h"
15
16#include <nlohmann/json.hpp>
17
18
19namespace ariles2
20{
21 namespace ns_nlohmann_json
22 {
23 namespace impl
24 {
25 class Writer : public ariles2::ns_nlohmann_json::ImplBase<::nlohmann::ordered_json>,
27 {
28 public:
29 template <class... t_Args>
30 explicit Writer(t_Args &&...args) : FileVisitorImplementation(std::forward<t_Args>(args)...)
31 {
32 document_ = ::nlohmann::ordered_json::object();
33 }
34 };
35 } // namespace impl
36 } // namespace ns_nlohmann_json
37} // namespace ariles2
38
39
40namespace ariles2
41{
42 namespace ns_nlohmann_json
43 {
44 Writer::Writer(const std::string &file_name)
45 {
46 makeImplPtr(file_name);
47 }
48
49
50 Writer::Writer(std::ostream &output_stream)
51 {
52 makeImplPtr(output_stream);
53 }
54
55
57 {
58 *impl_->output_stream_ << impl_->document_.dump(/*indent=*/4) << std::endl;
59 impl_->output_stream_->flush();
60 }
61
62
63 void Writer::startMap(const Parameters &, const std::size_t /*num_entries*/)
64 {
65 impl_->getRawNode() = ::nlohmann::ordered_json::object();
66 }
67
68 void Writer::startMapEntry(const std::string &map_name)
69 {
70 ::nlohmann::ordered_json &obj = impl_->getRawNode();
71 obj[map_name] = ::nlohmann::ordered_json(); // Create an empty value first
72
73 // Find the newly added element and push it to the stack
74 impl_->emplace(&obj.find(map_name).value());
75 }
76
78 {
79 impl_->pop();
80 }
81
82
83 void Writer::startArray(const std::size_t size, const bool /*compact*/)
84 {
86 impl_->getRawNode() = ::nlohmann::ordered_json::array();
87 ::nlohmann::ordered_json &arr = impl_->getRawNode();
88
89 // Add empty values
90 for (std::size_t i = 0; i < size; ++i)
91 {
92 arr.push_back(::nlohmann::ordered_json());
93 }
94 impl_->emplace(0, size);
95 }
96
98 {
101 impl_->back().index_ < impl_->back().size_,
102 "Internal error: array has more elements than expected.");
103 }
104
106 {
108 impl_->shiftArray();
109 }
110
112 {
114 impl_->pop();
115 }
116
117
118 /**
119 * @brief Write a configuration entry
120 *
121 * @param[in] element data
122 */
123 void Writer::writeElement(const std::string &element, const Parameters &)
124 {
125 impl_->getRawNode() = element;
126 }
127
128 void Writer::writeElement(const bool &element, const Parameters &)
129 {
130 impl_->getRawNode() = element;
131 }
132
133
134 void Writer::writeElement(const float &element, const Parameters &param)
135 {
136 if (param.fallback_to_string_floats_)
137 {
138 impl_->getRawNode() = boost::lexical_cast<std::string>(element);
139 }
140 else
141 {
142 impl_->getRawNode() = static_cast<double>(element);
143 }
144 }
145
146
147 void Writer::writeElement(const double &element, const Parameters &param)
148 {
149 if (param.fallback_to_string_floats_)
150 {
151 impl_->getRawNode() = boost::lexical_cast<std::string>(element);
152 }
153 else
154 {
155 impl_->getRawNode() = element;
156 }
157 }
158
159
160
161#define ARILES2_BASIC_TYPE(type) \
162 void Writer::writeElement(const type &element, const Parameters &) \
163 { \
164 impl_->getRawNode() = static_cast<std::int64_t>(element); \
165 }
166
168
169#undef ARILES2_BASIC_TYPE
170
171
172#define ARILES2_BASIC_TYPE(type) \
173 void Writer::writeElement(const type &element, const Parameters &) \
174 { \
175 impl_->getRawNode() = static_cast<std::uint64_t>(element); \
176 }
177
179
180#undef ARILES2_BASIC_TYPE
181 } // namespace ns_nlohmann_json
182} // namespace ariles2
::nlohmann::ordered_json document_
instance of the JSON value
Definition common.h:29
void startArray(const std::size_t size, const bool=false)
Definition writer.cpp:83
void startMapEntry(const std::string &map_name)
Starts a nested map in the configuration file.
Definition writer.cpp:68
Writer(const std::string &file_name)
Definition writer.cpp:44
void startMap(const Parameters &, const std::size_t)
Starts a nested map in the configuration file.
Definition writer.cpp:63
void flush()
Flush the configuration to the output.
Definition writer.cpp:56
FileVisitorImplementation(const std::string &file_name)
Definition write.h:384
void writeElement(const std::complex< t_Scalar > &entry, const Parameters &param)
Definition write.h:264
#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