Skip to content

Commit

Permalink
Made MemoizedFunction template more like std::function.
Browse files Browse the repository at this point in the history
  • Loading branch information
Morwenn committed Jul 31, 2015
1 parent 9f7ac49 commit 151e0c8
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 8 deletions.
11 changes: 6 additions & 5 deletions include/POLDER/details/functional.inl
Expand Up @@ -21,12 +21,12 @@
////////////////////////////////////////////////////////////

template<typename Ret, typename... Args>
MemoizedFunction<Ret, Args...>::MemoizedFunction(const std::function<Ret(Args...)>& func):
MemoizedFunction<Ret(Args...)>::MemoizedFunction(const std::function<Ret(Args...)>& func):
_func(func)
{}

template<typename Ret, typename... Args>
auto MemoizedFunction<Ret, Args...>::operator()(Args&&... args)
auto MemoizedFunction<Ret(Args...)>::operator()(Args&&... args)
-> Ret
{
const auto t_args = std::make_tuple(std::forward<Args>(args)...);
Expand All @@ -42,7 +42,7 @@ auto MemoizedFunction<Ret, Args...>::operator()(Args&&... args)
}

template<typename Ret, typename... Args>
auto MemoizedFunction<Ret, Args...>::clear() noexcept
auto MemoizedFunction<Ret(Args...)>::clear() noexcept
-> void
{
_cache.clear();
Expand All @@ -51,15 +51,16 @@ auto MemoizedFunction<Ret, Args...>::clear() noexcept
template<typename Function, std::size_t... Ind>
auto memoized_impl(Function&& func, std::index_sequence<Ind...>)
-> MemoizedFunction<
result_type<Function>,
argument_type<Function, Ind>...>
result_type<Function>(argument_type<Function, Ind>...)
>
{
using Ret = result_type<Function>;
return { std::function<Ret(argument_type<Function, Ind>...)>(func) };
}

template<typename Function>
auto memoized(Function&& func)
-> decltype(auto)
{
using Indices = std::make_index_sequence<arity<Function>>;
return memoized_impl(std::forward<Function>(func), Indices{});
Expand Down
10 changes: 7 additions & 3 deletions include/POLDER/functional.h
Expand Up @@ -128,10 +128,13 @@ namespace polder
* seen, this class calls the underlying function to
* compute the result, stores it and returns it.
*
* @warning Only works for pure functions
* @warning It only works with pure functions.
*/
template<typename Callable>
class MemoizedFunction;

template<typename Ret, typename... Args>
class MemoizedFunction
class MemoizedFunction<Ret(Args...)>
{
public:

Expand Down Expand Up @@ -174,7 +177,8 @@ namespace polder
* @return Memoized function corresponding to \a func
*/
template<typename Function>
auto memoized(Function&& func);
auto memoized(Function&& func)
-> decltype(auto);

////////////////////////////////////////////////////////////
// curried
Expand Down

0 comments on commit 151e0c8

Please sign in to comment.