123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120 |
- #ifndef MSGPACK_V1_CPP03_DEFINE_MAP_HPP
- #define MSGPACK_V1_CPP03_DEFINE_MAP_HPP
- #include "msgpack/v1/adaptor/detail/cpp03_define_map_decl.hpp"
- #include "msgpack/adaptor/msgpack_tuple.hpp"
- #include "msgpack/adaptor/adaptor_base.hpp"
- #include "msgpack/object_fwd.hpp"
- #include <map>
- namespace msgpack {
- MSGPACK_API_VERSION_NAMESPACE(v1) {
- namespace type {
- <% GENERATION_LIMIT = 31 %>
- template <>
- struct define_map<> {
- template <typename Packer>
- void msgpack_pack(Packer& pk) const
- {
- pk.pack_map(0);
- }
- void msgpack_unpack(msgpack::object const& o) const
- {
- if(o.type != msgpack::type::MAP) { throw msgpack::type_error(); }
- }
- void msgpack_object(msgpack::object* o, msgpack::zone&) const
- {
- o->type = msgpack::type::MAP;
- o->via.map.ptr = MSGPACK_NULLPTR;
- o->via.map.size = 0;
- }
- };
- <%1.step(GENERATION_LIMIT+1,2) {|i|%>
- template <typename A0<%1.upto(i) {|j|%>, typename A<%=j%><%}%>>
- struct define_map<A0<%1.upto(i) {|j|%>, A<%=j%><%}%>> {
- define_map(A0& _a0<%1.upto(i) {|j|%>, A<%=j%>& _a<%=j%><%}%>) :
- a0(_a0)<%1.upto(i) {|j|%>, a<%=j%>(_a<%=j%>)<%}%> {}
- template <typename Packer>
- void msgpack_pack(Packer& pk) const
- {
- pk.pack_map(<%=(i+1)/2%>);
- <%0.upto(i) {|j|%>
- pk.pack(a<%=j%>);<%}%>
- }
- void msgpack_unpack(msgpack::object const& o) const
- {
- if(o.type != msgpack::type::MAP) { throw msgpack::type_error(); }
- std::map<std::string, msgpack::object const*> kvmap;
- for (uint32_t i = 0; i < o.via.map.size; ++i) {
- if (o.via.map.ptr[i].key.type != msgpack::type::STR) { throw msgpack::type_error(); }
- kvmap.insert(
- std::map<std::string, msgpack::object const*>::value_type(
- std::string(
- o.via.map.ptr[i].key.via.str.ptr,
- o.via.map.ptr[i].key.via.str.size),
- &o.via.map.ptr[i].val
- )
- );
- }
- <%0.step(i,2) {|j|%>
- {
- std::map<std::string, msgpack::object const*>::const_iterator it = kvmap.find(a<%=j%>);
- if (it != kvmap.end()) {
- it->second->convert(a<%=j+1%>);
- }
- }
- <%}%>
- }
- void msgpack_object(msgpack::object* o, msgpack::zone& z) const
- {
- o->type = msgpack::type::MAP;
- 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)));
- o->via.map.size = <%=(i+1)/2%>;
- <%0.step(i,2) {|j|%>
- o->via.map.ptr[<%=j/2%>].key = msgpack::object(a<%=j%>, z);
- o->via.map.ptr[<%=j/2%>].val = msgpack::object(a<%=j+1%>, z);
- <%}%>
- }
- <%0.upto(i) {|j|%>
- A<%=j%>& a<%=j%>;<%}%>
- };
- <%}%>
- inline define_map<> make_define_map()
- {
- return define_map<>();
- }
- <%0.upto(GENERATION_LIMIT) {|i|%>
- template <typename A0<%1.upto(i) {|j|%>, typename A<%=j%><%}%>>
- inline define_map<A0<%1.upto(i) {|j|%>, A<%=j%><%}%>> make_define_map(A0& a0<%1.upto(i) {|j|%>, A<%=j%>& a<%=j%><%}%>)
- {
- return define_map<A0<%1.upto(i) {|j|%>, A<%=j%><%}%>>(a0<%1.upto(i) {|j|%>, a<%=j%><%}%>);
- }
- <%}%>
- }
- }
- }
- #endif
|