From a114f4a5a5136a24f46bb52ffef1d1f588ba94e1 Mon Sep 17 00:00:00 2001 From: frsyuki Date: Sun, 15 Feb 2009 09:10:02 +0000 Subject: update pack/unpack routines git-svn-id: file:///Users/frsyuki/project/msgpack-git/svn/x@100 5a5092ae-2292-43ba-b2d5-dcab9c1a2731 --- cpp/object.hpp | 132 +++++++++++++++++++++++++++++++++------------------------ 1 file changed, 76 insertions(+), 56 deletions(-) (limited to 'cpp/object.hpp') diff --git a/cpp/object.hpp b/cpp/object.hpp index 1f0dcdf..c6f1e4a 100644 --- a/cpp/object.hpp +++ b/cpp/object.hpp @@ -1,7 +1,7 @@ // // MessagePack for C++ static resolution routine // -// Copyright (C) 2008 FURUHASHI Sadayuki +// Copyright (C) 2008-2009 FURUHASHI Sadayuki // // Licensed under the Apache License, Version 2.0 (the "License"); // you may not use this file except in compliance with the License. @@ -32,14 +32,16 @@ class type_error : public std::bad_cast { }; namespace type { - static const unsigned char NIL = 0x01; - static const unsigned char BOOLEAN = 0x02; - static const unsigned char POSITIVE_INTEGER = 0x03; - static const unsigned char NEGATIVE_INTEGER = 0x04; - static const unsigned char DOUBLE = 0x05; - static const unsigned char RAW = 0x06; - static const unsigned char ARRAY = 0x07; - static const unsigned char MAP = 0x08; + enum object_type { + NIL = 0x01, + BOOLEAN = 0x02, + POSITIVE_INTEGER = 0x03, + NEGATIVE_INTEGER = 0x04, + DOUBLE = 0x05, + RAW = 0x06, + ARRAY = 0x07, + MAP = 0x08, + }; } @@ -59,7 +61,7 @@ struct object { } ref; }; - unsigned char type; + type::object_type type; union_type via; bool is_nil() { return type == type::NIL; } @@ -68,7 +70,7 @@ struct object { T as(); template - void convert(T& v); + void convert(T* v); private: struct implicit_type; @@ -77,6 +79,21 @@ public: implicit_type convert(); }; +bool operator==(const object x, const object y); +bool operator!=(const object x, const object y); + +std::ostream& operator<< (std::ostream& s, const object o); + + +template +inline void pack(Stream& s, const T& v); + +template +packer& operator<< (packer& o, const T& v); + +template +T& operator>> (object o, T& v); + struct object::implicit_type { implicit_type(object o) : obj(o) { } @@ -89,39 +106,41 @@ private: object obj; }; -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); } +template +class define : public Type { +public: + typedef Type msgpack_type; + typedef define define_type; -inline object& operator>> (object o, object& v) -{ - v = o; - return v; -} + define() {} + define(const msgpack_type& v) : msgpack_type(v) {} -template -packer& operator<< (packer& o, const object& v); + template + void msgpack_pack(Packer& o) const + { + o << static_cast(*this); + } + void msgpack_unpack(object o) + { + o >> static_cast(*this); + } +}; +template template -inline void convert(T& v, object o) -{ - o >> v; -} - -template -inline void pack(packer& o, const T& v) +inline packer& packer::pack(const T& v) { - o << v; + *this << v; + return *this; } template inline void pack(Stream& s, const T& v) { - packer pk(s); - pack(pk, v); + packer(s).pack(v); } template @@ -130,7 +149,11 @@ inline void pack_copy(packer& o, T v) pack(o, v); } - +inline object& operator>> (object o, object& v) +{ + v = o; + return v; +} template inline T& operator>> (object o, T& v) @@ -147,27 +170,8 @@ inline packer& operator<< (packer& o, const T& v) } -template -class define : public Type { -public: - typedef Type msgpack_type; - typedef define define_type; - - define() {} - define(msgpack_type v) : msgpack_type(v) {} - - template - void msgpack_pack(Packer& o) const - { - o << static_cast(*this); - } - - void msgpack_unpack(object o) - { - o >> static_cast(*this); - } -}; - +inline bool operator!=(const object x, const object y) +{ return !(x == y); } inline object::implicit_type object::convert() @@ -179,16 +183,32 @@ template inline T object::as() { T v; - msgpack::convert(v, *this); + convert(&v); return v; } template -void object::convert(T& v) +inline void object::convert(T* v) +{ + *this >> *v; +} + + +// obsolete +template +inline void convert(T& v, object o) +{ + o.convert(&v); +} + +// obsolete +template +inline void pack(packer& o, const T& v) { - msgpack::convert(v, *this); + o.pack(v); } + template packer& operator<< (packer& o, const object& v) { -- cgit v1.2.1