Nix 2.36.0
Nix, the purely functional package manager; unstable internal interfaces
Loading...
Searching...
No Matches
fun.hh
Go to the documentation of this file.
1#pragma once
3
4#include <functional>
5#include <stdexcept>
6#include <type_traits>
7#include <utility>
8
9namespace nix {
10
18template<typename Sig>
19class fun;
20
21template<typename Ret, typename... Args>
22class fun<Ret(Args...)>
23{
24private:
25
26 std::function<Ret(Args...)> f;
27
28 void assertCallable()
29 {
30 if (!f)
31 throw std::invalid_argument("null callable cast to fun");
32 }
33
34public:
35
36 using result_type = Ret;
37
44 template<typename F>
45 requires(
46 !std::is_same_v<std::decay_t<F>, fun> && !std::is_same_v<std::decay_t<F>, std::function<Ret(Args...)>>
47 && !std::is_same_v<std::decay_t<F>, std::nullptr_t>
48 && std::is_constructible_v<std::function<Ret(Args...)>, F>)
49 fun(F && callable)
50 : f(std::forward<F>(callable))
51 {
52 assertCallable();
53 }
54
59 explicit fun(const std::function<Ret(Args...)> & fn)
60 : f(fn)
61 {
62 assertCallable();
63 }
64
65 explicit fun(std::function<Ret(Args...)> && fn)
66 : f(std::move(fn))
67 {
68 assertCallable();
69 }
70
72 fun(std::nullptr_t) = delete;
73
74 template<typename... Ts>
75 Ret operator()(Ts &&... args) const
76 {
77 return f(std::forward<Ts>(args)...);
78 }
79
81 const std::function<Ret(Args...)> & get_fn() const &
82 {
83 return f;
84 }
85
86 std::function<Ret(Args...)> get_fn() &&
87 {
88 return std::move(f);
89 }
90};
91
92} // namespace nix
Definition args.hh:29
fun(F &&callable)
Definition fun.hh:49
const std::function< Ret(Args...)> & get_fn() const &
Definition fun.hh:81
fun(std::nullptr_t)=delete
fun(const std::function< Ret(Args...)> &fn)
Definition fun.hh:59
Definition fun.hh:19