cpp03_define_map.hpp.erb 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  1. //
  2. // MessagePack for C++ static resolution routine
  3. //
  4. // Copyright (C) 2015-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_V1_CPP03_DEFINE_MAP_HPP
  11. #define MSGPACK_V1_CPP03_DEFINE_MAP_HPP
  12. #include "msgpack/v1/adaptor/detail/cpp03_define_map_decl.hpp"
  13. #include "msgpack/adaptor/msgpack_tuple.hpp"
  14. #include "msgpack/adaptor/adaptor_base.hpp"
  15. #include "msgpack/object_fwd.hpp"
  16. #include <map>
  17. namespace msgpack {
  18. /// @cond
  19. MSGPACK_API_VERSION_NAMESPACE(v1) {
  20. /// @endcond
  21. namespace type {
  22. <% GENERATION_LIMIT = 31 %>
  23. template <>
  24. struct define_map<> {
  25. template <typename Packer>
  26. void msgpack_pack(Packer& pk) const
  27. {
  28. pk.pack_map(0);
  29. }
  30. void msgpack_unpack(msgpack::object const& o) const
  31. {
  32. if(o.type != msgpack::type::MAP) { throw msgpack::type_error(); }
  33. }
  34. void msgpack_object(msgpack::object* o, msgpack::zone&) const
  35. {
  36. o->type = msgpack::type::MAP;
  37. o->via.map.ptr = MSGPACK_NULLPTR;
  38. o->via.map.size = 0;
  39. }
  40. };
  41. /// @cond
  42. <%1.step(GENERATION_LIMIT+1,2) {|i|%>
  43. template <typename A0<%1.upto(i) {|j|%>, typename A<%=j%><%}%>>
  44. struct define_map<A0<%1.upto(i) {|j|%>, A<%=j%><%}%>> {
  45. define_map(A0& _a0<%1.upto(i) {|j|%>, A<%=j%>& _a<%=j%><%}%>) :
  46. a0(_a0)<%1.upto(i) {|j|%>, a<%=j%>(_a<%=j%>)<%}%> {}
  47. template <typename Packer>
  48. void msgpack_pack(Packer& pk) const
  49. {
  50. pk.pack_map(<%=(i+1)/2%>);
  51. <%0.upto(i) {|j|%>
  52. pk.pack(a<%=j%>);<%}%>
  53. }
  54. void msgpack_unpack(msgpack::object const& o) const
  55. {
  56. if(o.type != msgpack::type::MAP) { throw msgpack::type_error(); }
  57. std::map<std::string, msgpack::object const*> kvmap;
  58. for (uint32_t i = 0; i < o.via.map.size; ++i) {
  59. if (o.via.map.ptr[i].key.type != msgpack::type::STR) { throw msgpack::type_error(); }
  60. kvmap.insert(
  61. std::map<std::string, msgpack::object const*>::value_type(
  62. std::string(
  63. o.via.map.ptr[i].key.via.str.ptr,
  64. o.via.map.ptr[i].key.via.str.size),
  65. &o.via.map.ptr[i].val
  66. )
  67. );
  68. }
  69. <%0.step(i,2) {|j|%>
  70. {
  71. std::map<std::string, msgpack::object const*>::const_iterator it = kvmap.find(a<%=j%>);
  72. if (it != kvmap.end()) {
  73. it->second->convert(a<%=j+1%>);
  74. }
  75. }
  76. <%}%>
  77. }
  78. void msgpack_object(msgpack::object* o, msgpack::zone& z) const
  79. {
  80. o->type = msgpack::type::MAP;
  81. o->via.map.ptr = static_cast<msgpack::object_kv*>(z.allocate_align(sizeof(msgpack::object_kv)*<%=(i+1)/2%>, MSGPACK_ZONE_ALIGNOF(msgpack::object_kv)));
  82. o->via.map.size = <%=(i+1)/2%>;
  83. <%0.step(i,2) {|j|%>
  84. o->via.map.ptr[<%=j/2%>].key = msgpack::object(a<%=j%>, z);
  85. o->via.map.ptr[<%=j/2%>].val = msgpack::object(a<%=j+1%>, z);
  86. <%}%>
  87. }
  88. <%0.upto(i) {|j|%>
  89. A<%=j%>& a<%=j%>;<%}%>
  90. };
  91. <%}%>
  92. /// @endcond
  93. inline define_map<> make_define_map()
  94. {
  95. return define_map<>();
  96. }
  97. /// @cond
  98. <%0.upto(GENERATION_LIMIT) {|i|%>
  99. template <typename A0<%1.upto(i) {|j|%>, typename A<%=j%><%}%>>
  100. inline define_map<A0<%1.upto(i) {|j|%>, A<%=j%><%}%>> make_define_map(A0& a0<%1.upto(i) {|j|%>, A<%=j%>& a<%=j%><%}%>)
  101. {
  102. return define_map<A0<%1.upto(i) {|j|%>, A<%=j%><%}%>>(a0<%1.upto(i) {|j|%>, a<%=j%><%}%>);
  103. }
  104. <%}%>
  105. /// @endcond
  106. } // namespace type
  107. /// @cond
  108. } // MSGPACK_API_VERSION_NAMESPACE(v1)
  109. /// @endcond
  110. } // namespace msgpack
  111. #endif // MSGPACK_V1_CPP03_DEFINE_MAP_HPP