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 ariles
14 {
15  namespace ns_pugixml
16  {
17  namespace impl
18  {
20  {
21  public:
22  pugi::xml_document document_;
23 
24  std::vector<NodeWrapper> node_stack_;
25 
26 
27  /// output file stream
28  std::ofstream config_ofs_;
29 
30  /// output stream
31  std::ostream *output_stream_;
32 
33 
34 
35  public:
36  explicit Writer(const std::string &file_name)
37  {
38  ariles::write::Visitor::openFile(config_ofs_, file_name);
39  output_stream_ = &config_ofs_;
40  node_stack_.push_back(document_);
41  }
42 
43 
44  explicit Writer(std::ostream &output_stream)
45  {
46  output_stream_ = &output_stream;
47  node_stack_.push_back(document_);
48  }
49 
50 
51  /**
52  * @brief Get current node
53  *
54  * @return pointer to the current node
55  */
56  pugi::xml_node &getRawNode()
57  {
58  return (node_stack_.back().node_);
59  }
60  };
61  } // namespace impl
62  } // namespace ns_pugixml
63 } // namespace ariles
64 
65 
66 namespace ariles
67 {
68  namespace ns_pugixml
69  {
70  Writer::Writer(const std::string &file_name)
71  {
72  impl_ = ImplPtr(new Impl(file_name));
73  }
74 
75 
76  Writer::Writer(std::ostream &output_stream)
77  {
78  impl_ = ImplPtr(new Impl(output_stream));
79  }
80 
81 
82 
84  {
85  impl_->document_.save(*impl_->output_stream_, " ", pugi::format_indent);
86  impl_->output_stream_->flush();
87  }
88 
89 
90  void Writer::descend(const std::string &map_name)
91  {
92  impl_->node_stack_.push_back(impl_->getRawNode().append_child(map_name.c_str()));
93  }
94 
96  {
97  impl_->node_stack_.pop_back();
98  }
99 
100 
101  void Writer::startArray(const std::size_t size, const bool /*compact*/)
102  {
103  impl_->node_stack_.push_back(NodeWrapper(impl_->getRawNode(), 0, size));
104  if (size > 0)
105  {
106  impl_->node_stack_.push_back(impl_->getRawNode().append_child("item"));
107  }
108  }
109 
111  {
112  impl_->node_stack_.pop_back();
113  ARILES_ASSERT(true == impl_->node_stack_.back().isArray(), "Internal error: expected array.");
114  ARILES_ASSERT(
115  impl_->node_stack_.back().index_ < impl_->node_stack_.back().size_,
116  "Internal error: array has more elements than expected.");
117  ++impl_->node_stack_.back().index_;
118  if (impl_->node_stack_.back().index_ < impl_->node_stack_.back().size_)
119  {
120  impl_->node_stack_.push_back(impl_->getRawNode().append_child("item"));
121  }
122  }
123 
125  {
126  impl_->node_stack_.pop_back();
127  }
128 
129 
130  void Writer::startRoot(const std::string &name)
131  {
133  if (true == name.empty())
134  {
135  descend("ariles");
136  }
137  else
138  {
139  descend(name);
140  }
141  }
142 
143  void Writer::endRoot(const std::string & /*name*/)
144  {
146  ascend();
147  }
148 
149 
150  void Writer::writeElement(const std::string &element)
151  {
152  impl_->getRawNode().text() = element.c_str();
153  }
154 
155 
156 #define ARILES_BASIC_TYPE(type) \
157  void Writer::writeElement(const type &element) \
158  { \
159  impl_->getRawNode().text() = (boost::lexical_cast<std::string>(element)).c_str(); \
160  }
161 
163 
164 #undef ARILES_BASIC_TYPE
165  } // namespace ns_pugixml
166 } // namespace ariles
Writer(const std::string &file_name)
Definition: writer.cpp:36
#define ARILES_TRACE_FUNCTION
Definition: trace.h:118
pugi::xml_document document_
Definition: writer.cpp:22
std::ofstream config_ofs_
output file stream
Definition: writer.cpp:28
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 size, const bool=false)
Definition: writer.cpp:101
void endRoot(const std::string &name)
Definition: writer.cpp:143
Writer(std::ostream &output_stream)
Definition: writer.cpp:44
std::vector< NodeWrapper > node_stack_
Definition: writer.cpp:24
Writer(const std::string &file_name)
Definition: writer.cpp:70
void descend(const std::string &map_name)
Starts a nested map in the configuration file.
Definition: writer.cpp:90
void flush()
Flush the configuration to the output.
Definition: writer.cpp:83
static void openFile(std::ofstream &config_ofs, const std::string &file_name)
open configuration file
Definition: write.h:33
std::ostream * output_stream_
output stream
Definition: writer.cpp:31
void startRoot(const std::string &name)
Definition: writer.cpp:130
pugi::xml_node & getRawNode()
Get current node.
Definition: writer.cpp:56
ariles::Node< pugi::xml_node > NodeWrapper
Definition: common.h:22
#define ARILES_BASIC_NUMERIC_TYPES_LIST
Definition: helpers.h:100
#define ARILES_VISIBILITY_ATTRIBUTE
Definition: helpers.h:69
Definition: basic.h:17