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 #include <boost/math/special_functions.hpp>
13 #include <yaml-cpp/yaml.h>
14 
15 
16 namespace ariles2
17 {
18  namespace ns_yaml_cpp
19  {
20  namespace impl
21  {
23  {
24  public:
25  typedef ARILES2_SHARED_PTR<YAML::Emitter> EmitterPtr;
26 
27 
28  public:
29  /// output file stream
30  std::ofstream config_ofs_;
31 
32  /// output stream
33  std::ostream *output_stream_;
34 
35  /// instance of YAML emitter, is destroyed and reinitialized by flush()
37 
38  std::size_t map_depth_;
40 
41 
42  public:
43  void initEmitter()
44  {
45  ARILES2_TRACE_FUNCTION;
46  emitter_ = EmitterPtr(new YAML::Emitter);
47  emitter_->SetDoublePrecision(std::numeric_limits<double>::digits10);
48  if (output_stream_->tellp() != 0)
49  {
50  *emitter_ << YAML::Newline;
51  }
52  *emitter_ << YAML::BeginMap;
53  map_depth_ = 0;
54  skip_root_map_ = false;
55  }
56 
57 
59  {
60  ARILES2_TRACE_FUNCTION;
61  *emitter_ << YAML::EndMap;
62  *output_stream_ << emitter_->c_str();
63  emitter_.reset();
64  }
65 
66  public:
67  explicit Writer(const std::string &file_name)
68  {
69  ariles2::write::Visitor::openFile(config_ofs_, file_name);
70  output_stream_ = &config_ofs_;
71  initEmitter();
72  }
73 
74 
75  explicit Writer(std::ostream &output_stream)
76  {
77  output_stream_ = &output_stream;
78  initEmitter();
79  }
80 
81 
82  void flush()
83  {
84  destroyEmitter();
85  *output_stream_ << "\n";
86  output_stream_->flush();
87  initEmitter();
88  }
89  };
90  } // namespace impl
91  } // namespace ns_yaml_cpp
92 } // namespace ariles2
93 
94 
95 namespace ariles2
96 {
97  namespace ns_yaml_cpp
98  {
99  Writer::Writer(const std::string &file_name)
100  {
101  impl_ = ImplPtr(new impl::Writer(file_name));
102  }
103 
104 
105  Writer::Writer(std::ostream &output_stream)
106  {
107  impl_ = ImplPtr(new impl::Writer(output_stream));
108  }
109 
110 
111 
112  void Writer::startMap(const Parameters &, const std::size_t /*num_entries*/)
113  {
114  ARILES2_TRACE_FUNCTION;
115  if (impl_->map_depth_ > 0 or false == impl_->skip_root_map_)
116  {
117  *impl_->emitter_ << YAML::BeginMap;
118  }
119  ++impl_->map_depth_;
120  }
121 
122  void Writer::startMapEntry(const std::string &map_name)
123  {
124  ARILES2_TRACE_FUNCTION;
125  ARILES2_TRACE_VALUE(map_name);
126  *impl_->emitter_ << YAML::Key << map_name;
127  *impl_->emitter_ << YAML::Value;
128  }
129 
131  {
132  ARILES2_TRACE_FUNCTION;
133  ARILES2_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 
143  {
144  ARILES2_TRACE_FUNCTION;
145  impl_->flush();
146  }
147 
148 
149  void Writer::startArray(const std::size_t /*size*/, const bool compact)
150  {
151  ARILES2_TRACE_FUNCTION;
152  if (true == compact)
153  {
154  *impl_->emitter_ << YAML::Flow;
155  }
156  *impl_->emitter_ << YAML::BeginSeq;
157  }
158 
159 
161  {
162  ARILES2_TRACE_FUNCTION;
163  *impl_->emitter_ << YAML::EndSeq;
164  }
165 
166 
167  void Writer::startRoot(const std::string &name, const Parameters &)
168  {
169  ARILES2_TRACE_FUNCTION;
170  ARILES2_TRACE_VALUE(name);
171  if (true == name.empty())
172  {
173  impl_->skip_root_map_ = true;
174  }
175  else
176  {
177  startMapEntry(name);
178  }
179  }
180 
181  void Writer::endRoot(const std::string &name)
182  {
183  ARILES2_TRACE_FUNCTION;
184  if (false == name.empty())
185  {
186  endMapEntry();
187  }
188  impl_->skip_root_map_ = false;
189  }
190 
191 
192 #define ARILES2_BASIC_TYPE(type) \
193  void Writer::writeElement(const type &element, const Parameters &) \
194  { \
195  *impl_->emitter_ << element; \
196  }
197 
199 
200 #undef ARILES2_BASIC_TYPE
201 
202 
203 #define ARILES2_BASIC_TYPE(type) \
204  void Writer::writeElement(const type &element, const Parameters &) \
205  { \
206  if (true == boost::math::isnan(element)) \
207  { \
208  *impl_->emitter_ << ".nan"; \
209  } \
210  else \
211  { \
212  if (true == boost::math::isinf(element)) \
213  { \
214  if (element < 0.0) \
215  { \
216  *impl_->emitter_ << "-.inf"; \
217  } \
218  else \
219  { \
220  *impl_->emitter_ << ".inf"; \
221  } \
222  } \
223  else \
224  { \
225  *impl_->emitter_ << static_cast<double>(element); \
226  } \
227  } \
228  }
229 
231 
232 #undef ARILES2_BASIC_TYPE
233 
234 
235 
236  void Writer::writeElement(const std::string &element, const Parameters &)
237  {
238  *impl_->emitter_ << element;
239  }
240 
241  void Writer::writeElement(const bool &element, const Parameters &)
242  {
243  *impl_->emitter_ << element;
244  }
245  } // namespace ns_yaml_cpp
246 } // namespace ariles2
ariles2
Definition: basic.h:16
ariles2::ns_yaml_cpp::impl::Writer::Writer
Writer(const std::string &file_name)
Definition: writer.cpp:67
ariles2::ns_yaml_cpp::Writer::startArray
void startArray(const std::size_t, const bool compact=false)
Definition: writer.cpp:149
ariles2::ns_yaml_cpp::Writer::startRoot
void startRoot(const std::string &name, const Parameters &)
Definition: writer.cpp:167
yaml_cpp.h
ariles2::write::VisitorBase< Visitor, Parameters >::endMapEntry
virtual void endMapEntry()
Definition: write.h:109
ariles2::ns_yaml_cpp::Writer::endArray
void endArray()
Definition: writer.cpp:160
ariles2::ns_yaml_cpp::impl::Writer::skip_root_map_
bool skip_root_map_
Definition: writer.cpp:39
ariles2::ns_yaml_cpp::Writer::flush
void flush()
Flush the configuration to the output.
Definition: writer.cpp:142
ariles2::ns_yaml_cpp::impl::Writer::map_depth_
std::size_t map_depth_
Definition: writer.cpp:38
ariles2::ns_yaml_cpp::impl::Writer::emitter_
EmitterPtr emitter_
instance of YAML emitter, is destroyed and reinitialized by flush()
Definition: writer.cpp:36
ariles2::ns_yaml_cpp::impl::Writer::initEmitter
void initEmitter()
Definition: writer.cpp:43
ariles2::ns_yaml_cpp::impl::Writer::output_stream_
std::ostream * output_stream_
output stream
Definition: writer.cpp:33
ariles2::ns_yaml_cpp::Writer::startMap
void startMap(const Parameters &, const std::size_t)
Starts a nested map in the configuration file.
Definition: writer.cpp:112
ariles2::ns_yaml_cpp::impl::Writer::destroyEmitter
void destroyEmitter()
Definition: writer.cpp:58
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_yaml_cpp::impl::Writer::Writer
Writer(std::ostream &output_stream)
Definition: writer.cpp:75
ariles2::ns_yaml_cpp::impl::Writer::EmitterPtr
ARILES2_SHARED_PTR< YAML::Emitter > EmitterPtr
Definition: writer.cpp:25
ariles2::serialization::PIMPLVisitor< write::Visitor, impl::Writer >::impl_
ImplPtr impl_
Definition: serialization.h:128
ariles2::ns_yaml_cpp::Writer::startMapEntry
void startMapEntry(const std::string &map_name)
Starts a nested map in the configuration file.
Definition: writer.cpp:122
ariles2::ns_yaml_cpp::impl::Writer::flush
void flush()
Definition: writer.cpp:82
ariles2::ns_yaml_cpp::impl::Writer
Definition: writer.cpp:22
ariles2::ns_yaml_cpp::Writer::endMap
void endMap()
Ends a nested map in the configuration file.
Definition: writer.cpp:130
ariles2::ns_yaml_cpp::Writer::endRoot
void endRoot(const std::string &name)
Definition: writer.cpp:181
ARILES2_BASIC_REAL_TYPES_LIST
#define ARILES2_BASIC_REAL_TYPES_LIST
Definition: helpers.h:118
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
ariles2::ns_yaml_cpp::Writer::Writer
Writer(const std::string &file_name)
Definition: writer.cpp:99
ARILES2_BASIC_INTEGER_TYPES_LIST
#define ARILES2_BASIC_INTEGER_TYPES_LIST
Definition: helpers.h:114
ariles2::ns_yaml_cpp::impl::Writer::config_ofs_
std::ofstream config_ofs_
output file stream
Definition: writer.cpp:30
ariles2::serialization::PIMPLVisitor< write::Visitor, impl::Writer >::ImplPtr
ARILES2_SHARED_PTR< impl::Writer > ImplPtr
Definition: serialization.h:125