Ariles
writer.cpp
Go to the documentation of this file.
1 /**
2  @file
3  @author Jan Michalczyk
4  @author Alexander Sherikov
5 
6  @copyright 2014-2017 INRIA. Licensed under the Apache License, Version 2.0.
7  (see @ref LICENSE or http://www.apache.org/licenses/LICENSE-2.0)
8 
9  @copyright 2017-2020 Alexander Sherikov, Licensed under the Apache License, Version 2.0.
10  (see @ref LICENSE or http://www.apache.org/licenses/LICENSE-2.0)
11 
12  @brief
13 */
14 
16 #include "common.h"
17 
18 
19 namespace ariles
20 {
21  namespace ns_yaml_cpp03
22  {
23  namespace impl
24  {
26  {
27  public:
28  typedef ARILES_SHARED_PTR<YAML::Emitter> EmitterPtr;
29 
30 
31  public:
32  /// output file stream
33  std::ofstream config_ofs_;
34 
35  /// output stream
36  std::ostream *output_stream_;
37 
38  /// instance of YAML emitter, is destroyed and reinitialized by flush()
40 
41  std::size_t map_depth_;
43 
44 
45  protected:
46  void initEmitter()
47  {
48  emitter_ = EmitterPtr(new YAML::Emitter);
49  emitter_->SetDoublePrecision(std::numeric_limits<double>::digits10);
50  if (output_stream_->tellp() != 0)
51  {
52  *emitter_ << YAML::Newline;
53  }
54  *emitter_ << YAML::BeginMap;
55  map_depth_ = 0;
56  skip_root_map_ = false;
57  }
58 
59 
61  {
62  *emitter_ << YAML::EndMap;
63  *output_stream_ << emitter_->c_str();
64  emitter_.reset();
65  }
66 
67 
68  public:
69  explicit Writer(const std::string &file_name)
70  {
71  ariles::write::Visitor::openFile(config_ofs_, file_name);
72  output_stream_ = &config_ofs_;
73  initEmitter();
74  }
75 
76 
77  explicit Writer(std::ostream &output_stream)
78  {
79  output_stream_ = &output_stream;
80  initEmitter();
81  }
82 
83 
84  void flush()
85  {
86  destroyEmitter();
87  *output_stream_ << "\n";
88  output_stream_->flush();
89  initEmitter();
90  }
91  };
92  } // namespace impl
93  } // namespace ns_yaml_cpp03
94 } // namespace ariles
95 
96 
97 namespace ariles
98 {
99  namespace ns_yaml_cpp03
100  {
101  Writer::Writer(const std::string &file_name)
102  {
103  impl_ = ImplPtr(new Impl(file_name));
104  }
105 
106 
107  Writer::Writer(std::ostream &output_stream)
108  {
109  impl_ = ImplPtr(new Impl(output_stream));
110  }
111 
112 
113 
114  void Writer::descend(const std::string &map_name)
115  {
116  *impl_->emitter_ << YAML::Key << map_name;
117  *impl_->emitter_ << YAML::Value;
118  }
119 
120 
121  void Writer::startMap(const std::size_t /*num_entries*/)
122  {
123  if (impl_->map_depth_ > 0 or false == impl_->skip_root_map_)
124  {
125  *impl_->emitter_ << YAML::BeginMap;
126  }
127  ++impl_->map_depth_;
128  }
129 
130 
132  {
133  ARILES_ASSERT(impl_->map_depth_ > 0, "Internal logic error.");
134  --impl_->map_depth_;
135  if (impl_->map_depth_ > 0 or false == impl_->skip_root_map_)
136  {
137  *impl_->emitter_ << YAML::EndMap;
138  }
139  }
140 
141 
142 
144  {
145  impl_->flush();
146  }
147 
148 
149 
150  void Writer::startArray(const std::size_t /*size*/, const bool compact)
151  {
152  if (true == compact)
153  {
154  *impl_->emitter_ << YAML::Flow;
155  }
156  *impl_->emitter_ << YAML::BeginSeq;
157  }
158 
160  {
161  *impl_->emitter_ << YAML::EndSeq;
162  }
163 
164 
165  void Writer::startRoot(const std::string &name)
166  {
168  if (true == name.empty())
169  {
170  impl_->skip_root_map_ = true;
171  }
172  else
173  {
174  descend(name);
175  }
176  }
177 
178  void Writer::endRoot(const std::string &name)
179  {
181  if (false == name.empty())
182  {
183  ascend();
184  }
185  impl_->skip_root_map_ = false;
186  }
187 
188 
189 #define ARILES_BASIC_TYPE(type) \
190  void Writer::writeElement(const type &element) \
191  { \
192  *impl_->emitter_ << element; \
193  }
194 
196 
197 #undef ARILES_BASIC_TYPE
198 
199 
200 #define ARILES_BASIC_TYPE(type) \
201  void Writer::writeElement(const type &element) \
202  { \
203  if (true == ariles::isNaN(element)) \
204  { \
205  *impl_->emitter_ << ".nan"; \
206  } \
207  else \
208  { \
209  if (true == ariles::isInfinity(element)) \
210  { \
211  if (element < 0.0) \
212  { \
213  *impl_->emitter_ << "-.inf"; \
214  } \
215  else \
216  { \
217  *impl_->emitter_ << ".inf"; \
218  } \
219  } \
220  else \
221  { \
222  *impl_->emitter_ << element; \
223  } \
224  } \
225  }
226 
228 
229 #undef ARILES_BASIC_TYPE
230 
231 
232 
233  void Writer::writeElement(const std::string &element)
234  {
235  *impl_->emitter_ << element;
236  }
237 
238  void Writer::writeElement(const bool &element)
239  {
240  *impl_->emitter_ << element;
241  }
242  } // namespace ns_yaml_cpp03
243 } // namespace ariles
#define ARILES_BASIC_INTEGER_TYPES_LIST
Definition: helpers.h:92
#define ARILES_TRACE_FUNCTION
Definition: trace.h:118
void startMap(const std::size_t)
Definition: writer.cpp:121
ARILES_MACRO_SUBSTITUTE(ARILES_BASIC_INTEGER_TYPES_LIST) ARILES_MACRO_SUBSTITUTE(ARILES_BASIC_REAL_TYPES_LIST) void Writer
Definition: writer.cpp:195
void startArray(const std::size_t, const bool compact=false)
Definition: writer.cpp:150
std::ostream * output_stream_
output stream
Definition: writer.cpp:36
ARILES_SHARED_PTR< YAML::Emitter > EmitterPtr
Definition: writer.cpp:28
Writer(const std::string &file_name)
Definition: writer.cpp:69
void endRoot(const std::string &name)
Definition: writer.cpp:178
EmitterPtr emitter_
instance of YAML emitter, is destroyed and reinitialized by flush()
Definition: writer.cpp:39
void startRoot(const std::string &name)
Definition: writer.cpp:165
void descend(const std::string &map_name)
Starts a nested map in the configuration file.
Definition: writer.cpp:114
Writer(std::ostream &output_stream)
Definition: writer.cpp:77
static void openFile(std::ofstream &config_ofs, const std::string &file_name)
open configuration file
Definition: write.h:33
std::ofstream config_ofs_
output file stream
Definition: writer.cpp:33
#define ARILES_BASIC_REAL_TYPES_LIST
Definition: helpers.h:96
t_Implementation Impl
Definition: yaml_cpp03.h:31
Writer(const std::string &file_name)
Definition: writer.cpp:101
#define ARILES_VISIBILITY_ATTRIBUTE
Definition: helpers.h:69
ARILES_SHARED_PTR< t_Implementation > ImplPtr
Definition: yaml_cpp03.h:32
Definition: basic.h:17