Modified passed by value of object to const reference of object.

Removed C function call in operator==.
This commit is contained in:
Takatoshi Kondo 2013-09-04 13:28:23 +09:00
parent 56b0ad6809
commit 8a08548f36
5 changed files with 99 additions and 37 deletions

View File

@ -24,7 +24,7 @@
{ \
msgpack::type::make_define(__VA_ARGS__).msgpack_pack(pk); \
} \
void msgpack_unpack(msgpack::object o) \
void msgpack_unpack(msgpack::object const& o) \
{ \
msgpack::type::make_define(__VA_ARGS__).msgpack_unpack(o); \
}\
@ -38,7 +38,7 @@
#define MSGPACK_ADD_ENUM(enum) \
namespace msgpack { \
template <> \
inline enum& operator>> (object o, enum& v) \
inline enum& operator>> (object const& o, enum& v) \
{ \
int tmp; \
o >> tmp; \
@ -71,7 +71,7 @@ struct define_imp {
define_imp<Tuple, N-1>::pack(pk, t);
pk.pack(std::get<N-1>(t));
}
static void unpack(msgpack::object o, Tuple& t) {
static void unpack(msgpack::object const& o, Tuple& t) {
define_imp<Tuple, N-1>::unpack(o, t);
const size_t size = o.via.array.size;
if(size <= N-1) { return; }
@ -89,7 +89,7 @@ struct define_imp<Tuple, 1> {
static void pack(Packer& pk, Tuple const& t) {
pk.pack(std::get<0>(t));
}
static void unpack(msgpack::object o, Tuple& t) {
static void unpack(msgpack::object const& o, Tuple& t) {
const size_t size = o.via.array.size;
if(size <= 0) { return; }
o.via.array.ptr[0].convert(&std::get<0>(t));
@ -112,7 +112,7 @@ struct define {
define_imp<tuple<Args&...>, sizeof...(Args)>::pack(pk, a);
}
void msgpack_unpack(msgpack::object o)
void msgpack_unpack(msgpack::object const& o)
{
if(o.type != type::ARRAY) { throw type_error(); }
@ -139,7 +139,7 @@ struct define<> {
{
pk.pack_array(0);
}
void msgpack_unpack(msgpack::object o)
void msgpack_unpack(msgpack::object const& o)
{
if(o.type != type::ARRAY) { throw type_error(); }
}

View File

@ -138,7 +138,7 @@ const packer<Stream>& operator<< (
template <typename Tuple, std::size_t N>
struct Converter {
static void convert(
object o,
object const& o,
Tuple& v) {
Converter<Tuple, N-1>::convert(o, v);
o.via.array.ptr[N-1].convert<typename std::remove_reference<decltype(type::get<N-1>(v))>::type>(&type::get<N-1>(v));
@ -148,7 +148,7 @@ struct Converter {
template <typename Tuple>
struct Converter<Tuple, 1> {
static void convert (
object o,
object const& o,
Tuple& v) {
o.via.array.ptr[0].convert<typename std::remove_reference<decltype(type::get<0>(v))>::type>(&type::get<0>(v));
}
@ -156,7 +156,7 @@ struct Converter<Tuple, 1> {
template <typename... Args>
type::tuple<Args...>& operator>> (
object o,
object const& o,
type::tuple<Args...>& v) {
if(o.type != type::ARRAY) { throw type_error(); }
if(o.via.array.size < sizeof...(Args)) { throw type_error(); }

View File

@ -125,22 +125,22 @@ private:
};
bool operator==(const object x, const object y);
bool operator!=(const object x, const object y);
bool operator==(const object& x, const object& y);
bool operator!=(const object& x, const object& y);
template <typename T>
bool operator==(const object x, const T& y);
bool operator==(const object& x, const T& y);
template <typename T>
bool operator==(const T& y, const object x);
bool operator==(const T& y, const object& x);
template <typename T>
bool operator!=(const object x, const T& y);
bool operator!=(const object& x, const T& y);
template <typename T>
bool operator!=(const T& y, const object x);
bool operator!=(const T& y, const object& x);
std::ostream& operator<< (std::ostream& s, const object o);
std::ostream& operator<< (std::ostream& s, const object& o);
// serialize operator
@ -149,7 +149,7 @@ packer<Stream>& operator<< (packer<Stream>& o, const T& v);
// convert operator
template <typename T>
T& operator>> (object o, T& v);
T& operator>> (object const& o, T& v);
// deconvert operator
template <typename T>
@ -157,7 +157,7 @@ void operator<< (object::with_zone& o, const T& v);
struct object::implicit_type {
implicit_type(object o) : obj(o) { }
implicit_type(object const& o) : obj(o) { }
~implicit_type() { }
template <typename T>
@ -184,7 +184,7 @@ public:
o << static_cast<const msgpack_type&>(*this);
}
void msgpack_unpack(object o)
void msgpack_unpack(object const& o)
{
o >> static_cast<msgpack_type&>(*this);
}
@ -199,14 +199,14 @@ inline packer<Stream>& packer<Stream>::pack(const T& v)
return *this;
}
inline object& operator>> (object o, object& v)
inline object& operator>> (object const& o, object& v)
{
v = o;
return v;
}
template <typename T>
inline T& operator>> (object o, T& v)
inline T& operator>> (object const& o, T& v)
{
v.msgpack_unpack(o.convert());
return v;
@ -235,20 +235,82 @@ void operator<< (object::with_zone& o, const T& v)
}
inline bool operator==(const object x, const object y)
inline bool operator==(const object& x, const object& y)
{
return msgpack_object_equal(x, y);
if(x.type != y.type) { return false; }
switch(x.type) {
case type::NIL:
return true;
case type::BOOLEAN:
return x.via.boolean == y.via.boolean;
case type::POSITIVE_INTEGER:
return x.via.u64 == y.via.u64;
case type::NEGATIVE_INTEGER:
return x.via.i64 == y.via.i64;
case type::DOUBLE:
return x.via.dec == y.via.dec;
case type::RAW:
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.array.size != y.via.array.size) {
return false;
} else if(x.via.array.size == 0) {
return true;
} else {
object* px = x.via.array.ptr;
object* const pxend = x.via.array.ptr + x.via.array.size;
object* py = y.via.array.ptr;
do {
if(!(*px == *py)) {
return false;
}
++px;
++py;
} while(px < pxend);
return true;
}
case type::MAP:
if(x.via.map.size != y.via.map.size) {
return false;
} else if(x.via.map.size == 0) {
return true;
} else {
object_kv* px = x.via.map.ptr;
object_kv* const pxend = x.via.map.ptr + x.via.map.size;
object_kv* py = y.via.map.ptr;
do {
if(!(px->key == py->key) || !(px->val == py->val)) {
return false;
}
++px;
++py;
} while(px < pxend);
return true;
}
default:
return false;
}
}
template <typename T>
inline bool operator==(const object x, const T& y)
inline bool operator==(const object& x, const T& y)
try {
return x == object(y);
} catch (msgpack::type_error&) {
return false;
}
inline bool operator!=(const object x, const object y)
inline bool operator!=(const object& x, const object& y)
{ return !(x == y); }
template <typename T>
@ -256,11 +318,11 @@ inline bool operator==(const T& y, const object x)
{ return x == y; }
template <typename T>
inline bool operator!=(const object x, const T& y)
inline bool operator!=(const object& x, const T& y)
{ return !(x == y); }
template <typename T>
inline bool operator!=(const T& y, const object x)
inline bool operator!=(const T& y, const object& x)
{ return x != y; }
@ -335,7 +397,7 @@ inline object::operator msgpack_object() const
// obsolete
template <typename T>
inline void convert(T& v, object o)
inline void convert(T& v, object const& o)
{
o.convert(&v);
}
@ -412,7 +474,7 @@ packer<Stream>& operator<< (packer<Stream>& o, const object& v)
}
}
std::ostream& operator<< (std::ostream& s, const object o)
std::ostream& operator<< (std::ostream& s, const object& o)
{
switch(o.type) {
case type::NIL:

View File

@ -24,7 +24,7 @@
{ \
msgpack::type::make_define(__VA_ARGS__).msgpack_pack(pk); \
} \
void msgpack_unpack(msgpack::object o) \
void msgpack_unpack(msgpack::object const& o) \
{ \
msgpack::type::make_define(__VA_ARGS__).msgpack_unpack(o); \
}\
@ -38,7 +38,7 @@
#define MSGPACK_ADD_ENUM(enum) \
namespace msgpack { \
template <> \
inline enum& operator>> (object o, enum& v) \
inline enum& operator>> (object const& o, enum& v) \
{ \
int tmp; \
o >> tmp; \
@ -78,7 +78,7 @@ struct define<> {
{
pk.pack_array(0);
}
void msgpack_unpack(msgpack::object o)
void msgpack_unpack(msgpack::object const& o)
{
if(o.type != type::ARRAY) { throw type_error(); }
}
@ -103,7 +103,7 @@ struct define<A0<%1.upto(i) {|j|%>, A<%=j%><%}%>> {
<%0.upto(i) {|j|%>
pk.pack(a<%=j%>);<%}%>
}
void msgpack_unpack(msgpack::object o)
void msgpack_unpack(msgpack::object const& o)
{
if(o.type != type::ARRAY) { throw type_error(); }
const size_t size = o.via.array.size;

View File

@ -92,7 +92,7 @@ private:
template <>
struct tuple<> {
tuple() {}
tuple(object o) { o.convert(this); }
tuple(object const& o) { o.convert(this); }
typedef tuple<> value_type;
};
<%0.upto(GENERATION_LIMIT) {|i|%>
@ -102,7 +102,7 @@ struct tuple<A0<%1.upto(i) {|j|%>, A<%=j%><%}%>> {
tuple() {}
tuple(typename tuple_type<A0>::transparent_reference _a0<%1.upto(i) {|j|%>, typename tuple_type<A<%=j%>>::transparent_reference _a<%=j%><%}%>) :
a0(_a0)<%1.upto(i) {|j|%>, a<%=j%>(_a<%=j%>)<%}%> {}
tuple(object o) { o.convert(this); }
tuple(object const& o) { o.convert(this); }
template <int N> typename tuple_element<value_type, N>::reference get()
{ return tuple_element<value_type, N>(*this).get(); }
template <int N> typename const_tuple_element<value_type, N>::const_reference get() const
@ -134,7 +134,7 @@ tuple<A0<%1.upto(i) {|j|%>, A<%=j%><%}%>> make_tuple(typename tuple_type<A0>::tr
} // namespace type
inline type::tuple<>& operator>> (
object o,
object const& o,
type::tuple<>& v) {
if(o.type != type::ARRAY) { throw type_error(); }
return v;
@ -142,7 +142,7 @@ inline type::tuple<>& operator>> (
<%0.upto(GENERATION_LIMIT) {|i|%>
template <typename A0<%1.upto(i) {|j|%>, typename A<%=j%><%}%>>
type::tuple<A0<%1.upto(i) {|j|%>, A<%=j%><%}%>>& operator>> (
object o,
object const& o,
type::tuple<A0<%1.upto(i) {|j|%>, A<%=j%><%}%>>& v) {
if(o.type != type::ARRAY) { throw type_error(); }
if(o.via.array.size < <%=i+1%>) { throw type_error(); }