Skip to content

Commit

Permalink
Fixed #357.
Browse files Browse the repository at this point in the history
Added a conversion member function to msgpack::object.
If msgpack::object is nil then returns false else returns true and sets a value.
  • Loading branch information
redboltz committed Aug 31, 2015
1 parent 9ee1168 commit 33de242
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 0 deletions.
9 changes: 9 additions & 0 deletions include/msgpack/object.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -511,6 +511,15 @@ inline T* object::convert(T* v) const
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)

template <typename T>
Expand Down
3 changes: 3 additions & 0 deletions include/msgpack/object_fwd.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -149,6 +149,9 @@ struct object {
template <typename T>
T* convert(T* v) const;

template <typename T>
bool convert_if_not_nil(T& v) const;

object();

object(const msgpack_object& o);
Expand Down
17 changes: 17 additions & 0 deletions test/convert.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -93,3 +93,20 @@ TEST(convert, return_value_ref)
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);
}

0 comments on commit 33de242

Please sign in to comment.