forked from RDmitriyS/function_task
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfunction.h
80 lines (60 loc) · 1.96 KB
/
function.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
#pragma once
#include "storage.h"
template <typename F>
struct function;
template <typename R, typename... Args>
struct function<R(Args...)> {
template <typename Friend, typename... FriendArgs>
friend
struct storage;
function() noexcept = default;
function(function const& other) = default;
function(function&& other) noexcept = default;
template <typename T>
function(T val);
function& operator=(function const& rhs) = default;
function& operator=(function&& rhs) noexcept = default;
~function() = default;
explicit operator bool() const noexcept;
R operator()(Args... args) const;
R operator()(Args... args);
template <typename T>
T* target() noexcept;
template <typename T>
T const* target() const noexcept;
private:
storage<R, Args...> stg;
};
template <typename R, typename... Args>
template <typename T>
function<R(Args...)>::function(T val) : stg(std::move(val)) {}
template <typename R, typename... Args>
function<R(Args...)>::operator bool() const noexcept {
return stg.desc != empty_type_descriptor<R, Args...>();
}
template <typename R, typename... Args>
R function<R(Args...)>::operator()(Args... args) const {
return stg.invoke(std::forward<Args>(args)...);
}
template <typename R, typename... Args>
R function<R(Args...)>::operator()(Args... args) {
return stg.invoke(std::forward<Args>(args)...);
}
template <typename R, typename... Args>
template <typename T>
T* function<R(Args...)>::target() noexcept {
if (operator bool() && stg.template check_type<T>()) {
return function_traits<T>::template get_func_obj<R, Args...>(&stg);
} else {
return nullptr;
}
}
template <typename R, typename... Args>
template <typename T>
T const* function<R(Args...)>::target() const noexcept {
if (operator bool() && stg.template check_type<T>()) {
return function_traits<T>::template get_func_obj<R, Args...>(&stg);
} else {
return nullptr;
}
}