define_decl.hpp 5.0 KB

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