mirror of
https://github.com/msgpack/msgpack-c.git
synced 2025-05-29 15:34:08 +02: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>
|
||||||
template <typename T>
|
inline T& operator>> (object o, T& v)
|
||||||
inline T& operator>> (object o, T& v)
|
{
|
||||||
{
|
v.msgpack_unpack(o);
|
||||||
v.msgpack_unpack(o);
|
return v;
|
||||||
return v;
|
}
|
||||||
}
|
|
||||||
|
template <typename Stream, typename T>
|
||||||
template <typename Stream, typename T>
|
inline packer<Stream>& operator<< (packer<Stream>& o, const T& v)
|
||||||
inline packer<Stream>& operator<< (packer<Stream>& o, const T& v)
|
{
|
||||||
{
|
o << v.msgpack_pack();
|
||||||
pack_copy(v.msgpack_pack(), o);
|
return o;
|
||||||
return o;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
template <typename T>
|
template <typename T>
|
||||||
inline void convert(T& v, object o)
|
inline void convert(T& v, object o)
|
||||||
{
|
{
|
||||||
using namespace type;
|
|
||||||
o >> v;
|
o >> v;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
template <typename Stream, typename T>
|
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;
|
o << v;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
template <typename Stream, typename T>
|
template <typename Stream, typename T>
|
||||||
inline void pack(T& v, Stream& s)
|
inline void pack(Stream& s, const T& v)
|
||||||
{
|
{
|
||||||
packer<Stream> pk(s);
|
packer<Stream> pk(s);
|
||||||
pack(v, pk);
|
pack(pk, v);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
template <typename Stream, typename T>
|
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 {
|
try {
|
||||||
std::stringstream s;
|
std::stringstream s;
|
||||||
pack(should, s);
|
pack(s, should);
|
||||||
std::string str(s.str());
|
std::string str(s.str());
|
||||||
object ro = unpack(str.data(), str.size(), m_zone);
|
object ro = unpack(str.data(), str.size(), m_zone);
|
||||||
std::cout << ro << std::endl;
|
std::cout << ro << std::endl;
|
||||||
@ -139,7 +139,7 @@ int main(void)
|
|||||||
// send message
|
// send message
|
||||||
{
|
{
|
||||||
for(unsigned i=0; i < TASK_REPEAT; ++i) {
|
for(unsigned i=0; i < TASK_REPEAT; ++i) {
|
||||||
pack(task, stream);
|
pack(stream, task);
|
||||||
}
|
}
|
||||||
std::cout << "send " << stream.str().size() << " bytes" << std::endl;
|
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());
|
o.pack_array(v.size());
|
||||||
for(typename std::vector<T>::const_iterator it(v.begin()), it_end(v.end());
|
for(typename std::vector<T>::const_iterator it(v.begin()), it_end(v.end());
|
||||||
it != it_end; ++it) {
|
it != it_end; ++it) {
|
||||||
pack(*it, o);
|
pack(o, *it);
|
||||||
}
|
}
|
||||||
return o;
|
return o;
|
||||||
}
|
}
|
||||||
|
@ -22,12 +22,11 @@
|
|||||||
#include <vector>
|
#include <vector>
|
||||||
|
|
||||||
namespace msgpack {
|
namespace msgpack {
|
||||||
namespace type {
|
|
||||||
|
|
||||||
|
|
||||||
inline bool& operator>> (object o, bool& v)
|
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;
|
v = o.via.boolean;
|
||||||
return v;
|
return v;
|
||||||
}
|
}
|
||||||
@ -42,7 +41,6 @@ inline packer<Stream>& operator<< (packer<Stream>& o, const bool& v)
|
|||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
} // namespace type
|
|
||||||
} // namespace msgpack
|
} // namespace msgpack
|
||||||
|
|
||||||
#endif /* msgpack/type/bool.hpp */
|
#endif /* msgpack/type/bool.hpp */
|
||||||
|
@ -70,19 +70,19 @@ namespace detail {
|
|||||||
|
|
||||||
template <typename T, typename Stream>
|
template <typename T, typename Stream>
|
||||||
struct pack_integer_size_sign<T, Stream, 1, true> {
|
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); }
|
{ o.pack_int8(v); }
|
||||||
};
|
};
|
||||||
|
|
||||||
template <typename T, typename Stream>
|
template <typename T, typename Stream>
|
||||||
struct pack_integer_size_sign<T, Stream, 1, false> {
|
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); }
|
{ o.pack_uint8(v); }
|
||||||
};
|
};
|
||||||
|
|
||||||
template <typename T, typename Stream>
|
template <typename T, typename Stream>
|
||||||
struct pack_integer_size_sign<T, Stream, 2, true> {
|
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() &&
|
if( (int16_t)v <= (int16_t)std::numeric_limits<int8_t>::max() &&
|
||||||
(int16_t)v >= (int16_t)std::numeric_limits<int8_t>::min())
|
(int16_t)v >= (int16_t)std::numeric_limits<int8_t>::min())
|
||||||
{ o.pack_int8(v); }
|
{ o.pack_int8(v); }
|
||||||
@ -92,7 +92,7 @@ namespace detail {
|
|||||||
|
|
||||||
template <typename T, typename Stream>
|
template <typename T, typename Stream>
|
||||||
struct pack_integer_size_sign<T, Stream, 2, false> {
|
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())
|
if( (uint16_t)v <= (uint16_t)std::numeric_limits<uint8_t>::max())
|
||||||
{ o.pack_uint8(v); }
|
{ o.pack_uint8(v); }
|
||||||
else { o.pack_uint16(v); }
|
else { o.pack_uint16(v); }
|
||||||
@ -101,7 +101,7 @@ namespace detail {
|
|||||||
|
|
||||||
template <typename T, typename Stream>
|
template <typename T, typename Stream>
|
||||||
struct pack_integer_size_sign<T, Stream, 4, true> {
|
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() &&
|
if( (int32_t)v <= (int32_t)std::numeric_limits<int8_t>::max() &&
|
||||||
(int32_t)v >= (int32_t)std::numeric_limits<int8_t>::min())
|
(int32_t)v >= (int32_t)std::numeric_limits<int8_t>::min())
|
||||||
{ o.pack_int8(v); }
|
{ o.pack_int8(v); }
|
||||||
@ -114,7 +114,7 @@ namespace detail {
|
|||||||
|
|
||||||
template <typename T, typename Stream>
|
template <typename T, typename Stream>
|
||||||
struct pack_integer_size_sign<T, Stream, 4, false> {
|
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())
|
if( (uint32_t)v <= (uint32_t)std::numeric_limits<uint8_t>::max())
|
||||||
{ o.pack_uint8(v); }
|
{ o.pack_uint8(v); }
|
||||||
else if( (uint32_t)v <= (uint32_t)std::numeric_limits<uint16_t>::max())
|
else if( (uint32_t)v <= (uint32_t)std::numeric_limits<uint16_t>::max())
|
||||||
@ -125,7 +125,7 @@ namespace detail {
|
|||||||
|
|
||||||
template <typename T, typename Stream>
|
template <typename T, typename Stream>
|
||||||
struct pack_integer_size_sign<T, Stream, 8, true> {
|
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() &&
|
if( (int64_t)v <= (int64_t)std::numeric_limits<int8_t>::max() &&
|
||||||
(int64_t)v >= (int64_t)std::numeric_limits<int8_t>::min())
|
(int64_t)v >= (int64_t)std::numeric_limits<int8_t>::min())
|
||||||
{ o.pack_int8(v); }
|
{ o.pack_int8(v); }
|
||||||
@ -141,7 +141,7 @@ namespace detail {
|
|||||||
|
|
||||||
template <typename T, typename Stream>
|
template <typename T, typename Stream>
|
||||||
struct pack_integer_size_sign<T, Stream, 8, false> {
|
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())
|
if( (uint64_t)v <= (uint64_t)std::numeric_limits<uint8_t>::max())
|
||||||
{ o.pack_uint8(v); }
|
{ o.pack_uint8(v); }
|
||||||
else if( (uint64_t)v <= (uint64_t)std::numeric_limits<uint16_t>::max())
|
else if( (uint64_t)v <= (uint64_t)std::numeric_limits<uint16_t>::max())
|
||||||
@ -156,7 +156,7 @@ namespace detail {
|
|||||||
template <typename T, typename Stream>
|
template <typename T, typename Stream>
|
||||||
static inline void pack_integer(T v, packer<Stream>& o)
|
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
|
} // namespace detail
|
||||||
|
@ -63,8 +63,8 @@ inline packer<Stream>& operator<< (packer<Stream>& o, const type::assoc_vector<K
|
|||||||
o.pack_map(v.size());
|
o.pack_map(v.size());
|
||||||
for(typename type::assoc_vector<K,V>::const_iterator it(v.begin()), it_end(v.end());
|
for(typename type::assoc_vector<K,V>::const_iterator it(v.begin()), it_end(v.end());
|
||||||
it != it_end; ++it) {
|
it != it_end; ++it) {
|
||||||
pack(it->first, o);
|
pack(o, it->first);
|
||||||
pack(it->second, o);
|
pack(o, it->second);
|
||||||
}
|
}
|
||||||
return o;
|
return o;
|
||||||
}
|
}
|
||||||
@ -97,8 +97,8 @@ inline packer<Stream>& operator<< (packer<Stream>& o, const std::map<K,V>& v)
|
|||||||
o.pack_map(v.size());
|
o.pack_map(v.size());
|
||||||
for(typename std::map<K,V>::const_iterator it(v.begin()), it_end(v.end());
|
for(typename std::map<K,V>::const_iterator it(v.begin()), it_end(v.end());
|
||||||
it != it_end; ++it) {
|
it != it_end; ++it) {
|
||||||
pack(it->first, o);
|
pack(o, it->first);
|
||||||
pack(it->second, o);
|
pack(o, it->second);
|
||||||
}
|
}
|
||||||
return o;
|
return o;
|
||||||
}
|
}
|
||||||
@ -125,8 +125,8 @@ inline packer<Stream>& operator<< (packer<Stream>& o, const std::multimap<K,V>&
|
|||||||
o.pack_multimap(v.size());
|
o.pack_multimap(v.size());
|
||||||
for(typename std::multimap<K,V>::const_iterator it(v.begin()), it_end(v.end());
|
for(typename std::multimap<K,V>::const_iterator it(v.begin()), it_end(v.end());
|
||||||
it != it_end; ++it) {
|
it != it_end; ++it) {
|
||||||
pack(it->first, o);
|
pack(o, it->first);
|
||||||
pack(it->second, o);
|
pack(o, it->second);
|
||||||
}
|
}
|
||||||
return o;
|
return o;
|
||||||
}
|
}
|
||||||
|
@ -142,7 +142,7 @@ const packer<Stream>& operator<< (
|
|||||||
const type::tuple<A0<%1.upto(i) {|j|%>, A<%=j%><%}%>>& v) {
|
const type::tuple<A0<%1.upto(i) {|j|%>, A<%=j%><%}%>>& v) {
|
||||||
o.pack_array(<%=i+1%>);
|
o.pack_array(<%=i+1%>);
|
||||||
<%0.upto(i) {|j|%>
|
<%0.upto(i) {|j|%>
|
||||||
pack(v.template get<<%=j%>>(), o);<%}%>
|
pack(o, v.template get<<%=j%>>());<%}%>
|
||||||
return o;
|
return o;
|
||||||
}
|
}
|
||||||
<%}%>
|
<%}%>
|
||||||
|
@ -81,7 +81,7 @@ public:
|
|||||||
//
|
//
|
||||||
// // 2.
|
// // 2.
|
||||||
// ssize_t bytes =
|
// ssize_t bytes =
|
||||||
// read(the_source, pac.buffer, pac.buffer_capacity());
|
// read(the_source, pac.buffer(), pac.buffer_capacity());
|
||||||
//
|
//
|
||||||
// // error handling ...
|
// // error handling ...
|
||||||
//
|
//
|
||||||
|
Loading…
x
Reference in New Issue
Block a user