MessagePack for C++
define_decl.hpp
Go to the documentation of this file.
1 //
2 // MessagePack for C++ static resolution routine
3 //
4 // Copyright (C) 2016 KONDO Takatoshi
5 //
6 // Distributed under the Boost Software License, Version 1.0.
7 // (See accompanying file LICENSE_1_0.txt or copy at
8 // http://www.boost.org/LICENSE_1_0.txt)
9 //
10 #ifndef MSGPACK_DEFINE_DECL_HPP
11 #define MSGPACK_DEFINE_DECL_HPP
12 
13 #if defined(MSGPACK_NO_BOOST)
14 
15 // MSGPACK_PP_VARIADICS is defined in msgpack/preprocessor/config/config.hpp
16 // http://www.boost.org/libs/preprocessor/doc/ref/variadics.html
17 // However, supporting compiler detection is not complete. msgpack-c requires
18 // variadic macro arguments support. So MSGPACK_PP_VARIADICS is defined here explicitly.
19 #if !defined(MSGPACK_PP_VARIADICS)
20 #define MSGPACK_PP_VARIADICS
21 #endif
22 
23 #include <msgpack/preprocessor.hpp>
24 
25 #define MSGPACK_BASE_ARRAY(base) (*const_cast<base *>(static_cast<base const*>(this)))
26 #define MSGPACK_NVP(name, value) (name) (value)
27 
28 #define MSGPACK_DEFINE_MAP_EACH_PROC(r, data, elem) \
29  MSGPACK_PP_IF( \
30  MSGPACK_PP_IS_BEGIN_PARENS(elem), \
31  elem, \
32  (MSGPACK_PP_STRINGIZE(elem))(elem) \
33  )
34 
35 #define MSGPACK_DEFINE_MAP_IMPL(...) \
36  MSGPACK_PP_SEQ_TO_TUPLE( \
37  MSGPACK_PP_SEQ_FOR_EACH( \
38  MSGPACK_DEFINE_MAP_EACH_PROC, \
39  0, \
40  MSGPACK_PP_VARIADIC_TO_SEQ(__VA_ARGS__) \
41  ) \
42  )
43 
44 #define MSGPACK_DEFINE_MAP(...) \
45  template <typename Packer> \
46  void msgpack_pack(Packer& msgpack_pk) const \
47  { \
48  msgpack::type::make_define_map \
49  MSGPACK_DEFINE_MAP_IMPL(__VA_ARGS__) \
50  .msgpack_pack(msgpack_pk); \
51  } \
52  void msgpack_unpack(msgpack::object const& msgpack_o) \
53  { \
54  msgpack::type::make_define_map \
55  MSGPACK_DEFINE_MAP_IMPL(__VA_ARGS__) \
56  .msgpack_unpack(msgpack_o); \
57  }\
58  template <typename MSGPACK_OBJECT> \
59  void msgpack_object(MSGPACK_OBJECT* msgpack_o, msgpack::zone& msgpack_z) const \
60  { \
61  msgpack::type::make_define_map \
62  MSGPACK_DEFINE_MAP_IMPL(__VA_ARGS__) \
63  .msgpack_object(msgpack_o, msgpack_z); \
64  }
65 
66 #define MSGPACK_BASE_MAP(base) \
67  (MSGPACK_PP_STRINGIZE(base))(*const_cast<base *>(static_cast<base const*>(this)))
68 
69 #else // defined(MSGPACK_NO_BOOST)
70 
71 // BOOST_PP_VARIADICS is defined in boost/preprocessor/config/config.hpp
72 // http://www.boost.org/libs/preprocessor/doc/ref/variadics.html
73 // However, supporting compiler detection is not complete. msgpack-c requires
74 // variadic macro arguments support. So BOOST_PP_VARIADICS is defined here explicitly.
75 #if !defined(BOOST_PP_VARIADICS)
76 #define BOOST_PP_VARIADICS
77 #endif
78 
79 #include <boost/preprocessor.hpp>
80 
81 #define MSGPACK_BASE_ARRAY(base) (*const_cast<base *>(static_cast<base const*>(this)))
82 #define MSGPACK_NVP(name, value) (name) (value)
83 
84 #define MSGPACK_DEFINE_MAP_EACH_PROC(r, data, elem) \
85  BOOST_PP_IF( \
86  BOOST_PP_IS_BEGIN_PARENS(elem), \
87  elem, \
88  (BOOST_PP_STRINGIZE(elem))(elem) \
89  )
90 
91 #define MSGPACK_DEFINE_MAP_IMPL(...) \
92  BOOST_PP_SEQ_TO_TUPLE( \
93  BOOST_PP_SEQ_FOR_EACH( \
94  MSGPACK_DEFINE_MAP_EACH_PROC, \
95  0, \
96  BOOST_PP_VARIADIC_TO_SEQ(__VA_ARGS__) \
97  ) \
98  )
99 
100 #define MSGPACK_DEFINE_MAP(...) \
101  template <typename Packer> \
102  void msgpack_pack(Packer& msgpack_pk) const \
103  { \
104  msgpack::type::make_define_map \
105  MSGPACK_DEFINE_MAP_IMPL(__VA_ARGS__) \
106  .msgpack_pack(msgpack_pk); \
107  } \
108  void msgpack_unpack(msgpack::object const& msgpack_o) \
109  { \
110  msgpack::type::make_define_map \
111  MSGPACK_DEFINE_MAP_IMPL(__VA_ARGS__) \
112  .msgpack_unpack(msgpack_o); \
113  }\
114  template <typename MSGPACK_OBJECT> \
115  void msgpack_object(MSGPACK_OBJECT* msgpack_o, msgpack::zone& msgpack_z) const \
116  { \
117  msgpack::type::make_define_map \
118  MSGPACK_DEFINE_MAP_IMPL(__VA_ARGS__) \
119  .msgpack_object(msgpack_o, msgpack_z); \
120  }
121 
122 #define MSGPACK_BASE_MAP(base) \
123  (BOOST_PP_STRINGIZE(base))(*const_cast<base *>(static_cast<base const*>(this)))
124 
125 #endif // defined(MSGPACK_NO_BOOST)
126 
127 #include "msgpack/versioning.hpp"
128 
129 // for MSGPACK_ADD_ENUM
130 #include "msgpack/adaptor/int.hpp"
131 
132 #define MSGPACK_DEFINE_ARRAY(...) \
133  template <typename Packer> \
134  void msgpack_pack(Packer& msgpack_pk) const \
135  { \
136  msgpack::type::make_define_array(__VA_ARGS__).msgpack_pack(msgpack_pk); \
137  } \
138  void msgpack_unpack(msgpack::object const& msgpack_o) \
139  { \
140  msgpack::type::make_define_array(__VA_ARGS__).msgpack_unpack(msgpack_o); \
141  }\
142  template <typename MSGPACK_OBJECT> \
143  void msgpack_object(MSGPACK_OBJECT* msgpack_o, msgpack::zone& msgpack_z) const \
144  { \
145  msgpack::type::make_define_array(__VA_ARGS__).msgpack_object(msgpack_o, msgpack_z); \
146  }
147 
148 // MSGPACK_ADD_ENUM must be used in the global namespace.
149 #define MSGPACK_ADD_ENUM(enum_name) \
150  namespace msgpack { \
151  \
152  MSGPACK_API_VERSION_NAMESPACE(MSGPACK_DEFAULT_API_NS) { \
153  \
154  namespace adaptor { \
155  template<> \
156  struct convert<enum_name> { \
157  msgpack::object const& operator()(msgpack::object const& msgpack_o, enum_name& msgpack_v) const { \
158  msgpack::underlying_type<enum_name>::type tmp; \
159  msgpack::operator>>(msgpack_o, tmp); \
160  msgpack_v = static_cast<enum_name>(tmp); \
161  return msgpack_o; \
162  } \
163  }; \
164  template<> \
165  struct object<enum_name> { \
166  void operator()(msgpack::object& msgpack_o, const enum_name& msgpack_v) const { \
167  msgpack::underlying_type<enum_name>::type tmp = static_cast<msgpack::underlying_type<enum_name>::type>(msgpack_v); \
168  msgpack::operator<<(msgpack_o, tmp); \
169  } \
170  }; \
171  template<> \
172  struct object_with_zone<enum_name> { \
173  void operator()(msgpack::object::with_zone& msgpack_o, const enum_name& msgpack_v) const { \
174  msgpack::underlying_type<enum_name>::type tmp = static_cast<msgpack::underlying_type<enum_name>::type>(msgpack_v); \
175  msgpack::operator<<(msgpack_o, tmp); \
176  } \
177  }; \
178  template <> \
179  struct pack<enum_name> { \
180  template <typename Stream> \
181  msgpack::packer<Stream>& operator()(msgpack::packer<Stream>& msgpack_o, const enum_name& msgpack_v) const { \
182  return msgpack::operator<<(msgpack_o, static_cast<msgpack::underlying_type<enum_name>::type>(msgpack_v)); \
183  } \
184  }; \
185  } \
186  \
187  } \
188  \
189  }
190 
191 #if defined(MSGPACK_USE_DEFINE_MAP)
192 #define MSGPACK_DEFINE MSGPACK_DEFINE_MAP
193 #define MSGPACK_BASE MSGPACK_BASE_MAP
194 #else // defined(MSGPACK_USE_DEFINE_MAP)
195 #define MSGPACK_DEFINE MSGPACK_DEFINE_ARRAY
196 #define MSGPACK_BASE MSGPACK_BASE_ARRAY
197 #endif // defined(MSGPACK_USE_DEFINE_MAP)
198 
199 
203 
204 #endif // MSGPACK_DEFINE_DECL_HPP