Added a conversion member function to msgpack::object.
If msgpack::object is nil then returns false else returns true and sets a value.
This commit is contained in:
Takatoshi Kondo 2015-08-31 09:05:09 +09:00
parent 9ee1168cc4
commit 33de24239a
3 changed files with 29 additions and 0 deletions

View File

@ -511,6 +511,15 @@ inline T* object::convert(T* v) const
return v; return v;
} }
template <typename T>
inline bool object::convert_if_not_nil(T& v) const {
if (is_nil()) {
return false;
}
convert(v);
return true;
}
#if defined(MSGPACK_USE_CPP03) #if defined(MSGPACK_USE_CPP03)
template <typename T> template <typename T>

View File

@ -149,6 +149,9 @@ struct object {
template <typename T> template <typename T>
T* convert(T* v) const; T* convert(T* v) const;
template <typename T>
bool convert_if_not_nil(T& v) const;
object(); object();
object(const msgpack_object& o); object(const msgpack_object& o);

View File

@ -93,3 +93,20 @@ TEST(convert, return_value_ref)
EXPECT_EQ(&i, &j); EXPECT_EQ(&i, &j);
EXPECT_EQ(i, j); EXPECT_EQ(i, j);
} }
TEST(convert, if_not_nil_nil)
{
msgpack::object obj;
int i;
EXPECT_FALSE(obj.convert_if_not_nil(i));
}
TEST(convert, if_not_nil_not_nil)
{
msgpack::zone z;
msgpack::object obj(1, z);
int i;
EXPECT_TRUE(obj.convert_if_not_nil(i));
EXPECT_EQ(i, 1);
}