cpp03_msgpack_tuple.hpp.erb 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227
  1. //
  2. // MessagePack for C++ static resolution routine
  3. //
  4. // Copyright (C) 2008-2016 FURUHASHI Sadayuki and 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_V1_CPP03_MSGPACK_TUPLE_HPP
  11. #define MSGPACK_V1_CPP03_MSGPACK_TUPLE_HPP
  12. #include "msgpack/v1/adaptor/msgpack_tuple_decl.hpp"
  13. namespace msgpack {
  14. /// @cond
  15. MSGPACK_API_VERSION_NAMESPACE(v1) {
  16. /// @endcond
  17. namespace type {
  18. // FIXME operator==
  19. // FIXME operator!=
  20. <% GENERATION_LIMIT = 31 %>
  21. template <typename T>
  22. struct tuple_type {
  23. typedef T type;
  24. typedef T value_type;
  25. typedef T& reference;
  26. typedef const T& const_reference;
  27. typedef const T& transparent_reference;
  28. };
  29. template <typename T>
  30. struct tuple_type<T&> {
  31. typedef T type;
  32. typedef T& value_type;
  33. typedef T& reference;
  34. typedef const T& const_reference;
  35. typedef T& transparent_reference;
  36. };
  37. template <typename T>
  38. struct tuple_type<const T&> {
  39. typedef T type;
  40. typedef T& value_type;
  41. typedef T& reference;
  42. typedef const T& const_reference;
  43. typedef const T& transparent_reference;
  44. };
  45. /// @cond
  46. <%0.upto(GENERATION_LIMIT) {|i|%>
  47. <%0.upto(i) {|j|%>
  48. template <typename A0<%1.upto(i) {|k|%>, typename A<%=k%><%}%>>
  49. struct tuple_element<tuple<A0<%1.upto(i) {|k|%>, A<%=k%><%}%>>, <%=j%>> : tuple_type<A<%=j%>> {
  50. tuple_element(tuple<A0<%1.upto(i) {|k|%>, A<%=k%> <%}%>>& x) : m_x(x.a<%=j%>) {}
  51. typename tuple_type<A<%=j%>>::reference get() { return m_x; }
  52. typename tuple_type<A<%=j%>>::const_reference get() const { return m_x; }
  53. private:
  54. typename tuple_type<A<%=j%>>::reference m_x;
  55. };
  56. <%}%>
  57. <%}%>
  58. <%0.upto(GENERATION_LIMIT) {|i|%>
  59. <%0.upto(i) {|j|%>
  60. template <typename A0<%1.upto(i) {|k|%>, typename A<%=k%><%}%>>
  61. struct const_tuple_element<tuple<A0<%1.upto(i) {|k|%>, A<%=k%><%}%>>, <%=j%>> : tuple_type<A<%=j%>> {
  62. const_tuple_element(const tuple<A0<%1.upto(i) {|k|%>, A<%=k%><%}%>>& x) : m_x(x.a<%=j%>) {}
  63. typename tuple_type<A<%=j%>>::const_reference get() const { return m_x; }
  64. private:
  65. typename tuple_type<A<%=j%>>::const_reference m_x;
  66. };
  67. <%}%>
  68. <%}%>
  69. /// @endcond
  70. template <>
  71. struct tuple<> {
  72. tuple() {}
  73. tuple(msgpack::object const& o) { o.convert(*this); }
  74. typedef tuple<> value_type;
  75. std::size_t size() const { return 0; }
  76. };
  77. /// @cond
  78. <%0.upto(GENERATION_LIMIT) {|i|%>
  79. template <typename A0<%1.upto(i) {|j|%>, typename A<%=j%><%}%>>
  80. struct tuple<A0<%1.upto(i) {|j|%>, A<%=j%><%}%>> {
  81. typedef tuple<A0<%1.upto(i) {|j|%>, A<%=j%><%}%>> value_type;
  82. std::size_t size() const { return <%=i+1%>; }
  83. tuple() {}
  84. tuple(typename tuple_type<A0>::transparent_reference _a0<%1.upto(i) {|j|%>, typename tuple_type<A<%=j%>>::transparent_reference _a<%=j%><%}%>) :
  85. a0(_a0)<%1.upto(i) {|j|%>, a<%=j%>(_a<%=j%>)<%}%> {}
  86. tuple(msgpack::object const& o) { o.convert(*this); }
  87. template <int N> typename tuple_element<value_type, N>::reference get()
  88. { return tuple_element<value_type, N>(*this).get(); }
  89. template <int N> typename const_tuple_element<value_type, N>::const_reference get() const
  90. { return const_tuple_element<value_type, N>(*this).get(); }
  91. <%0.upto(i) {|j|%>
  92. A<%=j%> a<%=j%>;<%}%>
  93. };
  94. template <int N, typename A0<%1.upto(i) {|j|%>, typename A<%=j%><%}%>>
  95. inline typename type::tuple_element<type::tuple<A0<%1.upto(i) {|j|%>, A<%=j%><%}%>>, N>::reference get(type::tuple<A0<%1.upto(i) {|j|%>, A<%=j%><%}%>>& t)
  96. { return t.template get<N>(); }
  97. template <int N, typename A0<%1.upto(i) {|j|%>, typename A<%=j%><%}%>>
  98. inline typename type::const_tuple_element<type::tuple<A0<%1.upto(i) {|j|%>, A<%=j%><%}%>>, N>::const_reference get(type::tuple<A0<%1.upto(i) {|j|%>, A<%=j%><%}%>> const& t)
  99. { return t.template get<N>(); }
  100. <%}%>
  101. /// @endcond
  102. inline tuple<> make_tuple()
  103. {
  104. return tuple<>();
  105. }
  106. /// @cond
  107. <%0.upto(GENERATION_LIMIT) {|i|%>
  108. template <typename A0<%1.upto(i) {|j|%>, typename A<%=j%><%}%>>
  109. inline tuple<A0<%1.upto(i) {|j|%>, A<%=j%><%}%>> make_tuple(typename tuple_type<A0>::transparent_reference a0<%1.upto(i) {|j|%>, typename tuple_type<A<%=j%>>::transparent_reference a<%=j%><%}%>)
  110. {
  111. return tuple<A0<%1.upto(i) {|j|%>, A<%=j%><%}%>>(a0<%1.upto(i) {|j|%>, a<%=j%><%}%>);
  112. }
  113. <%}%>
  114. /// @endcond
  115. } // namespace type
  116. namespace adaptor {
  117. template <>
  118. struct convert<type::tuple<> > {
  119. msgpack::object const& operator()(
  120. msgpack::object const& o,
  121. type::tuple<>&) const {
  122. if(o.type != msgpack::type::ARRAY) { throw msgpack::type_error(); }
  123. return o;
  124. }
  125. };
  126. /// @cond
  127. <%0.upto(GENERATION_LIMIT) {|i|%>
  128. template <typename A0<%1.upto(i) {|j|%>, typename A<%=j%><%}%>>
  129. struct convert<type::tuple<A0<%1.upto(i) {|j|%>, A<%=j%><%}%>> > {
  130. msgpack::object const& operator()(
  131. msgpack::object const& o,
  132. type::tuple<A0<%1.upto(i) {|j|%>, A<%=j%><%}%>>& v) const {
  133. if(o.type != msgpack::type::ARRAY) { throw msgpack::type_error(); }
  134. <%0.upto(i) {|j|%>
  135. // In order to avoid clang++'s invalid warning, msgpack::object:: has been added.
  136. if(o.via.array.size > <%=j%>)
  137. o.via.array.ptr[<%=j%>].msgpack::object::convert<typename type::tuple_type<A<%=j%>>::type>(v.template get<<%=j%>>());<%}%>
  138. return o;
  139. }
  140. };
  141. <%}%>
  142. /// @endcond
  143. template <>
  144. struct pack<type::tuple<> > {
  145. template <typename Stream>
  146. msgpack::packer<Stream>& operator()(
  147. msgpack::packer<Stream>& o,
  148. const type::tuple<>&) const {
  149. o.pack_array(0);
  150. return o;
  151. }
  152. };
  153. /// @cond
  154. <%0.upto(GENERATION_LIMIT) {|i|%>
  155. template <typename A0<%1.upto(i) {|j|%>, typename A<%=j%><%}%>>
  156. struct pack<type::tuple<A0<%1.upto(i) {|j|%>, A<%=j%><%}%>> > {
  157. template <typename Stream>
  158. msgpack::packer<Stream>& operator()(
  159. msgpack::packer<Stream>& o,
  160. const type::tuple<A0<%1.upto(i) {|j|%>, A<%=j%><%}%>>& v) const {
  161. o.pack_array(<%=i+1%>);
  162. <%0.upto(i) {|j|%>
  163. o.pack(v.template get<<%=j%>>());<%}%>
  164. return o;
  165. }
  166. };
  167. <%}%>
  168. /// @endcond
  169. template <>
  170. struct object_with_zone<type::tuple<> > {
  171. void operator()(
  172. msgpack::object::with_zone& o,
  173. const type::tuple<>&) const {
  174. o.type = msgpack::type::ARRAY;
  175. o.via.array.ptr = MSGPACK_NULLPTR;
  176. o.via.array.size = 0;
  177. }
  178. };
  179. /// @cond
  180. <%0.upto(GENERATION_LIMIT) {|i|%>
  181. template <typename A0<%1.upto(i) {|j|%>, typename A<%=j%><%}%>>
  182. struct object_with_zone<type::tuple<A0<%1.upto(i) {|j|%>, A<%=j%><%}%>> > {
  183. void operator()(
  184. msgpack::object::with_zone& o,
  185. const type::tuple<A0<%1.upto(i) {|j|%>, A<%=j%><%}%>>& v) const {
  186. o.type = msgpack::type::ARRAY;
  187. o.via.array.ptr = static_cast<msgpack::object*>(o.zone.allocate_align(sizeof(msgpack::object)*<%=i+1%>, MSGPACK_ZONE_ALIGNOF(msgpack::object)));
  188. o.via.array.size = <%=i+1%>;
  189. <%0.upto(i) {|j|%>
  190. o.via.array.ptr[<%=j%>] = msgpack::object(v.template get<<%=j%>>(), o.zone);<%}%>
  191. }
  192. };
  193. <%}%>
  194. /// @endcond
  195. } // namespace adaptor
  196. /// @cond
  197. } // MSGPACK_API_VERSION_NAMESPACE(v1)
  198. /// @endcond
  199. } // namespace msgpack
  200. #endif // MSGPACK_V1_CPP03_MSGPACK_TUPLE_HPP