From 2c7f0b2b1aa78b28916c40171dc8fee07d414d64 Mon Sep 17 00:00:00 2001 From: frsyuki Date: Sun, 15 Feb 2009 09:09:58 +0000 Subject: [PATCH] lang/c/msgpack: C++ binding: reexamined global operators git-svn-id: file:///Users/frsyuki/project/msgpack-git/svn/x@72 5a5092ae-2292-43ba-b2d5-dcab9c1a2731 --- cpp/object.hpp | 78 +++++++++++--------- cpp/type/array.hpp | 10 +-- cpp/type/boolean.hpp | 6 +- cpp/type/float.hpp | 18 ++--- cpp/type/integer.hpp | 162 ++++++++++++++++++++--------------------- cpp/type/map.hpp | 32 ++++---- cpp/type/nil.hpp | 14 ++-- cpp/type/raw.hpp | 24 +++--- cpp/type/tuple.hpp.erb | 99 ++++++++++++++----------- cpp/unpack.hpp | 7 ++ 10 files changed, 241 insertions(+), 209 deletions(-) diff --git a/cpp/object.hpp b/cpp/object.hpp index a5e86829..8cce14cd 100644 --- a/cpp/object.hpp +++ b/cpp/object.hpp @@ -31,6 +31,18 @@ namespace msgpack { 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; +} + + struct object { unsigned char type; union { @@ -50,6 +62,11 @@ struct object { template operator T() { T v; convert(v, *this); return v; }; + + template + T as() { T v; convert(v, *this); return v; } + + bool is_nil() { return type == type::NIL; } }; std::ostream& operator<< (std::ostream& s, const object o); @@ -58,53 +75,36 @@ bool operator==(const object x, const object y); inline bool operator!=(const object x, const object y) { return !(x == y); } -inline object& operator<< (object& v, object o) +inline object& operator>> (object o, object& v) { v = o; return v; } template -const object& operator>> (const object& v, packer& o); +packer& operator<< (packer& o, const object& v); + 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; - - template - inline T& operator<< (T& v, object o) + inline T& operator>> (object o, T& v) { v.msgpack_unpack(o); return v; } - - - namespace detail { - template - inline void pack_copy(T v, packer& o) - { pack(v, o); } - } - + template - inline const T& operator>> (const T& v, packer& o) + inline packer& operator<< (packer& o, const T& v) { - detail::pack_copy(v.msgpack_pack(), o); - return v; + pack_copy(v.msgpack_pack(), o); + return o; } - -} // namespace type +} template inline void convert(T& v, object o) { using namespace type; - v << o; + o >> v; } @@ -112,7 +112,7 @@ template inline void pack(T& v, packer& o) { using namespace type; - v >> o; + o << v; } @@ -124,14 +124,20 @@ inline void pack(T& v, Stream& s) } +template +inline void pack_copy(T v, packer& o) +{ + pack(v, o); +} + template -const object& operator>> (const object& v, packer& o) +packer& operator<< (packer& o, const object& v) { switch(v.type) { case type::NIL: o.pack_nil(); - return v; + return o; case type::BOOLEAN: if(v.via.boolean) { @@ -139,7 +145,7 @@ const object& operator>> (const object& v, packer& o) } else { o.pack_false(); } - return v; + return o; case type::POSITIVE_INTEGER: if(v.via.u64 <= (uint64_t)std::numeric_limits::max()) { @@ -155,7 +161,7 @@ const object& operator>> (const object& v, packer& o) o.pack_uint64(v.via.u64); } } - return v; + return o; case type::NEGATIVE_INTEGER: if(v.via.i64 >= (int64_t)std::numeric_limits::min()) { @@ -171,11 +177,11 @@ const object& operator>> (const object& v, packer& o) o.pack_int64(v.via.i64); } } - return v; + return o; case type::RAW: o.pack_raw(v.via.ref.ptr, v.via.ref.size); - return v; + return o; case type::ARRAY: o.pack_array(v.via.container.size); @@ -184,7 +190,7 @@ const object& operator>> (const object& v, packer& o) p < pend; ++p) { *p >> o; } - return v; + return o; // FIXME loop optimiziation case type::MAP: @@ -195,7 +201,7 @@ const object& operator>> (const object& v, packer& o) *p >> o; ++p; *p >> o; ++p; } - return v; + return o; // FIXME loop optimiziation default: diff --git a/cpp/type/array.hpp b/cpp/type/array.hpp index 70667030..703ac55c 100644 --- a/cpp/type/array.hpp +++ b/cpp/type/array.hpp @@ -22,13 +22,12 @@ #include namespace msgpack { -namespace type { template -inline std::vector operator<< (std::vector& v, object o) +inline std::vector operator>> (object o, std::vector& v) { - if(o.type != ARRAY) { throw type_error(); } + 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); @@ -41,18 +40,17 @@ inline std::vector operator<< (std::vector& v, object o) template -inline const std::vector& operator>> (const std::vector& v, packer& o) +inline packer& operator<< (packer& o, const std::vector& v) { o.pack_array(v.size()); for(typename std::vector::const_iterator it(v.begin()), it_end(v.end()); it != it_end; ++it) { pack(*it, o); } - return v; + return o; } -} // namespace type } // namespace msgpack #endif /* msgpack/type/array.hpp */ diff --git a/cpp/type/boolean.hpp b/cpp/type/boolean.hpp index 0538495e..e9584789 100644 --- a/cpp/type/boolean.hpp +++ b/cpp/type/boolean.hpp @@ -25,7 +25,7 @@ namespace msgpack { namespace type { -inline bool& operator<< (bool& v, object o) +inline bool& operator>> (object o, bool& v) { if(o.type != BOOLEAN) { throw type_error(); } v = o.via.boolean; @@ -34,11 +34,11 @@ inline bool& operator<< (bool& v, object o) template -inline const bool& operator>> (const bool& v, packer o) +inline packer& operator<< (packer& o, const bool& v) { if(v) { o.pack_true(); } else { o.pack_false(); } - return v; + return o; } diff --git a/cpp/type/float.hpp b/cpp/type/float.hpp index 11fa2803..21784346 100644 --- a/cpp/type/float.hpp +++ b/cpp/type/float.hpp @@ -22,45 +22,43 @@ #include namespace msgpack { -namespace type { // FIXME check overflow, underflow -inline float& operator<< (float& v, object o) +inline float& operator>> (object o, float& v) { - if(o.type != DOUBLE) { throw type_error(); } + if(o.type != type::DOUBLE) { throw type_error(); } v = o.via.dec; return v; } template -inline const float& operator>> (const float& v, packer o) +inline packer& operator<< (packer& o, const float& v) { o.pack_float(v); - return v; + return o; } -inline double& operator<< (double& v, object o) +inline double& operator>> (object o, double& v) { - if(o.type != DOUBLE) { throw type_error(); } + if(o.type != type::DOUBLE) { throw type_error(); } v = o.via.dec; return v; } template -inline const double& operator>> (const double& v, packer o) +inline packer& operator<< (packer& o, const double& v) { o.pack_double(v); - return v; + return o; } -} // namespace type } // namespace msgpack #endif /* msgpack/type/float.hpp */ diff --git a/cpp/type/integer.hpp b/cpp/type/integer.hpp index 488c9d47..f5841a66 100644 --- a/cpp/type/integer.hpp +++ b/cpp/type/integer.hpp @@ -22,9 +22,9 @@ #include namespace msgpack { + + namespace type { - - namespace detail { template struct convert_integer_sign; @@ -32,11 +32,11 @@ namespace detail { template struct convert_integer_sign { static inline T convert(object o) { - if(o.type == POSITIVE_INTEGER) { + if(o.type == type::POSITIVE_INTEGER) { if(o.via.u64 > (uint64_t)std::numeric_limits::max()) { throw type_error(); } return o.via.u64; - } else if(o.type == NEGATIVE_INTEGER) { + } else if(o.type == type::NEGATIVE_INTEGER) { if(o.via.i64 < (int64_t)-std::numeric_limits::max()) { throw type_error(); } return o.via.i64; @@ -48,7 +48,7 @@ namespace detail { template struct convert_integer_sign { static inline T convert(object o) { - if(o.type == POSITIVE_INTEGER) { + if(o.type == type::POSITIVE_INTEGER) { if(o.via.u64 > (uint64_t)std::numeric_limits::max()) { throw type_error(); } return o.via.u64; @@ -160,83 +160,83 @@ namespace detail { } } // namespace detail - - -inline signed char& operator<< (signed char& v, object o) - { v = detail::convert_integer(o); return v; } - -inline signed short& operator<< (signed short& v, object o) - { v = detail::convert_integer(o); return v; } - -inline signed int& operator<< (signed int& v, object o) - { v = detail::convert_integer(o); return v; } - -inline signed long& operator<< (signed long& v, object o) - { v = detail::convert_integer(o); return v; } - -inline signed long long& operator<< (signed long long& v, object o) - { v = detail::convert_integer(o); return v; } - - -inline unsigned char& operator<< (unsigned char& v, object o) - { v = detail::convert_integer(o); return v; } - -inline unsigned short& operator<< (unsigned short& v, object o) - { v = detail::convert_integer(o); return v; } - -inline unsigned int& operator<< (unsigned int& v, object o) - { v = detail::convert_integer(o); return v; } - -inline unsigned long& operator<< (unsigned long& v, object o) - { v = detail::convert_integer(o); return v; } - -inline unsigned long long& operator<< (unsigned long long& v, object o) - { v = detail::convert_integer(o); return v; } - - -template -inline const signed char& operator>> (const signed char& v, packer o) - { detail::pack_integer(v, o); return v; } - -template -inline const signed short& operator>> (const signed short& v, packer o) - { detail::pack_integer(v, o); return v; } - -template -inline const signed int& operator>> (const signed int& v, packer o) - { detail::pack_integer(v, o); return v; } - -template -inline const signed long& operator>> (const signed long& v, packer o) - { detail::pack_integer(v, o); return v; } - -template -inline const signed long long& operator>> (const signed long long& v, packer o) - { detail::pack_integer(v, o); return v; } - - -template -inline const unsigned char& operator>> (const unsigned char& v, packer o) - { detail::pack_integer(v, o); return v; } - -template -inline const unsigned short& operator>> (const unsigned short& v, packer o) - { detail::pack_integer(v, o); return v; } - -template -inline const unsigned int& operator>> (const unsigned int& v, packer o) - { detail::pack_integer(v, o); return v; } - -template -inline const unsigned long& operator>> (const unsigned long& v, packer o) - { detail::pack_integer(v, o); return v; } - -template -inline const unsigned long long& operator>> (const unsigned long long& v, packer o) - { detail::pack_integer(v, o); return v; } - - } // namespace type + + +inline signed char& operator>> (object o, signed char& v) + { v = type::detail::convert_integer(o); return v; } + +inline signed short& operator>> (object o, signed short& v) + { v = type::detail::convert_integer(o); return v; } + +inline signed int& operator>> (object o, signed int& v) + { v = type::detail::convert_integer(o); return v; } + +inline signed long& operator>> (object o, signed long& v) + { v = type::detail::convert_integer(o); return v; } + +inline signed long long& operator>> (object o, signed long long& v) + { v = type::detail::convert_integer(o); return v; } + + +inline unsigned char& operator>> (object o, unsigned char& v) + { v = type::detail::convert_integer(o); return v; } + +inline unsigned short& operator>> (object o, unsigned short& v) + { v = type::detail::convert_integer(o); return v; } + +inline unsigned int& operator>> (object o, unsigned int& v) + { v = type::detail::convert_integer(o); return v; } + +inline unsigned long& operator>> (object o, unsigned long& v) + { v = type::detail::convert_integer(o); return v; } + +inline unsigned long long& operator>> (object o, unsigned long long& v) + { v = type::detail::convert_integer(o); return v; } + + +template +inline packer& operator<< (packer& o, const signed char& v) + { type::detail::pack_integer(v, o); return o; } + +template +inline packer& operator<< (packer& o, const signed short& v) + { type::detail::pack_integer(v, o); return o; } + +template +inline packer& operator<< (packer& o, const signed int& v) + { type::detail::pack_integer(v, o); return o; } + +template +inline packer& operator<< (packer& o, const signed long& v) + { type::detail::pack_integer(v, o); return o; } + +template +inline packer& operator<< (packer& o, const signed long long& v) + { type::detail::pack_integer(v, o); return o; } + + +template +inline packer& operator<< (packer& o, const unsigned char& v) + { type::detail::pack_integer(v, o); return o; } + +template +inline packer& operator<< (packer& o, const unsigned short& v) + { type::detail::pack_integer(v, o); return o; } + +template +inline packer& operator<< (packer& o, const unsigned int& v) + { type::detail::pack_integer(v, o); return o; } + +template +inline packer& operator<< (packer& o, const unsigned long& v) + { type::detail::pack_integer(v, o); return o; } + +template +inline packer& operator<< (packer& o, const unsigned long long& v) + { type::detail::pack_integer(v, o); return o; } + + } // namespace msgpack #endif /* msgpack/type/integer.hpp */ diff --git a/cpp/type/map.hpp b/cpp/type/map.hpp index 1efecece..1d5e054b 100644 --- a/cpp/type/map.hpp +++ b/cpp/type/map.hpp @@ -23,9 +23,10 @@ #include namespace msgpack { -namespace type { +namespace type { + template class assoc_vector : public std::vector< std::pair > {}; @@ -37,10 +38,13 @@ namespace detail { }; } +} //namespace type + + template -inline assoc_vector& operator<< (assoc_vector& v, object o) +inline type::assoc_vector& operator>> (object o, type::assoc_vector& v) { - if(o.type != MAP) { throw type_error(); } + 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); @@ -49,26 +53,27 @@ inline assoc_vector& operator<< (assoc_vector& v, object o) convert(it->first, *p); ++p; convert(it->second, *p); ++p; } - std::sort(v.begin(), v.end(), detail::pair_first_less()); + std::sort(v.begin(), v.end(), type::detail::pair_first_less()); return v; } template -inline const assoc_vector& operator>> (const assoc_vector& v, packer& o) +inline packer& operator<< (packer& o, const type::assoc_vector& v) { o.pack_map(v.size()); - for(typename assoc_vector::const_iterator it(v.begin()), it_end(v.end()); + for(typename type::assoc_vector::const_iterator it(v.begin()), it_end(v.end()); it != it_end; ++it) { pack(it->first, o); pack(it->second, o); } + return o; } template -inline std::map operator<< (std::map& v, object o) +inline std::map operator>> (object o, std::map& v) { - if(o.type != MAP) { throw type_error(); } + 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); while(p < pend) { @@ -87,7 +92,7 @@ inline std::map operator<< (std::map& v, object o) } template -inline const std::map& operator>> (const std::map& v, packer& o) +inline packer& operator<< (packer& o, const std::map& v) { o.pack_map(v.size()); for(typename std::map::const_iterator it(v.begin()), it_end(v.end()); @@ -95,13 +100,14 @@ inline const std::map& operator>> (const std::map& v, packer& pack(it->first, o); pack(it->second, o); } + return o; } template -inline std::multimap operator<< (std::multimap& v, object o) +inline std::multimap operator>> (object o, std::multimap& v) { - if(o.type != MAP) { throw type_error(); } + 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); while(p < pend) { @@ -114,7 +120,7 @@ inline std::multimap operator<< (std::multimap& v, object o) } template -inline const std::multimap& operator>> (const std::multimap& v, packer& o) +inline packer& operator<< (packer& o, const std::multimap& v) { o.pack_multimap(v.size()); for(typename std::multimap::const_iterator it(v.begin()), it_end(v.end()); @@ -122,10 +128,10 @@ inline const std::multimap& operator>> (const std::multimap& v, packer pack(it->first, o); pack(it->second, o); } + return o; } -} // namespace type } // namespace msgpack #endif /* msgpack/type/map.hpp */ diff --git a/cpp/type/nil.hpp b/cpp/type/nil.hpp index 73cedb49..ab0c3638 100644 --- a/cpp/type/nil.hpp +++ b/cpp/type/nil.hpp @@ -21,26 +21,28 @@ #include "msgpack/object.hpp" namespace msgpack { -namespace type { +namespace type { struct nil { }; -inline nil& operator<< (nil& v, object o) +} // namespace type + + +inline type::nil& operator>> (object o, type::nil& v) { - if(o.type != NIL) { throw type_error(); } + if(o.type != type::NIL) { throw type_error(); } return v; } template -inline const nil& operator>> (const nil& v, packer& o) +inline packer& operator<< (packer& o, const type::nil& v) { o.pack_nil(); - return v; + return o; } -} // namespace type } // namespace msgpack #endif /* msgpack/type/nil.hpp */ diff --git a/cpp/type/raw.hpp b/cpp/type/raw.hpp index 87672384..14e52f40 100644 --- a/cpp/type/raw.hpp +++ b/cpp/type/raw.hpp @@ -23,8 +23,8 @@ #include namespace msgpack { -namespace type { +namespace type { struct raw_ref { raw_ref() : ptr(NULL), size(0) {} @@ -58,41 +58,43 @@ struct raw_ref { } }; -inline raw_ref& operator<< (raw_ref& v, object o) +} // namespace type + + +inline type::raw_ref& operator>> (object o, type::raw_ref& v) { - if(o.type != RAW) { throw type_error(); } + if(o.type != 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) +inline std::string& operator>> (object o, std::string& v) { - raw_ref r; - r << o; + type::raw_ref r; + o >> r; v.assign(r.ptr, r.size); return v; } template -inline const raw_ref& operator>> (const raw_ref& v, packer& o) +inline packer& operator<< (packer& o, const type::raw_ref& v) { o.pack_raw(v.ptr, v.size); - return v; + return o; } template -inline const std::string& operator>> (const std::string& v, packer& o) +inline packer& operator<< (packer& o, const std::string& v) { o.pack_raw(v.data(), v.size()); - return v; + return o; } -} // namespace type } // namespace msgpack #endif /* msgpack/type/raw.hpp */ diff --git a/cpp/type/tuple.hpp.erb b/cpp/type/tuple.hpp.erb index 2a8a2dd6..c54739ae 100644 --- a/cpp/type/tuple.hpp.erb +++ b/cpp/type/tuple.hpp.erb @@ -21,15 +21,14 @@ #include "msgpack/object.hpp" namespace msgpack { -namespace type { +namespace type { // FIXME operator== // FIXME operator!= - <% GENERATION_LIMIT = 15 %> -template < typename A0 <%1.upto(GENERATION_LIMIT+1) {|i|%>, typename A<%=i%> = void<%}%> > +template , typename A<%=i%> = void<%}%>> struct tuple; template @@ -60,9 +59,9 @@ private: <%0.upto(GENERATION_LIMIT) {|i|%> <%0.upto(i) {|j|%> -template < typename A0 <%1.upto(i) {|k|%>, typename A<%=k%> <%}%>> -struct tuple_type, A<%=k%> <%}%>>, <%=j%>> : tuple_element> { - tuple_type(tuple, A<%=k%> <%}%>>& x) : tuple_element>(x.a<%=j%>) {} +template , typename A<%=k%><%}%>> +struct tuple_type, A<%=k%><%}%>>, <%=j%>> : tuple_element> { + tuple_type(tuple, A<%=k%> <%}%>>& x) : tuple_element>(x.a<%=j%>) {} }; <%}%> <%}%> @@ -70,46 +69,57 @@ struct tuple_type, A<%=k%> <%}%>>, <%=j%>> : tuple_e <%0.upto(GENERATION_LIMIT) {|i|%> <%0.upto(i) {|j|%> -template < typename A0 <%1.upto(i) {|k|%>, typename A<%=k%> <%}%>> -struct const_tuple_type, A<%=k%> <%}%>>, <%=j%>> : const_tuple_element> { - const_tuple_type(const tuple, A<%=k%> <%}%>>& x) : const_tuple_element>(x.a<%=j%>) {} +template , typename A<%=k%><%}%>> +struct const_tuple_type, A<%=k%><%}%>>, <%=j%>> : const_tuple_element> { + const_tuple_type(const tuple, A<%=k%><%}%>>& x) : const_tuple_element>(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%><%}%>) +template , typename A<%=j%><%}%>> +tuple, 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%><%}%>); + return tuple, A<%=j%><%}%>>(a0<%1.upto(i) {|j|%>, a<%=j%><%}%>); } <%}%> +template <> +struct tuple<> { +}; <%0.upto(GENERATION_LIMIT) {|i|%> -template < typename A0 <%1.upto(i) {|j|%>, typename A<%=j%> <%}%>> -struct tuple, A<%=j%> <%}%>> { +template , typename A<%=j%><%}%>> +struct tuple, A<%=j%><%}%>> { tuple() {} - tuple(const A0& _a0 <%1.upto(i) {|j|%>, const A<%=j%>& _a<%=j%><%}%>) : + 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 typename tuple_type, A<%=j%> <%}%>>, N>::type& get() - { return tuple_type, A<%=j%> <%}%>>, N>(*this).get(); } - template const typename const_tuple_type, A<%=j%> <%}%>>, N>::type& get() const - { return const_tuple_type, A<%=j%> <%}%>>, N>(*this).get(); } + template typename tuple_type, A<%=j%><%}%>>, N>::type& get() + { return tuple_type, A<%=j%><%}%>>, N>(*this).get(); } + template const typename const_tuple_type, A<%=j%><%}%>>, N>::type& get() const + { return const_tuple_type, A<%=j%><%}%>>, N>(*this).get(); } <%0.upto(i) {|j|%> A<%=j%> a<%=j%>;<%}%> }; <%}%> +} // namespace type + +inline type::tuple<>& operator>> ( + object o, + type::tuple<>& v) { + if(o.type != type::ARRAY) { throw type_error(); } + return v; +} <%0.upto(GENERATION_LIMIT) {|i|%> -template < typename A0 <%1.upto(i) {|j|%>, typename A<%=j%> <%}%>> -tuple, A<%=j%> <%}%>>& operator<< ( - tuple, A<%=j%> <%}%>>& v, - object o) { - if(o.type != ARRAY) { throw type_error(); } +template , typename A<%=j%><%}%>> +type::tuple, A<%=j%><%}%>>& operator>> ( + object o, + type::tuple, A<%=j%><%}%>>& v) { + if(o.type != type::ARRAY) { throw type_error(); } if(o.via.container.size < <%=i+1%>) { throw type_error(); } <%0.upto(i) {|j|%> convert>(v.template get<<%=j%>>(), o.via.container.ptr[<%=j%>]);<%}%> @@ -118,6 +128,26 @@ tuple, A<%=j%> <%}%>>& operator<< ( <%}%> +template +const packer& operator<< ( + packer& o, + const type::tuple<>& v) { + o.pack_array(0); + return o; +} +<%0.upto(GENERATION_LIMIT) {|i|%> +template , typename A<%=j%><%}%>> +const packer& operator<< ( + packer& o, + const type::tuple, A<%=j%><%}%>>& v) { + o.pack_array(<%=i+1%>); + <%0.upto(i) {|j|%> + pack(v.template get<<%=j%>>(), o);<%}%> + return o; +} +<%}%> + + // FIXME /* template @@ -128,7 +158,7 @@ struct tuple_just { A0 a0; static inline void convert(object o, tuple_just& v) { - if(o.type != ARRAY) { throw type_error(); } + if(o.type != type::ARRAY) { throw type_error(); } if(o.v.container.size != 1) { throw type_error(); } msgpack::convert(o.v.container.ptr[0], v.a0); } @@ -140,7 +170,7 @@ struct tuple_just { A1 a1; static inline void convert(object o, tuple_just& v) { - if(o.type != ARRAY) { throw type_error(); } + if(o.type != type::ARRAY) { throw type_error(); } if(o.v.container.size != 2) { throw type_error(); } msgpack::convert(o.v.container.ptr[0], v.a0); msgpack::convert(o.v.container.ptr[1], v.a1); @@ -149,24 +179,7 @@ struct tuple_just { */ - -<%0.upto(GENERATION_LIMIT) {|i|%> -template < typename Stream , typename A0 <%1.upto(i) {|j|%>, typename A<%=j%> <%}%>> -const tuple, A<%=j%> <%}%>>& operator>> ( - const tuple, A<%=j%> <%}%>>& v, - packer 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 */ - diff --git a/cpp/unpack.hpp b/cpp/unpack.hpp index 07c52e7f..de613a45 100644 --- a/cpp/unpack.hpp +++ b/cpp/unpack.hpp @@ -124,6 +124,10 @@ public: // Note that reset() leaves non-parsed buffer. void remove_nonparsed_buffer(); + /*! skip specified size of non-parsed buffer, leaving the buffer */ + // Note the size must be smaller than nonparsed_size() + void skip_nonparsed_buffer(size_t len); + private: char* m_buffer; size_t m_used; @@ -177,6 +181,9 @@ inline size_t unpacker::parsed_size() const inline void unpacker::remove_nonparsed_buffer() { m_used = m_off; } +inline void unpacker::skip_nonparsed_buffer(size_t len) + { m_off += len; } + inline object unpack(const char* data, size_t len, zone& z, size_t* off = NULL) {