LeechCraft 0.6.70-17335-ge406ffdcaf
Modular cross-platform feature rich live environment.
Loading...
Searching...
No Matches
curry.h
Go to the documentation of this file.
1/**********************************************************************
2 * LeechCraft - modular cross-platform feature rich internet client.
3 * Copyright (C) 2006-2014 Georg Rudoy
4 *
5 * Distributed under the Boost Software License, Version 1.0.
6 * (See accompanying file LICENSE or copy at https://www.boost.org/LICENSE_1_0.txt)
7 **********************************************************************/
8
9#pragma once
10
11#include <tuple>
12#include <type_traits>
13#include <utility>
14
15namespace LC
16{
17namespace Util
18{
19 template<typename F, typename... PrevArgs>
21 {
22 F m_f;
23
24 std::tuple<PrevArgs...> m_prevArgs;
25 public:
26 template<typename CF, typename CT>
27 CurryImpl (CF&& f, CT&& prev)
28 : m_f { std::forward<CF> (f) }
29 , m_prevArgs { std::forward<CT> (prev) }
30 {
31 }
32
33 template<typename T>
34 auto operator() (T&& arg) const &
35 {
36 return run (*this, std::forward<T> (arg));
37 }
38
39 template<typename T>
40 auto operator() (T&& arg) &
41 {
42 return run (std::as_const (*this), std::forward<T> (arg));
43 }
44
45 template<typename T>
46 auto operator() (T&& arg) &&
47 {
48 return run (std::move (*this), std::forward<T> (arg));
49 }
50 private:
51 template<typename This, typename T>
52 static auto run (This&& refThis, T&& arg)
53 {
54 if constexpr (std::is_invocable_v<F, PrevArgs..., T>)
55 {
56 auto wrapper = [&refThis, &arg] (auto&&... args)
57 {
58 return std::invoke (std::move (refThis.m_f), std::forward<decltype (args)> (args)..., std::forward<T> (arg));
59 };
60 return std::apply (std::move (wrapper), std::move (refThis.m_prevArgs));
61 }
62 else
63 return CurryImpl<F, PrevArgs..., T>
64 {
65 std::move (refThis.m_f),
66 std::tuple_cat (std::move (refThis.m_prevArgs), std::forward_as_tuple (std::forward<T> (arg)))
67 };
68 }
69 };
70
71 template<typename F, typename... Args>
72 CurryImpl<std::decay_t<F>, Args...> Curry (F&& f, Args&&... args)
73 {
74 return { std::forward<F> (f), std::forward_as_tuple (std::forward<Args> (args)...) };
75 }
76}
77}
auto operator()(T &&arg) const &
Definition curry.h:34
CurryImpl(CF &&f, CT &&prev)
Definition curry.h:27
CurryImpl< std::decay_t< F >, Args... > Curry(F &&f, Args &&... args)
Definition curry.h:72
Definition constants.h:15