Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Lazy any #4343

Merged
merged 11 commits into from
Jul 5, 2018
13 changes: 13 additions & 0 deletions src/shogun/lib/any.h
Original file line number Diff line number Diff line change
Expand Up @@ -948,6 +948,19 @@ namespace shogun
return Any(v);
}

/** Wraps a function as as instance of Any.
*
* @param func function to wrap
* @return Any object that uses the function to obtain its value
*/
template <typename T>
inline Any make_any(std::function<T()> func)
{
// TODO add policy
T value = func();
return Any(value);
}

template <typename T>
inline Any make_any_ref(T* v)
{
Expand Down
7 changes: 7 additions & 0 deletions tests/unit/lib/Any_unittest.cc
Original file line number Diff line number Diff line change
Expand Up @@ -636,3 +636,10 @@ TEST(Any, reset_array2d_reference)
EXPECT_EQ(obj->ref_count(), 1);
SG_UNREF(obj);
}

TEST(Any, lazy)
{
auto v = 9;
auto any = make_any<int>([=]() { return v; });
EXPECT_EQ(any.as<int32_t>(), v);
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@karlnapf this thing works now, but it definitely needs more thorough testing

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

shall we try to test a slightly more elaborate example of registering a member function that returns a constant or something?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah I can add one.

}