mirror of
https://github.com/msgpack/msgpack-c.git
synced 2025-05-21 05:18:37 +02:00
Added adaptors for the C++11 version of unordered containers.
(Fixed https://github.com/msgpack/msgpack-c/issues/168)
This commit is contained in:
parent
c52a58b322
commit
9162610682
@ -110,6 +110,10 @@ IF (MSGPACK_ENABLE_CXX)
|
||||
include/msgpack/adaptor/cpp11/forward_list_fwd.hpp
|
||||
include/msgpack/adaptor/cpp11/tuple.hpp
|
||||
include/msgpack/adaptor/cpp11/tuple_fwd.hpp
|
||||
include/msgpack/adaptor/cpp11/unordered_map.hpp
|
||||
include/msgpack/adaptor/cpp11/unordered_map_fwd.hpp
|
||||
include/msgpack/adaptor/cpp11/unordered_set.hpp
|
||||
include/msgpack/adaptor/cpp11/unordered_set_fwd.hpp
|
||||
include/msgpack/adaptor/define.hpp
|
||||
include/msgpack/adaptor/deque.hpp
|
||||
include/msgpack/adaptor/deque_fwd.hpp
|
||||
|
137
include/msgpack/adaptor/cpp11/unordered_map.hpp
Normal file
137
include/msgpack/adaptor/cpp11/unordered_map.hpp
Normal file
@ -0,0 +1,137 @@
|
||||
//
|
||||
// MessagePack for C++ static resolution routine
|
||||
//
|
||||
// Copyright (C) 2014 KONDO Takatoshi
|
||||
//
|
||||
// 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_UNORDERED_MAP_HPP
|
||||
#define MSGPACK_TYPE_UNORDERED_MAP_HPP
|
||||
|
||||
#include "msgpack/versioning.hpp"
|
||||
#include "msgpack/object_fwd.hpp"
|
||||
|
||||
#include <unordered_map>
|
||||
|
||||
namespace msgpack {
|
||||
|
||||
MSGPACK_API_VERSION_NAMESPACE(v1) {
|
||||
|
||||
template <typename K, typename V>
|
||||
inline object const& operator>> (object const& o, std::unordered_map<K, V>& v)
|
||||
{
|
||||
if(o.type != type::MAP) { throw type_error(); }
|
||||
object_kv* p(o.via.map.ptr);
|
||||
object_kv* const pend(o.via.map.ptr + o.via.map.size);
|
||||
std::unordered_map<K, V> tmp;
|
||||
for(; p != pend; ++p) {
|
||||
K key;
|
||||
p->key.convert(key);
|
||||
p->val.convert(tmp[key]);
|
||||
}
|
||||
tmp.swap(v);
|
||||
return o;
|
||||
}
|
||||
|
||||
template <typename Stream, typename K, typename V>
|
||||
inline packer<Stream>& operator<< (packer<Stream>& o, const std::unordered_map<K,V>& v)
|
||||
{
|
||||
o.pack_map(v.size());
|
||||
for(typename std::unordered_map<K,V>::const_iterator it(v.begin()), it_end(v.end());
|
||||
it != it_end; ++it) {
|
||||
o.pack(it->first);
|
||||
o.pack(it->second);
|
||||
}
|
||||
return o;
|
||||
}
|
||||
|
||||
template <typename K, typename V>
|
||||
inline void operator<< (object::with_zone& o, const std::unordered_map<K,V>& v)
|
||||
{
|
||||
o.type = type::MAP;
|
||||
if(v.empty()) {
|
||||
o.via.map.ptr = nullptr;
|
||||
o.via.map.size = 0;
|
||||
} else {
|
||||
object_kv* p = static_cast<object_kv*>(o.zone.allocate_align(sizeof(object_kv)*v.size()));
|
||||
object_kv* const pend = p + v.size();
|
||||
o.via.map.ptr = p;
|
||||
o.via.map.size = v.size();
|
||||
typename std::unordered_map<K,V>::const_iterator it(v.begin());
|
||||
do {
|
||||
p->key = object(it->first, o.zone);
|
||||
p->val = object(it->second, o.zone);
|
||||
++p;
|
||||
++it;
|
||||
} while(p < pend);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
template <typename K, typename V>
|
||||
inline object const& operator>> (object const& o, std::unordered_multimap<K, V>& v)
|
||||
{
|
||||
if(o.type != type::MAP) { throw type_error(); }
|
||||
object_kv* p(o.via.map.ptr);
|
||||
object_kv* const pend(o.via.map.ptr + o.via.map.size);
|
||||
std::unordered_multimap<K, V> tmp;
|
||||
for(; p != pend; ++p) {
|
||||
std::pair<K, V> value;
|
||||
p->key.convert(value.first);
|
||||
p->val.convert(value.second);
|
||||
tmp.insert(value);
|
||||
}
|
||||
tmp.swap(v);
|
||||
return o;
|
||||
}
|
||||
|
||||
template <typename Stream, typename K, typename V>
|
||||
inline packer<Stream>& operator<< (packer<Stream>& o, const std::unordered_multimap<K,V>& v)
|
||||
{
|
||||
o.pack_map(v.size());
|
||||
for(typename std::unordered_multimap<K,V>::const_iterator it(v.begin()), it_end(v.end());
|
||||
it != it_end; ++it) {
|
||||
o.pack(it->first);
|
||||
o.pack(it->second);
|
||||
}
|
||||
return o;
|
||||
}
|
||||
|
||||
template <typename K, typename V>
|
||||
inline void operator<< (object::with_zone& o, const std::unordered_multimap<K,V>& v)
|
||||
{
|
||||
o.type = type::MAP;
|
||||
if(v.empty()) {
|
||||
o.via.map.ptr = nullptr;
|
||||
o.via.map.size = 0;
|
||||
} else {
|
||||
object_kv* p = static_cast<object_kv*>(o.zone.allocate_align(sizeof(object_kv)*v.size()));
|
||||
object_kv* const pend = p + v.size();
|
||||
o.via.map.ptr = p;
|
||||
o.via.map.size = v.size();
|
||||
typename std::unordered_multimap<K,V>::const_iterator it(v.begin());
|
||||
do {
|
||||
p->key = object(it->first, o.zone);
|
||||
p->val = object(it->second, o.zone);
|
||||
++p;
|
||||
++it;
|
||||
} while(p < pend);
|
||||
}
|
||||
}
|
||||
|
||||
} // MSGPACK_API_VERSION_NAMESPACE(v1)
|
||||
|
||||
} // namespace msgpack
|
||||
|
||||
|
||||
#endif // MSGPACK_TYPE_UNORDERED_MAP_HPP
|
53
include/msgpack/adaptor/cpp11/unordered_map_fwd.hpp
Normal file
53
include/msgpack/adaptor/cpp11/unordered_map_fwd.hpp
Normal file
@ -0,0 +1,53 @@
|
||||
//
|
||||
// MessagePack for C++ static resolution routine
|
||||
//
|
||||
// Copyright (C) 2014 KONDO Takatoshi
|
||||
//
|
||||
// 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_UNORDERED_MAP_FWD_HPP
|
||||
#define MSGPACK_TYPE_UNORDERED_MAP_FWD_HPP
|
||||
|
||||
#include "msgpack/versioning.hpp"
|
||||
#include "msgpack/object_fwd.hpp"
|
||||
|
||||
#include <unordered_map>
|
||||
|
||||
namespace msgpack {
|
||||
|
||||
MSGPACK_API_VERSION_NAMESPACE(v1) {
|
||||
|
||||
template <typename K, typename V>
|
||||
object const& operator>> (object const& o, std::unordered_map<K, V>& v);
|
||||
|
||||
template <typename Stream, typename K, typename V>
|
||||
packer<Stream>& operator<< (packer<Stream>& o, const std::unordered_map<K,V>& v);
|
||||
|
||||
template <typename K, typename V>
|
||||
void operator<< (object::with_zone& o, const std::unordered_map<K,V>& v);
|
||||
|
||||
template <typename K, typename V>
|
||||
object const& operator>> (object const& o, std::unordered_multimap<K, V>& v);
|
||||
|
||||
template <typename Stream, typename K, typename V>
|
||||
packer<Stream>& operator<< (packer<Stream>& o, const std::unordered_multimap<K,V>& v);
|
||||
|
||||
template <typename K, typename V>
|
||||
void operator<< (object::with_zone& o, const std::unordered_multimap<K,V>& v);
|
||||
|
||||
} // MSGPACK_API_VERSION_NAMESPACE(v1)
|
||||
|
||||
} // namespace msgpack
|
||||
|
||||
|
||||
#endif // MSGPACK_TYPE_UNORDERED_MAP_FWD_HPP
|
129
include/msgpack/adaptor/cpp11/unordered_set.hpp
Normal file
129
include/msgpack/adaptor/cpp11/unordered_set.hpp
Normal file
@ -0,0 +1,129 @@
|
||||
//
|
||||
// MessagePack for C++ static resolution routine
|
||||
//
|
||||
// Copyright (C) 2014 KONDO Takatoshi
|
||||
//
|
||||
// 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_UNORDERED_SET_HPP
|
||||
#define MSGPACK_TYPE_UNORDERED_SET_HPP
|
||||
|
||||
#include "msgpack/versioning.hpp"
|
||||
#include "msgpack/object_fwd.hpp"
|
||||
|
||||
#include <unordered_set>
|
||||
|
||||
namespace msgpack {
|
||||
|
||||
MSGPACK_API_VERSION_NAMESPACE(v1) {
|
||||
|
||||
template <typename T>
|
||||
inline object const& operator>> (object const& o, std::unordered_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;
|
||||
std::unordered_set<T> tmp;
|
||||
while(p > pbegin) {
|
||||
--p;
|
||||
tmp.insert(p->as<T>());
|
||||
}
|
||||
tmp.swap(v);
|
||||
return o;
|
||||
}
|
||||
|
||||
template <typename Stream, typename T>
|
||||
inline packer<Stream>& operator<< (packer<Stream>& o, const std::unordered_set<T>& v)
|
||||
{
|
||||
o.pack_array(v.size());
|
||||
for(typename std::unordered_set<T>::const_iterator it(v.begin()), it_end(v.end());
|
||||
it != it_end; ++it) {
|
||||
o.pack(*it);
|
||||
}
|
||||
return o;
|
||||
}
|
||||
|
||||
template <typename T>
|
||||
inline void operator<< (object::with_zone& o, const std::unordered_set<T>& v)
|
||||
{
|
||||
o.type = type::ARRAY;
|
||||
if(v.empty()) {
|
||||
o.via.array.ptr = nullptr;
|
||||
o.via.array.size = 0;
|
||||
} else {
|
||||
object* p = static_cast<object*>(o.zone.allocate_align(sizeof(object)*v.size()));
|
||||
object* const pend = p + v.size();
|
||||
o.via.array.ptr = p;
|
||||
o.via.array.size = v.size();
|
||||
typename std::unordered_set<T>::const_iterator it(v.begin());
|
||||
do {
|
||||
*p = object(*it, o.zone);
|
||||
++p;
|
||||
++it;
|
||||
} while(p < pend);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
template <typename T>
|
||||
inline object const& operator>> (object const& o, std::unordered_multiset<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;
|
||||
std::unordered_multiset<T> tmp;
|
||||
while(p > pbegin) {
|
||||
--p;
|
||||
tmp.insert(p->as<T>());
|
||||
}
|
||||
tmp.swap(v);
|
||||
return o;
|
||||
}
|
||||
|
||||
template <typename Stream, typename T>
|
||||
inline packer<Stream>& operator<< (packer<Stream>& o, const std::unordered_multiset<T>& v)
|
||||
{
|
||||
o.pack_array(v.size());
|
||||
for(typename std::unordered_multiset<T>::const_iterator it(v.begin()), it_end(v.end());
|
||||
it != it_end; ++it) {
|
||||
o.pack(*it);
|
||||
}
|
||||
return o;
|
||||
}
|
||||
|
||||
template <typename T>
|
||||
inline void operator<< (object::with_zone& o, const std::unordered_multiset<T>& v)
|
||||
{
|
||||
o.type = type::ARRAY;
|
||||
if(v.empty()) {
|
||||
o.via.array.ptr = nullptr;
|
||||
o.via.array.size = 0;
|
||||
} else {
|
||||
object* p = static_cast<object*>(o.zone.allocate_align(sizeof(object)*v.size()));
|
||||
object* const pend = p + v.size();
|
||||
o.via.array.ptr = p;
|
||||
o.via.array.size = v.size();
|
||||
typename std::unordered_multiset<T>::const_iterator it(v.begin());
|
||||
do {
|
||||
*p = object(*it, o.zone);
|
||||
++p;
|
||||
++it;
|
||||
} while(p < pend);
|
||||
}
|
||||
}
|
||||
|
||||
} // MSGPACK_API_VERSION_NAMESPACE(v1)
|
||||
|
||||
} // namespace msgpack
|
||||
|
||||
#endif // MSGPACK_TYPE_UNORDERED_SET_HPP
|
52
include/msgpack/adaptor/cpp11/unordered_set_fwd.hpp
Normal file
52
include/msgpack/adaptor/cpp11/unordered_set_fwd.hpp
Normal file
@ -0,0 +1,52 @@
|
||||
//
|
||||
// MessagePack for C++ static resolution routine
|
||||
//
|
||||
// Copyright (C) 2014 KONDO Takatoshi
|
||||
//
|
||||
// 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_UNORDERED_SET_FWD_HPP
|
||||
#define MSGPACK_TYPE_UNORDERED_SET_FWD_HPP
|
||||
|
||||
#include "msgpack/versioning.hpp"
|
||||
#include "msgpack/object_fwd.hpp"
|
||||
|
||||
#include <unordered_set>
|
||||
|
||||
namespace msgpack {
|
||||
|
||||
MSGPACK_API_VERSION_NAMESPACE(v1) {
|
||||
|
||||
template <typename T>
|
||||
object const& operator>> (object const& o, std::unordered_set<T>& v);
|
||||
|
||||
template <typename Stream, typename T>
|
||||
packer<Stream>& operator<< (packer<Stream>& o, const std::unordered_set<T>& v);
|
||||
|
||||
template <typename T>
|
||||
void operator<< (object::with_zone& o, const std::unordered_set<T>& v);
|
||||
|
||||
template <typename T>
|
||||
object const& operator>> (object const& o, std::unordered_multiset<T>& v);
|
||||
|
||||
template <typename Stream, typename T>
|
||||
packer<Stream>& operator<< (packer<Stream>& o, const std::unordered_multiset<T>& v);
|
||||
|
||||
template <typename T>
|
||||
void operator<< (object::with_zone& o, const std::unordered_multiset<T>& v);
|
||||
|
||||
} // MSGPACK_API_VERSION_NAMESPACE(v1)
|
||||
|
||||
} // namespace msgpack
|
||||
|
||||
#endif // MSGPACK_TYPE_UNORDERED_SET_FWD_HPP
|
@ -39,15 +39,22 @@
|
||||
#include "msgpack/adaptor/string_fwd.hpp"
|
||||
#include "msgpack/adaptor/vector_fwd.hpp"
|
||||
#include "msgpack/adaptor/vector_char_fwd.hpp"
|
||||
|
||||
#if defined(MSGPACK_USE_CPP03)
|
||||
|
||||
#include "msgpack/adaptor/tr1/unordered_map_fwd.hpp"
|
||||
#include "msgpack/adaptor/tr1/unordered_set_fwd.hpp"
|
||||
|
||||
#if !defined(MSGPACK_USE_CPP03)
|
||||
#else // defined(MSGPACK_USE_CPP03)
|
||||
|
||||
#include "adaptor/cpp11/array_fwd.hpp"
|
||||
#include "adaptor/cpp11/array_char_fwd.hpp"
|
||||
#include "adaptor/cpp11/forward_list_fwd.hpp"
|
||||
#include "adaptor/cpp11/tuple_fwd.hpp"
|
||||
#endif // !defined(MSGPACK_USE_CPP03)
|
||||
#include "adaptor/cpp11/unordered_map_fwd.hpp"
|
||||
#include "adaptor/cpp11/unordered_set_fwd.hpp"
|
||||
|
||||
#endif // defined(MSGPACK_USE_CPP03)
|
||||
|
||||
#include <string.h>
|
||||
#include <stdexcept>
|
||||
|
@ -16,14 +16,19 @@
|
||||
#include "adaptor/vector_char.hpp"
|
||||
#include "adaptor/msgpack_tuple.hpp"
|
||||
#include "adaptor/define.hpp"
|
||||
|
||||
#if defined(MSGPACK_USE_CPP03)
|
||||
|
||||
#include "adaptor/tr1/unordered_map.hpp"
|
||||
#include "adaptor/tr1/unordered_set.hpp"
|
||||
|
||||
#if !defined(MSGPACK_USE_CPP03)
|
||||
#else // defined(MSGPACK_USE_CPP03)
|
||||
|
||||
#include "adaptor/cpp11/array.hpp"
|
||||
#include "adaptor/cpp11/array_char.hpp"
|
||||
#include "adaptor/cpp11/forward_list.hpp"
|
||||
#include "adaptor/cpp11/tuple.hpp"
|
||||
#include "adaptor/cpp11/unordered_map.hpp"
|
||||
#include "adaptor/cpp11/unordered_set.hpp"
|
||||
|
||||
#endif // !defined(MSGPACK_USE_CPP03)
|
||||
#endif // defined(MSGPACK_USE_CPP03)
|
||||
|
@ -66,6 +66,10 @@ nobase_include_HEADERS += \
|
||||
../include/msgpack/adaptor/cpp11/forward_list_fwd.hpp \
|
||||
../include/msgpack/adaptor/cpp11/tuple.hpp \
|
||||
../include/msgpack/adaptor/cpp11/tuple_fwd.hpp \
|
||||
../include/msgpack/adaptor/cpp11/unordered_map.hpp \
|
||||
../include/msgpack/adaptor/cpp11/unordered_map_fwd.hpp \
|
||||
../include/msgpack/adaptor/cpp11/unordered_set.hpp \
|
||||
../include/msgpack/adaptor/cpp11/unordered_set_fwd.hpp \
|
||||
../include/msgpack/adaptor/define.hpp \
|
||||
../include/msgpack/adaptor/deque.hpp \
|
||||
../include/msgpack/adaptor/deque_fwd.hpp \
|
||||
|
@ -86,6 +86,71 @@ TEST(MSGPACK_STL, simple_buffer_forward_list)
|
||||
}
|
||||
}
|
||||
|
||||
TEST(MSGPACK_STL, simple_buffer_unordered_map)
|
||||
{
|
||||
for (unsigned int k = 0; k < kLoop; k++) {
|
||||
unordered_map<int, int> val1;
|
||||
for (unsigned int i = 0; i < kElements; i++)
|
||||
val1[rand()] = rand();
|
||||
msgpack::sbuffer sbuf;
|
||||
msgpack::pack(sbuf, val1);
|
||||
msgpack::unpacked ret;
|
||||
msgpack::unpack(ret, sbuf.data(), sbuf.size());
|
||||
unordered_map<int, int> val2 = ret.get().as<unordered_map<int, int> >();
|
||||
EXPECT_EQ(val1, val2);
|
||||
}
|
||||
}
|
||||
|
||||
TEST(MSGPACK_STL, simple_buffer_unordered_multimap)
|
||||
{
|
||||
for (unsigned int k = 0; k < kLoop; k++) {
|
||||
unordered_multimap<int, int> val1;
|
||||
for (unsigned int i = 0; i < kElements; i++) {
|
||||
int i1 = rand();
|
||||
val1.insert(make_pair(i1, rand()));
|
||||
val1.insert(make_pair(i1, rand()));
|
||||
}
|
||||
msgpack::sbuffer sbuf;
|
||||
msgpack::pack(sbuf, val1);
|
||||
msgpack::unpacked ret;
|
||||
msgpack::unpack(ret, sbuf.data(), sbuf.size());
|
||||
unordered_multimap<int, int> val2 = ret.get().as<unordered_multimap<int, int> >();
|
||||
|
||||
EXPECT_EQ(val1, val2);
|
||||
}
|
||||
}
|
||||
|
||||
TEST(MSGPACK_STL, simple_buffer_unordered_set)
|
||||
{
|
||||
for (unsigned int k = 0; k < kLoop; k++) {
|
||||
unordered_set<int> val1;
|
||||
for (unsigned int i = 0; i < kElements; i++)
|
||||
val1.insert(rand());
|
||||
msgpack::sbuffer sbuf;
|
||||
msgpack::pack(sbuf, val1);
|
||||
msgpack::unpacked ret;
|
||||
msgpack::unpack(ret, sbuf.data(), sbuf.size());
|
||||
unordered_set<int> val2 = ret.get().as<unordered_set<int> >();
|
||||
EXPECT_EQ(val1, val2);
|
||||
}
|
||||
}
|
||||
|
||||
TEST(MSGPACK_STL, simple_buffer_unordered_multiset)
|
||||
{
|
||||
for (unsigned int k = 0; k < kLoop; k++) {
|
||||
unordered_multiset<int> val1;
|
||||
for (unsigned int i = 0; i < kElements; i++)
|
||||
val1.insert(rand());
|
||||
msgpack::sbuffer sbuf;
|
||||
msgpack::pack(sbuf, val1);
|
||||
msgpack::unpacked ret;
|
||||
msgpack::unpack(ret, sbuf.data(), sbuf.size());
|
||||
unordered_multiset<int> val2 = ret.get().as<unordered_multiset<int> >();
|
||||
EXPECT_EQ(val1, val2);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
class TestEnumClassMemberClass
|
||||
{
|
||||
|
Loading…
x
Reference in New Issue
Block a user