define_decl.hpp 6.9 KB

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