diff --git a/cpp/object.hpp b/cpp/object.hpp index 5ed1fab0..5f77d3ed 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; + }; - template - operator T() { T v; convert(v, *this); return v; }; - - template - T as() { T v; convert(v, *this); return v; } + unsigned char type; + union_type via; bool is_nil() { return type == type::NIL; } + + template + T as(); + + template + void convert(T& v); + +private: + struct implicit_type; + +public: + implicit_type convert(); +}; + + +struct object::implicit_type { + implicit_type(object o) : obj(o) { } + ~implicit_type() { } + + template + operator T() { return obj.as(); } + +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 packer& operator<< (packer& o, const object& v); @@ -147,6 +169,25 @@ public: +inline object::implicit_type object::convert() +{ + return implicit_type(*this); +} + +template +inline T object::as() +{ + T v; + msgpack::convert(v, *this); + return v; +} + +template +void object::convert(T& v) +{ + msgpack::convert(v, *this); +} + template packer& operator<< (packer& o, const object& v) {