Ariles
writer.cpp
Go to the documentation of this file.
1 /**
2  @file
3  @author Alexander Sherikov
4 
5  @copyright 2014-2017 INRIA. Licensed under the Apache License, Version 2.0.
6  (see @ref LICENSE or http://www.apache.org/licenses/LICENSE-2.0)
7 
8  @copyright 2017-2018 Alexander Sherikov, Licensed under the Apache License, Version 2.0.
9  (see @ref LICENSE or http://www.apache.org/licenses/LICENSE-2.0)
10 
11  @brief
12 */
13 
14 #include <msgpack.hpp>
16 
17 
18 namespace ariles
19 {
20  namespace ns_msgpack
21  {
22  namespace impl
23  {
25  {
26  private:
27  Writer(const Writer &);
28  void operator=(const Writer &);
29 
30 
31  public:
32  /// output file stream
33  std::ofstream config_ofs_;
34 
35  /// output stream
36  std::ostream *output_stream_;
37 
38  ::msgpack::packer<std::ostream> *packer_;
39 
40  std::size_t nameless_counter_;
41 
42 
43  public:
44  explicit Writer(const std::string &file_name)
45  {
46  ariles::write::Visitor::openFile(config_ofs_, file_name);
47  output_stream_ = &config_ofs_;
48  packer_ = new ::msgpack::packer<std::ostream>(*output_stream_);
49 
50  nameless_counter_ = 0;
51  }
52 
53 
54  explicit Writer(std::ostream &output_stream)
55  {
56  output_stream_ = &output_stream;
57  packer_ = new ::msgpack::packer<std::ostream>(*output_stream_);
58 
59  nameless_counter_ = 0;
60  }
61 
62 
64  {
65  delete packer_;
66  }
67  };
68  } // namespace impl
69  } // namespace ns_msgpack
70 } // namespace ariles
71 
72 
73 namespace ariles
74 {
75  namespace ns_msgpack
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 
89  void Writer::descend(const std::string &map_name)
90  {
92  ARILES_TRACE_ENTRY(map_name);
93  impl_->packer_->pack(map_name);
94  }
95 
96 
97  void Writer::startMap(const std::size_t num_entries)
98  {
100  impl_->packer_->pack_map(num_entries);
101  }
102 
103 
105  {
107  impl_->output_stream_->flush();
108  }
109 
110 
111  void Writer::startArray(const std::size_t size, const bool /*compact*/)
112  {
114  ARILES_ASSERT(size <= std::numeric_limits<uint32_t>::max(), "Vector is too long.");
115 
116  impl_->packer_->pack_array(size);
117  }
118 
119 
120  void Writer::startRoot(const std::string &name)
121  {
123  if (true == name.empty())
124  {
125  ARILES_ASSERT(
126  0 == impl_->nameless_counter_,
127  "Multiple nameless root entries are not supported, specify root names explicitly.");
128  ++impl_->nameless_counter_;
129  impl_->packer_->pack_map(1);
130  descend("ariles");
131  }
132  else
133  {
134  impl_->packer_->pack_map(1);
135  descend(name);
136  }
137  }
138 
139  void Writer::endRoot(const std::string & /*name*/)
140  {
142  ascend();
143  }
144 
145 
146 #define ARILES_BASIC_TYPE(type) \
147  void Writer::writeElement(const type &element) \
148  { \
149  ARILES_TRACE_FUNCTION; \
150  impl_->packer_->pack(element); \
151  }
152 
154 
155 #undef ARILES_BASIC_TYPE
156  } // namespace ns_msgpack
157 } // namespace ariles
#define ARILES_TRACE_FUNCTION
Definition: trace.h:118
Writer(const std::string &file_name)
Definition: writer.cpp:44
virtual void ascend()
Definition: write.h:58
void flush()
Flush the configuration to the output.
Definition: writer.cpp:104
ARILES_MACRO_SUBSTITUTE(ARILES_BASIC_INTEGER_TYPES_LIST) ARILES_MACRO_SUBSTITUTE(ARILES_BASIC_REAL_TYPES_LIST) void Writer
Definition: writer.cpp:195
std::ofstream config_ofs_
output file stream
Definition: writer.cpp:33
#define ARILES_BASIC_TYPES_LIST
Definition: helpers.h:105
void startMap(const std::size_t num_entries)
Starts a nested map in the configuration file.
Definition: writer.cpp:97
std::ostream * output_stream_
output stream
Definition: writer.cpp:36
static void openFile(std::ofstream &config_ofs, const std::string &file_name)
open configuration file
Definition: write.h:33
void startArray(const std::size_t size, const bool=false)
Definition: writer.cpp:111
class ARILES_VISIBILITY_ATTRIBUTE Writer
Definition: writer.h:22
::msgpack::packer< std::ostream > * packer_
Definition: writer.cpp:38
void endRoot(const std::string &name)
Definition: writer.cpp:139
Writer(std::ostream &output_stream)
Definition: writer.cpp:54
void descend(const std::string &map_name)
Starts a nested map in the configuration file.
Definition: writer.cpp:89
#define ARILES_VISIBILITY_ATTRIBUTE
Definition: helpers.h:69
void startRoot(const std::string &name)
Definition: writer.cpp:120
#define ARILES_TRACE_ENTRY(entry_name)
Definition: trace.h:119
Definition: basic.h:17
Writer(const std::string &file_name)
Constructor.
Definition: writer.cpp:77