cpp03_define_array.hpp.erb 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  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_DEFINE_ARRAY_HPP
  11. #define MSGPACK_V1_CPP03_DEFINE_ARRAY_HPP
  12. #include "msgpack/v1/adaptor/detail/cpp03_define_array_decl.hpp"
  13. #include "msgpack/adaptor/msgpack_tuple.hpp"
  14. #include "msgpack/adaptor/adaptor_base.hpp"
  15. #include "msgpack/object_fwd.hpp"
  16. namespace msgpack {
  17. /// @cond
  18. MSGPACK_API_VERSION_NAMESPACE(v1) {
  19. /// @endcond
  20. namespace type {
  21. <% GENERATION_LIMIT = 31 %>
  22. template <>
  23. struct define_array<> {
  24. typedef define_array<> value_type;
  25. typedef tuple<> tuple_type;
  26. template <typename Packer>
  27. void msgpack_pack(Packer& pk) const
  28. {
  29. pk.pack_array(0);
  30. }
  31. void msgpack_unpack(msgpack::object const& o)
  32. {
  33. if(o.type != msgpack::type::ARRAY) { throw msgpack::type_error(); }
  34. }
  35. void msgpack_object(msgpack::object* o, msgpack::zone&) const
  36. {
  37. o->type = msgpack::type::ARRAY;
  38. o->via.array.ptr = MSGPACK_NULLPTR;
  39. o->via.array.size = 0;
  40. }
  41. };
  42. /// @cond
  43. <%0.upto(GENERATION_LIMIT) {|i|%>
  44. template <typename A0<%1.upto(i) {|j|%>, typename A<%=j%><%}%>>
  45. struct define_array<A0<%1.upto(i) {|j|%>, A<%=j%><%}%>> {
  46. typedef define_array<A0<%1.upto(i) {|j|%>, A<%=j%><%}%>> value_type;
  47. typedef tuple<A0<%1.upto(i) {|j|%>, A<%=j%><%}%>> tuple_type;
  48. define_array(A0& _a0<%1.upto(i) {|j|%>, A<%=j%>& _a<%=j%><%}%>) :
  49. a0(_a0)<%1.upto(i) {|j|%>, a<%=j%>(_a<%=j%>)<%}%> {}
  50. template <typename Packer>
  51. void msgpack_pack(Packer& pk) const
  52. {
  53. pk.pack_array(<%=i+1%>);
  54. <%0.upto(i) {|j|%>
  55. pk.pack(a<%=j%>);<%}%>
  56. }
  57. void msgpack_unpack(msgpack::object const& o)
  58. {
  59. if(o.type != msgpack::type::ARRAY) { throw msgpack::type_error(); }
  60. const size_t size = o.via.array.size;
  61. if(size > 0) {
  62. msgpack::object *ptr = o.via.array.ptr;
  63. switch(size) {
  64. default:<%(i).downto(0) {|j|%>
  65. case <%=j+1%>: ptr[<%=j%>].convert(a<%=j%>);
  66. // fallthrough
  67. <%}%>
  68. }
  69. }
  70. }
  71. void msgpack_object(msgpack::object* o, msgpack::zone& z) const
  72. {
  73. o->type = msgpack::type::ARRAY;
  74. o->via.array.ptr = static_cast<msgpack::object*>(z.allocate_align(sizeof(msgpack::object)*<%=i+1%>, MSGPACK_ZONE_ALIGNOF(msgpack::object)));
  75. o->via.array.size = <%=i+1%>;
  76. <%0.upto(i) {|j|%>
  77. o->via.array.ptr[<%=j%>] = msgpack::object(a<%=j%>, z);<%}%>
  78. }
  79. <%0.upto(i) {|j|%>
  80. A<%=j%>& a<%=j%>;<%}%>
  81. };
  82. <%}%>
  83. /// @endcond
  84. inline define_array<> make_define_array()
  85. {
  86. return define_array<>();
  87. }
  88. /// @cond
  89. <%0.upto(GENERATION_LIMIT) {|i|%>
  90. template <typename A0<%1.upto(i) {|j|%>, typename A<%=j%><%}%>>
  91. inline define_array<A0<%1.upto(i) {|j|%>, A<%=j%><%}%>> make_define_array(A0& a0<%1.upto(i) {|j|%>, A<%=j%>& a<%=j%><%}%>)
  92. {
  93. return define_array<A0<%1.upto(i) {|j|%>, A<%=j%><%}%>>(a0<%1.upto(i) {|j|%>, a<%=j%><%}%>);
  94. }
  95. <%}%>
  96. /// @endcond
  97. } // namespace type
  98. /// @cond
  99. } // MSGPACK_API_VERSION_NAMESPACE(v1)
  100. /// @endcond
  101. } // namespace msgpack
  102. #endif // MSGPACK_V1_CPP03_DEFINE_ARRAY_HPP