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 <ariles/visitors/octave.h>
12 
13 #include <limits>
14 #include <iomanip>
15 #include <vector>
16 #include <boost/lexical_cast.hpp>
17 
18 
19 namespace ariles
20 {
21  namespace ns_octave
22  {
24  }
25 } // namespace ariles
26 
27 namespace ariles
28 {
29  namespace ns_octave
30  {
31  namespace impl
32  {
34  {
35  public:
36  std::vector<NodeWrapper> node_stack_;
37 
38  /// output file stream
39  std::ofstream config_ofs_;
40 
41  /// output stream
42  std::ostream *output_stream_;
43 
44 
45  protected:
46  /**
47  * @brief Initialize emitter
48  */
49  void initEmitter()
50  {
51  *output_stream_ << std::setprecision(std::numeric_limits<double>::digits10);
52  }
53 
54 
55  public:
56  explicit Writer(const std::string &file_name)
57  {
58  ariles::write::Visitor::openFile(config_ofs_, file_name);
59  output_stream_ = &config_ofs_;
60  initEmitter();
61  }
62 
63  explicit Writer(std::ostream &output_stream)
64  {
65  output_stream_ = &output_stream;
66  initEmitter();
67  }
68  };
69  } // namespace impl
70  } // namespace ns_octave
71 } // namespace ariles
72 
73 namespace ariles
74 {
75  namespace ns_octave
76  {
77  Writer::Writer(const std::string &file_name)
78  {
79  impl_ = ImplPtr(new Impl(file_name));
80  }
81 
82 
83  Writer::Writer(std::ostream &output_stream)
84  {
85  impl_ = ImplPtr(new Impl(output_stream));
86  }
87 
88 
90  {
92  return (parameters);
93  }
94 
95 
97  {
98  impl_->output_stream_->flush();
99  }
100 
101 
102 
103  void Writer::descend(const std::string &map_name)
104  {
105  if (0 == impl_->node_stack_.size())
106  {
107  impl_->node_stack_.push_back(map_name);
108  }
109  else
110  {
111  if (true == impl_->node_stack_.back().isArray())
112  {
113  std::string node = impl_->node_stack_.back().node_;
114  node += "{";
115  node += boost::lexical_cast<std::string>(impl_->node_stack_.back().index_ + 1);
116  node += "}.";
117  node += map_name;
118  impl_->node_stack_.push_back(NodeWrapper(node));
119  }
120  else
121  {
122  impl_->node_stack_.push_back(impl_->node_stack_.back().node_ + "." + map_name);
123  }
124  }
125  }
126 
128  {
129  impl_->node_stack_.pop_back();
130  }
131 
132 
133  void Writer::startArray(const std::size_t size, const bool compact)
134  {
135  if (true == impl_->node_stack_.back().isArray())
136  {
137  std::string node = impl_->node_stack_.back().node_;
138  node += "{";
139  node += boost::lexical_cast<std::string>(impl_->node_stack_.back().index_ + 1);
140  node += "}";
141  impl_->node_stack_.push_back(NodeWrapper(node, 0, size, compact));
142  }
143  else
144  {
145  impl_->node_stack_.push_back(NodeWrapper(impl_->node_stack_.back().node_, 0, size));
146  }
147  }
148 
150  {
151  ARILES_ASSERT(true == impl_->node_stack_.back().isArray(), "Internal error: array expected.");
152  ++impl_->node_stack_.back().index_;
153  }
154 
156  {
157  impl_->node_stack_.pop_back();
158  }
159 
160 
161  void Writer::startMatrix(const bool compact)
162  {
163  std::string node = impl_->node_stack_.back().node_;
164  if (compact)
165  {
166  node += " = [";
167  }
168  else
169  {
170  node += " = [...\n";
171  }
172 
173  impl_->node_stack_.push_back(NodeWrapper(node, NodeWrapper::MATRIX, compact));
174  *impl_->output_stream_ << impl_->node_stack_.back().node_;
175  }
176 
178  {
179  impl_->node_stack_.back().index_ = 0;
180  }
181 
183  {
184  if (impl_->node_stack_.back().isCompact())
185  {
186  *impl_->output_stream_ << "; ";
187  }
188  else
189  {
190  *impl_->output_stream_ << "; ...\n";
191  }
192  }
193 
195  {
196  *impl_->output_stream_ << "];\n";
197  impl_->node_stack_.pop_back();
198  }
199 
200 
201 #define ARILES_BASIC_TYPE(type) \
202  void Writer::writeElement(const type &element) \
203  { \
204  if (true == impl_->node_stack_.back().isMatrix()) \
205  { \
206  if (0 != impl_->node_stack_.back().index_) \
207  { \
208  *impl_->output_stream_ << ", "; \
209  } \
210  *impl_->output_stream_ << element; \
211  ++impl_->node_stack_.back().index_; \
212  } \
213  else \
214  { \
215  *impl_->output_stream_ << impl_->node_stack_.back().node_; \
216  if (true == impl_->node_stack_.back().isArray()) \
217  { \
218  *impl_->output_stream_ << "{" << impl_->node_stack_.back().index_ + 1 << "}"; \
219  } \
220  *impl_->output_stream_ << " = " << element << ";\n"; \
221  } \
222  }
223 
225 
226 #undef ARILES_BASIC_TYPE
227 
228 
229  void Writer::writeElement(const std::string &element)
230  {
231  *impl_->output_stream_ << impl_->node_stack_.back().node_;
232  if (true == impl_->node_stack_.back().isArray())
233  {
234  *impl_->output_stream_ << "{" << impl_->node_stack_.back().index_ + 1 << "}";
235  }
236  *impl_->output_stream_ << " = '" << element << "';\n";
237  }
238  } // namespace ns_octave
239 } // namespace ariles
Writer(const std::string &file_name)
Definition: writer.cpp:56
void flush()
Flush the configuration to the output.
Definition: writer.cpp:96
ARILES_SHARED_PTR< impl::Writer > ImplPtr
Definition: octave.h:37
void initEmitter()
Initialize emitter.
Definition: writer.cpp:49
impl::Writer Impl
Definition: octave.h:36
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)
Starts a nested map in the configuration file.
Definition: writer.cpp:103
void startArray(const std::size_t size, const bool compact=false)
Definition: writer.cpp:133
Writer(const std::string &file_name)
Definition: writer.cpp:77
std::ofstream config_ofs_
output file stream
Definition: writer.cpp:39
ARILES_MACRO_SUBSTITUTE(ARILES_BASIC_NUMERIC_TYPES_LIST) void Writer
Definition: writer.cpp:224
ariles::Node< std::string > NodeWrapper
Definition: writer.cpp:23
Writer(std::ostream &output_stream)
Definition: writer.cpp:63
void startMatrix(const bool compact=false)
Definition: writer.cpp:161
std::vector< NodeWrapper > node_stack_
Definition: writer.cpp:36
#define ARILES_BASIC_NUMERIC_TYPES_LIST
Definition: helpers.h:100
const serialization::Features & getSerializationFeatures() const
Definition: writer.cpp:89
#define ARILES_VISIBILITY_ATTRIBUTE
Definition: helpers.h:69
std::ostream * output_stream_
output stream
Definition: writer.cpp:42
Definition: basic.h:17