Replaced nil with nil_t.

Added nil typedef for existing codes.
This commit is contained in:
Takatoshi Kondo 2016-01-18 22:12:38 +09:00
parent 8bf2f33782
commit ed5a4123b5
3 changed files with 34 additions and 28 deletions

View File

@ -43,7 +43,7 @@ namespace type {
template <typename STR, typename BIN, typename EXT>
struct basic_variant :
boost::variant<
nil, // NIL
nil_t, // NIL
bool, // BOOL
int64_t, // NEGATIVE_INTEGER
uint64_t, // POSITIVE_INTEGER
@ -62,7 +62,7 @@ struct basic_variant :
>,
private boost::totally_ordered<basic_variant<STR, BIN, EXT> > {
typedef boost::variant<
nil, // NIL
nil_t, // NIL
bool, // BOOL
int64_t, // NEGATIVE_INTEGER
uint64_t, // POSITIVE_INTEGER
@ -104,7 +104,7 @@ struct basic_variant :
basic_variant(unsigned long long v):base(uint64_t(v)) {}
bool is_nil() const {
return boost::get<nil>(this);
return boost::get<msgpack::type::nil_t>(this);
}
bool is_bool() const {
return boost::get<bool>(this);
@ -268,7 +268,7 @@ struct as<msgpack::type::basic_variant<STR, BIN, EXT> > {
msgpack::type::basic_variant<STR, BIN, EXT> operator()(msgpack::object const& o) const {
switch(o.type) {
case type::NIL:
return o.as<msgpack::type::nil>();
return o.as<msgpack::type::nil_t>();
case type::BOOLEAN:
return o.as<bool>();
case type::POSITIVE_INTEGER:
@ -304,7 +304,7 @@ struct convert<msgpack::type::basic_variant<STR, BIN, EXT> > {
msgpack::type::basic_variant<STR, BIN, EXT>& v) const {
switch(o.type) {
case type::NIL:
v = o.as<msgpack::type::nil>();
v = o.as<msgpack::type::nil_t>();
break;
case type::BOOLEAN:
v = o.as<bool>();
@ -366,8 +366,8 @@ struct pack<msgpack::type::basic_variant<STR, BIN, EXT> > {
namespace detail {
struct object_imp : boost::static_visitor<void> {
void operator()(msgpack::type::nil const& v) const {
object<msgpack::type::nil>()(o_, v);
void operator()(msgpack::type::nil_t const& v) const {
object<msgpack::type::nil_t>()(o_, v);
}
void operator()(bool const& v) const {
object<bool>()(o_, v);

View File

@ -21,13 +21,19 @@ MSGPACK_API_VERSION_NAMESPACE(v1) {
namespace type {
struct nil { };
struct nil_t { };
inline bool operator<(nil const& lhs, nil const& rhs) {
#if defined(MSGPACK_USE_LEGACY_NIL)
typedef nil_t nil;
#endif // defined(MSGPACK_USE_LEGACY_NIL)
inline bool operator<(nil_t const& lhs, nil_t const& rhs) {
return &lhs < &rhs;
}
inline bool operator==(nil const& lhs, nil const& rhs) {
inline bool operator==(nil_t const& lhs, nil_t const& rhs) {
return &lhs == &rhs;
}
@ -36,32 +42,32 @@ inline bool operator==(nil const& lhs, nil const& rhs) {
namespace adaptor {
template <>
struct convert<type::nil> {
msgpack::object const& operator()(msgpack::object const& o, type::nil&) const {
struct convert<type::nil_t> {
msgpack::object const& operator()(msgpack::object const& o, type::nil_t&) const {
if(o.type != msgpack::type::NIL) { throw msgpack::type_error(); }
return o;
}
};
template <>
struct pack<type::nil> {
struct pack<type::nil_t> {
template <typename Stream>
msgpack::packer<Stream>& operator()(msgpack::packer<Stream>& o, const type::nil&) const {
msgpack::packer<Stream>& operator()(msgpack::packer<Stream>& o, const type::nil_t&) const {
o.pack_nil();
return o;
}
};
template <>
struct object<type::nil> {
void operator()(msgpack::object& o, type::nil) const {
struct object<type::nil_t> {
void operator()(msgpack::object& o, type::nil_t) const {
o.type = msgpack::type::NIL;
}
};
template <>
struct object_with_zone<type::nil> {
void operator()(msgpack::object::with_zone& o, type::nil v) const {
struct object_with_zone<type::nil_t> {
void operator()(msgpack::object::with_zone& o, type::nil_t v) const {
static_cast<msgpack::object&>(o) << v;
}
};
@ -71,7 +77,7 @@ struct object_with_zone<type::nil> {
template <>
inline void msgpack::object::as<void>() const
{
msgpack::type::nil v;
msgpack::type::nil_t v;
convert(v);
}

View File

@ -17,7 +17,7 @@ const double kEPS = 1e-10;
TEST(MSGPACK_BOOST, pack_convert_variant_nil)
{
std::stringstream ss;
msgpack::type::variant val1 = msgpack::type::nil();
msgpack::type::variant val1 = msgpack::type::nil_t();
EXPECT_TRUE(val1.is_nil());
msgpack::pack(ss, val1);
@ -25,28 +25,28 @@ TEST(MSGPACK_BOOST, pack_convert_variant_nil)
msgpack::unpack(ret, ss.str().data(), ss.str().size());
msgpack::type::variant val2 = ret.get().as<msgpack::type::variant>();
EXPECT_TRUE(val2.is_nil());
EXPECT_NO_THROW(boost::get<msgpack::type::nil>(val2));
EXPECT_NO_THROW(boost::get<msgpack::type::nil_t>(val2));
}
TEST(MSGPACK_BOOST, object_variant_nil)
{
msgpack::type::variant val1 = msgpack::type::nil();
msgpack::type::variant val1 = msgpack::type::nil_t();
EXPECT_TRUE(val1.is_nil());
msgpack::object obj(val1);
msgpack::type::variant val2 = obj.as<msgpack::type::variant>();
EXPECT_TRUE(val2.is_nil());
EXPECT_NO_THROW(boost::get<msgpack::type::nil>(val2));
EXPECT_NO_THROW(boost::get<msgpack::type::nil_t>(val2));
}
TEST(MSGPACK_BOOST, object_with_zone_variant_nil)
{
msgpack::zone z;
msgpack::type::variant val1 = msgpack::type::nil();
msgpack::type::variant val1 = msgpack::type::nil_t();
EXPECT_TRUE(val1.is_nil());
msgpack::object obj(val1, z);
msgpack::type::variant val2 = obj.as<msgpack::type::variant>();
EXPECT_TRUE(val2.is_nil());
EXPECT_NO_THROW(boost::get<msgpack::type::nil>(val2));
EXPECT_NO_THROW(boost::get<msgpack::type::nil_t>(val2));
}
// nil (default constructor)
@ -63,7 +63,7 @@ TEST(MSGPACK_BOOST, pack_convert_variant_nil_default)
msgpack::unpack(ret, ss.str().data(), ss.str().size());
msgpack::type::variant val2 = ret.get().as<msgpack::type::variant>();
EXPECT_TRUE(val2.is_nil());
EXPECT_NO_THROW(boost::get<msgpack::type::nil>(val2));
EXPECT_NO_THROW(boost::get<msgpack::type::nil_t>(val2));
}
TEST(MSGPACK_BOOST, object_variant_nil_default)
@ -73,7 +73,7 @@ TEST(MSGPACK_BOOST, object_variant_nil_default)
msgpack::object obj(val1);
msgpack::type::variant val2 = obj.as<msgpack::type::variant>();
EXPECT_TRUE(val2.is_nil());
EXPECT_NO_THROW(boost::get<msgpack::type::nil>(val2));
EXPECT_NO_THROW(boost::get<msgpack::type::nil_t>(val2));
}
TEST(MSGPACK_BOOST, object_with_zone_variant_nil_default)
@ -84,7 +84,7 @@ TEST(MSGPACK_BOOST, object_with_zone_variant_nil_default)
msgpack::object obj(val1, z);
msgpack::type::variant val2 = obj.as<msgpack::type::variant>();
EXPECT_TRUE(val2.is_nil());
EXPECT_NO_THROW(boost::get<msgpack::type::nil>(val2));
EXPECT_NO_THROW(boost::get<msgpack::type::nil_t>(val2));
}
// bool