1 /* Copyright (c) 2015-2018. The SimGrid Team. All rights reserved. */
3 /* This program is free software; you can redistribute it and/or modify it
4 * under the terms of the license (GNU LGPL) which comes with this package. */
6 #ifndef XBT_FUNCTIONAL_HPP
7 #define XBT_FUNCTIONAL_HPP
9 #include <xbt/sysdep.h>
10 #include <xbt/utility.hpp>
22 #include <type_traits>
29 template <class F> class MainFunction {
32 std::shared_ptr<const std::vector<std::string>> args_;
35 MainFunction(F code, std::vector<std::string> args)
36 : code_(std::move(code)), args_(std::make_shared<const std::vector<std::string>>(std::move(args)))
39 void operator()() const
41 const int argc = args_->size();
42 std::vector<std::string> args = *args_;
43 if (not args.empty()) {
44 char noarg[] = {'\0'};
45 std::unique_ptr<char* []> argv(new char*[argc + 1]);
46 for (int i = 0; i != argc; ++i)
47 argv[i] = args[i].empty() ? noarg : &args[i].front();
49 code_(argc, argv.get());
54 class MainStdFunction {
56 void (*code_)(std::vector<std::string>);
57 std::shared_ptr<const std::vector<std::string>> args_;
60 MainStdFunction(void (*code)(std::vector<std::string>), std::vector<std::string> args)
61 : code_(std::move(code)), args_(std::make_shared<const std::vector<std::string>>(std::move(args)))
64 void operator()() const
66 std::vector<std::string> args = *args_;
72 inline XBT_ATTRIB_DEPRECATED_v323("Please use wrap_main()") std::function<void()> wrapMain(
73 F code, std::vector<std::string> args)
75 return MainFunction<F>(std::move(code), std::move(args));
78 template <class F> inline std::function<void()> wrap_main(F code, std::vector<std::string> args)
80 return MainFunction<F>(std::move(code), std::move(args));
82 inline std::function<void()> wrap_main(void (*code)(std::vector<std::string>), std::vector<std::string> args)
84 return MainStdFunction(std::move(code), std::move(args));
88 inline XBT_ATTRIB_DEPRECATED_v323("Please use wrap_main()") std::function<void()> wrapMain(F code, int argc,
89 const char* const argv[])
91 std::vector<std::string> args(argv, argv + argc);
92 return MainFunction<F>(std::move(code), std::move(args));
94 template <class F> inline std::function<void()> wrap_main(F code, int argc, const char* const argv[])
96 std::vector<std::string> args(argv, argv + argc);
97 return MainFunction<F>(std::move(code), std::move(args));
101 template <class F, class Tuple, std::size_t... I>
102 constexpr auto apply(F&& f, Tuple&& t, simgrid::xbt::index_sequence<I...>)
103 -> decltype(std::forward<F>(f)(std::get<I>(std::forward<Tuple>(t))...))
105 return std::forward<F>(f)(std::get<I>(std::forward<Tuple>(t))...);
109 /** Call a functional object with the values in the given tuple (from C++17)
112 * int foo(int a, bool b);
114 * auto args = std::make_tuple(1, false);
115 * int res = apply(foo, args);
118 template <class F, class Tuple>
119 constexpr auto apply(F&& f, Tuple&& t)
120 -> decltype(simgrid::xbt::bits::apply(
122 std::forward<Tuple>(t),
123 simgrid::xbt::make_index_sequence<
124 std::tuple_size<typename std::decay<Tuple>::type>::value
127 return simgrid::xbt::bits::apply(
129 std::forward<Tuple>(t),
130 simgrid::xbt::make_index_sequence<
131 std::tuple_size<typename std::decay<Tuple>::type>::value
135 template<class T> class Task;
137 /** Type-erased run-once task
139 * * Like std::function but callable only once.
140 * However, it works with move-only types.
142 * * Like std::packaged_task<> but without the shared state.
144 template<class R, class... Args>
145 class Task<R(Args...)> {
148 // Placeholder for some class type:
151 // Union used for storage:
153 typedef typename std::aligned_union<0,
155 std::pair<void(*)(),void*>,
156 std::pair<void(whatever::*)(), whatever*>
161 std::pair<void(*)(),void*> funcptr;
162 std::pair<void(whatever::*)(), whatever*> memberptr;
163 char any1[sizeof(std::pair<void(*)(),void*>)];
164 char any2[sizeof(std::pair<void(whatever::*)(), whatever*>)];
165 TaskUnion() { /* Nothing to do */}
166 ~TaskUnion() { /* Nothing to do */}
170 // Is F suitable for small buffer optimization?
172 static constexpr bool canSBO()
174 return sizeof(F) <= sizeof(TaskUnion) &&
175 alignof(F) <= alignof(TaskUnion);
178 static_assert(canSBO<std::reference_wrapper<whatever>>(),
179 "SBO not working for reference_wrapper");
181 // Call (and possibly destroy) the function:
182 typedef R (*call_function)(TaskUnion&, Args...);
183 // Destroy the function (of needed):
184 typedef void (*destroy_function)(TaskUnion&);
185 // Move the function (otherwise memcpy):
186 typedef void (*move_function)(TaskUnion& dest, TaskUnion& src);
188 // Vtable of functions for manipulating whatever is in the TaskUnion:
191 destroy_function destroy;
196 const TaskVtable* vtable_ = nullptr;
200 if (vtable_ && vtable_->destroy)
201 vtable_->destroy(buffer_);
205 Task() { /* Nothing to do */}
206 explicit Task(std::nullptr_t) { /* Nothing to do */}
212 Task(Task const&) = delete;
216 if (that.vtable_ && that.vtable_->move)
217 that.vtable_->move(buffer_, that.buffer_);
219 std::memcpy(static_cast<void*>(&buffer_), static_cast<void*>(&that.buffer_), sizeof(buffer_));
221 vtable_ = that.vtable_;
222 that.vtable_ = nullptr;
224 Task& operator=(Task that)
227 if (that.vtable_ && that.vtable_->move)
228 that.vtable_->move(buffer_, that.buffer_);
230 std::memcpy(static_cast<void*>(&buffer_), static_cast<void*>(&that.buffer_), sizeof(buffer_));
231 vtable_ = that.vtable_;
232 that.vtable_ = nullptr;
239 typename std::enable_if<canSBO<F>()>::type
242 const static TaskVtable vtable {
244 [](TaskUnion& buffer, Args... args) {
245 F* src = reinterpret_cast<F*>(&buffer);
246 F code = std::move(*src);
248 return code(std::forward<Args>(args)...);
251 std::is_trivially_destructible<F>::value ?
252 static_cast<destroy_function>(nullptr) :
253 [](TaskUnion& buffer) {
254 F* code = reinterpret_cast<F*>(&buffer);
258 [](TaskUnion& dst, TaskUnion& src) {
259 F* src_code = reinterpret_cast<F*>(&src);
260 F* dst_code = reinterpret_cast<F*>(&dst);
261 new(dst_code) F(std::move(*src_code));
265 new(&buffer_) F(std::move(code));
269 template <class F> typename std::enable_if<not canSBO<F>()>::type init(F code)
271 const static TaskVtable vtable {
273 [](TaskUnion& buffer, Args... args) {
274 // Delete F when we go out of scope:
275 std::unique_ptr<F> code(*reinterpret_cast<F**>(&buffer));
276 return (*code)(std::forward<Args>(args)...);
279 [](TaskUnion& buffer) {
280 F* code = *reinterpret_cast<F**>(&buffer);
286 *reinterpret_cast<F**>(&buffer_) = new F(std::move(code));
291 template <class F> explicit Task(F code) { this->init(std::move(code)); }
293 operator bool() const { return vtable_ != nullptr; }
294 bool operator!() const { return vtable_ == nullptr; }
296 R operator()(Args... args)
298 if (vtable_ == nullptr)
299 throw std::bad_function_call();
300 const TaskVtable* vtable = vtable_;
302 return vtable->call(buffer_, std::forward<Args>(args)...);
306 template<class F, class... Args>
310 std::tuple<Args...> args_;
311 typedef decltype(simgrid::xbt::apply(std::move(code_), std::move(args_))) result_type;
313 TaskImpl(F code, std::tuple<Args...> args) :
314 code_(std::move(code)),
315 args_(std::move(args))
317 result_type operator()()
319 return simgrid::xbt::apply(std::move(code_), std::move(args_));
323 template <class F, class... Args>
324 XBT_ATTRIB_DEPRECATED_v323("Please use make_task()") auto makeTask(F code, Args... args)
325 -> Task<decltype(code(std::move(args)...))()>
327 TaskImpl<F, Args...> task(std::move(code), std::make_tuple(std::move(args)...));
328 return Task<decltype(code(std::move(args)...))()>(std::move(task));
331 template <class F, class... Args> auto make_task(F code, Args... args) -> Task<decltype(code(std::move(args)...))()>
333 TaskImpl<F, Args...> task(std::move(code), std::make_tuple(std::move(args)...));
334 return Task<decltype(code(std::move(args)...))()>(std::move(task));