cpp: fixes fix_int; updates test/fixint.cc

This commit is contained in:
frsyuki 2010-08-27 20:52:40 +09:00
parent 421bee3871
commit c87f7cb9ac
2 changed files with 115 additions and 1 deletions

View File

@ -19,6 +19,7 @@
#define MSGPACK_TYPE_FIXINT_HPP__ #define MSGPACK_TYPE_FIXINT_HPP__
#include "msgpack/object.hpp" #include "msgpack/object.hpp"
#include "msgpack/type/int.hpp"
namespace msgpack { namespace msgpack {
@ -29,10 +30,13 @@ template <typename T>
struct fix_int { struct fix_int {
fix_int() : value(0) { } fix_int() : value(0) { }
fix_int(T value) : value(value) { } fix_int(T value) : value(value) { }
operator T() const { return value; } operator T() const { return value; }
T get() const { return value; } T get() const { return value; }
private: private:
const T value; T value;
}; };
@ -50,6 +54,32 @@ typedef fix_int<int64_t> fix_int64;
} // namespace type } // 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> template <typename Stream>
inline packer<Stream>& operator<< (packer<Stream>& o, const type::fix_int8& v) inline packer<Stream>& operator<< (packer<Stream>& o, const type::fix_int8& v)
{ o.pack_fix_int8(v); return o; } { 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) inline packer<Stream>& operator<< (packer<Stream>& o, const type::fix_int64& v)
{ o.pack_fix_int64(v); return o; } { o.pack_fix_int64(v); return o; }
template <typename Stream> template <typename Stream>
inline packer<Stream>& operator<< (packer<Stream>& o, const type::fix_uint8& v) inline packer<Stream>& operator<< (packer<Stream>& o, const type::fix_uint8& v)
{ o.pack_fix_uint8(v); return o; } { 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; } { 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 } // namespace msgpack
#endif /* msgpack/type/fixint.hpp */ #endif /* msgpack/type/fixint.hpp */

View File

@ -22,3 +22,34 @@ TEST(fixint, size)
check_size<msgpack::type::fix_uint64>(9); 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>();
}