Ariles
writer.cpp
Go to the documentation of this file.
1 /**
2  @file
3  @author Alexander Sherikov
4 
5  @copyright 2018-2020 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 <rapidjson/writer.h>
17 #include <rapidjson/prettywriter.h>
18 #include <rapidjson/stringbuffer.h>
19 
20 
21 namespace ariles2
22 {
23  namespace ns_rapidjson
24  {
25  namespace impl
26  {
28  {
29  public:
30  /// output file stream
31  std::ofstream config_ofs_;
32 
33  /// output stream
34  std::ostream *output_stream_;
35 
36  public:
37  explicit Writer(const std::string &file_name)
38  {
39  ariles2::write::Visitor::openFile(config_ofs_, file_name);
40  output_stream_ = &config_ofs_;
41  document_.SetObject();
42  }
43 
44 
45  explicit Writer(std::ostream &output_stream)
46  {
47  output_stream_ = &output_stream;
48  document_.SetObject();
49  }
50  };
51  } // namespace impl
52  } // namespace ns_rapidjson
53 } // namespace ariles2
54 
55 
56 namespace ariles2
57 {
58  namespace ns_rapidjson
59  {
60  Writer::Writer(const std::string &file_name)
61  {
62  impl_ = ImplPtr(new impl::Writer(file_name));
63  }
64 
65 
66  Writer::Writer(std::ostream &output_stream)
67  {
68  impl_ = ImplPtr(new impl::Writer(output_stream));
69  }
70 
71 
73  {
74  ::rapidjson::StringBuffer buffer;
75  ::rapidjson::PrettyWriter< ::rapidjson::StringBuffer> writer(buffer);
76  impl_->document_.Accept(writer);
77  *impl_->output_stream_ << buffer.GetString() << std::endl;
78  impl_->output_stream_->flush();
79  }
80 
81 
82  void Writer::startMap(const Parameters &, const std::size_t /*num_entries*/)
83  {
84  impl_->getRawNode().SetObject();
85  // not provided in older versions
86  // impl_->getRawNode().MemberReserve(num_entries, impl_->document_.GetAllocator());
87  }
88 
89  void Writer::startMapEntry(const std::string &map_name)
90  {
91  ::rapidjson::Value key(map_name.c_str(), impl_->document_.GetAllocator());
92  ::rapidjson::Value value;
93  impl_->getRawNode().AddMember(key, value, impl_->document_.GetAllocator());
94 
95  // hack, we assume that the last added
96  // child is the last in the list
97  const ::rapidjson::Value::MemberIterator child = --(impl_->getRawNode().MemberEnd());
98  impl_->node_stack_.push_back(impl::Writer::NodeWrapper(&(child->value)));
99  }
100 
102  {
103  impl_->node_stack_.pop_back();
104  }
105 
106 
107  void Writer::startArray(const std::size_t size, const bool /*compact*/)
108  {
109  ARILES2_TRACE_FUNCTION;
110  impl_->getRawNode().SetArray();
111  impl_->getRawNode().Reserve(size, impl_->document_.GetAllocator());
112  for (std::size_t i = 0; i < size; ++i)
113  {
114  ::rapidjson::Value value;
115  impl_->getRawNode().PushBack(value, impl_->document_.GetAllocator());
116  }
117  impl_->node_stack_.push_back(impl::Writer::NodeWrapper(0, size));
118  }
119 
121  {
122  ARILES2_TRACE_FUNCTION;
123  ARILES2_ASSERT(
124  impl_->node_stack_.back().index_ < impl_->node_stack_.back().size_,
125  "Internal error: namevalue.has more elements than expected.");
126  }
127 
129  {
130  ARILES2_TRACE_FUNCTION;
131  ARILES2_ASSERT(true == impl_->node_stack_.back().isArray(), "Internal error: expected array.");
132  ++impl_->node_stack_.back().index_;
133  }
134 
136  {
137  ARILES2_TRACE_FUNCTION;
138  impl_->node_stack_.pop_back();
139  }
140 
141 
142  /**
143  * @brief Write a configuration entry
144  *
145  * @param[in] element data
146  */
147  void Writer::writeElement(const std::string &element, const Parameters &)
148  {
149  impl_->getRawNode().SetString(element.c_str(), impl_->document_.GetAllocator());
150  }
151 
152  void Writer::writeElement(const bool &element, const Parameters &)
153  {
154  impl_->getRawNode().SetBool(element);
155  }
156 
157 
158  void Writer::writeElement(const float &element, const Parameters &param)
159  {
160  if (true == param.fallback_to_string_floats_)
161  {
162  impl_->getRawNode().SetString(
163  boost::lexical_cast<std::string>(element).c_str(), impl_->document_.GetAllocator());
164  }
165  else
166  {
167  impl_->getRawNode().SetDouble(element); // old API compatibility
168  // impl_->getRawNode().SetFloat(element);
169  }
170  }
171 
172 
173  void Writer::writeElement(const double &element, const Parameters &param)
174  {
175  if (true == param.fallback_to_string_floats_)
176  {
177  impl_->getRawNode().SetString(
178  boost::lexical_cast<std::string>(element).c_str(), impl_->document_.GetAllocator());
179  }
180  else
181  {
182  impl_->getRawNode().SetDouble(element);
183  }
184  }
185 
186 
187 
188 #define ARILES2_BASIC_TYPE(type) \
189  void Writer::writeElement(const type &element, const Parameters &) \
190  { \
191  impl_->getRawNode().SetInt64(element); \
192  }
193 
195 
196 #undef ARILES2_BASIC_TYPE
197 
198 
199 #define ARILES2_BASIC_TYPE(type) \
200  void Writer::writeElement(const type &element, const Parameters &) \
201  { \
202  impl_->getRawNode().SetUint64(element); \
203  }
204 
206 
207 #undef ARILES2_BASIC_TYPE
208  } // namespace ns_rapidjson
209 } // namespace ariles2
ariles2
Definition: basic.h:16
ariles2::ns_rapidjson::Writer::endMapEntry
void endMapEntry()
Definition: writer.cpp:101
ariles2::ns_rapidjson::impl::Writer
Definition: writer.cpp:27
ariles2::ns_rapidjson::Writer::endArray
void endArray()
Definition: writer.cpp:135
rapidjson.h
ariles2::ns_rapidjson::ImplBase
Definition: common.h:33
ariles2::ns_rapidjson::impl::Writer::Writer
Writer(const std::string &file_name)
Definition: writer.cpp:37
ariles2::ns_rapidjson::Writer::startArray
void startArray(const std::size_t size, const bool=false)
Definition: writer.cpp:107
ariles2::ns_rapidjson::impl::Writer::output_stream_
std::ostream * output_stream_
output stream
Definition: writer.cpp:34
ariles2::ns_rapidjson::Writer::Writer
Writer(const std::string &file_name)
Definition: writer.cpp:60
ariles2::ns_rapidjson::Writer::endArrayElement
void endArrayElement()
Definition: writer.cpp:128
ARILES2_BASIC_SIGNED_INTEGER_TYPES_LIST
#define ARILES2_BASIC_SIGNED_INTEGER_TYPES_LIST
Definition: helpers.h:101
ariles2::ns_rapidjson::Writer::startArrayElement
void startArrayElement()
Definition: writer.cpp:120
ARILES2_BASIC_UNSIGNED_INTEGER_TYPES_LIST
#define ARILES2_BASIC_UNSIGNED_INTEGER_TYPES_LIST
Definition: helpers.h:107
ARILES2_VISIBILITY_ATTRIBUTE
#define ARILES2_VISIBILITY_ATTRIBUTE
Definition: helpers.h:138
ariles2::ns_yaml_cpp::ARILES2_MACRO_SUBSTITUTE
ARILES2_MACRO_SUBSTITUTE(ARILES2_BASIC_INTEGER_TYPES_LIST) ARILES2_MACRO_SUBSTITUTE(ARILES2_BASIC_REAL_TYPES_LIST) void Writer
Definition: writer.cpp:198
ariles2::ns_rapidjson::Writer::startMapEntry
void startMapEntry(const std::string &map_name)
Starts a nested map in the configuration file.
Definition: writer.cpp:89
ariles2::serialization::PIMPLVisitor< write::Visitor, impl::Writer >::impl_
ImplPtr impl_
Definition: serialization.h:128
ariles2::ns_rapidjson::impl::Writer::config_ofs_
std::ofstream config_ofs_
output file stream
Definition: writer.cpp:31
ariles2::ns_rapidjson::impl::Writer::Writer
Writer(std::ostream &output_stream)
Definition: writer.cpp:45
ariles2::write::VisitorBase< Visitor, Parameters >::openFile
static void openFile(std::ofstream &config_ofs, const std::string &file_name)
open configuration file
Definition: write.h:58
ariles2::write::VisitorBase< Visitor, Parameters >::writeElement
void writeElement(const std::complex< t_Scalar > &entry, const Parameters &param)
Definition: write.h:285
common.h
ariles2::serialization::Node
Definition: serialization.h:49
ariles2::ns_rapidjson::Writer::startMap
void startMap(const Parameters &, const std::size_t)
Starts a nested map in the configuration file.
Definition: writer.cpp:82
writer.h
ariles2::ns_rapidjson::Writer::flush
void flush()
Flush the configuration to the output.
Definition: writer.cpp:72
ariles2::serialization::PIMPLVisitor< write::Visitor, impl::Writer >::ImplPtr
ARILES2_SHARED_PTR< impl::Writer > ImplPtr
Definition: serialization.h:125