2009-02-15 09:09:58 +00:00
|
|
|
//
|
|
|
|
// MessagePack for C++ static resolution routine
|
|
|
|
//
|
|
|
|
// Copyright (C) 2008 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_MAP_HPP__
|
|
|
|
#define MSGPACK_TYPE_MAP_HPP__
|
|
|
|
|
|
|
|
#include "msgpack/object.hpp"
|
|
|
|
#include <map>
|
|
|
|
#include <vector>
|
2009-02-15 09:09:59 +00:00
|
|
|
#include <algorithm>
|
2009-02-15 09:09:58 +00:00
|
|
|
|
|
|
|
namespace msgpack {
|
|
|
|
|
|
|
|
|
2009-02-15 09:09:58 +00:00
|
|
|
namespace type {
|
|
|
|
|
2009-02-15 09:09:58 +00:00
|
|
|
template <typename K, typename V>
|
|
|
|
class assoc_vector : public std::vector< std::pair<K, V> > {};
|
|
|
|
|
|
|
|
namespace detail {
|
|
|
|
template <typename K, typename V>
|
|
|
|
struct pair_first_less {
|
|
|
|
bool operator() (const std::pair<K, V>& x, const std::pair<K, V>& y) const
|
|
|
|
{ return x.first < y.first; }
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2009-02-15 09:09:58 +00:00
|
|
|
} //namespace type
|
|
|
|
|
|
|
|
|
2009-02-15 09:09:58 +00:00
|
|
|
template <typename K, typename V>
|
2009-02-15 09:09:58 +00:00
|
|
|
inline type::assoc_vector<K,V>& operator>> (object o, type::assoc_vector<K,V>& v)
|
2009-02-15 09:09:58 +00:00
|
|
|
{
|
2009-02-15 09:09:58 +00:00
|
|
|
if(o.type != type::MAP) { throw type_error(); }
|
2009-02-15 09:09:58 +00:00
|
|
|
v.resize(o.via.container.size);
|
2009-02-15 09:10:02 +00:00
|
|
|
object* p = o.via.container.ptr;
|
|
|
|
object* const pend = o.via.container.ptr + o.via.container.size;
|
2009-02-15 09:09:58 +00:00
|
|
|
std::pair<K, V>* it(&v.front());
|
|
|
|
for(; p < pend; ++it) {
|
2009-02-15 09:10:02 +00:00
|
|
|
p->convert(&it->first); ++p;
|
|
|
|
p->convert(&it->second); ++p;
|
2009-02-15 09:09:58 +00:00
|
|
|
}
|
2009-02-15 09:09:58 +00:00
|
|
|
std::sort(v.begin(), v.end(), type::detail::pair_first_less<K,V>());
|
2009-02-15 09:09:58 +00:00
|
|
|
return v;
|
|
|
|
}
|
|
|
|
|
|
|
|
template <typename Stream, typename K, typename V>
|
2009-02-15 09:09:58 +00:00
|
|
|
inline packer<Stream>& operator<< (packer<Stream>& o, const type::assoc_vector<K,V>& v)
|
2009-02-15 09:09:58 +00:00
|
|
|
{
|
|
|
|
o.pack_map(v.size());
|
2009-02-15 09:09:58 +00:00
|
|
|
for(typename type::assoc_vector<K,V>::const_iterator it(v.begin()), it_end(v.end());
|
2009-02-15 09:09:58 +00:00
|
|
|
it != it_end; ++it) {
|
2009-02-15 09:10:02 +00:00
|
|
|
o.pack(it->first);
|
|
|
|
o.pack(it->second);
|
2009-02-15 09:09:58 +00:00
|
|
|
}
|
2009-02-15 09:09:58 +00:00
|
|
|
return o;
|
2009-02-15 09:09:58 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
template <typename K, typename V>
|
2009-02-15 09:09:58 +00:00
|
|
|
inline std::map<K, V> operator>> (object o, std::map<K, V>& v)
|
2009-02-15 09:09:58 +00:00
|
|
|
{
|
2009-02-15 09:09:58 +00:00
|
|
|
if(o.type != type::MAP) { throw type_error(); }
|
2009-02-15 09:09:58 +00:00
|
|
|
object* p(o.via.container.ptr);
|
|
|
|
object* const pend(o.via.container.ptr + o.via.container.size*2);
|
|
|
|
while(p < pend) {
|
|
|
|
K key;
|
2009-02-15 09:10:02 +00:00
|
|
|
p->convert(&key); ++p;
|
2009-02-15 09:09:58 +00:00
|
|
|
typename std::map<K,V>::iterator it(v.find(key));
|
|
|
|
if(it != v.end()) {
|
|
|
|
V val;
|
2009-02-15 09:10:02 +00:00
|
|
|
p->convert(&val); ++p;
|
2009-02-15 09:09:58 +00:00
|
|
|
it->insert( std::pair<K,V>(key, val) );
|
|
|
|
} else {
|
2009-02-15 09:10:02 +00:00
|
|
|
p->convert(&it->second); ++p;
|
2009-02-15 09:09:58 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return v;
|
|
|
|
}
|
|
|
|
|
|
|
|
template <typename Stream, typename K, typename V>
|
2009-02-15 09:09:58 +00:00
|
|
|
inline packer<Stream>& operator<< (packer<Stream>& o, const std::map<K,V>& v)
|
2009-02-15 09:09:58 +00:00
|
|
|
{
|
|
|
|
o.pack_map(v.size());
|
|
|
|
for(typename std::map<K,V>::const_iterator it(v.begin()), it_end(v.end());
|
|
|
|
it != it_end; ++it) {
|
2009-02-15 09:10:02 +00:00
|
|
|
o.pack(it->first);
|
|
|
|
o.pack(it->second);
|
2009-02-15 09:09:58 +00:00
|
|
|
}
|
2009-02-15 09:09:58 +00:00
|
|
|
return o;
|
2009-02-15 09:09:58 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
template <typename K, typename V>
|
2009-02-15 09:09:58 +00:00
|
|
|
inline std::multimap<K, V> operator>> (object o, std::multimap<K, V>& v)
|
2009-02-15 09:09:58 +00:00
|
|
|
{
|
2009-02-15 09:09:58 +00:00
|
|
|
if(o.type != type::MAP) { throw type_error(); }
|
2009-02-15 09:10:02 +00:00
|
|
|
object* p = o.via.container.ptr;
|
|
|
|
object* const pend = o.via.container.ptr + o.via.container.size*2;
|
2009-02-15 09:09:58 +00:00
|
|
|
while(p < pend) {
|
|
|
|
std::pair<K, V> value;
|
2009-02-15 09:10:02 +00:00
|
|
|
p->convert(&value.first); ++p;
|
|
|
|
p->convert(&value.second); ++p;
|
2009-02-15 09:09:58 +00:00
|
|
|
v.insert(value);
|
|
|
|
}
|
|
|
|
return v;
|
|
|
|
}
|
|
|
|
|
|
|
|
template <typename Stream, typename K, typename V>
|
2009-02-15 09:09:58 +00:00
|
|
|
inline packer<Stream>& operator<< (packer<Stream>& o, const std::multimap<K,V>& v)
|
2009-02-15 09:09:58 +00:00
|
|
|
{
|
|
|
|
o.pack_multimap(v.size());
|
|
|
|
for(typename std::multimap<K,V>::const_iterator it(v.begin()), it_end(v.end());
|
|
|
|
it != it_end; ++it) {
|
2009-02-15 09:10:02 +00:00
|
|
|
o.pack(it->first);
|
|
|
|
o.pack(it->second);
|
2009-02-15 09:09:58 +00:00
|
|
|
}
|
2009-02-15 09:09:58 +00:00
|
|
|
return o;
|
2009-02-15 09:09:58 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
} // namespace msgpack
|
|
|
|
|
|
|
|
#endif /* msgpack/type/map.hpp */
|
|
|
|
|