Hideaki Tai / msgpack-embedded

Dependents:   hello_message_pack

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers pair.hpp Source File

pair.hpp

00001 //
00002 // MessagePack for C++ static resolution routine
00003 //
00004 // Copyright (C) 2008-2009 FURUHASHI Sadayuki
00005 //
00006 //    Distributed under the Boost Software License, Version 1.0.
00007 //    (See accompanying file LICENSE_1_0.txt or copy at
00008 //    http://www.boost.org/LICENSE_1_0.txt)
00009 //
00010 #ifndef MSGPACK_TYPE_PAIR_HPP
00011 #define MSGPACK_TYPE_PAIR_HPP
00012 
00013 #include "msgpack/versioning.hpp"
00014 #include "msgpack/adaptor/adaptor_base.hpp"
00015 #include "msgpack/meta.hpp"
00016 
00017 #include <utility>
00018 
00019 namespace msgpack {
00020 
00021 /// @cond
00022 MSGPACK_API_VERSION_NAMESPACE(v1) {
00023 /// @endcond
00024 
00025 namespace adaptor {
00026 
00027 #if !defined(MSGPACK_USE_CPP03)
00028 
00029 template <typename T1, typename T2>
00030 struct as<std::pair<T1, T2>,
00031           typename std::enable_if<msgpack::all_of<msgpack::has_as, T1, T2>::value>::type> {
00032     std::pair<T1, T2> operator()(msgpack::object const& o) const {
00033         if (o.type != msgpack::type::ARRAY) { throw msgpack::type_error(); }
00034         if (o.via.array.size != 2) { throw msgpack::type_error(); }
00035         return std::make_pair(o.via.array.ptr[0].as<T1>(), o.via.array.ptr[1].as<T2>());
00036     }
00037 };
00038 
00039 #endif // !defined(MSGPACK_USE_CPP03)
00040 
00041 template <typename T1, typename T2>
00042 struct convert<std::pair<T1, T2> > {
00043     msgpack::object const& operator()(msgpack::object const& o, std::pair<T1, T2>& v) const {
00044         if(o.type != msgpack::type::ARRAY) { throw msgpack::type_error(); }
00045         if(o.via.array.size != 2) { throw msgpack::type_error(); }
00046         o.via.array.ptr[0].convert(v.first);
00047         o.via.array.ptr[1].convert(v.second);
00048         return o;
00049     }
00050 };
00051 
00052 template <typename T1, typename T2>
00053 struct pack<std::pair<T1, T2> > {
00054     template <typename Stream>
00055     msgpack::packer<Stream> & operator()(msgpack::packer<Stream> & o, const std::pair<T1, T2>& v) const {
00056         o.pack_array(2);
00057         o.pack(v.first);
00058         o.pack(v.second);
00059         return o;
00060     }
00061 };
00062 
00063 template <typename T1, typename T2>
00064 struct object_with_zone<std::pair<T1, T2> > {
00065     void operator()(msgpack::object::with_zone& o, const std::pair<T1, T2>& v) const {
00066         o.type = msgpack::type::ARRAY;
00067         msgpack::object* p = static_cast<msgpack::object*>(o.zone.allocate_align(sizeof(msgpack::object)*2));
00068         o.via.array.ptr = p;
00069         o.via.array.size = 2;
00070         p[0] = msgpack::object(v.first, o.zone);
00071         p[1] = msgpack::object(v.second, o.zone);
00072     }
00073 };
00074 
00075 } // namespace adaptor
00076 
00077 /// @cond
00078 }  // MSGPACK_API_VERSION_NAMESPACE(v1)
00079 /// @endcond
00080 
00081 }  // namespace msgpack
00082 
00083 #endif // MSGPACK_TYPE_PAIR_HPP