Skip to content

Commit

Permalink
Merge pull request #358 from redboltz/fix_357
Browse files Browse the repository at this point in the history
Fixed #357.
  • Loading branch information
redboltz committed Aug 31, 2015
2 parents 9ee1168 + dc2e1a4 commit 9b15682
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 0 deletions.
10 changes: 10 additions & 0 deletions include/msgpack/object.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -511,6 +511,16 @@ 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 9b15682

Please sign in to comment.