From 921b0ff62ec99e9339d6cfaa6ba724ae7bb5f9ce 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: pack() git-svn-id: file:///Users/frsyuki/project/msgpack-git/svn/x@73 5a5092ae-2292-43ba-b2d5-dcab9c1a2731 --- cpp/object.hpp | 41 +++++++++++++++++------------------------ cpp/test.cpp | 4 ++-- cpp/type/array.hpp | 2 +- cpp/type/boolean.hpp | 4 +--- cpp/type/integer.hpp | 18 +++++++++--------- cpp/type/map.hpp | 12 ++++++------ cpp/type/tuple.hpp.erb | 2 +- cpp/unpack.hpp | 2 +- 8 files changed, 38 insertions(+), 47 deletions(-) diff --git a/cpp/object.hpp b/cpp/object.hpp index 8cce14cd..77d55bbc 100644 --- a/cpp/object.hpp +++ b/cpp/object.hpp @@ -83,51 +83,44 @@ packer& operator<< (packer& o, const object& v); -namespace type { - template - inline T& operator>> (object o, T& v) - { - v.msgpack_unpack(o); - return v; - } - - template - inline packer& operator<< (packer& o, const T& v) - { - pack_copy(v.msgpack_pack(), o); - return o; - } +template +inline T& operator>> (object o, T& v) +{ + v.msgpack_unpack(o); + return v; +} + +template +inline packer& operator<< (packer& o, const T& v) +{ + o << v.msgpack_pack(); + return o; } template inline void convert(T& v, object o) { - using namespace type; o >> v; } - template -inline void pack(T& v, packer& o) +inline void pack(packer& o, const T& v) { - using namespace type; o << v; } - template -inline void pack(T& v, Stream& s) +inline void pack(Stream& s, const T& v) { packer pk(s); - pack(v, pk); + pack(pk, v); } - template -inline void pack_copy(T v, packer& o) +inline void pack_copy(packer& o, T v) { - pack(v, o); + pack(o, v); } diff --git a/cpp/test.cpp b/cpp/test.cpp index c632ec51..02d461c4 100644 --- a/cpp/test.cpp +++ b/cpp/test.cpp @@ -26,7 +26,7 @@ public: try { std::stringstream s; - pack(should, s); + pack(s, should); std::string str(s.str()); object ro = unpack(str.data(), str.size(), m_zone); std::cout << ro << std::endl; @@ -139,7 +139,7 @@ int main(void) // send message { for(unsigned i=0; i < TASK_REPEAT; ++i) { - pack(task, stream); + pack(stream, task); } std::cout << "send " << stream.str().size() << " bytes" << std::endl; } diff --git a/cpp/type/array.hpp b/cpp/type/array.hpp index 703ac55c..0522d4ca 100644 --- a/cpp/type/array.hpp +++ b/cpp/type/array.hpp @@ -45,7 +45,7 @@ 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); + pack(o, *it); } return o; } diff --git a/cpp/type/boolean.hpp b/cpp/type/boolean.hpp index e9584789..60f17141 100644 --- a/cpp/type/boolean.hpp +++ b/cpp/type/boolean.hpp @@ -22,12 +22,11 @@ #include namespace msgpack { -namespace type { inline bool& operator>> (object o, bool& v) { - if(o.type != BOOLEAN) { throw type_error(); } + if(o.type != type::BOOLEAN) { throw type_error(); } v = o.via.boolean; return v; } @@ -42,7 +41,6 @@ inline packer& operator<< (packer& o, const bool& v) } -} // namespace type } // namespace msgpack #endif /* msgpack/type/bool.hpp */ diff --git a/cpp/type/integer.hpp b/cpp/type/integer.hpp index f5841a66..3fd57b41 100644 --- a/cpp/type/integer.hpp +++ b/cpp/type/integer.hpp @@ -70,19 +70,19 @@ namespace detail { template struct pack_integer_size_sign { - static inline void pack(T v, packer& o) + static inline void pack(packer& o, T v) { o.pack_int8(v); } }; template struct pack_integer_size_sign { - static inline void pack(T v, packer& o) + static inline void pack(packer& o, T v) { o.pack_uint8(v); } }; template struct pack_integer_size_sign { - static inline void pack(T v, packer& o) { + static inline void pack(packer& o, T v) { if( (int16_t)v <= (int16_t)std::numeric_limits::max() && (int16_t)v >= (int16_t)std::numeric_limits::min()) { o.pack_int8(v); } @@ -92,7 +92,7 @@ namespace detail { template struct pack_integer_size_sign { - static inline void pack(T v, packer& o) { + static inline void pack(packer& o, T v) { if( (uint16_t)v <= (uint16_t)std::numeric_limits::max()) { o.pack_uint8(v); } else { o.pack_uint16(v); } @@ -101,7 +101,7 @@ namespace detail { template struct pack_integer_size_sign { - static inline void pack(T v, packer& o) { + static inline void pack(packer& o, T v) { if( (int32_t)v <= (int32_t)std::numeric_limits::max() && (int32_t)v >= (int32_t)std::numeric_limits::min()) { o.pack_int8(v); } @@ -114,7 +114,7 @@ namespace detail { template struct pack_integer_size_sign { - static inline void pack(T v, packer& o) { + static inline void pack(packer& o, T v) { if( (uint32_t)v <= (uint32_t)std::numeric_limits::max()) { o.pack_uint8(v); } else if( (uint32_t)v <= (uint32_t)std::numeric_limits::max()) @@ -125,7 +125,7 @@ namespace detail { template struct pack_integer_size_sign { - static inline void pack(T v, packer& o) { + static inline void pack(packer& o, T v) { if( (int64_t)v <= (int64_t)std::numeric_limits::max() && (int64_t)v >= (int64_t)std::numeric_limits::min()) { o.pack_int8(v); } @@ -141,7 +141,7 @@ namespace detail { template struct pack_integer_size_sign { - static inline void pack(T v, packer& o) { + static inline void pack(packer& o, T v) { if( (uint64_t)v <= (uint64_t)std::numeric_limits::max()) { o.pack_uint8(v); } else if( (uint64_t)v <= (uint64_t)std::numeric_limits::max()) @@ -156,7 +156,7 @@ namespace detail { template static inline void pack_integer(T v, packer& o) { - pack_integer_size_sign::is_signed>::pack(v, o); + pack_integer_size_sign::is_signed>::pack(o, v); } } // namespace detail diff --git a/cpp/type/map.hpp b/cpp/type/map.hpp index 1d5e054b..3b544df9 100644 --- a/cpp/type/map.hpp +++ b/cpp/type/map.hpp @@ -63,8 +63,8 @@ inline packer& operator<< (packer& o, const type::assoc_vector::const_iterator it(v.begin()), it_end(v.end()); it != it_end; ++it) { - pack(it->first, o); - pack(it->second, o); + pack(o, it->first); + pack(o, it->second); } return o; } @@ -97,8 +97,8 @@ 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()); it != it_end; ++it) { - pack(it->first, o); - pack(it->second, o); + pack(o, it->first); + pack(o, it->second); } return o; } @@ -125,8 +125,8 @@ inline packer& operator<< (packer& o, const std::multimap& o.pack_multimap(v.size()); for(typename std::multimap::const_iterator it(v.begin()), it_end(v.end()); it != it_end; ++it) { - pack(it->first, o); - pack(it->second, o); + pack(o, it->first); + pack(o, it->second); } return o; } diff --git a/cpp/type/tuple.hpp.erb b/cpp/type/tuple.hpp.erb index c54739ae..397f660b 100644 --- a/cpp/type/tuple.hpp.erb +++ b/cpp/type/tuple.hpp.erb @@ -142,7 +142,7 @@ const packer& operator<< ( const type::tuple, A<%=j%><%}%>>& v) { o.pack_array(<%=i+1%>); <%0.upto(i) {|j|%> - pack(v.template get<<%=j%>>(), o);<%}%> + pack(o, v.template get<<%=j%>>());<%}%> return o; } <%}%> diff --git a/cpp/unpack.hpp b/cpp/unpack.hpp index de613a45..ffee60d6 100644 --- a/cpp/unpack.hpp +++ b/cpp/unpack.hpp @@ -81,7 +81,7 @@ public: // // // 2. // ssize_t bytes = - // read(the_source, pac.buffer, pac.buffer_capacity()); + // read(the_source, pac.buffer(), pac.buffer_capacity()); // // // error handling ... //