diff options
| author | frsyuki <frsyuki@users.sourceforge.jp> | 2010-04-25 01:12:25 +0900 |
|---|---|---|
| committer | frsyuki <frsyuki@users.sourceforge.jp> | 2010-04-25 01:12:25 +0900 |
| commit | 4e85ebbf9863201107a8f5c68fdabc92749e04fc (patch) | |
| tree | c1c5e8a8f45400216267b220eecad7d47c448d28 /cpp/msgpack | |
| parent | 120e8bffd7917e9529229e796b21ececc51df016 (diff) | |
| download | msgpack-python-4e85ebbf9863201107a8f5c68fdabc92749e04fc.tar.gz | |
cpp: object::object(const T& v, zone* z)
Diffstat (limited to 'cpp/msgpack')
| -rw-r--r-- | cpp/msgpack/object.hpp | 25 | ||||
| -rw-r--r-- | cpp/msgpack/type/deque.hpp | 16 | ||||
| -rw-r--r-- | cpp/msgpack/type/list.hpp | 16 | ||||
| -rw-r--r-- | cpp/msgpack/type/pair.hpp | 11 | ||||
| -rw-r--r-- | cpp/msgpack/type/raw.hpp | 7 | ||||
| -rw-r--r-- | cpp/msgpack/type/set.hpp | 32 | ||||
| -rw-r--r-- | cpp/msgpack/type/string.hpp | 9 | ||||
| -rw-r--r-- | cpp/msgpack/type/tuple.hpp.erb | 20 | ||||
| -rw-r--r-- | cpp/msgpack/type/vector.hpp | 16 | ||||
| -rw-r--r-- | cpp/msgpack/zone.hpp.erb | 1 |
10 files changed, 152 insertions, 1 deletions
diff --git a/cpp/msgpack/object.hpp b/cpp/msgpack/object.hpp index 0125d0a..a094c56 100644 --- a/cpp/msgpack/object.hpp +++ b/cpp/msgpack/object.hpp @@ -20,6 +20,7 @@ #include "msgpack/object.h" #include "msgpack/pack.hpp" +#include "msgpack/zone.hpp" #include <string.h> #include <stdexcept> #include <typeinfo> @@ -93,6 +94,9 @@ struct object { object(const T& v); template <typename T> + object(const T& v, zone* z); + + template <typename T> object& operator=(const T& v); operator msgpack_object(); @@ -101,6 +105,10 @@ private: struct implicit_type; public: + // FIXME private? + struct object_zone; + +public: implicit_type convert() const; }; @@ -109,6 +117,14 @@ struct object_kv { object val; }; +struct object::object_zone : object { + object_zone(msgpack::zone* zone) : zone(zone) { } + msgpack::zone* zone; +private: + object_zone(); +}; + + bool operator==(const object x, const object y); bool operator!=(const object x, const object y); @@ -230,6 +246,15 @@ inline object& object::operator=(const T& v) return *this; } +template <typename T> +object::object(const T& v, zone* z) +{ + object_zone oz(z); + oz << v; + type = oz.type; + via = oz.via; +} + inline object::operator msgpack_object() { diff --git a/cpp/msgpack/type/deque.hpp b/cpp/msgpack/type/deque.hpp index d34d243..5e919a8 100644 --- a/cpp/msgpack/type/deque.hpp +++ b/cpp/msgpack/type/deque.hpp @@ -49,6 +49,22 @@ inline packer<Stream>& operator<< (packer<Stream>& o, const std::deque<T>& v) return o; } +template <typename T> +inline void operator<< (object::object_zone& o, const std::deque<T>& v) +{ + o.type = type::ARRAY; + object* p = (object*)o.zone->malloc(sizeof(object)*v.size()); + object* const pend = p + v.size(); + o.via.array.ptr = p; + o.via.array.size = v.size(); + typename std::deque<T>::const_iterator it(v.begin()); + do { + *p = object(*it, o.zone); + ++p; + ++it; + } while(p < pend); +} + } // namespace msgpack diff --git a/cpp/msgpack/type/list.hpp b/cpp/msgpack/type/list.hpp index 6ecc02f..5318639 100644 --- a/cpp/msgpack/type/list.hpp +++ b/cpp/msgpack/type/list.hpp @@ -49,6 +49,22 @@ inline packer<Stream>& operator<< (packer<Stream>& o, const std::list<T>& v) return o; } +template <typename T> +inline void operator<< (object::object_zone& o, const std::list<T>& v) +{ + o.type = type::ARRAY; + object* p = (object*)o.zone->malloc(sizeof(object)*v.size()); + object* const pend = p + v.size(); + o.via.array.ptr = p; + o.via.array.size = v.size(); + typename std::list<T>::const_iterator it(v.begin()); + do { + *p = object(*it, o.zone); + ++p; + ++it; + } while(p < pend); +} + } // namespace msgpack diff --git a/cpp/msgpack/type/pair.hpp b/cpp/msgpack/type/pair.hpp index ba72c1f..6c68288 100644 --- a/cpp/msgpack/type/pair.hpp +++ b/cpp/msgpack/type/pair.hpp @@ -43,6 +43,17 @@ inline packer<Stream>& operator<< (packer<Stream>& o, const std::pair<T1, T2>& v return o; } +template <typename T1, typename T2> +inline void operator<< (object::object_zone& o, const std::pair<T1, T2>& v) +{ + o.type = type::ARRAY; + object* p = (object*)o.zone->malloc(sizeof(object)*2); + o.via.array.ptr = p; + o.via.array.size = 2; + p[0] = object(v.first, o.zone); + p[1] = object(v.second, o.zone); +} + } // namespace msgpack diff --git a/cpp/msgpack/type/raw.hpp b/cpp/msgpack/type/raw.hpp index 8c68aba..d854d28 100644 --- a/cpp/msgpack/type/raw.hpp +++ b/cpp/msgpack/type/raw.hpp @@ -77,6 +77,13 @@ inline packer<Stream>& operator<< (packer<Stream>& o, const type::raw_ref& v) return o; } +inline void operator<< (object& o, const type::raw_ref& v) +{ + o.type = type::RAW; + o.via.raw.ptr = v.ptr; + o.via.raw.size = v.size; +} + } // namespace msgpack diff --git a/cpp/msgpack/type/set.hpp b/cpp/msgpack/type/set.hpp index f2c5bfb..6f5fb18 100644 --- a/cpp/msgpack/type/set.hpp +++ b/cpp/msgpack/type/set.hpp @@ -48,6 +48,22 @@ inline packer<Stream>& operator<< (packer<Stream>& o, const std::set<T>& v) return o; } +template <typename T> +inline void operator<< (object::object_zone& o, const std::set<T>& v) +{ + o.type = type::ARRAY; + object* p = (object*)o.zone->malloc(sizeof(object)*v.size()); + object* const pend = p + v.size(); + o.via.array.ptr = p; + o.via.array.size = v.size(); + typename std::set<T>::const_iterator it(v.begin()); + do { + *p = object(*it, o.zone); + ++p; + ++it; + } while(p < pend); +} + template <typename T> inline std::multiset<T>& operator>> (object o, std::multiset<T>& v) @@ -73,6 +89,22 @@ inline packer<Stream>& operator<< (packer<Stream>& o, const std::multiset<T>& v) return o; } +template <typename T> +inline void operator<< (object::object_zone& o, const std::multiset<T>& v) +{ + o.type = type::ARRAY; + object* p = (object*)o.zone->malloc(sizeof(object)*v.size()); + object* const pend = p + v.size(); + o.via.array.ptr = p; + o.via.array.size = v.size(); + typename std::multiset<T>::const_iterator it(v.begin()); + do { + *p = object(*it, o.zone); + ++p; + ++it; + } while(p < pend); +} + } // namespace msgpack diff --git a/cpp/msgpack/type/string.hpp b/cpp/msgpack/type/string.hpp index a085d53..796603f 100644 --- a/cpp/msgpack/type/string.hpp +++ b/cpp/msgpack/type/string.hpp @@ -39,6 +39,15 @@ inline packer<Stream>& operator<< (packer<Stream>& o, const std::string& v) return o; } +inline void operator<< (object::object_zone& o, const std::string& v) +{ + o.type = type::RAW; + char* ptr = (char*)o.zone->malloc(v.size()); + o.via.raw.ptr = ptr; + o.via.raw.size = v.size(); + memcpy(ptr, v.data(), v.size()); +} + } // namespace msgpack diff --git a/cpp/msgpack/type/tuple.hpp.erb b/cpp/msgpack/type/tuple.hpp.erb index 2930ae0..e57fe98 100644 --- a/cpp/msgpack/type/tuple.hpp.erb +++ b/cpp/msgpack/type/tuple.hpp.erb @@ -165,6 +165,26 @@ const packer<Stream>& operator<< ( } <%}%> +inline void operator<< ( + object::object_zone& o, + const type::tuple<>& v) { + o.type = type::ARRAY; + o.via.array.ptr = NULL; + o.via.array.size = 0; +} +<%0.upto(GENERATION_LIMIT) {|i|%> +template <typename A0<%1.upto(i) {|j|%>, typename A<%=j%><%}%>> +inline void operator<< ( + object::object_zone& o, + const type::tuple<A0<%1.upto(i) {|j|%>, A<%=j%><%}%>>& v) { + o.type = type::ARRAY; + o.via.array.ptr = (object*)o.zone->malloc(sizeof(object)*<%=i+1%>); + o.via.array.size = <%=i+1%>; + <%0.upto(i) {|j|%> + o.via.array.ptr[<%=j%>] = object(v.template get<<%=j%>>(), o.zone);<%}%> +} +<%}%> + } // namespace msgpack #endif /* msgpack/type/tuple.hpp */ diff --git a/cpp/msgpack/type/vector.hpp b/cpp/msgpack/type/vector.hpp index 385d070..f9e709b 100644 --- a/cpp/msgpack/type/vector.hpp +++ b/cpp/msgpack/type/vector.hpp @@ -53,6 +53,22 @@ inline packer<Stream>& operator<< (packer<Stream>& o, const std::vector<T>& v) return o; } +template <typename T> +inline void operator<< (object::object_zone& o, const std::vector<T>& v) +{ + o.type = type::ARRAY; + object* p = (object*)o.zone->malloc(sizeof(object)*v.size()); + object* const pend = p + v.size(); + o.via.array.ptr = p; + o.via.array.size = v.size(); + typename std::vector<T>::const_iterator it(v.begin()); + do { + *p = object(*it, o.zone); + ++p; + ++it; + } while(p < pend); +} + } // namespace msgpack diff --git a/cpp/msgpack/zone.hpp.erb b/cpp/msgpack/zone.hpp.erb index 48988ab..8e69aa4 100644 --- a/cpp/msgpack/zone.hpp.erb +++ b/cpp/msgpack/zone.hpp.erb @@ -18,7 +18,6 @@ #ifndef MSGPACK_ZONE_HPP__ #define MSGPACK_ZONE_HPP__ -#include "msgpack/object.hpp" #include "msgpack/zone.h" #include <cstdlib> #include <memory> |
