summaryrefslogtreecommitdiff
path: root/cpp
diff options
context:
space:
mode:
authorfrsyuki <frsyuki@5a5092ae-2292-43ba-b2d5-dcab9c1a2731>2009-02-15 09:09:58 +0000
committerfrsyuki <frsyuki@5a5092ae-2292-43ba-b2d5-dcab9c1a2731>2009-02-15 09:09:58 +0000
commita7936ba05b15ec7a19e8dc75667fec2df13b7ea7 (patch)
treebe42e088b4a54b819052c2651ed0bdd80a432b91 /cpp
parentb790df530aa28099aa130dafad0a591004b88650 (diff)
downloadmsgpack-python-a7936ba05b15ec7a19e8dc75667fec2df13b7ea7.tar.gz
lang/c/msgpack: C++ binding: implemented msgpack::object >> packer<Stream>
git-svn-id: file:///Users/frsyuki/project/msgpack-git/svn/x@69 5a5092ae-2292-43ba-b2d5-dcab9c1a2731
Diffstat (limited to 'cpp')
-rw-r--r--cpp/object.cpp11
-rw-r--r--cpp/object.hpp92
2 files changed, 92 insertions, 11 deletions
diff --git a/cpp/object.cpp b/cpp/object.cpp
index 276732c..45cfb5f 100644
--- a/cpp/object.cpp
+++ b/cpp/object.cpp
@@ -81,7 +81,7 @@ std::ostream& operator<< (std::ostream& s, const object o)
default:
// FIXME
- s << "#<UNKNOWN " << (uint16_t)o.type << ">" << std::endl;
+ s << "#<UNKNOWN " << (uint16_t)o.type << ">";
}
return s;
}
@@ -113,7 +113,7 @@ bool operator==(const object x, const object y)
for(object* px(x.via.container.ptr),
* const pxend(x.via.container.ptr + x.via.container.size),
* py(y.via.container.ptr);
- px != pxend; ++px, ++py) {
+ px < pxend; ++px, ++py) {
if(*px != *py) { return false; }
}
return true;
@@ -124,7 +124,7 @@ bool operator==(const object x, const object y)
for(object* px(x.via.container.ptr),
* const pxend(x.via.container.ptr + x.via.container.size*2),
* py(y.via.container.ptr);
- px != pxend; ++px, ++py) {
+ px < pxend; ++px, ++py) {
if(*px != *py) { return false; }
}
return true;
@@ -135,10 +135,5 @@ bool operator==(const object x, const object y)
}
-// FIXME
-//template <typename Stream>
-//const object& operator>> (const object& v, packer<Stream>& o);
-
-
} // namespace msgpack
diff --git a/cpp/object.hpp b/cpp/object.hpp
index 7007b90..a5e8682 100644
--- a/cpp/object.hpp
+++ b/cpp/object.hpp
@@ -22,6 +22,7 @@
#include <stdint.h>
#include <stdexcept>
#include <typeinfo>
+#include <limits>
#include <ostream>
namespace msgpack {
@@ -46,7 +47,9 @@ struct object {
uint32_t size;
} ref;
} via;
- // FIXME template <typename T> operator T() { T v; convert(*this, v); return v; };
+
+ template <typename T>
+ operator T() { T v; convert(v, *this); return v; };
};
std::ostream& operator<< (std::ostream& s, const object o);
@@ -55,6 +58,9 @@ bool operator==(const object x, const object y);
inline bool operator!=(const object x, const object y) { return !(x == y); }
+inline object& operator<< (object& v, object o)
+ { v = o; return v; }
+
template <typename Stream>
const object& operator>> (const object& v, packer<Stream>& o);
@@ -73,7 +79,7 @@ namespace type {
template <typename T>
inline T& operator<< (T& v, object o)
{
- v = o;
+ v.msgpack_unpack(o);
return v;
}
@@ -87,7 +93,7 @@ namespace type {
template <typename Stream, typename T>
inline const T& operator>> (const T& v, packer<Stream>& o)
{
- detail::pack_copy(v.pack(), o);
+ detail::pack_copy(v.msgpack_pack(), o);
return v;
}
@@ -118,6 +124,86 @@ inline void pack(T& v, Stream& s)
}
+
+template <typename Stream>
+const object& operator>> (const object& v, packer<Stream>& o)
+{
+ switch(v.type) {
+ case type::NIL:
+ o.pack_nil();
+ return v;
+
+ case type::BOOLEAN:
+ if(v.via.boolean) {
+ o.pack_true();
+ } else {
+ o.pack_false();
+ }
+ return v;
+
+ case type::POSITIVE_INTEGER:
+ if(v.via.u64 <= (uint64_t)std::numeric_limits<uint16_t>::max()) {
+ if(v.via.u64 <= (uint16_t)std::numeric_limits<uint8_t>::max()) {
+ o.pack_uint8(v.via.u64);
+ } else {
+ o.pack_uint16(v.via.u64);
+ }
+ } else {
+ if(v.via.u64 <= (uint64_t)std::numeric_limits<uint32_t>::max()) {
+ o.pack_uint32(v.via.u64);
+ } else {
+ o.pack_uint64(v.via.u64);
+ }
+ }
+ return v;
+
+ case type::NEGATIVE_INTEGER:
+ if(v.via.i64 >= (int64_t)std::numeric_limits<int16_t>::min()) {
+ if(v.via.i64 >= (int64_t)std::numeric_limits<int8_t>::min()) {
+ o.pack_int8(v.via.i64);
+ } else {
+ o.pack_int16(v.via.i64);
+ }
+ } else {
+ if(v.via.i64 >= (int64_t)std::numeric_limits<int32_t>::min()) {
+ o.pack_int64(v.via.i64);
+ } else {
+ o.pack_int64(v.via.i64);
+ }
+ }
+ return v;
+
+ case type::RAW:
+ o.pack_raw(v.via.ref.ptr, v.via.ref.size);
+ return v;
+
+ case type::ARRAY:
+ o.pack_array(v.via.container.size);
+ for(object* p(v.via.container.ptr),
+ * const pend(v.via.container.ptr + v.via.container.size);
+ p < pend; ++p) {
+ *p >> o;
+ }
+ return v;
+ // FIXME loop optimiziation
+
+ case type::MAP:
+ o.pack_map(v.via.container.size);
+ for(object* p(v.via.container.ptr),
+ * const pend(v.via.container.ptr + v.via.container.size*2);
+ p < pend; ) {
+ *p >> o; ++p;
+ *p >> o; ++p;
+ }
+ return v;
+ // FIXME loop optimiziation
+
+ default:
+ throw type_error();
+ }
+}
+
+
} // namespace msgpack
#include "msgpack/type.hpp"