summaryrefslogtreecommitdiff
path: root/cpp
diff options
context:
space:
mode:
authorfrsyuki <frsyuki@users.sourceforge.jp>2010-08-27 20:52:40 +0900
committerfrsyuki <frsyuki@users.sourceforge.jp>2010-08-27 20:52:40 +0900
commitc87f7cb9ac87525c9531ddf6b2fd16499535967c (patch)
treee5ef732578faafad99d5579a9d6eb760e9ebb854 /cpp
parent421bee38719ee66d79cd8c376c871678dbb55a1d (diff)
downloadmsgpack-python-c87f7cb9ac87525c9531ddf6b2fd16499535967c.tar.gz
cpp: fixes fix_int; updates test/fixint.cc
Diffstat (limited to 'cpp')
-rw-r--r--cpp/src/msgpack/type/fixint.hpp85
-rw-r--r--cpp/test/fixint.cc31
2 files changed, 115 insertions, 1 deletions
diff --git a/cpp/src/msgpack/type/fixint.hpp b/cpp/src/msgpack/type/fixint.hpp
index da58415..ebe2696 100644
--- a/cpp/src/msgpack/type/fixint.hpp
+++ b/cpp/src/msgpack/type/fixint.hpp
@@ -19,6 +19,7 @@
#define MSGPACK_TYPE_FIXINT_HPP__
#include "msgpack/object.hpp"
+#include "msgpack/type/int.hpp"
namespace msgpack {
@@ -29,10 +30,13 @@ template <typename T>
struct fix_int {
fix_int() : value(0) { }
fix_int(T value) : value(value) { }
+
operator T() const { return value; }
+
T get() const { return value; }
+
private:
- const T value;
+ T value;
};
@@ -50,6 +54,32 @@ typedef fix_int<int64_t> fix_int64;
} // namespace type
+inline type::fix_int8& operator>> (object o, type::fix_int8& v)
+ { v = type::detail::convert_integer<int8_t>(o); return v; }
+
+inline type::fix_int16& operator>> (object o, type::fix_int16& v)
+ { v = type::detail::convert_integer<int16_t>(o); return v; }
+
+inline type::fix_int32& operator>> (object o, type::fix_int32& v)
+ { v = type::detail::convert_integer<int32_t>(o); return v; }
+
+inline type::fix_int64& operator>> (object o, type::fix_int64& v)
+ { v = type::detail::convert_integer<int64_t>(o); return v; }
+
+
+inline type::fix_uint8& operator>> (object o, type::fix_uint8& v)
+ { v = type::detail::convert_integer<uint8_t>(o); return v; }
+
+inline type::fix_uint16& operator>> (object o, type::fix_uint16& v)
+ { v = type::detail::convert_integer<uint16_t>(o); return v; }
+
+inline type::fix_uint32& operator>> (object o, type::fix_uint32& v)
+ { v = type::detail::convert_integer<uint32_t>(o); return v; }
+
+inline type::fix_uint64& operator>> (object o, type::fix_uint64& v)
+ { v = type::detail::convert_integer<uint64_t>(o); return v; }
+
+
template <typename Stream>
inline packer<Stream>& operator<< (packer<Stream>& o, const type::fix_int8& v)
{ o.pack_fix_int8(v); return o; }
@@ -66,6 +96,7 @@ template <typename Stream>
inline packer<Stream>& operator<< (packer<Stream>& o, const type::fix_int64& v)
{ o.pack_fix_int64(v); return o; }
+
template <typename Stream>
inline packer<Stream>& operator<< (packer<Stream>& o, const type::fix_uint8& v)
{ o.pack_fix_uint8(v); return o; }
@@ -83,6 +114,58 @@ inline packer<Stream>& operator<< (packer<Stream>& o, const type::fix_uint64& v)
{ o.pack_fix_uint64(v); return o; }
+inline void operator<< (object& o, type::fix_int8 v)
+ { v.get() < 0 ? o.type = type::NEGATIVE_INTEGER, o.via.i64 = v.get() : o.type = type::POSITIVE_INTEGER, o.via.u64 = v.get(); }
+
+inline void operator<< (object& o, type::fix_int16 v)
+ { v.get() < 0 ? o.type = type::NEGATIVE_INTEGER, o.via.i64 = v.get() : o.type = type::POSITIVE_INTEGER, o.via.u64 = v.get(); }
+
+inline void operator<< (object& o, type::fix_int32 v)
+ { v.get() < 0 ? o.type = type::NEGATIVE_INTEGER, o.via.i64 = v.get() : o.type = type::POSITIVE_INTEGER, o.via.u64 = v.get(); }
+
+inline void operator<< (object& o, type::fix_int64 v)
+ { v.get() < 0 ? o.type = type::NEGATIVE_INTEGER, o.via.i64 = v.get() : o.type = type::POSITIVE_INTEGER, o.via.u64 = v.get(); }
+
+
+inline void operator<< (object& o, type::fix_uint8 v)
+ { o.type = type::POSITIVE_INTEGER, o.via.u64 = v.get(); }
+
+inline void operator<< (object& o, type::fix_uint16 v)
+ { o.type = type::POSITIVE_INTEGER, o.via.u64 = v.get(); }
+
+inline void operator<< (object& o, type::fix_uint32 v)
+ { o.type = type::POSITIVE_INTEGER, o.via.u64 = v.get(); }
+
+inline void operator<< (object& o, type::fix_uint64 v)
+ { o.type = type::POSITIVE_INTEGER, o.via.u64 = v.get(); }
+
+
+inline void operator<< (object::with_zone& o, type::fix_int8 v)
+ { static_cast<object&>(o) << v; }
+
+inline void operator<< (object::with_zone& o, type::fix_int16 v)
+ { static_cast<object&>(o) << v; }
+
+inline void operator<< (object::with_zone& o, type::fix_int32 v)
+ { static_cast<object&>(o) << v; }
+
+inline void operator<< (object::with_zone& o, type::fix_int64 v)
+ { static_cast<object&>(o) << v; }
+
+
+inline void operator<< (object::with_zone& o, type::fix_uint8 v)
+ { static_cast<object&>(o) << v; }
+
+inline void operator<< (object::with_zone& o, type::fix_uint16 v)
+ { static_cast<object&>(o) << v; }
+
+inline void operator<< (object::with_zone& o, type::fix_uint32 v)
+ { static_cast<object&>(o) << v; }
+
+inline void operator<< (object::with_zone& o, type::fix_uint64 v)
+ { static_cast<object&>(o) << v; }
+
+
} // namespace msgpack
#endif /* msgpack/type/fixint.hpp */
diff --git a/cpp/test/fixint.cc b/cpp/test/fixint.cc
index 64a39ac..63288a1 100644
--- a/cpp/test/fixint.cc
+++ b/cpp/test/fixint.cc
@@ -22,3 +22,34 @@ TEST(fixint, size)
check_size<msgpack::type::fix_uint64>(9);
}
+
+template <typename T>
+void check_convert() {
+ T v1(-11);
+ msgpack::sbuffer sbuf;
+ msgpack::pack(sbuf, v1);
+
+ msgpack::unpacked msg;
+ msgpack::unpack(&msg, sbuf.data(), sbuf.size());
+
+ T v2;
+ msg.get().convert(&v2);
+
+ EXPECT_EQ(v1.get(), v2.get());
+
+ EXPECT_EQ(msg.get(), msgpack::object(T(v1.get())));
+}
+
+TEST(fixint, convert)
+{
+ check_convert<msgpack::type::fix_int8>();
+ check_convert<msgpack::type::fix_int16>();
+ check_convert<msgpack::type::fix_int32>();
+ check_convert<msgpack::type::fix_int64>();
+
+ check_convert<msgpack::type::fix_uint8>();
+ check_convert<msgpack::type::fix_uint16>();
+ check_convert<msgpack::type::fix_uint32>();
+ check_convert<msgpack::type::fix_uint64>();
+}
+