c: msgpack_pack_object

This commit is contained in:
frsyuki 2009-02-24 16:37:47 +09:00
parent aaaaecb8ba
commit bdd13859b6
7 changed files with 106 additions and 25 deletions

View File

@ -16,9 +16,78 @@
* limitations under the License. * limitations under the License.
*/ */
#include "msgpack/object.h" #include "msgpack/object.h"
#include "msgpack/pack.h"
#include <stdio.h> #include <stdio.h>
#include <inttypes.h> #include <inttypes.h>
int msgpack_pack_object(msgpack_packer* pk, msgpack_object d)
{
switch(d.type) {
case MSGPACK_OBJECT_NIL:
return msgpack_pack_nil(pk);
case MSGPACK_OBJECT_BOOLEAN:
if(d.via.boolean) {
return msgpack_pack_true(pk);
} else {
return msgpack_pack_false(pk);
}
case MSGPACK_OBJECT_POSITIVE_INTEGER:
return msgpack_pack_uint64(pk, d.via.u64);
case MSGPACK_OBJECT_NEGATIVE_INTEGER:
return msgpack_pack_int64(pk, d.via.i64);
case MSGPACK_OBJECT_DOUBLE:
return msgpack_pack_double(pk, d.via.dec);
case MSGPACK_OBJECT_RAW:
{
int ret = msgpack_pack_raw(pk, d.via.raw.size);
if(ret < 0) { return ret; }
return msgpack_pack_raw_body(pk, d.via.raw.ptr, d.via.raw.size);
}
case MSGPACK_OBJECT_ARRAY:
{
int ret = msgpack_pack_array(pk, d.via.array.size);
if(ret < 0) { return ret; }
msgpack_object* o = d.via.array.ptr;
msgpack_object* const oend = d.via.array.ptr + d.via.array.size;
for(; o != oend; ++o) {
ret = msgpack_pack_object(pk, *o);
if(ret < 0) { return ret; }
}
return 0;
}
case MSGPACK_OBJECT_MAP:
{
int ret = msgpack_pack_map(pk, d.via.map.size);
if(ret < 0) { return ret; }
msgpack_object_kv* kv = d.via.map.ptr;
msgpack_object_kv* const kvend = d.via.map.ptr + d.via.map.size;
for(; kv != kvend; ++kv) {
ret = msgpack_pack_object(pk, kv->key);
if(ret < 0) { return ret; }
ret = msgpack_pack_object(pk, kv->val);
if(ret < 0) { return ret; }
}
return 0;
}
default:
return -1;
}
}
void msgpack_object_print(FILE* out, msgpack_object o) void msgpack_object_print(FILE* out, msgpack_object o)
{ {
switch(o.type) { switch(o.type) {

View File

@ -22,6 +22,7 @@
#include <stdint.h> #include <stdint.h>
#include <stdlib.h> #include <stdlib.h>
#include "msgpack/pack_define.h" #include "msgpack/pack_define.h"
#include "msgpack/object.h"
#ifdef __cplusplus #ifdef __cplusplus
extern "C" { extern "C" {
@ -72,6 +73,8 @@ static int msgpack_pack_map(msgpack_packer* pk, unsigned int n);
static int msgpack_pack_raw(msgpack_packer* pk, size_t l); static int msgpack_pack_raw(msgpack_packer* pk, size_t l);
static int msgpack_pack_raw_body(msgpack_packer* pk, const void* b, size_t l); static int msgpack_pack_raw_body(msgpack_packer* pk, const void* b, size_t l);
int msgpack_pack_object(msgpack_packer* pk, msgpack_object d);
#define msgpack_pack_inline_func(name) \ #define msgpack_pack_inline_func(name) \

View File

@ -20,3 +20,4 @@
#include "msgpack/pack.hpp" #include "msgpack/pack.hpp"
#include "msgpack/unpack.hpp" #include "msgpack/unpack.hpp"
#include "msgpack/sbuffer.hpp" #include "msgpack/sbuffer.hpp"
#include "msgpack.h"

View File

@ -162,18 +162,6 @@ inline packer<Stream>& packer<Stream>::pack(const T& v)
return *this; return *this;
} }
template <typename Stream, typename T>
inline void pack(Stream& s, const T& v)
{
packer<Stream>(s).pack(v);
}
template <typename Stream, typename T>
inline void pack_copy(packer<Stream>& o, T v)
{
pack(o, v);
}
inline object& operator>> (object o, object& v) inline object& operator>> (object o, object& v)
{ {
v = o; v = o;
@ -250,6 +238,20 @@ inline void pack(packer<Stream>& o, const T& v)
o.pack(v); o.pack(v);
} }
// obsolete
template <typename Stream, typename T>
inline void pack(Stream& s, const T& v)
{
packer<Stream>(s).pack(v);
}
// obsolete
template <typename Stream, typename T>
inline void pack_copy(packer<Stream>& o, T v)
{
pack(o, v);
}
template <typename Stream> template <typename Stream>
packer<Stream>& operator<< (packer<Stream>& o, const object& v) packer<Stream>& operator<< (packer<Stream>& o, const object& v)
@ -309,7 +311,7 @@ packer<Stream>& operator<< (packer<Stream>& o, const object& v)
for(object* p(v.via.array.ptr), for(object* p(v.via.array.ptr),
* const pend(v.via.array.ptr + v.via.array.size); * const pend(v.via.array.ptr + v.via.array.size);
p < pend; ++p) { p < pend; ++p) {
*p >> o; o << *p;
} }
return o; return o;
// FIXME loop optimiziation // FIXME loop optimiziation
@ -319,8 +321,8 @@ packer<Stream>& operator<< (packer<Stream>& o, const object& v)
for(object_kv* p(v.via.map.ptr), for(object_kv* p(v.via.map.ptr),
* const pend(v.via.map.ptr + v.via.map.size); * const pend(v.via.map.ptr + v.via.map.size);
p < pend; ++p) { p < pend; ++p) {
p->key >> o; o << p->key;
p->val >> o; o << p->val;
} }
return o; return o;
// FIXME loop optimiziation // FIXME loop optimiziation

View File

@ -243,6 +243,7 @@ inline unpack_return unpack(const char* data, size_t len, size_t* off,
z, reinterpret_cast<msgpack_object*>(result)); z, reinterpret_cast<msgpack_object*>(result));
} }
// obsolete
inline object unpack(const char* data, size_t len, zone& z, size_t* off) inline object unpack(const char* data, size_t len, zone& z, size_t* off)
{ {
object result; object result;

View File

@ -5,27 +5,33 @@
int main(void) int main(void)
{ {
// this is target object
msgpack::type::tuple<int, bool, std::string> src(1, true, "example"); msgpack::type::tuple<int, bool, std::string> src(1, true, "example");
// any classes that implements write(const char*,size_t) can be a buffer // serialize the object into the buffer.
// any classes that implements write(const char*,size_t) can be a buffer.
std::stringstream buffer; std::stringstream buffer;
msgpack::pack(buffer, src); msgpack::pack(buffer, src);
// send the buffer ... // send the buffer ...
buffer.seekg(0); buffer.seekg(0);
// deserialize the buffer into msgpack::object type // deserialize the buffer into msgpack::object instance.
msgpack::zone mempool;
std::string str(buffer.str()); std::string str(buffer.str());
msgpack::object deserialized =
msgpack::unpack(str.data(), str.size(), mempool);
// msgpack::object supports ostream // deserialized object is valid during the msgpack::zone instance alive.
msgpack::zone mempool;
msgpack::object deserialized;
msgpack::unpack(str.data(), str.size(), NULL, &mempool, &deserialized);
// msgpack::object supports ostream.
std::cout << deserialized << std::endl; std::cout << deserialized << std::endl;
// convert msgpack::object type into the original type // convert msgpack::object instance into the original type.
// if the type is mismatched, it throws msgpack::type_error exception.
msgpack::type::tuple<int, bool, std::string> dst; msgpack::type::tuple<int, bool, std::string> dst;
msgpack::convert(dst, deserialized); deserialized.convert(&dst);
return 0;
} }

View File

@ -84,7 +84,6 @@ struct fwriter {
void write(const char* buf, size_t buflen) void write(const char* buf, size_t buflen)
{ {
size_t count = fwrite(buf, buflen, 1, m_fp); size_t count = fwrite(buf, buflen, 1, m_fp);
//if(fwrite(buf, buflen, 1, m_fp) < 1) {
if(count < 1) { if(count < 1) {
std::cout << buflen << std::endl; std::cout << buflen << std::endl;
std::cout << count << std::endl; std::cout << count << std::endl;