Ariles
writer.cpp
Go to the documentation of this file.
1 /**
2  @file
3  @author Alexander Sherikov
4 
5  @copyright 2018 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 "common.h"
12 
13 namespace ariles2
14 {
15  namespace ns_pugixml
16  {
17  namespace impl
18  {
20  {
21  public:
22  pugi::xml_document document_;
23 
24 
25  public:
26  template <class... t_Args>
27  explicit Writer(t_Args &&...args) : FileVisitorImplementation(std::forward<t_Args>(args)...)
28  {
29  node_stack_.emplace_back(document_);
30  }
31 
32 
33  /**
34  * @brief Get current node
35  *
36  * @return pointer to the current node
37  */
38  pugi::xml_node &getRawNode()
39  {
40  return (back().node_);
41  }
42  };
43  } // namespace impl
44  } // namespace ns_pugixml
45 } // namespace ariles2
46 
47 
48 namespace ariles2
49 {
50  namespace ns_pugixml
51  {
52  Writer::Writer(const std::string &file_name)
53  {
54  impl_ = std::make_shared<impl::Writer>(file_name);
55  }
56 
57 
58  Writer::Writer(std::ostream &output_stream)
59  {
60  impl_ = std::make_shared<impl::Writer>(output_stream);
61  }
62 
63 
64 
66  {
67  impl_->document_.save(*impl_->output_stream_, " ", pugi::format_indent);
68  impl_->output_stream_->flush();
69  }
70 
71 
72  void Writer::startMapEntry(const std::string &map_name)
73  {
74  impl_->emplace(impl_->getRawNode().append_child(map_name.c_str()));
75  }
76 
78  {
79  impl_->pop();
80  }
81 
82 
83  void Writer::startArray(const std::size_t size, const bool /*compact*/)
84  {
85  impl_->emplace(impl_->getRawNode(), 0, size);
86  }
87 
89  {
91  impl_->back().index_ < impl_->back().size_,
92  "Internal error: array has more elements than expected.");
93  impl_->emplace(impl_->getRawNode().append_child("item"));
94  }
95 
97  {
98  impl_->pop();
99  impl_->shiftArray();
100  }
101 
103  {
104  impl_->pop();
105  }
106 
107 
108  void Writer::startRoot(const std::string &name, const Parameters &)
109  {
111  if (name.empty())
112  {
113  startMapEntry("ariles");
114  }
115  else
116  {
117  startMapEntry(name);
118  }
119  }
120 
121  void Writer::endRoot(const std::string & /*name*/)
122  {
124  endMapEntry();
125  }
126 
127 
128  void Writer::writeElement(const std::string &element, const Parameters &)
129  {
130  impl_->getRawNode().text() = element.c_str();
131  }
132 
133 
134 #define ARILES2_BASIC_TYPE(type) \
135  void Writer::writeElement(const type &element, const Parameters &) \
136  { \
137  impl_->getRawNode().text() = (boost::lexical_cast<std::string>(element)).c_str(); \
138  }
139 
141 
142 #undef ARILES2_BASIC_TYPE
143  } // namespace ns_pugixml
144 } // namespace ariles2
void startRoot(const std::string &name, const Parameters &)
Definition: writer.cpp:108
void startArray(const std::size_t size, const bool=false)
Definition: writer.cpp:83
void endRoot(const std::string &name)
Definition: writer.cpp:121
void startMapEntry(const std::string &map_name)
Starts a nested map in the configuration file.
Definition: writer.cpp:72
Writer(const std::string &file_name)
Definition: writer.cpp:52
void flush()
Flush the configuration to the output.
Definition: writer.cpp:65
Writer(t_Args &&...args)
Definition: writer.cpp:27
pugi::xml_node & getRawNode()
Get current node.
Definition: writer.cpp:38
pugi::xml_document document_
Definition: writer.cpp:22
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_NUMERIC_TYPES_LIST
Definition: helpers.h:63
#define CPPUT_MACRO_SUBSTITUTE(macro)
Definition: misc.h:21
visitor::Parameters Parameters
Definition: count.h:26
Definition: basic.h:17
#define CPPUT_TRACE_FUNCTION
Definition: trace.h:126