mirror of
https://github.com/msgpack/msgpack-c.git
synced 2025-03-14 18:10:30 +01:00
lang/c/msgpack: C++ binding: pack()
git-svn-id: file:///Users/frsyuki/project/msgpack-git/svn/x@73 5a5092ae-2292-43ba-b2d5-dcab9c1a2731
This commit is contained in:
parent
2c7f0b2b1a
commit
921b0ff62e
@ -83,51 +83,44 @@ packer<Stream>& operator<< (packer<Stream>& o, const object& v);
|
||||
|
||||
|
||||
|
||||
namespace type {
|
||||
template <typename T>
|
||||
inline T& operator>> (object o, T& v)
|
||||
{
|
||||
v.msgpack_unpack(o);
|
||||
return v;
|
||||
}
|
||||
|
||||
template <typename Stream, typename T>
|
||||
inline packer<Stream>& operator<< (packer<Stream>& o, const T& v)
|
||||
{
|
||||
pack_copy(v.msgpack_pack(), o);
|
||||
return o;
|
||||
}
|
||||
template <typename T>
|
||||
inline T& operator>> (object o, T& v)
|
||||
{
|
||||
v.msgpack_unpack(o);
|
||||
return v;
|
||||
}
|
||||
|
||||
template <typename Stream, typename T>
|
||||
inline packer<Stream>& operator<< (packer<Stream>& o, const T& v)
|
||||
{
|
||||
o << v.msgpack_pack();
|
||||
return o;
|
||||
}
|
||||
|
||||
|
||||
template <typename T>
|
||||
inline void convert(T& v, object o)
|
||||
{
|
||||
using namespace type;
|
||||
o >> v;
|
||||
}
|
||||
|
||||
|
||||
template <typename Stream, typename T>
|
||||
inline void pack(T& v, packer<Stream>& o)
|
||||
inline void pack(packer<Stream>& o, const T& v)
|
||||
{
|
||||
using namespace type;
|
||||
o << v;
|
||||
}
|
||||
|
||||
|
||||
template <typename Stream, typename T>
|
||||
inline void pack(T& v, Stream& s)
|
||||
inline void pack(Stream& s, const T& v)
|
||||
{
|
||||
packer<Stream> pk(s);
|
||||
pack(v, pk);
|
||||
pack(pk, v);
|
||||
}
|
||||
|
||||
|
||||
template <typename Stream, typename T>
|
||||
inline void pack_copy(T v, packer<Stream>& o)
|
||||
inline void pack_copy(packer<Stream>& o, T v)
|
||||
{
|
||||
pack(v, o);
|
||||
pack(o, v);
|
||||
}
|
||||
|
||||
|
||||
|
@ -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;
|
||||
}
|
||||
|
@ -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(*it, o);
|
||||
pack(o, *it);
|
||||
}
|
||||
return o;
|
||||
}
|
||||
|
@ -22,12 +22,11 @@
|
||||
#include <vector>
|
||||
|
||||
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<Stream>& operator<< (packer<Stream>& o, const bool& v)
|
||||
}
|
||||
|
||||
|
||||
} // namespace type
|
||||
} // namespace msgpack
|
||||
|
||||
#endif /* msgpack/type/bool.hpp */
|
||||
|
@ -70,19 +70,19 @@ namespace detail {
|
||||
|
||||
template <typename T, typename Stream>
|
||||
struct pack_integer_size_sign<T, Stream, 1, true> {
|
||||
static inline void pack(T v, packer<Stream>& o)
|
||||
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(T v, packer<Stream>& o)
|
||||
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(T v, packer<Stream>& o) {
|
||||
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); }
|
||||
@ -92,7 +92,7 @@ namespace detail {
|
||||
|
||||
template <typename T, typename Stream>
|
||||
struct pack_integer_size_sign<T, Stream, 2, false> {
|
||||
static inline void pack(T v, packer<Stream>& o) {
|
||||
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); }
|
||||
@ -101,7 +101,7 @@ namespace detail {
|
||||
|
||||
template <typename T, typename Stream>
|
||||
struct pack_integer_size_sign<T, Stream, 4, true> {
|
||||
static inline void pack(T v, packer<Stream>& o) {
|
||||
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); }
|
||||
@ -114,7 +114,7 @@ namespace detail {
|
||||
|
||||
template <typename T, typename Stream>
|
||||
struct pack_integer_size_sign<T, Stream, 4, false> {
|
||||
static inline void pack(T v, packer<Stream>& o) {
|
||||
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())
|
||||
@ -125,7 +125,7 @@ namespace detail {
|
||||
|
||||
template <typename T, typename Stream>
|
||||
struct pack_integer_size_sign<T, Stream, 8, true> {
|
||||
static inline void pack(T v, packer<Stream>& o) {
|
||||
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); }
|
||||
@ -141,7 +141,7 @@ namespace detail {
|
||||
|
||||
template <typename T, typename Stream>
|
||||
struct pack_integer_size_sign<T, Stream, 8, false> {
|
||||
static inline void pack(T v, packer<Stream>& o) {
|
||||
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())
|
||||
@ -156,7 +156,7 @@ namespace detail {
|
||||
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);
|
||||
pack_integer_size_sign<T, Stream, sizeof(T), std::numeric_limits<T>::is_signed>::pack(o, v);
|
||||
}
|
||||
|
||||
} // namespace detail
|
||||
|
@ -63,8 +63,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(it->first, o);
|
||||
pack(it->second, o);
|
||||
pack(o, it->first);
|
||||
pack(o, it->second);
|
||||
}
|
||||
return o;
|
||||
}
|
||||
@ -97,8 +97,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(it->first, o);
|
||||
pack(it->second, o);
|
||||
pack(o, it->first);
|
||||
pack(o, it->second);
|
||||
}
|
||||
return o;
|
||||
}
|
||||
@ -125,8 +125,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(it->first, o);
|
||||
pack(it->second, o);
|
||||
pack(o, it->first);
|
||||
pack(o, it->second);
|
||||
}
|
||||
return o;
|
||||
}
|
||||
|
@ -142,7 +142,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(v.template get<<%=j%>>(), o);<%}%>
|
||||
pack(o, v.template get<<%=j%>>());<%}%>
|
||||
return o;
|
||||
}
|
||||
<%}%>
|
||||
|
@ -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 ...
|
||||
//
|
||||
|
Loading…
x
Reference in New Issue
Block a user