summaryrefslogtreecommitdiff
path: root/cpp
diff options
context:
space:
mode:
Diffstat (limited to 'cpp')
-rw-r--r--cpp/object.cpp48
-rw-r--r--cpp/object.hpp56
-rw-r--r--cpp/type/array.hpp6
-rw-r--r--cpp/type/map.hpp36
-rw-r--r--cpp/type/raw.hpp10
-rw-r--r--cpp/type/tuple.hpp.erb4
-rw-r--r--cpp/unpack.cpp22
7 files changed, 98 insertions, 84 deletions
diff --git a/cpp/object.cpp b/cpp/object.cpp
index d217c06..eb45f77 100644
--- a/cpp/object.cpp
+++ b/cpp/object.cpp
@@ -46,16 +46,16 @@ std::ostream& operator<< (std::ostream& s, const object o)
break;
case type::RAW:
- (s << '"').write(o.via.ref.ptr, o.via.ref.size) << '"';
+ (s << '"').write(o.via.raw.ptr, o.via.raw.size) << '"';
break;
case type::ARRAY:
s << "[";
- if(o.via.container.size != 0) {
- object* p(o.via.container.ptr);
+ if(o.via.array.size != 0) {
+ object* p(o.via.array.ptr);
s << *p;
++p;
- for(object* const pend(o.via.container.ptr + o.via.container.size);
+ for(object* const pend(o.via.array.ptr + o.via.array.size);
p < pend; ++p) {
s << ", " << *p;
}
@@ -66,17 +66,13 @@ std::ostream& operator<< (std::ostream& s, const object o)
case type::MAP:
s << "{";
- if(o.via.container.size != 0) {
- object* p(o.via.container.ptr);
- object* const pend(o.via.container.ptr + o.via.container.size*2);
- s << *p; ++p;
- s << "=>";
- s << *p; ++p;
- while(p < pend) {
- s << ", ";
- s << *p; ++p;
- s << "=>";
- s << *p; ++p;
+ if(o.via.map.size != 0) {
+ object_kv* p(o.via.map.ptr);
+ s << p->key << "=>" << p->val;
+ ++p;
+ for(object_kv* const pend(o.via.map.ptr + o.via.map.size);
+ p < pend; ++p) {
+ s << ", " << p->key << "=>" << p->val;
}
}
s << "}";
@@ -109,14 +105,14 @@ bool operator==(const object x, const object y)
return x.via.i64 == y.via.i64;
case type::RAW:
- return x.via.ref.size == y.via.ref.size &&
- memcmp(x.via.ref.ptr, y.via.ref.ptr, x.via.ref.size) == 0;
+ return x.via.raw.size == y.via.raw.size &&
+ memcmp(x.via.raw.ptr, y.via.raw.ptr, x.via.raw.size) == 0;
case type::ARRAY:
- if(x.via.container.size != y.via.container.size) { return false; }
- for(object* px(x.via.container.ptr),
- * const pxend(x.via.container.ptr + x.via.container.size),
- * py(y.via.container.ptr);
+ if(x.via.array.size != y.via.array.size) { return false; }
+ for(object* px(x.via.array.ptr),
+ * const pxend(x.via.array.ptr + x.via.array.size),
+ * py(y.via.array.ptr);
px < pxend; ++px, ++py) {
if(*px != *py) { return false; }
}
@@ -124,12 +120,12 @@ bool operator==(const object x, const object y)
// FIXME loop optimiziation
case type::MAP:
- if(x.via.container.size != y.via.container.size) { return false; }
- for(object* px(x.via.container.ptr),
- * const pxend(x.via.container.ptr + x.via.container.size*2),
- * py(y.via.container.ptr);
+ if(x.via.map.size != y.via.map.size) { return false; }
+ for(object_kv* px(x.via.map.ptr),
+ * const pxend(x.via.map.ptr + x.via.map.size),
+ * py(y.via.map.ptr);
px < pxend; ++px, ++py) {
- if(*px != *py) { return false; }
+ if(px->key != py->key || px->val != py->val) { return false; }
}
return true;
diff --git a/cpp/object.hpp b/cpp/object.hpp
index c6f1e4a..08e715d 100644
--- a/cpp/object.hpp
+++ b/cpp/object.hpp
@@ -45,20 +45,33 @@ namespace type {
}
+struct object;
+struct object_kv;
+
+struct object_array {
+ uint32_t size;
+ object* ptr;
+};
+
+struct object_map {
+ uint32_t size;
+ object_kv* ptr;
+};
+
+struct object_raw {
+ uint32_t size;
+ const char* ptr;
+};
+
struct object {
union union_type {
bool boolean;
uint64_t u64;
int64_t i64;
double dec;
- struct {
- object* ptr;
- uint32_t size;
- } container;
- struct {
- const char* ptr;
- uint32_t size;
- } ref;
+ object_array array;
+ object_map map;
+ object_raw raw;
};
type::object_type type;
@@ -79,6 +92,11 @@ public:
implicit_type convert();
};
+struct object_kv {
+ object key;
+ object val;
+};
+
bool operator==(const object x, const object y);
bool operator!=(const object x, const object y);
@@ -258,14 +276,14 @@ packer<Stream>& operator<< (packer<Stream>& o, const object& v)
return o;
case type::RAW:
- o.pack_raw(v.via.ref.size);
- o.pack_raw_body(v.via.ref.ptr, v.via.ref.size);
+ o.pack_raw(v.via.raw.size);
+ o.pack_raw_body(v.via.raw.ptr, v.via.raw.size);
return o;
case type::ARRAY:
- o.pack_array(v.via.container.size);
- for(object* p(v.via.container.ptr),
- * const pend(v.via.container.ptr + v.via.container.size);
+ o.pack_array(v.via.array.size);
+ for(object* p(v.via.array.ptr),
+ * const pend(v.via.array.ptr + v.via.array.size);
p < pend; ++p) {
*p >> o;
}
@@ -273,12 +291,12 @@ packer<Stream>& operator<< (packer<Stream>& o, const object& v)
// FIXME loop optimiziation
case type::MAP:
- o.pack_map(v.via.container.size);
- for(object* p(v.via.container.ptr),
- * const pend(v.via.container.ptr + v.via.container.size*2);
- p < pend; ) {
- *p >> o; ++p;
- *p >> o; ++p;
+ o.pack_map(v.via.map.size);
+ for(object_kv* p(v.via.map.ptr),
+ * const pend(v.via.map.ptr + v.via.map.size);
+ p < pend; ++p) {
+ p->key >> o;
+ p->val >> o;
}
return o;
// FIXME loop optimiziation
diff --git a/cpp/type/array.hpp b/cpp/type/array.hpp
index f9f8038..b2a81ef 100644
--- a/cpp/type/array.hpp
+++ b/cpp/type/array.hpp
@@ -28,9 +28,9 @@ template <typename T>
inline std::vector<T> operator>> (object o, std::vector<T>& v)
{
if(o.type != type::ARRAY) { throw type_error(); }
- v.resize(o.via.container.size);
- object* p = o.via.container.ptr;
- object* const pend = o.via.container.ptr + o.via.container.size;
+ v.resize(o.via.array.size);
+ object* p = o.via.array.ptr;
+ object* const pend = o.via.array.ptr + o.via.array.size;
T* it = &v.front();
for(; p < pend; ++p, ++it) {
p->convert(it);
diff --git a/cpp/type/map.hpp b/cpp/type/map.hpp
index 619c36e..4585d66 100644
--- a/cpp/type/map.hpp
+++ b/cpp/type/map.hpp
@@ -46,13 +46,13 @@ template <typename K, typename V>
inline type::assoc_vector<K,V>& operator>> (object o, type::assoc_vector<K,V>& v)
{
if(o.type != type::MAP) { throw type_error(); }
- v.resize(o.via.container.size);
- object* p = o.via.container.ptr;
- object* const pend = o.via.container.ptr + o.via.container.size;
+ v.resize(o.via.map.size);
+ object_kv* p = o.via.map.ptr;
+ object_kv* const pend = o.via.map.ptr + o.via.map.size;
std::pair<K, V>* it(&v.front());
- for(; p < pend; ++it) {
- p->convert(&it->first); ++p;
- p->convert(&it->second); ++p;
+ for(; p < pend; ++p, ++it) {
+ p->key.convert(&it->first);
+ p->val.convert(&it->second);
}
std::sort(v.begin(), v.end(), type::detail::pair_first_less<K,V>());
return v;
@@ -75,18 +75,18 @@ template <typename K, typename V>
inline std::map<K, V> operator>> (object o, std::map<K, V>& v)
{
if(o.type != type::MAP) { throw type_error(); }
- object* p(o.via.container.ptr);
- object* const pend(o.via.container.ptr + o.via.container.size*2);
- while(p < pend) {
+ object_kv* p(o.via.map.ptr);
+ object_kv* const pend(o.via.map.ptr + o.via.map.size);
+ for(; p != pend; ++p) {
K key;
- p->convert(&key); ++p;
+ p->key.convert(&key);
typename std::map<K,V>::iterator it(v.find(key));
if(it != v.end()) {
V val;
- p->convert(&val); ++p;
+ p->val.convert(&val);
it->insert( std::pair<K,V>(key, val) );
} else {
- p->convert(&it->second); ++p;
+ p->val.convert(&it->second);
}
}
return v;
@@ -109,12 +109,12 @@ template <typename K, typename V>
inline std::multimap<K, V> operator>> (object o, std::multimap<K, V>& v)
{
if(o.type != type::MAP) { throw type_error(); }
- object* p = o.via.container.ptr;
- object* const pend = o.via.container.ptr + o.via.container.size*2;
- while(p < pend) {
+ object_kv* p(o.via.map.ptr);
+ object_kv* const pend(o.via.map.ptr + o.via.map.size);
+ for(; p != pend; ++p) {
std::pair<K, V> value;
- p->convert(&value.first); ++p;
- p->convert(&value.second); ++p;
+ p->key.convert(&value.first);
+ p->val.convert(&value.second);
v.insert(value);
}
return v;
@@ -123,7 +123,7 @@ inline std::multimap<K, V> operator>> (object o, std::multimap<K, V>& v)
template <typename Stream, typename K, typename V>
inline packer<Stream>& operator<< (packer<Stream>& o, const std::multimap<K,V>& v)
{
- o.pack_multimap(v.size());
+ o.pack_map(v.size());
for(typename std::multimap<K,V>::const_iterator it(v.begin()), it_end(v.end());
it != it_end; ++it) {
o.pack(it->first);
diff --git a/cpp/type/raw.hpp b/cpp/type/raw.hpp
index e4f9dd3..b102ee8 100644
--- a/cpp/type/raw.hpp
+++ b/cpp/type/raw.hpp
@@ -27,11 +27,11 @@ namespace msgpack {
namespace type {
struct raw_ref {
- raw_ref() : ptr(NULL), size(0) {}
- raw_ref(const char* p, uint32_t s) : ptr(p), size(s) {}
+ raw_ref() : size(0), ptr(NULL) {}
+ raw_ref(const char* p, uint32_t s) : size(s), ptr(p) {}
- const char* ptr;
uint32_t size;
+ const char* ptr;
std::string str() { return std::string(ptr, size); }
@@ -64,8 +64,8 @@ struct raw_ref {
inline type::raw_ref& operator>> (object o, type::raw_ref& v)
{
if(o.type != type::RAW) { throw type_error(); }
- v.ptr = o.via.ref.ptr;
- v.size = o.via.ref.size;
+ v.ptr = o.via.raw.ptr;
+ v.size = o.via.raw.size;
return v;
}
diff --git a/cpp/type/tuple.hpp.erb b/cpp/type/tuple.hpp.erb
index a66c57e..a20f5d9 100644
--- a/cpp/type/tuple.hpp.erb
+++ b/cpp/type/tuple.hpp.erb
@@ -136,9 +136,9 @@ type::tuple<A0<%1.upto(i) {|j|%>, A<%=j%><%}%>>& operator>> (
object o,
type::tuple<A0<%1.upto(i) {|j|%>, A<%=j%><%}%>>& v) {
if(o.type != type::ARRAY) { throw type_error(); }
- if(o.via.container.size < <%=i+1%>) { throw type_error(); }
+ if(o.via.array.size < <%=i+1%>) { throw type_error(); }
<%0.upto(i) {|j|%>
- o.via.container.ptr[<%=j%>].convert<A<%=j%>>(&v.template get<<%=j%>>());<%}%>
+ o.via.array.ptr[<%=j%>].convert<A<%=j%>>(&v.template get<<%=j%>>());<%}%>
return v;
}
<%}%>
diff --git a/cpp/unpack.cpp b/cpp/unpack.cpp
index 62088d6..0b9a476 100644
--- a/cpp/unpack.cpp
+++ b/cpp/unpack.cpp
@@ -104,36 +104,36 @@ static inline object msgpack_unpack_array(unpack_user* u, unsigned int n)
{
object o;
o.type = type::ARRAY;
- o.via.container.size = 0;
- o.via.container.ptr = (object*)u->z->malloc(n*sizeof(object));
+ o.via.array.size = 0;
+ o.via.array.ptr = (object*)u->z->malloc(n*sizeof(object));
return o;
}
static inline void msgpack_unpack_array_item(unpack_user* u, object* c, object o)
-{ c->via.container.ptr[ c->via.container.size++ ] = o; }
+{ c->via.array.ptr[c->via.array.size++] = o; }
static inline object msgpack_unpack_map(unpack_user* u, unsigned int n)
{
object o;
o.type = type::MAP;
- o.via.container.size = 0;
- o.via.container.ptr = (object*)u->z->malloc(n*2*sizeof(object));
+ o.via.map.size = 0;
+ o.via.map.ptr = (object_kv*)u->z->malloc(n*sizeof(object_kv));
return o;
}
static inline void msgpack_unpack_map_item(unpack_user* u, object* c, object k, object v)
{
- c->via.container.ptr[ c->via.container.size ] = k;
- c->via.container.ptr[ c->via.container.size+1 ] = v;
- ++c->via.container.size;
+ c->via.map.ptr[c->via.map.size].key = k;
+ c->via.map.ptr[c->via.map.size].val = v;
+ ++c->via.map.size;
}
static inline object msgpack_unpack_raw(unpack_user* u, const char* b, const char* p, unsigned int l)
{
object o;
o.type = type::RAW;
- o.via.ref.ptr = p;
- o.via.ref.size = l;
+ o.via.raw.ptr = p;
+ o.via.raw.size = l;
u->referenced = true;
return o;
}
@@ -332,7 +332,7 @@ object unpacker::data()
void unpacker::reset()
{
- if(!m_zone->empty()) { delete release_zone(); }
+ //if(!m_zone->empty()) { delete release_zone(); }
as_ctx(m_ctx)->reset();
}