mirror of
https://github.com/msgpack/msgpack-python.git
synced 2026-02-07 02:09:59 +00:00
integrate machine-dependent integer serialization routine to msgpack/pack_template.h
git-svn-id: file:///Users/frsyuki/project/msgpack-git/svn/x@90 5a5092ae-2292-43ba-b2d5-dcab9c1a2731
This commit is contained in:
parent
adba617f45
commit
cd973b8483
10 changed files with 468 additions and 272 deletions
74
cpp/pack.hpp
74
cpp/pack.hpp
|
|
@ -20,6 +20,8 @@
|
|||
|
||||
#include <arpa/inet.h> // __BYTE_ORDER
|
||||
#include <stdexcept>
|
||||
#include <limits.h>
|
||||
#include "msgpack/pack_define.h"
|
||||
|
||||
namespace msgpack {
|
||||
|
||||
|
|
@ -30,33 +32,38 @@ public:
|
|||
packer(Stream& s);
|
||||
|
||||
public:
|
||||
void pack_int(int d) { pack_int_impl(m_stream, d); }
|
||||
void pack_long(long d) { pack_long_impl(m_stream, d); }
|
||||
void pack_unsigned_int(unsigned int d) { pack_unsigned_int_impl(m_stream, d); }
|
||||
void pack_unsigned_long(unsigned long d) { pack_unsigned_long_impl(m_stream, d); }
|
||||
void pack_uint8(uint8_t d) { pack_uint8_impl(m_stream, d); }
|
||||
void pack_uint16(uint16_t d) { pack_uint16_impl(m_stream, d); }
|
||||
void pack_uint32(uint32_t d) { pack_uint32_impl(m_stream, d); }
|
||||
void pack_uint64(uint64_t d) { pack_uint64_impl(m_stream, d); }
|
||||
void pack_int8(uint8_t d) { pack_int8_impl(m_stream, d); }
|
||||
void pack_int16(uint16_t d) { pack_int16_impl(m_stream, d); }
|
||||
void pack_int32(uint32_t d) { pack_int32_impl(m_stream, d); }
|
||||
void pack_int64(uint64_t d) { pack_int64_impl(m_stream, d); }
|
||||
void pack_float(float d) { pack_float_impl(m_stream, d); }
|
||||
void pack_double(double d) { pack_double_impl(m_stream, d); }
|
||||
void pack_nil() { pack_nil_impl(m_stream); }
|
||||
void pack_true() { pack_true_impl(m_stream); }
|
||||
void pack_false() { pack_false_impl(m_stream); }
|
||||
void pack_array(unsigned int n) { pack_array_impl(m_stream, n); }
|
||||
void pack_map(unsigned int n) { pack_map_impl(m_stream, n); }
|
||||
void pack_uint8(uint8_t d) { pack_uint8_impl(m_stream, d); }
|
||||
void pack_uint16(uint16_t d) { pack_uint16_impl(m_stream, d); }
|
||||
void pack_uint32(uint32_t d) { pack_uint32_impl(m_stream, d); }
|
||||
void pack_uint64(uint64_t d) { pack_uint64_impl(m_stream, d); }
|
||||
void pack_int8(uint8_t d) { pack_int8_impl(m_stream, d); }
|
||||
void pack_int16(uint16_t d) { pack_int16_impl(m_stream, d); }
|
||||
void pack_int32(uint32_t d) { pack_int32_impl(m_stream, d); }
|
||||
void pack_int64(uint64_t d) { pack_int64_impl(m_stream, d); }
|
||||
|
||||
void pack_short(int d) { pack_short_impl(m_stream, d); }
|
||||
void pack_int(int d) { pack_int_impl(m_stream, d); }
|
||||
void pack_long(long d) { pack_long_impl(m_stream, d); }
|
||||
void pack_long_long(long long d) { pack_long_long_impl(m_stream, d); }
|
||||
void pack_unsigned_short(unsigned short d) { pack_unsigned_short_impl(m_stream, d); }
|
||||
void pack_unsigned_int(unsigned int d) { pack_unsigned_int_impl(m_stream, d); }
|
||||
void pack_unsigned_long_long(unsigned long long d) { pack_unsigned_long_long_impl(m_stream, d); }
|
||||
|
||||
void pack_float(float d) { pack_float_impl(m_stream, d); }
|
||||
void pack_double(double d) { pack_double_impl(m_stream, d); }
|
||||
|
||||
void pack_nil() { pack_nil_impl(m_stream); }
|
||||
void pack_true() { pack_true_impl(m_stream); }
|
||||
void pack_false() { pack_false_impl(m_stream); }
|
||||
|
||||
void pack_array(unsigned int n) { pack_array_impl(m_stream, n); }
|
||||
|
||||
void pack_map(unsigned int n) { pack_map_impl(m_stream, n); }
|
||||
|
||||
void pack_raw(size_t l) { pack_raw_impl(m_stream, l); }
|
||||
void pack_raw_body(const char* b, size_t l) { pack_raw_body_impl(m_stream, b, l); }
|
||||
|
||||
private:
|
||||
static void pack_int_impl(Stream& x, int d);
|
||||
static void pack_long_impl(Stream& x, long d);
|
||||
static void pack_unsigned_int_impl(Stream& x, unsigned int d);
|
||||
static void pack_unsigned_long_impl(Stream& x, unsigned long d);
|
||||
static void pack_uint8_impl(Stream& x, uint8_t d);
|
||||
static void pack_uint16_impl(Stream& x, uint16_t d);
|
||||
static void pack_uint32_impl(Stream& x, uint32_t d);
|
||||
|
|
@ -65,13 +72,27 @@ private:
|
|||
static void pack_int16_impl(Stream& x, int16_t d);
|
||||
static void pack_int32_impl(Stream& x, int32_t d);
|
||||
static void pack_int64_impl(Stream& x, int64_t d);
|
||||
|
||||
static void pack_short_impl(Stream& x, short d);
|
||||
static void pack_int_impl(Stream& x, int d);
|
||||
static void pack_long_impl(Stream& x, long d);
|
||||
static void pack_long_long_impl(Stream& x, long long d);
|
||||
static void pack_unsigned_short_impl(Stream& x, unsigned short d);
|
||||
static void pack_unsigned_int_impl(Stream& x, unsigned int d);
|
||||
static void pack_unsigned_long_impl(Stream& x, unsigned long d);
|
||||
static void pack_unsigned_long_long_impl(Stream& x, unsigned long long d);
|
||||
|
||||
static void pack_float_impl(Stream& x, float d);
|
||||
static void pack_double_impl(Stream& x, double d);
|
||||
|
||||
static void pack_nil_impl(Stream& x);
|
||||
static void pack_true_impl(Stream& x);
|
||||
static void pack_false_impl(Stream& x);
|
||||
|
||||
static void pack_array_impl(Stream& x, unsigned int n);
|
||||
|
||||
static void pack_map_impl(Stream& x, unsigned int n);
|
||||
|
||||
static void pack_raw_impl(Stream& x, size_t l);
|
||||
static void pack_raw_body_impl(Stream& x, const void* b, size_t l);
|
||||
|
||||
|
|
@ -88,8 +109,15 @@ private:
|
|||
#define msgpack_pack_inline_func(name) \
|
||||
template <typename Stream> \
|
||||
inline void packer<Stream>::pack_ ## name ## _impl
|
||||
|
||||
#define msgpack_pack_inline_func_cint(name) \
|
||||
template <typename Stream> \
|
||||
inline void packer<Stream>::pack_ ## name ## _impl
|
||||
|
||||
#define msgpack_pack_user Stream&
|
||||
|
||||
#define msgpack_pack_append_buffer append_buffer
|
||||
|
||||
#include "msgpack/pack_template.h"
|
||||
|
||||
|
||||
|
|
|
|||
|
|
@ -63,102 +63,6 @@ namespace detail {
|
|||
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(packer<Stream>& o, T v)
|
||||
{ o.pack_int8(v); }
|
||||
};
|
||||
|
||||
template <typename T, typename Stream>
|
||||
struct pack_integer_size_sign<T, Stream, 1, false> {
|
||||
static inline void pack(packer<Stream>& o, T v)
|
||||
{ o.pack_uint8(v); }
|
||||
};
|
||||
|
||||
template <typename T, typename Stream>
|
||||
struct pack_integer_size_sign<T, Stream, 2, true> {
|
||||
static inline void pack(packer<Stream>& o, T v) {
|
||||
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(packer<Stream>& o, T v) {
|
||||
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(packer<Stream>& o, T v) {
|
||||
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(packer<Stream>& o, T v) {
|
||||
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(packer<Stream>& o, T v) {
|
||||
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(packer<Stream>& o, T v) {
|
||||
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(o, v);
|
||||
}
|
||||
|
||||
} // namespace detail
|
||||
} // namespace type
|
||||
|
||||
|
|
@ -197,44 +101,44 @@ inline unsigned long long& operator>> (object o, unsigned long long& v)
|
|||
|
||||
template <typename Stream>
|
||||
inline packer<Stream>& operator<< (packer<Stream>& o, const signed char& v)
|
||||
{ type::detail::pack_integer<signed char>(v, o); return o; }
|
||||
{ o.pack_int8(v); return o; }
|
||||
|
||||
template <typename Stream>
|
||||
inline packer<Stream>& operator<< (packer<Stream>& o, const signed short& v)
|
||||
{ type::detail::pack_integer<signed short>(v, o); return o; }
|
||||
{ o.pack_short(v); return o; }
|
||||
|
||||
template <typename Stream>
|
||||
inline packer<Stream>& operator<< (packer<Stream>& o, const signed int& v)
|
||||
{ type::detail::pack_integer<signed int>(v, o); return o; }
|
||||
{ o.pack_int(v); return o; }
|
||||
|
||||
template <typename Stream>
|
||||
inline packer<Stream>& operator<< (packer<Stream>& o, const signed long& v)
|
||||
{ type::detail::pack_integer<signed long>(v, o); return o; }
|
||||
{ o.pack_long(v); return o; }
|
||||
|
||||
template <typename Stream>
|
||||
inline packer<Stream>& operator<< (packer<Stream>& o, const signed long long& v)
|
||||
{ type::detail::pack_integer<signed long long>(v, o); return o; }
|
||||
{ o.pack_long_long(v); return o; }
|
||||
|
||||
|
||||
template <typename Stream>
|
||||
inline packer<Stream>& operator<< (packer<Stream>& o, const unsigned char& v)
|
||||
{ type::detail::pack_integer<unsigned char>(v, o); return o; }
|
||||
{ o.pack_uint8(v); return o; }
|
||||
|
||||
template <typename Stream>
|
||||
inline packer<Stream>& operator<< (packer<Stream>& o, const unsigned short& v)
|
||||
{ type::detail::pack_integer<unsigned short>(v, o); return o; }
|
||||
{ o.pack_unsigned_short(v); return o; }
|
||||
|
||||
template <typename Stream>
|
||||
inline packer<Stream>& operator<< (packer<Stream>& o, const unsigned int& v)
|
||||
{ type::detail::pack_integer<unsigned int>(v, o); return o; }
|
||||
{ o.pack_unsigned_int(v); return o; }
|
||||
|
||||
template <typename Stream>
|
||||
inline packer<Stream>& operator<< (packer<Stream>& o, const unsigned long& v)
|
||||
{ type::detail::pack_integer<unsigned long>(v, o); return o; }
|
||||
{ o.pack_unsigned_long(v); return o; }
|
||||
|
||||
template <typename Stream>
|
||||
inline packer<Stream>& operator<< (packer<Stream>& o, const unsigned long long& v)
|
||||
{ type::detail::pack_integer<unsigned long long>(v, o); return o; }
|
||||
{ o.pack_unsigned_long_long(v); return o; }
|
||||
|
||||
|
||||
} // namespace msgpack
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue