summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorfrsyuki <frsyuki@5a5092ae-2292-43ba-b2d5-dcab9c1a2731>2009-02-15 09:09:59 +0000
committerfrsyuki <frsyuki@5a5092ae-2292-43ba-b2d5-dcab9c1a2731>2009-02-15 09:09:59 +0000
commitd930090f130bf363d2330de1a15877ed9c5e9edc (patch)
treeec8ba3e7f650e278142014fd78a2a790ab3529ca
parentbb4ea9d17c20007405b0b6e703f5f47bf54e5da0 (diff)
downloadmsgpack-python-d930090f130bf363d2330de1a15877ed9c5e9edc.tar.gz
lang/c/msgpack: C++ binding: abolished implicit convertion and added object::convert()
git-svn-id: file:///Users/frsyuki/project/msgpack-git/svn/x@79 5a5092ae-2292-43ba-b2d5-dcab9c1a2731
-rw-r--r--cpp/object.hpp57
1 files changed, 49 insertions, 8 deletions
diff --git a/cpp/object.hpp b/cpp/object.hpp
index 5ed1fab..5f77d3e 100644
--- a/cpp/object.hpp
+++ b/cpp/object.hpp
@@ -44,8 +44,7 @@ namespace type {
struct object {
- unsigned char type;
- union {
+ union union_type {
bool boolean;
uint64_t u64;
int64_t i64;
@@ -58,15 +57,36 @@ struct object {
const char* ptr;
uint32_t size;
} ref;
- } via;
+ };
+
+ unsigned char type;
+ union_type via;
+
+ bool is_nil() { return type == type::NIL; }
template <typename T>
- operator T() { T v; convert(v, *this); return v; };
+ T as();
template <typename T>
- T as() { T v; convert(v, *this); return v; }
+ void convert(T& v);
- bool is_nil() { return type == type::NIL; }
+private:
+ struct implicit_type;
+
+public:
+ implicit_type convert();
+};
+
+
+struct object::implicit_type {
+ implicit_type(object o) : obj(o) { }
+ ~implicit_type() { }
+
+ template <typename T>
+ operator T() { return obj.as<T>(); }
+
+private:
+ object obj;
};
std::ostream& operator<< (std::ostream& s, const object o);
@@ -74,9 +94,11 @@ std::ostream& operator<< (std::ostream& s, const object o);
bool operator==(const object x, const object y);
inline bool operator!=(const object x, const object y) { return !(x == y); }
-
inline object& operator>> (object o, object& v)
- { v = o; return v; }
+{
+ v = o;
+ return v;
+}
template <typename Stream>
packer<Stream>& operator<< (packer<Stream>& o, const object& v);
@@ -147,6 +169,25 @@ public:
+inline object::implicit_type object::convert()
+{
+ return implicit_type(*this);
+}
+
+template <typename T>
+inline T object::as()
+{
+ T v;
+ msgpack::convert(v, *this);
+ return v;
+}
+
+template <typename T>
+void object::convert(T& v)
+{
+ msgpack::convert(v, *this);
+}
+
template <typename Stream>
packer<Stream>& operator<< (packer<Stream>& o, const object& v)
{