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 ariles
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  ariles::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 ariles
54 
55 
56 namespace ariles
57 {
58  namespace ns_rapidjson
59  {
60  Writer::Writer(const std::string &file_name, const Flags &flags) : Base(flags)
61  {
62  impl_ = ImplPtr(new Impl(file_name));
63  }
64 
65 
66  Writer::Writer(std::ostream &output_stream, const Flags &flags) : Base(flags)
67  {
68  impl_ = ImplPtr(new Impl(output_stream));
69  }
70 
71 
72 
74  {
75  ::rapidjson::StringBuffer buffer;
76  ::rapidjson::PrettyWriter< ::rapidjson::StringBuffer> writer(buffer);
77  impl_->document_.Accept(writer);
78  *impl_->output_stream_ << buffer.GetString() << std::endl;
79  impl_->output_stream_->flush();
80  }
81 
82 
83 
84  void Writer::descend(const std::string &map_name)
85  {
86  ::rapidjson::Value key(map_name.c_str(), impl_->document_.GetAllocator());
87  ::rapidjson::Value value;
88  impl_->getRawNode().AddMember(key, value, impl_->document_.GetAllocator());
89 
90  // hack, we assume that the last added
91  // child is the last in the list
92  const ::rapidjson::Value::MemberIterator child = --(impl_->getRawNode().MemberEnd());
93  impl_->node_stack_.push_back(impl::Writer::NodeWrapper(&(child->value)));
94  }
95 
97  {
98  impl_->node_stack_.pop_back();
99  }
100 
101 
102  void Writer::startMap(const std::size_t /*num_entries*/)
103  {
104  impl_->getRawNode().SetObject();
105  // not provided in older versions
106  // impl_->getRawNode().MemberReserve(num_entries, impl_->document_.GetAllocator());
107  }
108 
109 
110 
111  void Writer::startArray(const std::size_t size, const bool /*compact*/)
112  {
113  impl_->getRawNode().SetArray();
114  impl_->getRawNode().Reserve(size, impl_->document_.GetAllocator());
115  for (std::size_t i = 0; i < size; ++i)
116  {
117  ::rapidjson::Value value;
118  impl_->getRawNode().PushBack(value, impl_->document_.GetAllocator());
119  }
120  impl_->node_stack_.push_back(impl::Writer::NodeWrapper(0, size));
121  }
122 
124  {
125  ARILES_ASSERT(true == impl_->node_stack_.back().isArray(), "Internal error: expected array.");
126  ARILES_ASSERT(
127  impl_->node_stack_.back().index_ < impl_->node_stack_.back().size_,
128  "Internal error: array has more elements than expected.");
129  ++impl_->node_stack_.back().index_;
130  }
131 
133  {
134  impl_->node_stack_.pop_back();
135  }
136 
137 
138  /**
139  * @brief Write a configuration entry
140  *
141  * @param[in] element data
142  */
143  void Writer::writeElement(const std::string &element)
144  {
145  impl_->getRawNode().SetString(element.c_str(), impl_->document_.GetAllocator());
146  }
147 
148  void Writer::writeElement(const bool &element)
149  {
150  impl_->getRawNode().SetBool(element);
151  }
152 
153 
154  void Writer::writeElement(const float &element)
155  {
156  if (true == flags_.isSet(Flags::DISABLE_STRING_FLOATS))
157  {
158  impl_->getRawNode().SetDouble(element); // old API compatibility
159  // impl_->getRawNode().SetFloat(element);
160  }
161  else
162  {
163  impl_->getRawNode().SetString(
164  boost::lexical_cast<std::string>(element).c_str(), impl_->document_.GetAllocator());
165  }
166  }
167 
168 
169  void Writer::writeElement(const double &element)
170  {
171  if (true == flags_.isSet(Flags::DISABLE_STRING_FLOATS))
172  {
173  impl_->getRawNode().SetDouble(element);
174  }
175  else
176  {
177  impl_->getRawNode().SetString(
178  boost::lexical_cast<std::string>(element).c_str(), impl_->document_.GetAllocator());
179  }
180  }
181 
182 
183 
184 #define ARILES_BASIC_TYPE(type) \
185  void Writer::writeElement(const type &element) \
186  { \
187  impl_->getRawNode().SetInt64(element); \
188  }
189 
191 
192 #undef ARILES_BASIC_TYPE
193 
194 
195 #define ARILES_BASIC_TYPE(type) \
196  void Writer::writeElement(const type &element) \
197  { \
198  impl_->getRawNode().SetUint64(element); \
199  }
200 
202 
203 #undef ARILES_BASIC_TYPE
204  } // namespace ns_rapidjson
205 } // namespace ariles
std::ostream * output_stream_
output stream
Definition: writer.cpp:34
t_Implementation Impl
Definition: rapidjson.h:61
Floats are stored as strings by default to allow NaN and Inf (writer only)
Definition: rapidjson.h:31
Writer(const std::string &file_name)
Definition: writer.cpp:37
#define ARILES_BASIC_UNSIGNED_INTEGER_TYPES_LIST
Definition: helpers.h:86
ARILES_MACRO_SUBSTITUTE(ARILES_BASIC_INTEGER_TYPES_LIST) ARILES_MACRO_SUBSTITUTE(ARILES_BASIC_REAL_TYPES_LIST) void Writer
Definition: writer.cpp:195
ARILES_SHARED_PTR< t_Implementation > ImplPtr
Definition: rapidjson.h:62
Writer(const std::string &file_name, const Flags &flags=Flags::DEFAULT)
Definition: writer.cpp:60
void startArray(const std::size_t size, const bool=false)
Definition: writer.cpp:111
void startMap(const std::size_t)
Definition: writer.cpp:102
#define ARILES_BASIC_SIGNED_INTEGER_TYPES_LIST
Definition: helpers.h:80
std::ofstream config_ofs_
output file stream
Definition: writer.cpp:31
static void openFile(std::ofstream &config_ofs, const std::string &file_name)
open configuration file
Definition: write.h:33
void descend(const std::string &map_name)
Definition: writer.cpp:84
Writer(std::ostream &output_stream)
Definition: writer.cpp:45
#define ARILES_VISIBILITY_ATTRIBUTE
Definition: helpers.h:69
Definition: basic.h:17