mirror of
https://github.com/msgpack/msgpack-python.git
synced 2026-02-08 18:59:59 +00:00
lang/c/msgpack: reimplemented C++ binding with template-based static resolution design
git-svn-id: file:///Users/frsyuki/project/msgpack-git/svn/x@67 5a5092ae-2292-43ba-b2d5-dcab9c1a2731
This commit is contained in:
parent
1222466a1c
commit
9923cf4daf
26 changed files with 1338 additions and 1116 deletions
59
cpp/type/array.hpp
Normal file
59
cpp/type/array.hpp
Normal file
|
|
@ -0,0 +1,59 @@
|
|||
//
|
||||
// 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_ARRAY_HPP__
|
||||
#define MSGPACK_TYPE_ARRAY_HPP__
|
||||
|
||||
#include "msgpack/object.hpp"
|
||||
#include <vector>
|
||||
|
||||
namespace msgpack {
|
||||
namespace type {
|
||||
|
||||
|
||||
template <typename T>
|
||||
inline std::vector<T> operator<< (std::vector<T>& v, object o)
|
||||
{
|
||||
if(o.type != ARRAY) { throw type_error(); }
|
||||
v.resize(o.via.container.size);
|
||||
object* p(o.via.container.ptr);
|
||||
object* const pend(o.via.container.ptr + o.via.container.size);
|
||||
T* it(&v.front());
|
||||
for(; p < pend; ++p, ++it) {
|
||||
convert(*it, *p);
|
||||
}
|
||||
return v;
|
||||
}
|
||||
|
||||
|
||||
template <typename Stream, typename T>
|
||||
inline const std::vector<T>& operator>> (const std::vector<T>& v, packer<Stream>& o)
|
||||
{
|
||||
o.pack_array(v.size());
|
||||
for(typename std::vector<T>::const_iterator it(v.begin()), it_end(v.end());
|
||||
it != it_end; ++it) {
|
||||
pack(*it, o);
|
||||
}
|
||||
return v;
|
||||
}
|
||||
|
||||
|
||||
} // namespace type
|
||||
} // namespace msgpack
|
||||
|
||||
#endif /* msgpack/type/array.hpp */
|
||||
|
||||
49
cpp/type/boolean.hpp
Normal file
49
cpp/type/boolean.hpp
Normal file
|
|
@ -0,0 +1,49 @@
|
|||
//
|
||||
// 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_BOOLEAN_HPP__
|
||||
#define MSGPACK_TYPE_BOOLEAN_HPP__
|
||||
|
||||
#include "msgpack/object.hpp"
|
||||
#include <vector>
|
||||
|
||||
namespace msgpack {
|
||||
namespace type {
|
||||
|
||||
|
||||
inline bool& operator<< (bool& v, object o)
|
||||
{
|
||||
if(o.type != BOOLEAN) { throw type_error(); }
|
||||
v = o.via.boolean;
|
||||
return v;
|
||||
}
|
||||
|
||||
|
||||
template <typename Stream>
|
||||
inline const bool& operator>> (const bool& v, packer<Stream> o)
|
||||
{
|
||||
if(v) { o.pack_true(); }
|
||||
else { o.pack_false(); }
|
||||
return v;
|
||||
}
|
||||
|
||||
|
||||
} // namespace type
|
||||
} // namespace msgpack
|
||||
|
||||
#endif /* msgpack/type/bool.hpp */
|
||||
|
||||
67
cpp/type/float.hpp
Normal file
67
cpp/type/float.hpp
Normal file
|
|
@ -0,0 +1,67 @@
|
|||
//
|
||||
// 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_FLOAT_HPP__
|
||||
#define MSGPACK_TYPE_FLOAT_HPP__
|
||||
|
||||
#include "msgpack/object.hpp"
|
||||
#include <vector>
|
||||
|
||||
namespace msgpack {
|
||||
namespace type {
|
||||
|
||||
|
||||
// FIXME check overflow, underflow
|
||||
|
||||
|
||||
inline float& operator<< (float& v, object o)
|
||||
{
|
||||
if(o.type != DOUBLE) { throw type_error(); }
|
||||
v = o.via.dec;
|
||||
return v;
|
||||
}
|
||||
|
||||
|
||||
template <typename Stream>
|
||||
inline const float& operator>> (const float& v, packer<Stream> o)
|
||||
{
|
||||
o.pack_float(v);
|
||||
return v;
|
||||
}
|
||||
|
||||
|
||||
inline double& operator<< (double& v, object o)
|
||||
{
|
||||
if(o.type != DOUBLE) { throw type_error(); }
|
||||
v = o.via.dec;
|
||||
return v;
|
||||
}
|
||||
|
||||
|
||||
template <typename Stream>
|
||||
inline const double& operator>> (const double& v, packer<Stream> o)
|
||||
{
|
||||
o.pack_double(v);
|
||||
return v;
|
||||
}
|
||||
|
||||
|
||||
} // namespace type
|
||||
} // namespace msgpack
|
||||
|
||||
#endif /* msgpack/type/float.hpp */
|
||||
|
||||
243
cpp/type/integer.hpp
Normal file
243
cpp/type/integer.hpp
Normal file
|
|
@ -0,0 +1,243 @@
|
|||
//
|
||||
// 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_INTEGER_HPP__
|
||||
#define MSGPACK_TYPE_INTEGER_HPP__
|
||||
|
||||
#include "msgpack/object.hpp"
|
||||
#include <limits>
|
||||
|
||||
namespace msgpack {
|
||||
namespace type {
|
||||
|
||||
|
||||
namespace detail {
|
||||
template <typename T, bool Signed>
|
||||
struct convert_integer_sign;
|
||||
|
||||
template <typename T>
|
||||
struct convert_integer_sign<T, true> {
|
||||
static inline T convert(object o) {
|
||||
if(o.type == POSITIVE_INTEGER) {
|
||||
if(o.via.u64 > (uint64_t)std::numeric_limits<T>::max())
|
||||
{ throw type_error(); }
|
||||
return o.via.u64;
|
||||
} else if(o.type == NEGATIVE_INTEGER) {
|
||||
if(o.via.i64 < (int64_t)-std::numeric_limits<T>::max())
|
||||
{ throw type_error(); }
|
||||
return o.via.i64;
|
||||
}
|
||||
throw type_error();
|
||||
}
|
||||
};
|
||||
|
||||
template <typename T>
|
||||
struct convert_integer_sign<T, false> {
|
||||
static inline T convert(object o) {
|
||||
if(o.type == POSITIVE_INTEGER) {
|
||||
if(o.via.u64 > (uint64_t)std::numeric_limits<T>::max())
|
||||
{ throw type_error(); }
|
||||
return o.via.u64;
|
||||
}
|
||||
throw type_error();
|
||||
}
|
||||
};
|
||||
|
||||
template <typename T>
|
||||
static inline T convert_integer(object o)
|
||||
{
|
||||
return detail::convert_integer_sign<T, std::numeric_limits<T>::is_signed>::convert(o);
|
||||
}
|
||||
|
||||
|
||||
|
||||
template <typename T, typename Stream, int Size, bool Signed>
|
||||
struct pack_integer_size_sign;
|
||||
|
||||
template <typename T, typename Stream>
|
||||
struct pack_integer_size_sign<T, Stream, 1, true> {
|
||||
static inline void pack(T v, packer<Stream>& o)
|
||||
{ o.pack_int8(v); }
|
||||
};
|
||||
|
||||
template <typename T, typename Stream>
|
||||
struct pack_integer_size_sign<T, Stream, 1, false> {
|
||||
static inline void pack(T v, packer<Stream>& o)
|
||||
{ o.pack_uint8(v); }
|
||||
};
|
||||
|
||||
template <typename T, typename Stream>
|
||||
struct pack_integer_size_sign<T, Stream, 2, true> {
|
||||
static inline void pack(T v, packer<Stream>& o) {
|
||||
if( (int16_t)v <= (int16_t)std::numeric_limits<int8_t>::max() &&
|
||||
(int16_t)v >= (int16_t)std::numeric_limits<int8_t>::min())
|
||||
{ o.pack_int8(v); }
|
||||
else { o.pack_int16(v); }
|
||||
}
|
||||
};
|
||||
|
||||
template <typename T, typename Stream>
|
||||
struct pack_integer_size_sign<T, Stream, 2, false> {
|
||||
static inline void pack(T v, packer<Stream>& o) {
|
||||
if( (uint16_t)v <= (uint16_t)std::numeric_limits<uint8_t>::max())
|
||||
{ o.pack_uint8(v); }
|
||||
else { o.pack_uint16(v); }
|
||||
}
|
||||
};
|
||||
|
||||
template <typename T, typename Stream>
|
||||
struct pack_integer_size_sign<T, Stream, 4, true> {
|
||||
static inline void pack(T v, packer<Stream>& o) {
|
||||
if( (int32_t)v <= (int32_t)std::numeric_limits<int8_t>::max() &&
|
||||
(int32_t)v >= (int32_t)std::numeric_limits<int8_t>::min())
|
||||
{ o.pack_int8(v); }
|
||||
else if( (int32_t)v <= (int32_t)std::numeric_limits<int16_t>::max() &&
|
||||
(int32_t)v >= (int32_t)std::numeric_limits<int16_t>::min())
|
||||
{ o.pack_int16(v); }
|
||||
else { o.pack_int32(v); }
|
||||
}
|
||||
};
|
||||
|
||||
template <typename T, typename Stream>
|
||||
struct pack_integer_size_sign<T, Stream, 4, false> {
|
||||
static inline void pack(T v, packer<Stream>& o) {
|
||||
if( (uint32_t)v <= (uint32_t)std::numeric_limits<uint8_t>::max())
|
||||
{ o.pack_uint8(v); }
|
||||
else if( (uint32_t)v <= (uint32_t)std::numeric_limits<uint16_t>::max())
|
||||
{ o.pack_uint16(v); }
|
||||
else { o.pack_uint32(v); }
|
||||
}
|
||||
};
|
||||
|
||||
template <typename T, typename Stream>
|
||||
struct pack_integer_size_sign<T, Stream, 8, true> {
|
||||
static inline void pack(T v, packer<Stream>& o) {
|
||||
if( (int64_t)v <= (int64_t)std::numeric_limits<int8_t>::max() &&
|
||||
(int64_t)v >= (int64_t)std::numeric_limits<int8_t>::min())
|
||||
{ o.pack_int8(v); }
|
||||
else if( (int64_t)v <= (int64_t)std::numeric_limits<int16_t>::max() &&
|
||||
(int64_t)v >= (int64_t)std::numeric_limits<int16_t>::min())
|
||||
{ o.pack_int16(v); }
|
||||
else if( (int64_t)v <= (int64_t)std::numeric_limits<int32_t>::max() &&
|
||||
(int64_t)v >= (int64_t)std::numeric_limits<int32_t>::min())
|
||||
{ o.pack_int32(v); }
|
||||
else { o.pack_int64(v); }
|
||||
}
|
||||
};
|
||||
|
||||
template <typename T, typename Stream>
|
||||
struct pack_integer_size_sign<T, Stream, 8, false> {
|
||||
static inline void pack(T v, packer<Stream>& o) {
|
||||
if( (uint64_t)v <= (uint64_t)std::numeric_limits<uint8_t>::max())
|
||||
{ o.pack_uint8(v); }
|
||||
else if( (uint64_t)v <= (uint64_t)std::numeric_limits<uint16_t>::max())
|
||||
{ o.pack_uint16(v); }
|
||||
else if( (uint64_t)v <= (uint64_t)std::numeric_limits<uint32_t>::max())
|
||||
{ o.pack_uint32(v); }
|
||||
else { o.pack_uint64(v); }
|
||||
}
|
||||
};
|
||||
|
||||
|
||||
template <typename T, typename Stream>
|
||||
static inline void pack_integer(T v, packer<Stream>& o)
|
||||
{
|
||||
pack_integer_size_sign<T, Stream, sizeof(T), std::numeric_limits<T>::is_signed>::pack(v, o);
|
||||
}
|
||||
|
||||
} // namespace detail
|
||||
|
||||
|
||||
inline signed char& operator<< (signed char& v, object o)
|
||||
{ v = detail::convert_integer<signed char>(o); return v; }
|
||||
|
||||
inline signed short& operator<< (signed short& v, object o)
|
||||
{ v = detail::convert_integer<signed short>(o); return v; }
|
||||
|
||||
inline signed int& operator<< (signed int& v, object o)
|
||||
{ v = detail::convert_integer<signed int>(o); return v; }
|
||||
|
||||
inline signed long& operator<< (signed long& v, object o)
|
||||
{ v = detail::convert_integer<signed long>(o); return v; }
|
||||
|
||||
inline signed long long& operator<< (signed long long& v, object o)
|
||||
{ v = detail::convert_integer<signed long long>(o); return v; }
|
||||
|
||||
|
||||
inline unsigned char& operator<< (unsigned char& v, object o)
|
||||
{ v = detail::convert_integer<unsigned char>(o); return v; }
|
||||
|
||||
inline unsigned short& operator<< (unsigned short& v, object o)
|
||||
{ v = detail::convert_integer<unsigned short>(o); return v; }
|
||||
|
||||
inline unsigned int& operator<< (unsigned int& v, object o)
|
||||
{ v = detail::convert_integer<unsigned int>(o); return v; }
|
||||
|
||||
inline unsigned long& operator<< (unsigned long& v, object o)
|
||||
{ v = detail::convert_integer<unsigned long>(o); return v; }
|
||||
|
||||
inline unsigned long long& operator<< (unsigned long long& v, object o)
|
||||
{ v = detail::convert_integer<unsigned long long>(o); return v; }
|
||||
|
||||
|
||||
template <typename Stream>
|
||||
inline const signed char& operator>> (const signed char& v, packer<Stream> o)
|
||||
{ detail::pack_integer<signed char>(v, o); return v; }
|
||||
|
||||
template <typename Stream>
|
||||
inline const signed short& operator>> (const signed short& v, packer<Stream> o)
|
||||
{ detail::pack_integer<signed short>(v, o); return v; }
|
||||
|
||||
template <typename Stream>
|
||||
inline const signed int& operator>> (const signed int& v, packer<Stream> o)
|
||||
{ detail::pack_integer<signed int>(v, o); return v; }
|
||||
|
||||
template <typename Stream>
|
||||
inline const signed long& operator>> (const signed long& v, packer<Stream> o)
|
||||
{ detail::pack_integer<signed long>(v, o); return v; }
|
||||
|
||||
template <typename Stream>
|
||||
inline const signed long long& operator>> (const signed long long& v, packer<Stream> o)
|
||||
{ detail::pack_integer<signed long long>(v, o); return v; }
|
||||
|
||||
|
||||
template <typename Stream>
|
||||
inline const unsigned char& operator>> (const unsigned char& v, packer<Stream> o)
|
||||
{ detail::pack_integer<unsigned char>(v, o); return v; }
|
||||
|
||||
template <typename Stream>
|
||||
inline const unsigned short& operator>> (const unsigned short& v, packer<Stream> o)
|
||||
{ detail::pack_integer<unsigned short>(v, o); return v; }
|
||||
|
||||
template <typename Stream>
|
||||
inline const unsigned int& operator>> (const unsigned int& v, packer<Stream> o)
|
||||
{ detail::pack_integer<unsigned int>(v, o); return v; }
|
||||
|
||||
template <typename Stream>
|
||||
inline const unsigned long& operator>> (const unsigned long& v, packer<Stream> o)
|
||||
{ detail::pack_integer<unsigned long>(v, o); return v; }
|
||||
|
||||
template <typename Stream>
|
||||
inline const unsigned long long& operator>> (const unsigned long long& v, packer<Stream> o)
|
||||
{ detail::pack_integer<unsigned long long>(v, o); return v; }
|
||||
|
||||
|
||||
} // namespace type
|
||||
} // namespace msgpack
|
||||
|
||||
#endif /* msgpack/type/integer.hpp */
|
||||
|
||||
132
cpp/type/map.hpp
Normal file
132
cpp/type/map.hpp
Normal file
|
|
@ -0,0 +1,132 @@
|
|||
//
|
||||
// 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>
|
||||
|
||||
namespace msgpack {
|
||||
namespace type {
|
||||
|
||||
|
||||
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; }
|
||||
};
|
||||
}
|
||||
|
||||
template <typename K, typename V>
|
||||
inline assoc_vector<K,V>& operator<< (assoc_vector<K,V>& v, object o)
|
||||
{
|
||||
if(o.type != MAP) { throw type_error(); }
|
||||
v.resize(o.via.container.size);
|
||||
object* p(o.via.container.ptr);
|
||||
object* const pend(o.via.container.ptr + o.via.container.size);
|
||||
std::pair<K, V>* it(&v.front());
|
||||
for(; p < pend; ++it) {
|
||||
convert(it->first, *p); ++p;
|
||||
convert(it->second, *p); ++p;
|
||||
}
|
||||
std::sort(v.begin(), v.end(), detail::pair_first_less<K,V>());
|
||||
return v;
|
||||
}
|
||||
|
||||
template <typename Stream, typename K, typename V>
|
||||
inline const assoc_vector<K,V>& operator>> (const assoc_vector<K,V>& v, packer<Stream>& o)
|
||||
{
|
||||
o.pack_map(v.size());
|
||||
for(typename assoc_vector<K,V>::const_iterator it(v.begin()), it_end(v.end());
|
||||
it != it_end; ++it) {
|
||||
pack(it->first, o);
|
||||
pack(it->second, o);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
template <typename K, typename V>
|
||||
inline std::map<K, V> operator<< (std::map<K, V>& v, object o)
|
||||
{
|
||||
if(o.type != MAP) { throw type_error(); }
|
||||
object* p(o.via.container.ptr);
|
||||
object* const pend(o.via.container.ptr + o.via.container.size*2);
|
||||
while(p < pend) {
|
||||
K key;
|
||||
convert(key, *p); ++p;
|
||||
typename std::map<K,V>::iterator it(v.find(key));
|
||||
if(it != v.end()) {
|
||||
V val;
|
||||
convert(val, *p); ++p;
|
||||
it->insert( std::pair<K,V>(key, val) );
|
||||
} else {
|
||||
convert(it->second, *p); ++p;
|
||||
}
|
||||
}
|
||||
return v;
|
||||
}
|
||||
|
||||
template <typename Stream, typename K, typename V>
|
||||
inline const std::map<K,V>& operator>> (const std::map<K,V>& v, packer<Stream>& o)
|
||||
{
|
||||
o.pack_map(v.size());
|
||||
for(typename std::map<K,V>::const_iterator it(v.begin()), it_end(v.end());
|
||||
it != it_end; ++it) {
|
||||
pack(it->first, o);
|
||||
pack(it->second, o);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
template <typename K, typename V>
|
||||
inline std::multimap<K, V> operator<< (std::multimap<K, V>& v, object o)
|
||||
{
|
||||
if(o.type != MAP) { throw type_error(); }
|
||||
object* p(o.via.container.ptr);
|
||||
object* const pend(o.via.container.ptr + o.via.container.size*2);
|
||||
while(p < pend) {
|
||||
std::pair<K, V> value;
|
||||
convert(value.first, *p); ++p;
|
||||
convert(value.second, *p); ++p;
|
||||
v.insert(value);
|
||||
}
|
||||
return v;
|
||||
}
|
||||
|
||||
template <typename Stream, typename K, typename V>
|
||||
inline const std::multimap<K,V>& operator>> (const std::multimap<K,V>& v, packer<Stream>& o)
|
||||
{
|
||||
o.pack_multimap(v.size());
|
||||
for(typename std::multimap<K,V>::const_iterator it(v.begin()), it_end(v.end());
|
||||
it != it_end; ++it) {
|
||||
pack(it->first, o);
|
||||
pack(it->second, o);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
} // namespace type
|
||||
} // namespace msgpack
|
||||
|
||||
#endif /* msgpack/type/map.hpp */
|
||||
|
||||
47
cpp/type/nil.hpp
Normal file
47
cpp/type/nil.hpp
Normal file
|
|
@ -0,0 +1,47 @@
|
|||
//
|
||||
// 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_NIL_HPP__
|
||||
#define MSGPACK_TYPE_NIL_HPP__
|
||||
|
||||
#include "msgpack/object.hpp"
|
||||
|
||||
namespace msgpack {
|
||||
namespace type {
|
||||
|
||||
|
||||
struct nil { };
|
||||
|
||||
inline nil& operator<< (nil& v, object o)
|
||||
{
|
||||
if(o.type != NIL) { throw type_error(); }
|
||||
return v;
|
||||
}
|
||||
|
||||
template <typename Stream>
|
||||
inline const nil& operator>> (const nil& v, packer<Stream>& o)
|
||||
{
|
||||
o.pack_nil();
|
||||
return v;
|
||||
}
|
||||
|
||||
|
||||
} // namespace type
|
||||
} // namespace msgpack
|
||||
|
||||
#endif /* msgpack/type/nil.hpp */
|
||||
|
||||
99
cpp/type/raw.hpp
Normal file
99
cpp/type/raw.hpp
Normal file
|
|
@ -0,0 +1,99 @@
|
|||
//
|
||||
// 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_RAW_HPP__
|
||||
#define MSGPACK_TYPE_RAW_HPP__
|
||||
|
||||
#include "msgpack/object.hpp"
|
||||
#include <string.h>
|
||||
#include <string>
|
||||
|
||||
namespace msgpack {
|
||||
namespace type {
|
||||
|
||||
|
||||
struct raw_ref {
|
||||
raw_ref() : ptr(NULL), size(0) {}
|
||||
raw_ref(const char* p, uint32_t s) : ptr(p), size(s) {}
|
||||
|
||||
const char* ptr;
|
||||
uint32_t size;
|
||||
|
||||
std::string str() { return std::string(ptr, size); }
|
||||
|
||||
bool operator== (const raw_ref& x)
|
||||
{
|
||||
return size == x.size && memcmp(ptr, x.ptr, size) == 0;
|
||||
}
|
||||
|
||||
bool operator!= (const raw_ref& x)
|
||||
{
|
||||
return !(*this != x);
|
||||
}
|
||||
|
||||
bool operator< (const raw_ref& x)
|
||||
{
|
||||
if(size == x.size) { return memcmp(ptr, x.ptr, size) < 0; }
|
||||
else { return size < x.size; }
|
||||
}
|
||||
|
||||
bool operator> (const raw_ref& x)
|
||||
{
|
||||
if(size == x.size) { return memcmp(ptr, x.ptr, size) > 0; }
|
||||
else { return size > x.size; }
|
||||
}
|
||||
};
|
||||
|
||||
inline raw_ref& operator<< (raw_ref& v, object o)
|
||||
{
|
||||
if(o.type != RAW) { throw type_error(); }
|
||||
v.ptr = o.via.ref.ptr;
|
||||
v.size = o.via.ref.size;
|
||||
return v;
|
||||
}
|
||||
|
||||
|
||||
inline std::string& operator<< (std::string& v, object o)
|
||||
{
|
||||
raw_ref r;
|
||||
r << o;
|
||||
v.assign(r.ptr, r.size);
|
||||
return v;
|
||||
}
|
||||
|
||||
|
||||
template <typename Stream>
|
||||
inline const raw_ref& operator>> (const raw_ref& v, packer<Stream>& o)
|
||||
{
|
||||
o.pack_raw(v.ptr, v.size);
|
||||
return v;
|
||||
}
|
||||
|
||||
|
||||
template <typename Stream>
|
||||
inline const std::string& operator>> (const std::string& v, packer<Stream>& o)
|
||||
{
|
||||
o.pack_raw(v.data(), v.size());
|
||||
return v;
|
||||
}
|
||||
|
||||
|
||||
} // namespace type
|
||||
} // namespace msgpack
|
||||
|
||||
#endif /* msgpack/type/raw.hpp */
|
||||
|
||||
172
cpp/type/tuple.hpp.erb
Normal file
172
cpp/type/tuple.hpp.erb
Normal file
|
|
@ -0,0 +1,172 @@
|
|||
//
|
||||
// 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_TUPLE_HPP__
|
||||
#define MSGPACK_TYPE_TUPLE_HPP__
|
||||
|
||||
#include "msgpack/object.hpp"
|
||||
|
||||
namespace msgpack {
|
||||
namespace type {
|
||||
|
||||
|
||||
// FIXME operator==
|
||||
// FIXME operator!=
|
||||
|
||||
<% GENERATION_LIMIT = 15 %>
|
||||
|
||||
template < typename A0 <%1.upto(GENERATION_LIMIT+1) {|i|%>, typename A<%=i%> = void<%}%> >
|
||||
struct tuple;
|
||||
|
||||
template <typename Tuple, int N>
|
||||
struct tuple_type;
|
||||
|
||||
template <typename Tuple, int N>
|
||||
struct const_tuple_type;
|
||||
|
||||
template <typename T>
|
||||
struct tuple_element {
|
||||
typedef T type;
|
||||
tuple_element(T& x) : _x(x) {}
|
||||
type& get() { return _x; }
|
||||
const type& get() const { return _x; }
|
||||
private:
|
||||
type& _x;
|
||||
};
|
||||
|
||||
template <typename T>
|
||||
struct const_tuple_element {
|
||||
typedef T type;
|
||||
const_tuple_element(const T& x) : _x(x) {}
|
||||
const type& get() const { return _x; }
|
||||
private:
|
||||
const type& _x;
|
||||
};
|
||||
|
||||
|
||||
<%0.upto(GENERATION_LIMIT) {|i|%>
|
||||
<%0.upto(i) {|j|%>
|
||||
template < typename A0 <%1.upto(i) {|k|%>, typename A<%=k%> <%}%>>
|
||||
struct tuple_type<tuple<A0 <%1.upto(i) {|k|%>, A<%=k%> <%}%>>, <%=j%>> : tuple_element<A<%=j%>> {
|
||||
tuple_type(tuple<A0 <%1.upto(i) {|k|%>, A<%=k%> <%}%>>& x) : tuple_element<A<%=j%>>(x.a<%=j%>) {}
|
||||
};
|
||||
<%}%>
|
||||
<%}%>
|
||||
|
||||
|
||||
<%0.upto(GENERATION_LIMIT) {|i|%>
|
||||
<%0.upto(i) {|j|%>
|
||||
template < typename A0 <%1.upto(i) {|k|%>, typename A<%=k%> <%}%>>
|
||||
struct const_tuple_type<tuple<A0 <%1.upto(i) {|k|%>, A<%=k%> <%}%>>, <%=j%>> : const_tuple_element<A<%=j%>> {
|
||||
const_tuple_type(const tuple<A0 <%1.upto(i) {|k|%>, A<%=k%> <%}%>>& x) : const_tuple_element<A<%=j%>>(x.a<%=j%>) {}
|
||||
};
|
||||
<%}%>
|
||||
<%}%>
|
||||
|
||||
|
||||
<%0.upto(GENERATION_LIMIT) {|i|%>
|
||||
template < typename A0 <%1.upto(i) {|j|%>, typename A<%=j%> <%}%>>
|
||||
tuple< A0 <%1.upto(i) {|j|%>, A<%=j%> <%}%>> make_tuple(const A0& a0 <%1.upto(i) {|j|%>, const A<%=j%>& a<%=j%><%}%>)
|
||||
{
|
||||
return tuple< A0 <%1.upto(i) {|j|%>, A<%=j%> <%}%>>(a0 <%1.upto(i) {|j|%>, a<%=j%><%}%>);
|
||||
}
|
||||
<%}%>
|
||||
|
||||
|
||||
<%0.upto(GENERATION_LIMIT) {|i|%>
|
||||
template < typename A0 <%1.upto(i) {|j|%>, typename A<%=j%> <%}%>>
|
||||
struct tuple<A0 <%1.upto(i) {|j|%>, A<%=j%> <%}%>> {
|
||||
tuple() {}
|
||||
tuple(const A0& _a0 <%1.upto(i) {|j|%>, const A<%=j%>& _a<%=j%><%}%>) :
|
||||
a0(_a0) <%1.upto(i) {|j|%>, a<%=j%>(_a<%=j%>)<%}%> {}
|
||||
tuple(object o) { convert(*this, o); }
|
||||
template <int N> typename tuple_type<tuple<A0 <%1.upto(i) {|j|%>, A<%=j%> <%}%>>, N>::type& get()
|
||||
{ return tuple_type<tuple<A0 <%1.upto(i) {|j|%>, A<%=j%> <%}%>>, N>(*this).get(); }
|
||||
template <int N> const typename const_tuple_type<tuple<A0 <%1.upto(i) {|j|%>, A<%=j%> <%}%>>, N>::type& get() const
|
||||
{ return const_tuple_type<tuple<A0 <%1.upto(i) {|j|%>, A<%=j%> <%}%>>, N>(*this).get(); }
|
||||
<%0.upto(i) {|j|%>
|
||||
A<%=j%> a<%=j%>;<%}%>
|
||||
};
|
||||
<%}%>
|
||||
|
||||
|
||||
<%0.upto(GENERATION_LIMIT) {|i|%>
|
||||
template < typename A0 <%1.upto(i) {|j|%>, typename A<%=j%> <%}%>>
|
||||
tuple<A0 <%1.upto(i) {|j|%>, A<%=j%> <%}%>>& operator<< (
|
||||
tuple<A0 <%1.upto(i) {|j|%>, A<%=j%> <%}%>>& v,
|
||||
object o) {
|
||||
if(o.type != ARRAY) { throw type_error(); }
|
||||
if(o.via.container.size < <%=i+1%>) { throw type_error(); }
|
||||
<%0.upto(i) {|j|%>
|
||||
convert<A<%=j%>>(v.template get<<%=j%>>(), o.via.container.ptr[<%=j%>]);<%}%>
|
||||
return v;
|
||||
}
|
||||
<%}%>
|
||||
|
||||
|
||||
// FIXME
|
||||
/*
|
||||
template <typename A0, typename A1 = void, typename A2 = void>
|
||||
struct tuple_just;
|
||||
|
||||
template <typename A0>
|
||||
struct tuple_just<A0> {
|
||||
A0 a0;
|
||||
static inline void convert(object o, tuple_just<A0>& v)
|
||||
{
|
||||
if(o.type != ARRAY) { throw type_error(); }
|
||||
if(o.v.container.size != 1) { throw type_error(); }
|
||||
msgpack::convert<A0>(o.v.container.ptr[0], v.a0);
|
||||
}
|
||||
};
|
||||
|
||||
template <typename A0, typename A1>
|
||||
struct tuple_just<A0, A1> {
|
||||
A0 a0;
|
||||
A1 a1;
|
||||
static inline void convert(object o, tuple_just<A0, A1>& v)
|
||||
{
|
||||
if(o.type != ARRAY) { throw type_error(); }
|
||||
if(o.v.container.size != 2) { throw type_error(); }
|
||||
msgpack::convert<A0>(o.v.container.ptr[0], v.a0);
|
||||
msgpack::convert<A1>(o.v.container.ptr[1], v.a1);
|
||||
}
|
||||
};
|
||||
*/
|
||||
|
||||
|
||||
|
||||
<%0.upto(GENERATION_LIMIT) {|i|%>
|
||||
template < typename Stream , typename A0 <%1.upto(i) {|j|%>, typename A<%=j%> <%}%>>
|
||||
const tuple<A0 <%1.upto(i) {|j|%>, A<%=j%> <%}%>>& operator>> (
|
||||
const tuple<A0 <%1.upto(i) {|j|%>, A<%=j%> <%}%>>& v,
|
||||
packer<Stream> o) {
|
||||
o.pack_array(<%=i+1%>);
|
||||
<%0.upto(i) {|j|%>
|
||||
pack(v.template get<<%=j%>>(), o);<%}%>
|
||||
return v;
|
||||
}
|
||||
<%}%>
|
||||
|
||||
|
||||
|
||||
} // namespace type
|
||||
} // namespace msgpack
|
||||
|
||||
#endif /* msgpack/type/tuple.hpp */
|
||||
|
||||
|
||||
Loading…
Add table
Add a link
Reference in a new issue