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 ariles2
19 {
20  namespace ns_msgpack
21  {
22  namespace impl
23  {
25  {
26  public:
27  using PackerPtr = std::shared_ptr<::msgpack::packer<std::ostream>>;
28 
29 
30  public:
32 
33  std::size_t nameless_counter_;
34 
35 
36  public:
37  Writer(const Writer &) = delete;
38  void operator=(const Writer &) = delete;
39 
40 
41  template <class... t_Args>
42  explicit Writer(t_Args &&...args) : FileVisitorImplementation(std::forward<t_Args>(args)...)
43  {
44  packer_ = std::make_shared<::msgpack::packer<std::ostream>>(*output_stream_);
45 
47  }
48  };
49  } // namespace impl
50  } // namespace ns_msgpack
51 } // namespace ariles2
52 
53 
54 namespace ariles2
55 {
56  namespace ns_msgpack
57  {
58  Writer::Writer(const std::string &file_name)
59  {
60  makeImplPtr(file_name);
61  }
62 
63 
64  Writer::Writer(std::ostream &output_stream)
65  {
66  makeImplPtr(output_stream);
67  }
68 
69 
70  void Writer::startMap(const Parameters &, const std::size_t num_entries)
71  {
73  impl_->packer_->pack_map(num_entries);
74  }
75 
76  void Writer::startMapEntry(const std::string &map_name)
77  {
79  CPPUT_TRACE_VALUE(map_name);
80  impl_->packer_->pack(map_name);
81  }
82 
83 
85  {
87  impl_->output_stream_->flush();
88  }
89 
90 
91  void Writer::startArray(const std::size_t size, const bool /*compact*/)
92  {
94  CPPUT_ASSERT(size <= std::numeric_limits<uint32_t>::max(), "Vector is too long.");
95 
96  impl_->packer_->pack_array(size);
97  }
98 
99 
100  void Writer::startRoot(const std::string &name, const Parameters &)
101  {
103  if (name.empty())
104  {
105  CPPUT_ASSERT(
106  0 == impl_->nameless_counter_,
107  "Multiple nameless root entries are not supported, specify root names explicitly.");
108  ++impl_->nameless_counter_;
109  impl_->packer_->pack_map(1);
110  startMapEntry("ariles");
111  }
112  else
113  {
114  impl_->packer_->pack_map(1);
115  startMapEntry(name);
116  }
117  }
118 
119  void Writer::endRoot(const std::string & /*name*/)
120  {
122  endMapEntry();
123  }
124 
125 
126 #define ARILES2_BASIC_TYPE(type) \
127  void Writer::writeElement(const type &element, const Parameters &) \
128  { \
129  CPPUT_TRACE_FUNCTION; \
130  impl_->packer_->pack(element); \
131  }
132 
134 
135 #undef ARILES2_BASIC_TYPE
136  } // namespace ns_msgpack
137 } // namespace ariles2
void startMapEntry(const std::string &map_name)
Starts a nested map in the configuration file.
Definition: writer.cpp:76
void startArray(const std::size_t size, const bool=false)
Definition: writer.cpp:91
void startMap(const Parameters &, const std::size_t num_entries)
Starts a nested map in the configuration file.
Definition: writer.cpp:70
void startRoot(const std::string &name, const Parameters &)
Definition: writer.cpp:100
void flush()
Flush the configuration to the output.
Definition: writer.cpp:84
Writer(const std::string &file_name)
Constructor.
Definition: writer.cpp:58
void endRoot(const std::string &name)
Definition: writer.cpp:119
Writer(t_Args &&...args)
Definition: writer.cpp:42
Writer(const Writer &)=delete
void operator=(const Writer &)=delete
std::shared_ptr<::msgpack::packer< std::ostream > > PackerPtr
Definition: writer.cpp:27
FileVisitorImplementation(const std::string &file_name)
Definition: write.h:384
#define CPPUT_ASSERT(condition,...)
Definition: exception.h:32
#define ARILES2_BASIC_TYPES_LIST
Definition: helpers.h:72
#define CPPUT_MACRO_SUBSTITUTE(macro)
Definition: misc.h:21
Definition: basic.h:17
#define CPPUT_TRACE_FUNCTION
Definition: trace.h:126
#define CPPUT_TRACE_VALUE(value)
Definition: trace.h:127