mirror of
https://github.com/msgpack/msgpack-c.git
synced 2025-03-18 20:38:00 +01:00
c++: add std::pair and std::set serializer
This commit is contained in:
parent
0491768fb2
commit
394331cd4e
@ -18,7 +18,9 @@ nobase_include_HEADERS = \
|
||||
msgpack/type/integer.hpp \
|
||||
msgpack/type/map.hpp \
|
||||
msgpack/type/nil.hpp \
|
||||
msgpack/type/pair.hpp \
|
||||
msgpack/type/raw.hpp \
|
||||
msgpack/type/set.hpp \
|
||||
msgpack/type/tuple.hpp \
|
||||
msgpack/type/define.hpp
|
||||
|
||||
|
@ -4,7 +4,9 @@
|
||||
#include "msgpack/type/integer.hpp"
|
||||
#include "msgpack/type/map.hpp"
|
||||
#include "msgpack/type/nil.hpp"
|
||||
#include "msgpack/type/pair.hpp"
|
||||
#include "msgpack/type/raw.hpp"
|
||||
#include "msgpack/type/set.hpp"
|
||||
#include "msgpack/type/tuple.hpp"
|
||||
#include "msgpack/type/define.hpp"
|
||||
|
||||
|
51
cpp/type/pair.hpp
Normal file
51
cpp/type/pair.hpp
Normal file
@ -0,0 +1,51 @@
|
||||
//
|
||||
// MessagePack for C++ static resolution routine
|
||||
//
|
||||
// Copyright (C) 2008-2009 FURUHASHI Sadayuki
|
||||
//
|
||||
// Licensed under the Apache License, Version 2.0 (the "License");
|
||||
// you may not use this file except in compliance with the License.
|
||||
// You may obtain a copy of the License at
|
||||
//
|
||||
// http://www.apache.org/licenses/LICENSE-2.0
|
||||
//
|
||||
// Unless required by applicable law or agreed to in writing, software
|
||||
// distributed under the License is distributed on an "AS IS" BASIS,
|
||||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
// See the License for the specific language governing permissions and
|
||||
// limitations under the License.
|
||||
//
|
||||
#ifndef MSGPACK_TYPE_PAIR_HPP__
|
||||
#define MSGPACK_TYPE_PAIR_HPP__
|
||||
|
||||
#include "msgpack/object.hpp"
|
||||
#include <utility>
|
||||
|
||||
namespace msgpack {
|
||||
|
||||
|
||||
template <typename T1, typename T2>
|
||||
inline std::pair<T1, T2>& operator>> (object o, std::pair<T1, T2>& v)
|
||||
{
|
||||
if(o.type != type::ARRAY) { throw type_error(); }
|
||||
if(o.via.array.size != 2) { throw type_error(); }
|
||||
o.via.array.ptr[0].convert(&v.first);
|
||||
o.via.array.ptr[1].convert(&v.second);
|
||||
return v;
|
||||
}
|
||||
|
||||
|
||||
template <typename Stream, typename T1, typename T2>
|
||||
inline packer<Stream>& operator<< (packer<Stream>& o, const std::pair<T1, T2>& v)
|
||||
{
|
||||
o.pack_array(2);
|
||||
o.pack(v.first);
|
||||
o.pack(v.second);
|
||||
return o;
|
||||
}
|
||||
|
||||
|
||||
} // namespace msgpack
|
||||
|
||||
#endif /* msgpack/type/pair.hpp */
|
||||
|
56
cpp/type/set.hpp
Normal file
56
cpp/type/set.hpp
Normal file
@ -0,0 +1,56 @@
|
||||
//
|
||||
// MessagePack for C++ static resolution routine
|
||||
//
|
||||
// Copyright (C) 2008-2009 FURUHASHI Sadayuki
|
||||
//
|
||||
// Licensed under the Apache License, Version 2.0 (the "License");
|
||||
// you may not use this file except in compliance with the License.
|
||||
// You may obtain a copy of the License at
|
||||
//
|
||||
// http://www.apache.org/licenses/LICENSE-2.0
|
||||
//
|
||||
// Unless required by applicable law or agreed to in writing, software
|
||||
// distributed under the License is distributed on an "AS IS" BASIS,
|
||||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
// See the License for the specific language governing permissions and
|
||||
// limitations under the License.
|
||||
//
|
||||
#ifndef MSGPACK_TYPE_SET_HPP__
|
||||
#define MSGPACK_TYPE_SET_HPP__
|
||||
|
||||
#include "msgpack/object.hpp"
|
||||
#include <set>
|
||||
|
||||
namespace msgpack {
|
||||
|
||||
|
||||
template <typename T>
|
||||
inline std::set<T>& operator>> (object o, std::set<T>& v)
|
||||
{
|
||||
if(o.type != type::ARRAY) { throw type_error(); }
|
||||
object* p = o.via.array.ptr + o.via.array.size;
|
||||
object* const pbegin = o.via.array.ptr;
|
||||
while(p > pbegin) {
|
||||
--p;
|
||||
v.insert(p->as<T>());
|
||||
}
|
||||
return v;
|
||||
}
|
||||
|
||||
|
||||
template <typename Stream, typename T>
|
||||
inline packer<Stream>& operator<< (packer<Stream>& o, const std::set<T>& v)
|
||||
{
|
||||
o.pack_array(v.size());
|
||||
for(typename std::set<T>::const_iterator it(v.begin()), it_end(v.end());
|
||||
it != it_end; ++it) {
|
||||
o.pack(*it);
|
||||
}
|
||||
return o;
|
||||
}
|
||||
|
||||
|
||||
} // namespace msgpack
|
||||
|
||||
#endif /* msgpack/type/set.hpp */
|
||||
|
Loading…
x
Reference in New Issue
Block a user