diff options
author | frsyuki <frsyuki@5a5092ae-2292-43ba-b2d5-dcab9c1a2731> | 2009-02-15 09:10:02 +0000 |
---|---|---|
committer | frsyuki <frsyuki@5a5092ae-2292-43ba-b2d5-dcab9c1a2731> | 2009-02-15 09:10:02 +0000 |
commit | a114f4a5a5136a24f46bb52ffef1d1f588ba94e1 (patch) | |
tree | b5f5a3319ec12a90f9ee5296ce91410d7346ba68 /cpp | |
parent | 81a771094d2c54437ca20c3c09a5c8f05f81d0c5 (diff) | |
download | msgpack-python-a114f4a5a5136a24f46bb52ffef1d1f588ba94e1.tar.gz |
update pack/unpack routines
git-svn-id: file:///Users/frsyuki/project/msgpack-git/svn/x@100 5a5092ae-2292-43ba-b2d5-dcab9c1a2731
Diffstat (limited to 'cpp')
-rw-r--r-- | cpp/COPYING | 2 | ||||
-rw-r--r-- | cpp/Makefile.am | 5 | ||||
-rw-r--r-- | cpp/README | 2 | ||||
-rw-r--r-- | cpp/msgpack.hpp | 3 | ||||
-rw-r--r-- | cpp/object.cpp | 2 | ||||
-rw-r--r-- | cpp/object.hpp | 132 | ||||
-rw-r--r-- | cpp/pack.hpp | 240 | ||||
-rw-r--r-- | cpp/type/array.hpp | 10 | ||||
-rw-r--r-- | cpp/type/map.hpp | 34 | ||||
-rw-r--r-- | cpp/type/tuple.hpp.erb | 6 | ||||
-rw-r--r-- | cpp/unpack.cpp | 71 | ||||
-rw-r--r-- | cpp/unpack.hpp | 2 | ||||
-rw-r--r-- | cpp/zone.cpp | 2 | ||||
-rw-r--r-- | cpp/zone.hpp.erb | 2 |
14 files changed, 320 insertions, 193 deletions
diff --git a/cpp/COPYING b/cpp/COPYING index 5f100cd..6f5f220 100644 --- a/cpp/COPYING +++ b/cpp/COPYING @@ -1,4 +1,4 @@ -Copyright (C) 2008 FURUHASHI Sadayuki +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. diff --git a/cpp/Makefile.am b/cpp/Makefile.am index 45c75e0..c7ddf4b 100644 --- a/cpp/Makefile.am +++ b/cpp/Makefile.am @@ -7,6 +7,7 @@ libmsgpack_la_SOURCES = \ nobase_include_HEADERS = \ msgpack.hpp \ + msgpack/sbuffer.hpp \ msgpack/pack.hpp \ msgpack/unpack.hpp \ msgpack/object.hpp \ @@ -41,6 +42,6 @@ MOSTLYCLEANFILES = \ msgpack/type/tuple.hpp \ msgpack/zone.hpp -# FIXME -libmsgpack_la_LDFLAGS = -version-info 0:0:0 +# -version-info CURRENT:REVISION:AGE +libmsgpack_la_LDFLAGS = -version-info 1:0:0 @@ -4,7 +4,7 @@ MessagePack is a binary-based efficient data interchange format. -Copyright (C) 2008 FURUHASHI Sadayuki +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. diff --git a/cpp/msgpack.hpp b/cpp/msgpack.hpp index 9f635d0..f00da06 100644 --- a/cpp/msgpack.hpp +++ b/cpp/msgpack.hpp @@ -1,7 +1,7 @@ // // MessagePack for C++ // -// Copyright (C) 2008 FURUHASHI Sadayuki +// 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. @@ -19,3 +19,4 @@ #include "msgpack/zone.hpp" #include "msgpack/pack.hpp" #include "msgpack/unpack.hpp" +#include "msgpack/sbuffer.hpp" diff --git a/cpp/object.cpp b/cpp/object.cpp index 52b6ad0..d217c06 100644 --- a/cpp/object.cpp +++ b/cpp/object.cpp @@ -1,7 +1,7 @@ // // MessagePack for C++ dynamic typed objects // -// Copyright (C) 2008 FURUHASHI Sadayuki +// 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. diff --git a/cpp/object.hpp b/cpp/object.hpp index 1f0dcdf..c6f1e4a 100644 --- a/cpp/object.hpp +++ b/cpp/object.hpp @@ -1,7 +1,7 @@ // // MessagePack for C++ static resolution routine // -// Copyright (C) 2008 FURUHASHI Sadayuki +// 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. @@ -32,14 +32,16 @@ class type_error : public std::bad_cast { }; namespace type { - static const unsigned char NIL = 0x01; - static const unsigned char BOOLEAN = 0x02; - static const unsigned char POSITIVE_INTEGER = 0x03; - static const unsigned char NEGATIVE_INTEGER = 0x04; - static const unsigned char DOUBLE = 0x05; - static const unsigned char RAW = 0x06; - static const unsigned char ARRAY = 0x07; - static const unsigned char MAP = 0x08; + enum object_type { + NIL = 0x01, + BOOLEAN = 0x02, + POSITIVE_INTEGER = 0x03, + NEGATIVE_INTEGER = 0x04, + DOUBLE = 0x05, + RAW = 0x06, + ARRAY = 0x07, + MAP = 0x08, + }; } @@ -59,7 +61,7 @@ struct object { } ref; }; - unsigned char type; + type::object_type type; union_type via; bool is_nil() { return type == type::NIL; } @@ -68,7 +70,7 @@ struct object { T as(); template <typename T> - void convert(T& v); + void convert(T* v); private: struct implicit_type; @@ -77,6 +79,21 @@ public: implicit_type convert(); }; +bool operator==(const object x, const object y); +bool operator!=(const object x, const object y); + +std::ostream& operator<< (std::ostream& s, const object o); + + +template <typename Stream, typename T> +inline void pack(Stream& s, const T& v); + +template <typename Stream, typename T> +packer<Stream>& operator<< (packer<Stream>& o, const T& v); + +template <typename T> +T& operator>> (object o, T& v); + struct object::implicit_type { implicit_type(object o) : obj(o) { } @@ -89,39 +106,41 @@ private: object obj; }; -std::ostream& operator<< (std::ostream& s, const object o); -bool operator==(const object x, const object y); -inline bool operator!=(const object x, const object y) { return !(x == y); } +template <typename Type> +class define : public Type { +public: + typedef Type msgpack_type; + typedef define<Type> define_type; -inline object& operator>> (object o, object& v) -{ - v = o; - return v; -} + define() {} + define(const msgpack_type& v) : msgpack_type(v) {} -template <typename Stream> -packer<Stream>& operator<< (packer<Stream>& o, const object& v); + template <typename Packer> + void msgpack_pack(Packer& o) const + { + o << static_cast<const msgpack_type&>(*this); + } + void msgpack_unpack(object o) + { + o >> static_cast<msgpack_type&>(*this); + } +}; +template <typename Stream> template <typename T> -inline void convert(T& v, object o) -{ - o >> v; -} - -template <typename Stream, typename T> -inline void pack(packer<Stream>& o, const T& v) +inline packer<Stream>& packer<Stream>::pack(const T& v) { - o << v; + *this << v; + return *this; } template <typename Stream, typename T> inline void pack(Stream& s, const T& v) { - packer<Stream> pk(s); - pack(pk, v); + packer<Stream>(s).pack(v); } template <typename Stream, typename T> @@ -130,7 +149,11 @@ inline void pack_copy(packer<Stream>& o, T v) pack(o, v); } - +inline object& operator>> (object o, object& v) +{ + v = o; + return v; +} template <typename T> inline T& operator>> (object o, T& v) @@ -147,27 +170,8 @@ inline packer<Stream>& operator<< (packer<Stream>& o, const T& v) } -template <typename Type> -class define : public Type { -public: - typedef Type msgpack_type; - typedef define<Type> define_type; - - define() {} - define(msgpack_type v) : msgpack_type(v) {} - - template <typename Packer> - void msgpack_pack(Packer& o) const - { - o << static_cast<const msgpack_type&>(*this); - } - - void msgpack_unpack(object o) - { - o >> static_cast<msgpack_type&>(*this); - } -}; - +inline bool operator!=(const object x, const object y) +{ return !(x == y); } inline object::implicit_type object::convert() @@ -179,16 +183,32 @@ template <typename T> inline T object::as() { T v; - msgpack::convert(v, *this); + convert(&v); return v; } template <typename T> -void object::convert(T& v) +inline void object::convert(T* v) +{ + *this >> *v; +} + + +// obsolete +template <typename T> +inline void convert(T& v, object o) +{ + o.convert(&v); +} + +// obsolete +template <typename Stream, typename T> +inline void pack(packer<Stream>& o, const T& v) { - msgpack::convert(v, *this); + o.pack(v); } + template <typename Stream> packer<Stream>& operator<< (packer<Stream>& o, const object& v) { diff --git a/cpp/pack.hpp b/cpp/pack.hpp index c07bcc3..52854ac 100644 --- a/cpp/pack.hpp +++ b/cpp/pack.hpp @@ -1,7 +1,7 @@ // // MessagePack for C++ serializing routine // -// Copyright (C) 2008 FURUHASHI Sadayuki +// 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. @@ -32,70 +32,73 @@ public: packer(Stream& s); public: - 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(unsigned long d) { pack_unsigned_long_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); } + template <typename T> + packer<Stream>& pack(const T& v); + + packer<Stream>& pack_uint8(uint8_t d); + packer<Stream>& pack_uint16(uint16_t d); + packer<Stream>& pack_uint32(uint32_t d); + packer<Stream>& pack_uint64(uint64_t d); + packer<Stream>& pack_int8(uint8_t d); + packer<Stream>& pack_int16(uint16_t d); + packer<Stream>& pack_int32(uint32_t d); + packer<Stream>& pack_int64(uint64_t d); + + packer<Stream>& pack_short(int d); + packer<Stream>& pack_int(int d); + packer<Stream>& pack_long(long d); + packer<Stream>& pack_long_long(long long d); + packer<Stream>& pack_unsigned_short(unsigned short d); + packer<Stream>& pack_unsigned_int(unsigned int d); + packer<Stream>& pack_unsigned_long(unsigned long d); + packer<Stream>& pack_unsigned_long_long(unsigned long long d); + + packer<Stream>& pack_float(float d); + packer<Stream>& pack_double(double d); + + packer<Stream>& pack_nil(); + packer<Stream>& pack_true(); + packer<Stream>& pack_false(); + + packer<Stream>& pack_array(unsigned int n); + + packer<Stream>& pack_map(unsigned int n); + + packer<Stream>& pack_raw(size_t l); + packer<Stream>& pack_raw_body(const char* b, size_t l); private: - 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); - static void pack_uint64_impl(Stream& x, uint64_t d); - static void pack_int8_impl(Stream& x, int8_t d); - 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); + static void _pack_uint8(Stream& x, uint8_t d); + static void _pack_uint16(Stream& x, uint16_t d); + static void _pack_uint32(Stream& x, uint32_t d); + static void _pack_uint64(Stream& x, uint64_t d); + static void _pack_int8(Stream& x, int8_t d); + static void _pack_int16(Stream& x, int16_t d); + static void _pack_int32(Stream& x, int32_t d); + static void _pack_int64(Stream& x, int64_t d); + + static void _pack_short(Stream& x, short d); + static void _pack_int(Stream& x, int d); + static void _pack_long(Stream& x, long d); + static void _pack_long_long(Stream& x, long long d); + static void _pack_unsigned_short(Stream& x, unsigned short d); + static void _pack_unsigned_int(Stream& x, unsigned int d); + static void _pack_unsigned_long(Stream& x, unsigned long d); + static void _pack_unsigned_long_long(Stream& x, unsigned long long d); + + static void _pack_float(Stream& x, float d); + static void _pack_double(Stream& x, double d); + + static void _pack_nil(Stream& x); + static void _pack_true(Stream& x); + static void _pack_false(Stream& x); + + static void _pack_array(Stream& x, unsigned int n); + + static void _pack_map(Stream& x, unsigned int n); + + static void _pack_raw(Stream& x, size_t l); + static void _pack_raw_body(Stream& x, const void* b, size_t l); static void append_buffer(Stream& x, const unsigned char* buf, unsigned int len) { x.write((const char*)buf, len); } @@ -107,13 +110,14 @@ private: packer(); }; + #define msgpack_pack_inline_func(name) \ template <typename Stream> \ - inline void packer<Stream>::pack_ ## name ## _impl + inline void packer<Stream>::_pack ## name #define msgpack_pack_inline_func_cint(name) \ template <typename Stream> \ - inline void packer<Stream>::pack_ ## name ## _impl + inline void packer<Stream>::_pack ## name #define msgpack_pack_user Stream& @@ -125,6 +129,112 @@ private: template <typename Stream> packer<Stream>::packer(Stream& s) : m_stream(s) { } +template <typename Stream> +inline packer<Stream>& packer<Stream>::pack_uint8(uint8_t d) +{ _pack_uint8(m_stream, d); return *this; } + +template <typename Stream> +inline packer<Stream>& packer<Stream>::pack_uint16(uint16_t d) +{ _pack_uint16(m_stream, d); return *this; } + +template <typename Stream> +inline packer<Stream>& packer<Stream>::pack_uint32(uint32_t d) +{ _pack_uint32(m_stream, d); return *this; } + +template <typename Stream> +inline packer<Stream>& packer<Stream>::pack_uint64(uint64_t d) +{ _pack_uint64(m_stream, d); return *this; } + +template <typename Stream> +inline packer<Stream>& packer<Stream>::pack_int8(uint8_t d) +{ _pack_int8(m_stream, d); return *this; } + +template <typename Stream> +inline packer<Stream>& packer<Stream>::pack_int16(uint16_t d) +{ _pack_int16(m_stream, d); return *this; } + +template <typename Stream> +inline packer<Stream>& packer<Stream>::pack_int32(uint32_t d) +{ _pack_int32(m_stream, d); return *this; } + +template <typename Stream> +inline packer<Stream>& packer<Stream>::pack_int64(uint64_t d) +{ _pack_int64(m_stream, d); return *this;} + + +template <typename Stream> +inline packer<Stream>& packer<Stream>::pack_short(int d) +{ _pack_short(m_stream, d); return *this; } + +template <typename Stream> +inline packer<Stream>& packer<Stream>::pack_int(int d) +{ _pack_int(m_stream, d); return *this; } + +template <typename Stream> +inline packer<Stream>& packer<Stream>::pack_long(long d) +{ _pack_long(m_stream, d); return *this; } + +template <typename Stream> +inline packer<Stream>& packer<Stream>::pack_long_long(long long d) +{ _pack_long_long(m_stream, d); return *this; } + +template <typename Stream> +inline packer<Stream>& packer<Stream>::pack_unsigned_short(unsigned short d) +{ _pack_unsigned_short(m_stream, d); return *this; } + +template <typename Stream> +inline packer<Stream>& packer<Stream>::pack_unsigned_int(unsigned int d) +{ _pack_unsigned_int(m_stream, d); return *this; } + +template <typename Stream> +inline packer<Stream>& packer<Stream>::pack_unsigned_long(unsigned long d) +{ _pack_unsigned_long(m_stream, d); return *this; } + +template <typename Stream> +inline packer<Stream>& packer<Stream>::pack_unsigned_long_long(unsigned long long d) +{ _pack_unsigned_long_long(m_stream, d); return *this; } + + +template <typename Stream> +inline packer<Stream>& packer<Stream>::pack_float(float d) +{ _pack_float(m_stream, d); return *this; } + +template <typename Stream> +inline packer<Stream>& packer<Stream>::pack_double(double d) +{ _pack_double(m_stream, d); return *this; } + + +template <typename Stream> +inline packer<Stream>& packer<Stream>::pack_nil() +{ _pack_nil(m_stream); return *this; } + +template <typename Stream> +inline packer<Stream>& packer<Stream>::pack_true() +{ _pack_true(m_stream); return *this; } + +template <typename Stream> +inline packer<Stream>& packer<Stream>::pack_false() +{ _pack_false(m_stream); return *this; } + + +template <typename Stream> +inline packer<Stream>& packer<Stream>::pack_array(unsigned int n) +{ _pack_array(m_stream, n); return *this; } + + +template <typename Stream> +inline packer<Stream>& packer<Stream>::pack_map(unsigned int n) +{ _pack_map(m_stream, n); return *this; } + + +template <typename Stream> +inline packer<Stream>& packer<Stream>::pack_raw(size_t l) +{ _pack_raw(m_stream, l); return *this; } + +template <typename Stream> +inline packer<Stream>& packer<Stream>::pack_raw_body(const char* b, size_t l) +{ _pack_raw_body(m_stream, b, l); return *this; } + } // namespace msgpack diff --git a/cpp/type/array.hpp b/cpp/type/array.hpp index 0522d4c..f9f8038 100644 --- a/cpp/type/array.hpp +++ b/cpp/type/array.hpp @@ -29,11 +29,11 @@ inline std::vector<T> operator>> (object o, std::vector<T>& v) { if(o.type != 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()); + 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); + p->convert(it); } return v; } @@ -45,7 +45,7 @@ inline packer<Stream>& operator<< (packer<Stream>& o, const std::vector<T>& v) o.pack_array(v.size()); for(typename std::vector<T>::const_iterator it(v.begin()), it_end(v.end()); it != it_end; ++it) { - pack(o, *it); + o.pack(*it); } return o; } diff --git a/cpp/type/map.hpp b/cpp/type/map.hpp index c79f31c..619c36e 100644 --- a/cpp/type/map.hpp +++ b/cpp/type/map.hpp @@ -47,12 +47,12 @@ inline type::assoc_vector<K,V>& operator>> (object o, type::assoc_vector<K,V>& v { if(o.type != 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); + 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; + p->convert(&it->first); ++p; + p->convert(&it->second); ++p; } std::sort(v.begin(), v.end(), type::detail::pair_first_less<K,V>()); return v; @@ -64,8 +64,8 @@ inline packer<Stream>& operator<< (packer<Stream>& o, const type::assoc_vector<K o.pack_map(v.size()); for(typename type::assoc_vector<K,V>::const_iterator it(v.begin()), it_end(v.end()); it != it_end; ++it) { - pack(o, it->first); - pack(o, it->second); + o.pack(it->first); + o.pack(it->second); } return o; } @@ -79,14 +79,14 @@ inline std::map<K, V> operator>> (object o, std::map<K, V>& v) object* const pend(o.via.container.ptr + o.via.container.size*2); while(p < pend) { K key; - convert(key, *p); ++p; + p->convert(&key); ++p; typename std::map<K,V>::iterator it(v.find(key)); if(it != v.end()) { V val; - convert(val, *p); ++p; + p->convert(&val); ++p; it->insert( std::pair<K,V>(key, val) ); } else { - convert(it->second, *p); ++p; + p->convert(&it->second); ++p; } } return v; @@ -98,8 +98,8 @@ inline packer<Stream>& operator<< (packer<Stream>& o, const std::map<K,V>& v) 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(o, it->first); - pack(o, it->second); + o.pack(it->first); + o.pack(it->second); } return o; } @@ -109,12 +109,12 @@ template <typename K, typename V> inline std::multimap<K, V> operator>> (object o, std::multimap<K, V>& v) { if(o.type != type::MAP) { throw type_error(); } - object* p(o.via.container.ptr); - object* const pend(o.via.container.ptr + o.via.container.size*2); + 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; + p->convert(&value.first); ++p; + p->convert(&value.second); ++p; v.insert(value); } return v; @@ -126,8 +126,8 @@ inline packer<Stream>& operator<< (packer<Stream>& o, const std::multimap<K,V>& 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(o, it->first); - pack(o, it->second); + o.pack(it->first); + o.pack(it->second); } return o; } diff --git a/cpp/type/tuple.hpp.erb b/cpp/type/tuple.hpp.erb index 13d4bd7..a66c57e 100644 --- a/cpp/type/tuple.hpp.erb +++ b/cpp/type/tuple.hpp.erb @@ -111,7 +111,7 @@ struct tuple<A0<%1.upto(i) {|j|%>, A<%=j%><%}%>> { tuple() {} tuple(typename tuple_type<A0>::transparent_reference _a0<%1.upto(i) {|j|%>, typename tuple_type<A<%=j%>>::transparent_reference _a<%=j%><%}%>) : a0(_a0)<%1.upto(i) {|j|%>, a<%=j%>(_a<%=j%>)<%}%> {} - tuple(object o) { convert(*this, o); } + tuple(object o) { o.convert(this); } template <int N> typename tuple_element<value_type, N>::reference get() { return tuple_element<value_type, N>(*this).get(); } template <int N> typename const_tuple_element<value_type, N>::const_reference get() const @@ -138,7 +138,7 @@ type::tuple<A0<%1.upto(i) {|j|%>, A<%=j%><%}%>>& operator>> ( if(o.type != 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%>]);<%}%> + o.via.container.ptr[<%=j%>].convert<A<%=j%>>(&v.template get<<%=j%>>());<%}%> return v; } <%}%> @@ -158,7 +158,7 @@ const packer<Stream>& operator<< ( const type::tuple<A0<%1.upto(i) {|j|%>, A<%=j%><%}%>>& v) { o.pack_array(<%=i+1%>); <%0.upto(i) {|j|%> - pack(o, v.template get<<%=j%>>());<%}%> + o.pack(v.template get<<%=j%>>());<%}%> return o; } <%}%> diff --git a/cpp/unpack.cpp b/cpp/unpack.cpp index f2ef330..62088d6 100644 --- a/cpp/unpack.cpp +++ b/cpp/unpack.cpp @@ -1,7 +1,7 @@ // // MessagePack for C++ deserializing routine // -// Copyright (C) 2008 FURUHASHI Sadayuki +// 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. @@ -23,30 +23,25 @@ namespace msgpack { //namespace { -struct allocator { +struct unpack_user { zone* z; bool referenced; - - inline object* malloc_object(size_t n) - { - return (object*)z->malloc(sizeof(object)*n); - } }; //} // noname namespace #define msgpack_unpack_struct(name) \ - struct msgpack_unpacker_##name + struct msgpack_unpacker ## name #define msgpack_unpack_func(ret, name) \ - ret msgpack_unpacker_##name + ret msgpack_unpacker ## name #define msgpack_unpack_callback(name) \ - msgpack_unpack_##name + msgpack_unpack ## name #define msgpack_unpack_object object -#define msgpack_unpack_user allocator +#define msgpack_unpack_user unpack_user struct msgpack_unpacker_context; @@ -59,87 +54,87 @@ static int msgpack_unpacker_execute(struct msgpack_unpacker_context* ctx, const char* data, size_t len, size_t* off); -static inline object msgpack_unpack_init(allocator* a) +static inline object msgpack_unpack_init(unpack_user* u) { return object(); } -static inline object msgpack_unpack_uint8(allocator* a, uint8_t d) +static inline object msgpack_unpack_uint8(unpack_user* u, uint8_t d) { object o; o.type = type::POSITIVE_INTEGER; o.via.u64 = d; return o; } -static inline object msgpack_unpack_uint16(allocator* a, uint16_t d) +static inline object msgpack_unpack_uint16(unpack_user* u, uint16_t d) { object o; o.type = type::POSITIVE_INTEGER; o.via.u64 = d; return o; } -static inline object msgpack_unpack_uint32(allocator* a, uint32_t d) +static inline object msgpack_unpack_uint32(unpack_user* u, uint32_t d) { object o; o.type = type::POSITIVE_INTEGER; o.via.u64 = d; return o; } -static inline object msgpack_unpack_uint64(allocator* a, uint64_t d) +static inline object msgpack_unpack_uint64(unpack_user* u, uint64_t d) { object o; o.type = type::POSITIVE_INTEGER; o.via.u64 = d; return o; } -static inline object msgpack_unpack_int8(allocator* a, int8_t d) +static inline object msgpack_unpack_int8(unpack_user* u, int8_t d) { if(d >= 0) { object o; o.type = type::POSITIVE_INTEGER; o.via.u64 = d; return o; } else { object o; o.type = type::NEGATIVE_INTEGER; o.via.i64 = d; return o; } } -static inline object msgpack_unpack_int16(allocator* a, int16_t d) +static inline object msgpack_unpack_int16(unpack_user* u, int16_t d) { if(d >= 0) { object o; o.type = type::POSITIVE_INTEGER; o.via.u64 = d; return o; } else { object o; o.type = type::NEGATIVE_INTEGER; o.via.i64 = d; return o; } } -static inline object msgpack_unpack_int32(allocator* a, int32_t d) +static inline object msgpack_unpack_int32(unpack_user* u, int32_t d) { if(d >= 0) { object o; o.type = type::POSITIVE_INTEGER; o.via.u64 = d; return o; } else { object o; o.type = type::NEGATIVE_INTEGER; o.via.i64 = d; return o; } } -static inline object msgpack_unpack_int64(allocator* a, int64_t d) +static inline object msgpack_unpack_int64(unpack_user* u, int64_t d) { if(d >= 0) { object o; o.type = type::POSITIVE_INTEGER; o.via.u64 = d; return o; } else { object o; o.type = type::NEGATIVE_INTEGER; o.via.i64 = d; return o; } } -static inline object msgpack_unpack_float(allocator* a, float d) +static inline object msgpack_unpack_float(unpack_user* u, float d) { object o; o.type = type::DOUBLE; o.via.dec = d; return o; } -static inline object msgpack_unpack_double(allocator* a, double d) +static inline object msgpack_unpack_double(unpack_user* u, double d) { object o; o.type = type::DOUBLE; o.via.dec = d; return o; } -static inline object msgpack_unpack_nil(allocator* a) +static inline object msgpack_unpack_nil(unpack_user* u) { object o; o.type = type::NIL; return o; } -static inline object msgpack_unpack_true(allocator* a) +static inline object msgpack_unpack_true(unpack_user* u) { object o; o.type = type::BOOLEAN; o.via.boolean = true; return o; } -static inline object msgpack_unpack_false(allocator* a) +static inline object msgpack_unpack_false(unpack_user* u) { object o; o.type = type::BOOLEAN; o.via.boolean = false; return o; } -static inline object msgpack_unpack_array(allocator* a, unsigned int n) +static inline object msgpack_unpack_array(unpack_user* u, unsigned int n) { object o; o.type = type::ARRAY; o.via.container.size = 0; - o.via.container.ptr = a->malloc_object(n); + o.via.container.ptr = (object*)u->z->malloc(n*sizeof(object)); return o; } -static inline void msgpack_unpack_array_item(allocator* a, object* c, object o) +static inline void msgpack_unpack_array_item(unpack_user* u, object* c, object o) { c->via.container.ptr[ c->via.container.size++ ] = o; } -static inline object msgpack_unpack_map(allocator* a, unsigned int n) +static inline object msgpack_unpack_map(unpack_user* u, unsigned int n) { object o; o.type = type::MAP; o.via.container.size = 0; - o.via.container.ptr = a->malloc_object(n*2); + o.via.container.ptr = (object*)u->z->malloc(n*2*sizeof(object)); return o; } -static inline void msgpack_unpack_map_item(allocator* a, object* c, object k, object v) +static inline void msgpack_unpack_map_item(unpack_user* u, object* c, object k, object v) { c->via.container.ptr[ c->via.container.size ] = k; c->via.container.ptr[ c->via.container.size+1 ] = v; ++c->via.container.size; } -static inline object msgpack_unpack_raw(allocator* a, const char* b, const char* p, unsigned int l) +static inline object msgpack_unpack_raw(unpack_user* u, const char* b, const char* p, unsigned int l) { object o; o.type = type::RAW; o.via.ref.ptr = p; o.via.ref.size = l; - a->referenced = true; + u->referenced = true; return o; } @@ -151,8 +146,8 @@ struct context { context() { msgpack_unpacker_init(&m_ctx); - allocator a = {NULL, false}; - m_ctx.user = a; + unpack_user u = {NULL, false}; + m_ctx.user = u; } ~context() { } @@ -171,8 +166,8 @@ struct context { { zone* z = m_ctx.user.z; msgpack_unpacker_init(&m_ctx); - allocator a = {z, false}; - m_ctx.user = a; + unpack_user u = {z, false}; + m_ctx.user = u; } void set_zone(zone* z) @@ -193,7 +188,7 @@ private: context(const context&); }; -context* as_ctx(void* m) +static inline context* as_ctx(void* m) { return reinterpret_cast<context*>(m); } diff --git a/cpp/unpack.hpp b/cpp/unpack.hpp index 293a5d3..cde45e7 100644 --- a/cpp/unpack.hpp +++ b/cpp/unpack.hpp @@ -1,7 +1,7 @@ // // MessagePack for C++ deserializing routine // -// Copyright (C) 2008 FURUHASHI Sadayuki +// 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. diff --git a/cpp/zone.cpp b/cpp/zone.cpp index 7c5a1c9..f765266 100644 --- a/cpp/zone.cpp +++ b/cpp/zone.cpp @@ -1,7 +1,7 @@ // // MessagePack for C++ memory pool // -// Copyright (C) 2008 FURUHASHI Sadayuki +// 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. diff --git a/cpp/zone.hpp.erb b/cpp/zone.hpp.erb index 9a8c81a..c0eb2e3 100644 --- a/cpp/zone.hpp.erb +++ b/cpp/zone.hpp.erb @@ -1,7 +1,7 @@ // // MessagePack for C++ memory pool // -// Copyright (C) 2008 FURUHASHI Sadayuki +// 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. |