Ariles
writer_compact.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 namespace ariles
18 {
19  namespace ns_msgpack_compact
20  {
21  namespace impl
22  {
24  {
25  private:
26  Writer(const Writer &);
27  void operator=(const Writer &);
28 
29 
30  public:
31  /// output file stream
32  std::ofstream config_ofs_;
33 
34  /// output stream
35  std::ostream *output_stream_;
36 
37  ::msgpack::packer<std::ostream> *packer_;
38 
39  public:
40  explicit Writer(const std::string &file_name)
41  {
42  ariles::write::Visitor::openFile(config_ofs_, file_name);
43  output_stream_ = &config_ofs_;
44  packer_ = new ::msgpack::packer<std::ostream>(*output_stream_);
45  }
46 
47 
48  explicit Writer(std::ostream &output_stream)
49  {
50  output_stream_ = &output_stream;
51  packer_ = new ::msgpack::packer<std::ostream>(*output_stream_);
52  }
53 
54 
56  {
57  delete packer_;
58  }
59  };
60  } // namespace impl
61  } // namespace ns_msgpack_compact
62 } // namespace ariles
63 
64 
65 namespace ariles
66 {
67  namespace ns_msgpack_compact
68  {
69  Writer::Writer(const std::string &file_name)
70  {
71  impl_ = ImplPtr(new Impl(file_name));
72  }
73 
74 
75  Writer::Writer(std::ostream &output_stream)
76  {
77  impl_ = ImplPtr(new Impl(output_stream));
78  }
79 
80 
81  void Writer::startMap(const std::size_t num_entries)
82  {
83  impl_->packer_->pack_array(num_entries);
84  }
85 
86 
88  {
89  impl_->output_stream_->flush();
90  }
91 
92 
93  void Writer::startArray(const std::size_t size, const bool /*compact*/)
94  {
95  ARILES_ASSERT(size <= std::numeric_limits<uint32_t>::max(), "Vector is too long.");
96  impl_->packer_->pack_array(size);
97  }
98 
99 
100 #define ARILES_BASIC_TYPE(type) \
101  void Writer::writeElement(const type &element) \
102  { \
103  impl_->packer_->pack(element); \
104  }
105 
107 
108 #undef ARILES_BASIC_TYPE
109  } // namespace ns_msgpack_compact
110 } // namespace ariles
Writer(std::ostream &output_stream)
Writer(const std::string &file_name)
Constructor.
::msgpack::packer< std::ostream > * packer_
void startMap(const std::size_t num_entries)
Starts a nested map in the configuration file.
class ARILES_VISIBILITY_ATTRIBUTE Writer
Writer(const std::string &file_name)
#define ARILES_BASIC_TYPES_LIST
Definition: helpers.h:105
void flush()
Flush the configuration to the output.
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)
std::ostream * output_stream_
output stream
ARILES_MACRO_SUBSTITUTE(ARILES_BASIC_NUMERIC_TYPES_LIST) void Writer
Definition: writer.cpp:224
std::ofstream config_ofs_
output file stream
#define ARILES_VISIBILITY_ATTRIBUTE
Definition: helpers.h:69
Definition: basic.h:17