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
This commit is contained in:
frsyuki 2009-02-15 09:09:59 +00:00
parent bb4ea9d17c
commit d930090f13

View File

@ -44,8 +44,7 @@ namespace type {
struct object { struct object {
unsigned char type; union union_type {
union {
bool boolean; bool boolean;
uint64_t u64; uint64_t u64;
int64_t i64; int64_t i64;
@ -58,15 +57,36 @@ struct object {
const char* ptr; const char* ptr;
uint32_t size; uint32_t size;
} ref; } ref;
} via; };
template <typename T> unsigned char type;
operator T() { T v; convert(v, *this); return v; }; union_type via;
template <typename T>
T as() { T v; convert(v, *this); return v; }
bool is_nil() { return type == type::NIL; } bool is_nil() { return type == type::NIL; }
template <typename T>
T as();
template <typename T>
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 <typename T>
operator T() { return obj.as<T>(); }
private:
object obj;
}; };
std::ostream& operator<< (std::ostream& s, const object o); 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); bool operator==(const object x, const object y);
inline bool operator!=(const object x, const object y) { return !(x == y); } inline bool operator!=(const object x, const object y) { return !(x == y); }
inline object& operator>> (object o, object& v) inline object& operator>> (object o, object& v)
{ v = o; return v; } {
v = o;
return 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);
@ -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> template <typename Stream>
packer<Stream>& operator<< (packer<Stream>& o, const object& v) packer<Stream>& operator<< (packer<Stream>& o, const object& v)
{ {