1 /* Copyright (c) 2015-2016. The SimGrid Team.
2 * All rights reserved. */
4 /* This program is free software; you can redistribute it and/or modify it
5 * under the terms of the license (GNU LGPL) which comes with this package. */
7 #ifndef XBT_FUNCTIONAL_HPP
8 #define XBT_FUNCTIONAL_HPP
22 #include <xbt/sysdep.h>
23 #include <xbt/utility.hpp>
32 std::shared_ptr<const std::vector<std::string>> args_;
34 MainFunction(F code, std::vector<std::string> args) :
35 code_(std::move(code)),
36 args_(std::make_shared<const std::vector<std::string>>(std::move(args)))
38 int operator()() const
40 const int argc = args_->size();
41 std::vector<std::string> args = *args_;
42 std::unique_ptr<char*[]> argv(new char*[argc + 1]);
43 for (int i = 0; i != argc; ++i)
44 argv[i] = args[i].empty() ? const_cast<char*>(""): &args[i].front();
46 return code_(argc, argv.get());
50 template<class F> inline
51 std::function<void()> wrapMain(F code, std::vector<std::string> args)
53 return MainFunction<F>(std::move(code), std::move(args));
56 template<class F> inline
57 std::function<void()> wrapMain(F code, int argc, const char*const argv[])
59 std::vector<std::string> args(argv, argv + argc);
60 return MainFunction<F>(std::move(code), std::move(args));
64 template <class F, class Tuple, std::size_t... I>
65 constexpr auto apply(F&& f, Tuple&& t, simgrid::xbt::index_sequence<I...>)
66 -> decltype(std::forward<F>(f)(std::get<I>(std::forward<Tuple>(t))...))
68 return std::forward<F>(f)(std::get<I>(std::forward<Tuple>(t))...);
72 /** Call a functional object with the values in the given tuple (from C++17)
75 * int foo(int a, bool b);
77 * auto args = std::make_tuple(1, false);
78 * int res = apply(foo, args);
81 template <class F, class Tuple>
82 constexpr auto apply(F&& f, Tuple&& t)
83 -> decltype(simgrid::xbt::bits::apply(
85 std::forward<Tuple>(t),
86 simgrid::xbt::make_index_sequence<
87 std::tuple_size<typename std::decay<Tuple>::type>::value
90 return simgrid::xbt::bits::apply(
92 std::forward<Tuple>(t),
93 simgrid::xbt::make_index_sequence<
94 std::tuple_size<typename std::decay<Tuple>::type>::value
98 template<class T> class Task;
102 // Something similar exist in C++14:
104 constexpr T max(T a, T b)
106 return (a > b) ? a : b;
108 template<class T, class... Args>
109 constexpr T max(T a, Args... b)
111 return max(std::forward<T>(a), max(std::forward<Args>(b)...));
116 // What we can store in a Task:
117 typedef void* ptr_callback;
118 struct funcptr_callback {
119 // Placeholder for any function pointer:
123 struct member_funcptr_callback {
124 // Placeholder for any pointer to member function:
125 void (whatever::* callback)();
128 typedef char any_callback[max(
129 sizeof(ptr_callback),
130 sizeof(funcptr_callback),
131 sizeof(member_funcptr_callback)
134 // Union of what we can store in a Task:
137 funcptr_callback funcptr;
138 member_funcptr_callback member_funcptr;
142 // Can we copy F in Task (or do we have to use the heap)?
144 constexpr bool isUsableDirectlyInTask()
146 // The only types we can portably store directly in the Task are the
147 // trivially copyable ones (we can memcpy) which are small enough to fit:
148 return std::is_trivially_copyable<F>::value &&
149 sizeof(F) <= sizeof(bits::any_callback);
154 /** Type-erased run-once task
156 * * Like std::function but callable only once.
157 * However, it works with move-only types.
159 * * Like std::packaged_task<> but without the shared state.
161 template<class R, class... Args>
162 class Task<R(Args...)> {
165 typedef bits::TaskErasure TaskErasure;
166 struct TaskErasureVtable {
167 // Call (and possibly destroy) the function:
168 R (*call)(TaskErasure&, Args...);
169 // Destroy the function:
170 void (*destroy)(TaskErasure&);
174 const TaskErasureVtable* vtable_ = nullptr;
178 Task(std::nullptr_t) {}
181 if (vtable_ && vtable_->destroy)
182 vtable_->destroy(code_);
185 Task(Task const&) = delete;
186 Task& operator=(Task const&) = delete;
190 std::memcpy(&code_, &that.code_, sizeof(code_));
191 vtable_ = that.vtable_;
192 that.vtable_ = nullptr;
194 Task& operator=(Task&& that)
196 if (vtable_ && vtable_->destroy)
197 vtable_->destroy(code_);
198 std::memcpy(&code_, &that.code_, sizeof(code_));
199 vtable_ = that.vtable_;
200 that.vtable_ = nullptr;
205 typename = typename std::enable_if<bits::isUsableDirectlyInTask<F>()>::type>
208 const static TaskErasureVtable vtable {
210 [](TaskErasure& erasure, Args... args) -> R {
211 // We need to wrap F un a union because F might not have a default
212 // constructor: this is especially the case for lambdas.
218 if (!std::is_empty<F>::value)
219 // AFAIU, this is safe as per [basic.types]:
220 std::memcpy(&code.code, &erasure.any, sizeof(code.code));
221 code.code(std::forward<Args>(args)...);
226 if (!std::is_empty<F>::value)
227 std::memcpy(&code_.any, &code, sizeof(code));
232 typename = typename std::enable_if<!bits::isUsableDirectlyInTask<F>()>::type>
235 const static TaskErasureVtable vtable {
237 [](TaskErasure& erasure, Args... args) -> R {
238 // Delete F when we go out of scope:
239 std::unique_ptr<F> code(static_cast<F*>(erasure.ptr));
240 (*code)(std::forward<Args>(args)...);
243 [](TaskErasure& erasure) {
244 F* code = static_cast<F*>(erasure.ptr);
248 code_.ptr = new F(std::move(code));
253 Task(std::reference_wrapper<F> code)
255 const static TaskErasureVtable vtable {
257 [](TaskErasure& erasure, Args... args) -> R {
258 F* code = static_cast<F*>(erasure.ptr);
259 (*code)(std::forward<Args>(args)...);
264 code.code_.ptr = code.get();
268 operator bool() const { return vtable_ != nullptr; }
269 bool operator!() const { return vtable_ == nullptr; }
271 R operator()(Args... args)
274 throw std::bad_function_call();
275 const TaskErasureVtable* vtable = vtable_;
277 return vtable->call(code_, std::forward<Args>(args)...);
281 template<class F, class... Args>
285 std::tuple<Args...> args_;
286 typedef decltype(simgrid::xbt::apply(std::move(code_), std::move(args_))) result_type;
288 TaskImpl(F code, std::tuple<Args...> args) :
289 code_(std::move(code)),
290 args_(std::move(args))
292 result_type operator()()
294 return simgrid::xbt::apply(std::move(code_), std::move(args_));
298 template<class F, class... Args>
299 auto makeTask(F code, Args... args)
300 -> Task< decltype(code(std::move(args)...))() >
302 TaskImpl<F, Args...> task(std::move(code), std::make_tuple(std::move(args)...));
303 return std::move(task);