Scarab  v1.6.1
Project 8 C++ Utility Library
param_msgpack.hh
Go to the documentation of this file.
1 /*
2  * param_msgpack.hh
3  *
4  * Created on: March 27, 2015
5  * Author: bhlaroque
6  */
7 
8 #ifndef SCARAB_PARAM_MSGPACK_HH_
9 #define SCARAB_PARAM_MSGPACK_HH_
10 
11 #include "msgpack_fwd.hpp"
12 
13 #include <deque>
14 #include <map>
15 #include <sstream>
16 #include <string>
17 
18 #include "logger.hh"
19 #include "param.hh"
20 
21 
22 namespace scarab
23 {
24  LOGGER(dlog_param_msgpack, "param_msgpack");
25 
26  //***************************************
27  //************** INPUT ******************
28  //***************************************
29 
31  {
32  public:
34  virtual ~param_input_msgpack();
35 
36  static param* read_file( const std::string& a_filename );
37  static param* read_string( const std::string& a_msgpack_str );
38  static param* read_msgpack_element( const msgpack::object& a_msgpack_array );
39  //static param_node* read_document( const rapidjson::Document& a_document );
40  //static param* read_value( const rapidjson::Value& a_value );
41  };
42 
43  //***************************************
44  //************** OUTPUT *****************
45  //***************************************
46 
48  {
49  public:
50  //typedef rapidjson::Writer< rapidjson::FileStream, rapidjson::UTF8<>, rapidjson::MemoryPoolAllocator<> > rj_file_writer;
51  //typedef rapidjson::PrettyWriter< rapidjson::FileStream, rapidjson::UTF8<>, rapidjson::MemoryPoolAllocator<> > rj_pretty_file_writer;
52  //typedef rapidjson::Writer< rapidjson::StringBuffer, rapidjson::UTF8<>, rapidjson::MemoryPoolAllocator<> > rj_string_writer;
53  //typedef rapidjson::PrettyWriter< rapidjson::StringBuffer, rapidjson::UTF8<>, rapidjson::MemoryPoolAllocator<> > rj_pretty_string_writer;
54 
56  {
58  k_pretty
59  };
60 
61  public:
63  virtual ~param_output_msgpack();
64 
65  static bool write_file( const param& a_to_write, const std::string& a_filename, json_writing_style a_style );
66  static bool write_string( const param& a_to_write, std::string& a_string, json_writing_style a_style );
67  template< class XWriter >
68  static bool write_param( const param& a_to_write, XWriter* a_writer );
69  template< class XWriter >
70  static bool write_param_null( const param& a_to_write, XWriter* a_writer );
71  template< class XWriter >
72  static bool write_param_value( const param_value& a_to_write, XWriter* a_writer );
73  template< class XWriter >
74  static bool write_param_array( const param_array& a_to_write, XWriter* a_writer );
75  template< class XWriter >
76  static bool write_param_node( const param_node& a_to_write, XWriter* a_writer );
77 
78  };
79 
80  template< class XWriter >
81  bool param_output_msgpack::write_param( const param& a_to_write, XWriter* a_writer )
82  {
83  if( a_to_write.is_null() )
84  {
85  return param_output_msgpack::write_param_null( a_to_write, a_writer );
86  }
87  if( a_to_write.is_value() )
88  {
89  return param_output_msgpack::write_param_value( a_to_write.as_value(), a_writer );
90  }
91  if( a_to_write.is_array() )
92  {
93  return param_output_msgpack::write_param_array( a_to_write.as_array(), a_writer );
94  }
95  if( a_to_write.is_node() )
96  {
97  return param_output_msgpack::write_param_node( a_to_write.as_node(), a_writer );
98  }
99  LWARN( dlog_param_msgpack, "parameter not written: <" << a_to_write << ">" );
100  return false;
101  }
102  template< class XWriter >
103  bool param_output_msgpack::write_param_null( const param& /*a_to_write*/, XWriter* a_writer )
104  {
105  //LWARN( dlog_param_msgpack, "writing null" );
106  a_writer->Null();
107  return true;
108  }
109  template< class XWriter >
110  bool param_output_msgpack::write_param_value( const param_value& a_to_write, XWriter* a_writer )
111  {
112  //LWARN( dlog_param_msgpack, "writing value" );
113  if( a_to_write.is_string() )
114  {
115  a_writer->String( a_to_write.as_string().c_str() );
116  //LWARN( dlog_param_msgpack, "writing string to msgpack: " << a_to_write.as_string() );
117  }
118  else if( a_to_write.is_bool() )
119  {
120  a_writer->Bool( a_to_write.as_bool() );
121  //LWARN( dlog_param_msgpack, "writing bool to msgpack: " << a_to_write.as_bool() );
122  }
123  else if( a_to_write.is_int() )
124  {
125  a_writer->Int64( a_to_write.as_int() );
126  //LWARN( dlog_param_msgpack, "writing int to msgpack: " << a_to_write.as_int() );
127  }
128  else if( a_to_write.is_uint() )
129  {
130  a_writer->Uint64( a_to_write.as_uint() );
131  //LWARN( dlog_param_msgpack, "writing uint to msgpack: " << a_to_write.as_uint() );
132  }
133  else if( a_to_write.is_double() )
134  {
135  a_writer->Double( a_to_write.as_double() );
136  //LWARN( dlog_param_msgpack, "writing double to msgpack: " << a_to_write.as_double() );
137  }
138  return true;
139  }
140  template< class XWriter >
141  bool param_output_msgpack::write_param_array( const param_array& a_to_write, XWriter* a_writer )
142  {
143  //LWARN( dlog_param_msgpack, "writing array" );
144  a_writer->StartArray();
145  for( param_array::const_iterator it = a_to_write.begin(); it != a_to_write.end(); ++it )
146  {
147  if( ! param_output_msgpack::write_param( *(*it), a_writer ) )
148  {
149  LERROR( dlog_param_msgpack, "Error while writing parameter array" );
150  return false;
151  }
152  }
153  a_writer->EndArray();
154  return true;
155  }
156  template< class XWriter >
157  bool param_output_msgpack::write_param_node( const param_node& a_to_write, XWriter* a_writer )
158  {
159  //LWARN( dlog_param_msgpack, "writing node" );
160  a_writer->StartObject();
161  for( param_node::const_iterator it = a_to_write.begin(); it != a_to_write.end(); ++it )
162  {
163  a_writer->String( it->first.c_str() );
164  if( ! param_output_msgpack::write_param( *(it->second), a_writer ) )
165  {
166  LERROR( dlog_param_msgpack, "Error while writing parameter node" );
167  return false;
168  }
169  }
170  a_writer->EndObject();
171  return true;
172  }
173 
174 
175 } /* namespace mantis */
176 
177 #endif /* _PARAM_MSGPACK_HH_ */
virtual bool is_node() const
Definition: param.hh:420
static bool write_param_node(const param_node &a_to_write, XWriter *a_writer)
#define LWARN(...)
Definition: logger.hh:364
static bool write_param_value(const param_value &a_to_write, XWriter *a_writer)
static bool write_param_null(const param &a_to_write, XWriter *a_writer)
iterator begin()
Definition: param.hh:1189
virtual bool is_value() const
Definition: param.hh:410
static bool write_param(const param &a_to_write, XWriter *a_writer)
#define SCARAB_API
Definition: scarab_api.hh:24
bool is_uint() const
Definition: param.hh:578
int64_t as_int() const
Definition: param.cc:286
#define LERROR(...)
Definition: logger.hh:365
bool is_bool() const
Definition: param.hh:573
static bool write_param_array(const param_array &a_to_write, XWriter *a_writer)
Contains the logger class and macros, based on Kasper&#39;s KLogger class.
bool is_int() const
Definition: param.hh:583
iterator end()
Definition: param.hh:920
param_value & as_value()
Definition: param.hh:425
contents::const_iterator const_iterator
Definition: param.hh:305
iterator end()
Definition: param.hh:1199
iterator begin()
Definition: param.hh:911
virtual bool is_null() const
Definition: param.hh:405
bool is_double() const
Definition: param.hh:588
const std::string & as_string() const
Definition: param.cc:318
param_node & as_node()
Definition: param.hh:437
bool is_string() const
Definition: param.hh:593
param_array & as_array()
Definition: param.hh:431
bool as_bool() const
Definition: param.cc:242
contents::const_iterator const_iterator
Definition: param.hh:179
double as_double() const
Definition: param.cc:302
uint64_t as_uint() const
Definition: param.cc:270
virtual bool is_array() const
Definition: param.hh:415
LOGGER(mtlog,"authentication")