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>
33 std::shared_ptr<const std::vector<std::string>> args_;
35 MainFunction(F code, std::vector<std::string> args) :
36 code_(std::move(code)),
37 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());
55 template<class F> inline
56 std::function<void()> wrapMain(F code, std::vector<std::string> args)
58 return MainFunction<F>(std::move(code), std::move(args));
61 template<class F> inline
62 std::function<void()> wrapMain(F code, int argc, const char*const argv[])
64 std::vector<std::string> args(argv, argv + argc);
65 return MainFunction<F>(std::move(code), std::move(args));
69 template <class F, class Tuple, std::size_t... I>
70 constexpr auto apply(F&& f, Tuple&& t, simgrid::xbt::index_sequence<I...>)
71 -> decltype(std::forward<F>(f)(std::get<I>(std::forward<Tuple>(t))...))
73 return std::forward<F>(f)(std::get<I>(std::forward<Tuple>(t))...);
77 /** Call a functional object with the values in the given tuple (from C++17)
80 * int foo(int a, bool b);
82 * auto args = std::make_tuple(1, false);
83 * int res = apply(foo, args);
86 template <class F, class Tuple>
87 constexpr auto apply(F&& f, Tuple&& t)
88 -> decltype(simgrid::xbt::bits::apply(
90 std::forward<Tuple>(t),
91 simgrid::xbt::make_index_sequence<
92 std::tuple_size<typename std::decay<Tuple>::type>::value
95 return simgrid::xbt::bits::apply(
97 std::forward<Tuple>(t),
98 simgrid::xbt::make_index_sequence<
99 std::tuple_size<typename std::decay<Tuple>::type>::value
103 template<class T> class Task;
105 /** Type-erased run-once task
107 * * Like std::function but callable only once.
108 * However, it works with move-only types.
110 * * Like std::packaged_task<> but without the shared state.
112 template<class R, class... Args>
113 class Task<R(Args...)> {
116 // Placeholder for some class type:
119 // Union used for storage:
121 typedef typename std::aligned_union<0,
123 std::pair<void(*)(),void*>,
124 std::pair<void(whatever::*)(), whatever*>
129 std::pair<void(*)(),void*> funcptr;
130 std::pair<void(whatever::*)(), whatever*> memberptr;
131 char any1[sizeof(std::pair<void(*)(),void*>)];
132 char any2[sizeof(std::pair<void(whatever::*)(), whatever*>)];
133 TaskUnion() { /* Nothing to do */}
134 ~TaskUnion() { /* Nothing to do */}
138 // Is F suitable for small buffer optimization?
140 static constexpr bool canSBO()
142 return sizeof(F) <= sizeof(TaskUnion) &&
143 alignof(F) <= alignof(TaskUnion);
146 static_assert(canSBO<std::reference_wrapper<whatever>>(),
147 "SBO not working for reference_wrapper");
149 // Call (and possibly destroy) the function:
150 typedef R (*call_function)(TaskUnion&, Args...);
151 // Destroy the function (of needed):
152 typedef void (*destroy_function)(TaskUnion&);
153 // Move the function (otherwise memcpy):
154 typedef void (*move_function)(TaskUnion& dest, TaskUnion& src);
156 // Vtable of functions for manipulating whatever is in the TaskUnion:
159 destroy_function destroy;
164 const TaskVtable* vtable_ = nullptr;
168 if (vtable_ && vtable_->destroy)
169 vtable_->destroy(buffer_);
173 Task() { /* Nothing to do */}
174 explicit Task(std::nullptr_t) { /* Nothing to do */}
180 Task(Task const&) = delete;
184 if (that.vtable_ && that.vtable_->move)
185 that.vtable_->move(buffer_, that.buffer_);
187 std::memcpy(static_cast<void*>(&buffer_), static_cast<void*>(&that.buffer_), sizeof(buffer_));
189 vtable_ = that.vtable_;
190 that.vtable_ = nullptr;
192 Task& operator=(Task that)
195 if (that.vtable_ && that.vtable_->move)
196 that.vtable_->move(buffer_, that.buffer_);
198 std::memcpy(static_cast<void*>(&buffer_), static_cast<void*>(&that.buffer_), sizeof(buffer_));
199 vtable_ = that.vtable_;
200 that.vtable_ = nullptr;
207 typename std::enable_if<canSBO<F>()>::type
210 const static TaskVtable vtable {
212 [](TaskUnion& buffer, Args... args) {
213 F* src = reinterpret_cast<F*>(&buffer);
214 F code = std::move(*src);
216 return code(std::forward<Args>(args)...);
219 std::is_trivially_destructible<F>::value ?
220 static_cast<destroy_function>(nullptr) :
221 [](TaskUnion& buffer) {
222 F* code = reinterpret_cast<F*>(&buffer);
226 [](TaskUnion& dst, TaskUnion& src) {
227 F* src_code = reinterpret_cast<F*>(&src);
228 F* dst_code = reinterpret_cast<F*>(&dst);
229 new(dst_code) F(std::move(*src_code));
233 new(&buffer_) F(std::move(code));
237 template <class F> typename std::enable_if<not canSBO<F>()>::type init(F code)
239 const static TaskVtable vtable {
241 [](TaskUnion& buffer, Args... args) {
242 // Delete F when we go out of scope:
243 std::unique_ptr<F> code(*reinterpret_cast<F**>(&buffer));
244 return (*code)(std::forward<Args>(args)...);
247 [](TaskUnion& buffer) {
248 F* code = *reinterpret_cast<F**>(&buffer);
254 *reinterpret_cast<F**>(&buffer_) = new F(std::move(code));
259 template <class F> explicit Task(F code) { this->init(std::move(code)); }
261 operator bool() const { return vtable_ != nullptr; }
262 bool operator!() const { return vtable_ == nullptr; }
264 R operator()(Args... args)
266 if (vtable_ == nullptr)
267 throw std::bad_function_call();
268 const TaskVtable* vtable = vtable_;
270 return vtable->call(buffer_, std::forward<Args>(args)...);
274 template<class F, class... Args>
278 std::tuple<Args...> args_;
279 typedef decltype(simgrid::xbt::apply(std::move(code_), std::move(args_))) result_type;
281 TaskImpl(F code, std::tuple<Args...> args) :
282 code_(std::move(code)),
283 args_(std::move(args))
285 result_type operator()()
287 return simgrid::xbt::apply(std::move(code_), std::move(args_));
291 template<class F, class... Args>
292 auto makeTask(F code, Args... args)
293 -> Task< decltype(code(std::move(args)...))() >
295 TaskImpl<F, Args...> task(std::move(code), std::make_tuple(std::move(args)...));
296 return Task<decltype(code(std::move(args)...))()>(std::move(task));