mirror of
https://github.com/msgpack/msgpack-c.git
synced 2025-05-20 21:08:36 +02:00
Fixed #357.
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:
parent
9ee1168cc4
commit
33de24239a
@ -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>
|
||||||
|
@ -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);
|
||||||
|
@ -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);
|
||||||
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user