From 394331cd4ece745b9c5b5eb155560aaece8dc2bc Mon Sep 17 00:00:00 2001 From: frsyuki Date: Mon, 10 Aug 2009 17:34:21 +0900 Subject: [PATCH] c++: add std::pair and std::set serializer --- cpp/Makefile.am | 2 ++ cpp/type.hpp | 2 ++ cpp/type/pair.hpp | 51 ++++++++++++++++++++++++++++++++++++++++++ cpp/type/set.hpp | 56 +++++++++++++++++++++++++++++++++++++++++++++++ 4 files changed, 111 insertions(+) create mode 100644 cpp/type/pair.hpp create mode 100644 cpp/type/set.hpp diff --git a/cpp/Makefile.am b/cpp/Makefile.am index b7bf069a..edbeb281 100644 --- a/cpp/Makefile.am +++ b/cpp/Makefile.am @@ -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 diff --git a/cpp/type.hpp b/cpp/type.hpp index 8ffe3bfc..2a6aa624 100644 --- a/cpp/type.hpp +++ b/cpp/type.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" diff --git a/cpp/type/pair.hpp b/cpp/type/pair.hpp new file mode 100644 index 00000000..316b9fe8 --- /dev/null +++ b/cpp/type/pair.hpp @@ -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 + +namespace msgpack { + + +template +inline std::pair& operator>> (object o, std::pair& 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 +inline packer& operator<< (packer& o, const std::pair& v) +{ + o.pack_array(2); + o.pack(v.first); + o.pack(v.second); + return o; +} + + +} // namespace msgpack + +#endif /* msgpack/type/pair.hpp */ + diff --git a/cpp/type/set.hpp b/cpp/type/set.hpp new file mode 100644 index 00000000..3f1920a3 --- /dev/null +++ b/cpp/type/set.hpp @@ -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 + +namespace msgpack { + + +template +inline std::set& operator>> (object o, std::set& 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()); + } + return v; +} + + +template +inline packer& operator<< (packer& o, const std::set& v) +{ + o.pack_array(v.size()); + for(typename std::set::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 */ +