1 //
2 // MessagePack for C++ static resolution routine
3 //
4 // Copyright (C) 2008-2009 FURUHASHI Sadayuki
5 //
6 //    Licensed under the Apache License, Version 2.0 (the "License");
7 //    you may not use this file except in compliance with the License.
8 //    You may obtain a copy of the License at
9 //
10 //        http://www.apache.org/licenses/LICENSE-2.0
11 //
12 //    Unless required by applicable law or agreed to in writing, software
13 //    distributed under the License is distributed on an "AS IS" BASIS,
14 //    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15 //    See the License for the specific language governing permissions and
16 //    limitations under the License.
17 //
18 #ifndef MSGPACK_TYPE_PAIR_HPP
19 #define MSGPACK_TYPE_PAIR_HPP
20 
21 #include "rpc/msgpack/versioning.hpp"
22 #include "rpc/msgpack/adaptor/adaptor_base.hpp"
23 #include "rpc/msgpack/meta.hpp"
24 
25 #include <utility>
26 
27 namespace clmdep_msgpack {
28 
29 /// @cond
MSGPACK_API_VERSION_NAMESPACE(v1)30 MSGPACK_API_VERSION_NAMESPACE(v1) {
31 /// @endcond
32 
33 namespace adaptor {
34 
35 #if !defined(MSGPACK_USE_CPP03)
36 
37 template <typename T1, typename T2>
38 struct as<std::pair<T1, T2>,
39           typename std::enable_if<clmdep_msgpack::all_of<clmdep_msgpack::has_as, T1, T2>::value>::type> {
40     std::pair<T1, T2> operator()(clmdep_msgpack::object const& o) const {
41         if (o.type != clmdep_msgpack::type::ARRAY) { throw clmdep_msgpack::type_error(); }
42         if (o.via.array.size != 2) { throw clmdep_msgpack::type_error(); }
43         return std::make_pair(o.via.array.ptr[0].as<T1>(), o.via.array.ptr[1].as<T2>());
44     }
45 };
46 
47 #endif // !defined(MSGPACK_USE_CPP03)
48 
49 template <typename T1, typename T2>
50 struct convert<std::pair<T1, T2> > {
51     clmdep_msgpack::object const& operator()(clmdep_msgpack::object const& o, std::pair<T1, T2>& v) const {
52         if(o.type != clmdep_msgpack::type::ARRAY) { throw clmdep_msgpack::type_error(); }
53         if(o.via.array.size != 2) { throw clmdep_msgpack::type_error(); }
54         o.via.array.ptr[0].convert(v.first);
55         o.via.array.ptr[1].convert(v.second);
56         return o;
57     }
58 };
59 
60 template <typename T1, typename T2>
61 struct pack<std::pair<T1, T2> > {
62     template <typename Stream>
63     clmdep_msgpack::packer<Stream>& operator()(clmdep_msgpack::packer<Stream>& o, const std::pair<T1, T2>& v) const {
64         o.pack_array(2);
65         o.pack(v.first);
66         o.pack(v.second);
67         return o;
68     }
69 };
70 
71 template <typename T1, typename T2>
72 struct object_with_zone<std::pair<T1, T2> > {
73     void operator()(clmdep_msgpack::object::with_zone& o, const std::pair<T1, T2>& v) const {
74         o.type = clmdep_msgpack::type::ARRAY;
75         clmdep_msgpack::object* p = static_cast<clmdep_msgpack::object*>(o.zone.allocate_align(sizeof(clmdep_msgpack::object)*2));
76         o.via.array.ptr = p;
77         o.via.array.size = 2;
78         p[0] = clmdep_msgpack::object(v.first, o.zone);
79         p[1] = clmdep_msgpack::object(v.second, o.zone);
80     }
81 };
82 
83 } // namespace adaptor
84 
85 /// @cond
86 }  // MSGPACK_API_VERSION_NAMESPACE(v1)
87 /// @endcond
88 
89 }  // namespace clmdep_msgpack
90 
91 #endif // MSGPACK_TYPE_PAIR_HPP
92