libcopp 2.3.1
All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
task_actions.h
Go to the documentation of this file.
1// Copyright 2023 owent
2
3#pragma once
4
5#include <libcopp/utils/config/libcopp_build_features.h>
6
8
9LIBCOPP_COTASK_NAMESPACE_BEGIN
10
11namespace detail {
12
14 // ================================================
15 template <typename TR, typename TF>
16 static LIBCOPP_COTASK_API_HEAD_ONLY int call(TR (TF::*)(void *), TF &fn, void *priv_data) {
17 fn(priv_data);
18 return 0;
19 }
20
21 template <typename TR, typename TF>
22 static LIBCOPP_COTASK_API_HEAD_ONLY int call(TR (TF::*)(void *) const, const TF &fn, void *priv_data) {
23 fn(priv_data);
24 return 0;
25 }
26
27 // ------------------------------------------------
28 template <typename TF>
29 static LIBCOPP_COTASK_API_HEAD_ONLY int call(int (TF::*)(void *), TF &fn, void *priv_data) {
30 return fn(priv_data);
31 }
32
33 template <typename TF>
34 static LIBCOPP_COTASK_API_HEAD_ONLY int call(int (TF::*)(void *) const, const TF &fn, void *priv_data) {
35 return fn(priv_data);
36 }
37
38 // ------------------------------------------------
39 template <typename TR, typename TF>
40 static LIBCOPP_COTASK_API_HEAD_ONLY int call(TR (TF::*)(), TF &fn, void * /*priv_data*/) {
41 fn();
42 return 0;
43 }
44
45 template <typename TR, typename TF>
46 static LIBCOPP_COTASK_API_HEAD_ONLY int call(TR (TF::*)() const, const TF &fn, void * /*priv_data*/) {
47 fn();
48 return 0;
49 }
50
51 // ------------------------------------------------
52 template <typename TF>
53 static LIBCOPP_COTASK_API_HEAD_ONLY int call(int (TF::*)(), TF &fn, void * /*priv_data*/) {
54 return fn();
55 }
56
57 template <typename TF>
58 static LIBCOPP_COTASK_API_HEAD_ONLY int call(int (TF::*)() const, const TF &fn, void * /*priv_data*/) {
59 return fn();
60 }
61};
62} // namespace detail
63
64// functor
65template <typename Ty>
66class LIBCOPP_COTASK_API_HEAD_ONLY task_action_functor : public impl::task_action_impl {
67 public:
68 using value_type = typename std::decay<Ty>::type;
69
70 template <typename... TARG>
71 task_action_functor(TARG &&...arg) : functor_(std::forward<TARG>(arg)...) {}
72
73 template <typename... TARG>
74 task_action_functor(task_action_functor &&other, TARG &&...) : functor_(std::move(other.functor_)) {}
75
76 template <typename... TARG>
77 task_action_functor(const task_action_functor &other, TARG &&...) : functor_(other.functor_) {}
78
80 int operator()(void *priv_data) override {
81 return detail::task_action_functor_check::call(&value_type::operator(), functor_, priv_data);
82 }
83
84 static void placement_destroy(void *selfp) {
85 if (nullptr == selfp) {
86 return;
87 }
88
89 task_action_functor<Ty> *self = reinterpret_cast<task_action_functor<Ty> *>(selfp);
91 }
92
93 private:
95};
96
97// function
98template <typename Ty>
99class LIBCOPP_COTASK_API_HEAD_ONLY task_action_function : public impl::task_action_impl {
100 public:
101 using value_type = Ty (*)(void *);
102
103 private:
104 template <class Tz, bool IS_INTEGRAL>
105 struct invoker;
106
107 template <class Tz>
108 struct invoker<Tz, true> {
109 LIBCOPP_UTIL_FORCEINLINE static int invoke(task_action_function &in, void *priv_data) {
110 return static_cast<int>((*in.func_)(priv_data));
111 }
112 };
113
114 template <class Tz>
115 struct invoker<Tz, false> {
116 LIBCOPP_UTIL_FORCEINLINE static int invoke(task_action_function &in, void *priv_data) {
117 (*in.func_)(priv_data);
118 return 0;
119 }
120 };
121
122 public:
123 task_action_function(value_type func) : func_(func) {}
125
126 int operator()(void *priv_data) override {
127 return invoker<Ty, std::is_integral<typename std::decay<Ty>::type>::value>::invoke(*this, priv_data);
128 }
129
130 static void placement_destroy(void *selfp) {
131 if (nullptr == selfp) {
132 return;
133 }
134
135 task_action_function<Ty> *self = reinterpret_cast<task_action_function<Ty> *>(selfp);
136 self->~task_action_function();
137 }
138
139 private:
141};
142
143// mem function
144template <typename Ty, typename Tc>
145class LIBCOPP_COTASK_API_HEAD_ONLY task_action_mem_function : public impl::task_action_impl {
146 public:
147 using value_type = Ty Tc::*;
148
149 private:
150 template <class Tz, bool IS_INTEGRAL>
151 struct invoker;
152
153 template <class Tz>
154 struct invoker<Tz, true> {
156 return static_cast<int>((in.instacne_->*in.func_)(priv_data));
157 }
158 };
159
160 template <class Tz>
161 struct invoker<Tz, false> {
163 (in.instacne_->*in.func_)(priv_data);
164 return 0;
165 }
166 };
167
168 public:
169 task_action_mem_function(value_type func, Tc *inst) : instacne_(inst), func_(func) {}
171
172 int operator()(void *priv_data) override {
173 return invoker<Ty, std::is_integral<typename std::decay<Ty>::type>::value>::invoke(*this, priv_data);
174 }
175
176 static void placement_destroy(void *selfp) {
177 if (nullptr == selfp) {
178 return;
179 }
180
183 }
184
185 private:
188};
189
190template <typename Ty>
191LIBCOPP_COTASK_API_HEAD_ONLY void placement_destroy(void *selfp) {
192 if (nullptr == selfp) {
193 return;
194 }
195
196 Ty *self = reinterpret_cast<Ty *>(selfp);
197 self->~Ty();
198}
199
200using placement_destroy_fn_t = void (*)(void *);
201
202template <typename Ty>
206
207template <typename Ty>
211
212template <typename Ty, typename Tc>
213LIBCOPP_COTASK_API_HEAD_ONLY placement_destroy_fn_t
217
218template <typename Ty>
219LIBCOPP_COTASK_API_HEAD_ONLY placement_destroy_fn_t get_placement_destroy(Ty * /*selfp*/) {
220 return &placement_destroy<Ty>;
221}
222LIBCOPP_COTASK_NAMESPACE_END
task_action_function(value_type func)
static void placement_destroy(void *selfp)
int operator()(void *priv_data) override
Ty(*)(void *) value_type
int operator()(void *priv_data) override
static void placement_destroy(void *selfp)
task_action_functor(task_action_functor &&other, TARG &&...)
task_action_functor(TARG &&...arg)
task_action_functor(const task_action_functor &other, TARG &&...)
typename std::decay< Ty >::type value_type
task_action_mem_function(value_type func, Tc *inst)
int operator()(void *priv_data) override
static void placement_destroy(void *selfp)
#define LIBCOPP_UTIL_FORCEINLINE
STL namespace.
static LIBCOPP_COTASK_API_HEAD_ONLY int call(TR(TF::*)() const, const TF &fn, void *)
static LIBCOPP_COTASK_API_HEAD_ONLY int call(TR(TF::*)(void *), TF &fn, void *priv_data)
static LIBCOPP_COTASK_API_HEAD_ONLY int call(TR(TF::*)(void *) const, const TF &fn, void *priv_data)
static LIBCOPP_COTASK_API_HEAD_ONLY int call(int(TF::*)(void *) const, const TF &fn, void *priv_data)
static LIBCOPP_COTASK_API_HEAD_ONLY int call(int(TF::*)(), TF &fn, void *)
static LIBCOPP_COTASK_API_HEAD_ONLY int call(int(TF::*)() const, const TF &fn, void *)
static LIBCOPP_COTASK_API_HEAD_ONLY int call(int(TF::*)(void *), TF &fn, void *priv_data)
static LIBCOPP_COTASK_API_HEAD_ONLY int call(TR(TF::*)(), TF &fn, void *)
static LIBCOPP_UTIL_FORCEINLINE int invoke(task_action_function &in, void *priv_data)
static LIBCOPP_UTIL_FORCEINLINE int invoke(task_action_function &in, void *priv_data)
static LIBCOPP_UTIL_FORCEINLINE int invoke(task_action_mem_function &in, void *priv_data)
static LIBCOPP_UTIL_FORCEINLINE int invoke(task_action_mem_function &in, void *priv_data)
void(*)(void *) placement_destroy_fn_t
LIBCOPP_COTASK_API_HEAD_ONLY void placement_destroy(void *selfp)
LIBCOPP_COTASK_API_HEAD_ONLY placement_destroy_fn_t get_placement_destroy(task_action_functor< Ty > *)