summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorfrsyuki <frsyuki@vcore.(none)>2009-02-24 16:37:47 +0900
committerfrsyuki <frsyuki@vcore.(none)>2009-02-24 16:37:47 +0900
commitbdd13859b60d06ec6fabdbaccea81cbee189b8bc (patch)
treec76ff69b75cd98c669ead4c25ca1d3fd33b0666a
parentaaaaecb8bad862afc66ad5a2772adb78b9082df8 (diff)
downloadmsgpack-python-bdd13859b60d06ec6fabdbaccea81cbee189b8bc.tar.gz
c: msgpack_pack_object
-rw-r--r--c/object.c69
-rw-r--r--c/pack.h3
-rw-r--r--cpp/msgpack.hpp1
-rw-r--r--cpp/object.hpp32
-rw-r--r--cpp/unpack.hpp1
-rw-r--r--example/simple.cc24
-rw-r--r--example/stream.cc1
7 files changed, 106 insertions, 25 deletions
diff --git a/c/object.c b/c/object.c
index 871153d..bcb2537 100644
--- a/c/object.c
+++ b/c/object.c
@@ -16,9 +16,78 @@
* limitations under the License.
*/
#include "msgpack/object.h"
+#include "msgpack/pack.h"
#include <stdio.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)
{
switch(o.type) {
diff --git a/c/pack.h b/c/pack.h
index 46de722..1a57ea4 100644
--- a/c/pack.h
+++ b/c/pack.h
@@ -22,6 +22,7 @@
#include <stdint.h>
#include <stdlib.h>
#include "msgpack/pack_define.h"
+#include "msgpack/object.h"
#ifdef __cplusplus
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_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) \
diff --git a/cpp/msgpack.hpp b/cpp/msgpack.hpp
index f00da06..58b40ac 100644
--- a/cpp/msgpack.hpp
+++ b/cpp/msgpack.hpp
@@ -20,3 +20,4 @@
#include "msgpack/pack.hpp"
#include "msgpack/unpack.hpp"
#include "msgpack/sbuffer.hpp"
+#include "msgpack.h"
diff --git a/cpp/object.hpp b/cpp/object.hpp
index c3f2872..b4c21f1 100644
--- a/cpp/object.hpp
+++ b/cpp/object.hpp
@@ -162,18 +162,6 @@ inline packer<Stream>& packer<Stream>::pack(const T& v)
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)
{
v = o;
@@ -250,6 +238,20 @@ inline void pack(packer<Stream>& o, const T& 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>
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),
* const pend(v.via.array.ptr + v.via.array.size);
p < pend; ++p) {
- *p >> o;
+ o << *p;
}
return o;
// FIXME loop optimiziation
@@ -319,8 +321,8 @@ packer<Stream>& operator<< (packer<Stream>& o, const object& v)
for(object_kv* p(v.via.map.ptr),
* const pend(v.via.map.ptr + v.via.map.size);
p < pend; ++p) {
- p->key >> o;
- p->val >> o;
+ o << p->key;
+ o << p->val;
}
return o;
// FIXME loop optimiziation
diff --git a/cpp/unpack.hpp b/cpp/unpack.hpp
index 57623f2..4e7ccf5 100644
--- a/cpp/unpack.hpp
+++ b/cpp/unpack.hpp
@@ -243,6 +243,7 @@ inline unpack_return unpack(const char* data, size_t len, size_t* off,
z, reinterpret_cast<msgpack_object*>(result));
}
+// obsolete
inline object unpack(const char* data, size_t len, zone& z, size_t* off)
{
object result;
diff --git a/example/simple.cc b/example/simple.cc
index 74beeae..55ecdf9 100644
--- a/example/simple.cc
+++ b/example/simple.cc
@@ -5,27 +5,33 @@
int main(void)
{
- // this is target object
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;
msgpack::pack(buffer, src);
// send the buffer ...
buffer.seekg(0);
- // deserialize the buffer into msgpack::object type
- msgpack::zone mempool;
+ // deserialize the buffer into msgpack::object instance.
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;
- // 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::convert(dst, deserialized);
+ deserialized.convert(&dst);
+
+ return 0;
}
diff --git a/example/stream.cc b/example/stream.cc
index aef4ec8..e57318f 100644
--- a/example/stream.cc
+++ b/example/stream.cc
@@ -84,7 +84,6 @@ struct fwriter {
void write(const char* buf, size_t buflen)
{
size_t count = fwrite(buf, buflen, 1, m_fp);
- //if(fwrite(buf, buflen, 1, m_fp) < 1) {
if(count < 1) {
std::cout << buflen << std::endl;
std::cout << count << std::endl;