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  typedef ARILES2_SHARED_PTR< ::msgpack::packer<std::ostream> > PackerPtr;
28 
29 
30  private:
31  Writer(const Writer &);
32  void operator=(const Writer &);
33 
34 
35  public:
36  /// output file stream
37  std::ofstream config_ofs_;
38 
39  /// output stream
40  std::ostream *output_stream_;
41 
43 
44  std::size_t nameless_counter_;
45 
46 
47  public:
48  explicit Writer(const std::string &file_name)
49  {
50  ariles2::write::Visitor::openFile(config_ofs_, file_name);
51  output_stream_ = &config_ofs_;
52  packer_ = PackerPtr(new ::msgpack::packer<std::ostream>(*output_stream_));
53 
54  nameless_counter_ = 0;
55  }
56 
57 
58  explicit Writer(std::ostream &output_stream)
59  {
60  output_stream_ = &output_stream;
61  packer_ = PackerPtr(new ::msgpack::packer<std::ostream>(*output_stream_));
62 
63  nameless_counter_ = 0;
64  }
65  };
66  } // namespace impl
67  } // namespace ns_msgpack
68 } // namespace ariles2
69 
70 
71 namespace ariles2
72 {
73  namespace ns_msgpack
74  {
75  Writer::Writer(const std::string &file_name)
76  {
77  impl_ = ImplPtr(new Impl(file_name));
78  }
79 
80 
81  Writer::Writer(std::ostream &output_stream)
82  {
83  impl_ = ImplPtr(new Impl(output_stream));
84  }
85 
86 
87  void Writer::startMap(const Parameters &, const std::size_t num_entries)
88  {
89  ARILES2_TRACE_FUNCTION;
90  impl_->packer_->pack_map(num_entries);
91  }
92 
93  void Writer::startMapEntry(const std::string &map_name)
94  {
95  ARILES2_TRACE_FUNCTION;
96  ARILES2_TRACE_VALUE(map_name);
97  impl_->packer_->pack(map_name);
98  }
99 
100 
102  {
103  ARILES2_TRACE_FUNCTION;
104  impl_->output_stream_->flush();
105  }
106 
107 
108  void Writer::startArray(const std::size_t size, const bool /*compact*/)
109  {
110  ARILES2_TRACE_FUNCTION;
111  ARILES2_ASSERT(size <= std::numeric_limits<uint32_t>::max(), "Vector is too long.");
112 
113  impl_->packer_->pack_array(size);
114  }
115 
116 
117  void Writer::startRoot(const std::string &name, const Parameters &)
118  {
119  ARILES2_TRACE_FUNCTION;
120  if (true == name.empty())
121  {
122  ARILES2_ASSERT(
123  0 == impl_->nameless_counter_,
124  "Multiple nameless root entries are not supported, specify root names explicitly.");
125  ++impl_->nameless_counter_;
126  impl_->packer_->pack_map(1);
127  startMapEntry("ariles");
128  }
129  else
130  {
131  impl_->packer_->pack_map(1);
132  startMapEntry(name);
133  }
134  }
135 
136  void Writer::endRoot(const std::string & /*name*/)
137  {
138  ARILES2_TRACE_FUNCTION;
139  endMapEntry();
140  }
141 
142 
143 #define ARILES2_BASIC_TYPE(type) \
144  void Writer::writeElement(const type &element, const Parameters &) \
145  { \
146  ARILES2_TRACE_FUNCTION; \
147  impl_->packer_->pack(element); \
148  }
149 
151 
152 #undef ARILES2_BASIC_TYPE
153  } // namespace ns_msgpack
154 } // namespace ariles2
ariles2::ns_msgpack::impl::Writer
Definition: writer.cpp:24
ariles2
Definition: basic.h:16
ARILES2_BASIC_TYPES_LIST
#define ARILES2_BASIC_TYPES_LIST
Definition: helpers.h:131
ariles2::ns_msgpack::Writer::Writer
Writer(const std::string &file_name)
Constructor.
Definition: writer.cpp:75
ariles2::write::VisitorBase< Visitor, Parameters >::endMapEntry
virtual void endMapEntry()
Definition: write.h:109
ariles2::ns_msgpack::Writer::startArray
void startArray(const std::size_t size, const bool=false)
Definition: writer.cpp:108
ariles2::ns_msgpack::Writer::startMap
void startMap(const Parameters &, const std::size_t num_entries)
Starts a nested map in the configuration file.
Definition: writer.cpp:87
ariles2::ns_msgpack::impl::Writer::PackerPtr
ARILES2_SHARED_PTR< ::msgpack::packer< std::ostream > > PackerPtr
Definition: writer.cpp:27
ariles2::ns_msgpack::impl::Writer::nameless_counter_
std::size_t nameless_counter_
Definition: writer.cpp:44
msgpack.h
ariles2::serialization::PIMPLVisitor< write::Visitor, impl::Writer >::Impl
impl::Writer Impl
Definition: serialization.h:124
ariles2::ns_msgpack::impl::Writer::Writer
Writer(std::ostream &output_stream)
Definition: writer.cpp:58
ariles2::ns_msgpack::Writer::flush
void flush()
Flush the configuration to the output.
Definition: writer.cpp:101
ariles2::ns_msgpack::impl::Writer
class ARILES2_VISIBILITY_ATTRIBUTE Writer
Definition: writer.h:22
ariles2::ns_msgpack::impl::Writer::Writer
Writer(const std::string &file_name)
Definition: writer.cpp:48
ARILES2_VISIBILITY_ATTRIBUTE
#define ARILES2_VISIBILITY_ATTRIBUTE
Definition: helpers.h:138
ariles2::ns_msgpack::Writer::startRoot
void startRoot(const std::string &name, const Parameters &)
Definition: writer.cpp:117
ariles2::ns_yaml_cpp::ARILES2_MACRO_SUBSTITUTE
ARILES2_MACRO_SUBSTITUTE(ARILES2_BASIC_INTEGER_TYPES_LIST) ARILES2_MACRO_SUBSTITUTE(ARILES2_BASIC_REAL_TYPES_LIST) void Writer
Definition: writer.cpp:198
ariles2::ns_msgpack::Writer::startMapEntry
void startMapEntry(const std::string &map_name)
Starts a nested map in the configuration file.
Definition: writer.cpp:93
ariles2::serialization::PIMPLVisitor< write::Visitor, impl::Writer >::impl_
ImplPtr impl_
Definition: serialization.h:128
ariles2::ns_msgpack::impl::Writer::config_ofs_
std::ofstream config_ofs_
output file stream
Definition: writer.cpp:37
ariles2::write::VisitorBase< Visitor, Parameters >::openFile
static void openFile(std::ofstream &config_ofs, const std::string &file_name)
open configuration file
Definition: write.h:58
ariles2::ns_msgpack::impl::Writer::output_stream_
std::ostream * output_stream_
output stream
Definition: writer.cpp:40
ariles2::ns_msgpack::Writer::endRoot
void endRoot(const std::string &name)
Definition: writer.cpp:136
ariles2::ns_msgpack::impl::Writer::packer_
PackerPtr packer_
Definition: writer.cpp:42
ariles2::serialization::PIMPLVisitor< write::Visitor, impl::Writer >::ImplPtr
ARILES2_SHARED_PTR< impl::Writer > ImplPtr
Definition: serialization.h:125