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
19 #include <type_traits>
23 #include <xbt/sysdep.h>
24 #include <xbt/utility.hpp>
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 int operator()() const
41 const int argc = args_->size();
42 std::vector<std::string> args = *args_;
43 std::unique_ptr<char*[]> argv(new char*[argc + 1]);
44 for (int i = 0; i != argc; ++i)
45 argv[i] = args[i].empty() ? const_cast<char*>(""): &args[i].front();
47 return code_(argc, argv.get());
51 template<class F> inline
52 std::function<void()> wrapMain(F code, std::vector<std::string> args)
54 return MainFunction<F>(std::move(code), std::move(args));
57 template<class F> inline
58 std::function<void()> wrapMain(F code, int argc, const char*const argv[])
60 std::vector<std::string> args(argv, argv + argc);
61 return MainFunction<F>(std::move(code), std::move(args));
65 template <class F, class Tuple, std::size_t... I>
66 constexpr auto apply(F&& f, Tuple&& t, simgrid::xbt::index_sequence<I...>)
67 -> decltype(std::forward<F>(f)(std::get<I>(std::forward<Tuple>(t))...))
69 return std::forward<F>(f)(std::get<I>(std::forward<Tuple>(t))...);
73 /** Call a functional object with the values in the given tuple (from C++17)
76 * int foo(int a, bool b);
78 * auto args = std::make_tuple(1, false);
79 * int res = apply(foo, args);
82 template <class F, class Tuple>
83 constexpr auto apply(F&& f, Tuple&& t)
84 -> decltype(simgrid::xbt::bits::apply(
86 std::forward<Tuple>(t),
87 simgrid::xbt::make_index_sequence<
88 std::tuple_size<typename std::decay<Tuple>::type>::value
91 return simgrid::xbt::bits::apply(
93 std::forward<Tuple>(t),
94 simgrid::xbt::make_index_sequence<
95 std::tuple_size<typename std::decay<Tuple>::type>::value
99 template<class T> class Task;
103 // Something similar exist in C++14:
105 constexpr T max(T a, T b)
107 return (a > b) ? a : b;
109 template<class T, class... Args>
110 constexpr T max(T a, Args... b)
112 return max(std::forward<T>(a), max(std::forward<Args>(b)...));
117 // What we can store in a Task:
118 typedef void* ptr_callback;
119 struct funcptr_callback {
120 // Placeholder for any function pointer:
124 struct member_funcptr_callback {
125 // Placeholder for any pointer to member function:
126 void (whatever::* callback)();
129 typedef char any_callback[max(
130 sizeof(ptr_callback),
131 sizeof(funcptr_callback),
132 sizeof(member_funcptr_callback)
135 // Union of what we can store in a Task:
138 funcptr_callback funcptr;
139 member_funcptr_callback member_funcptr;
143 // Can we copy F in Task (or do we have to use the heap)?
145 constexpr bool isUsableDirectlyInTask()
147 // The only types we can portably store directly in the Task are the
148 // trivially copyable ones (we can memcpy) which are small enough to fit:
149 return std::is_trivially_copyable<F>::value &&
150 sizeof(F) <= sizeof(bits::any_callback);
155 /** Type-erased run-once task
157 * * Like std::function but callable only once.
158 * However, it works with move-only types.
160 * * Like std::packaged_task<> but without the shared state.
162 template<class R, class... Args>
163 class Task<R(Args...)> {
166 typedef bits::TaskErasure TaskErasure;
167 struct TaskErasureVtable {
168 // Call (and possibly destroy) the function:
169 R (*call)(TaskErasure&, Args...);
170 // Destroy the function:
171 void (*destroy)(TaskErasure&);
175 const TaskErasureVtable* vtable_ = nullptr;
179 Task(std::nullptr_t) {}
182 if (vtable_ && vtable_->destroy)
183 vtable_->destroy(code_);
186 Task(Task const&) = delete;
187 Task& operator=(Task const&) = delete;
191 std::memcpy(&code_, &that.code_, sizeof(code_));
192 vtable_ = that.vtable_;
193 that.vtable_ = nullptr;
195 Task& operator=(Task&& that)
197 if (vtable_ && vtable_->destroy)
198 vtable_->destroy(code_);
199 std::memcpy(&code_, &that.code_, sizeof(code_));
200 vtable_ = that.vtable_;
201 that.vtable_ = nullptr;
206 typename = typename std::enable_if<bits::isUsableDirectlyInTask<F>()>::type>
209 const static TaskErasureVtable vtable {
211 [](TaskErasure& erasure, Args... args) -> R {
212 // We need to wrap F un a union because F might not have a default
213 // constructor: this is especially the case for lambdas.
219 if (!std::is_empty<F>::value)
220 // AFAIU, this is safe as per [basic.types]:
221 std::memcpy(&code.code, &erasure.any, sizeof(code.code));
222 code.code(std::forward<Args>(args)...);
227 if (!std::is_empty<F>::value)
228 std::memcpy(&code_.any, &code, sizeof(code));
233 typename = typename std::enable_if<!bits::isUsableDirectlyInTask<F>()>::type>
236 const static TaskErasureVtable vtable {
238 [](TaskErasure& erasure, Args... args) -> R {
239 // Delete F when we go out of scope:
240 std::unique_ptr<F> code(static_cast<F*>(erasure.ptr));
241 (*code)(std::forward<Args>(args)...);
244 [](TaskErasure& erasure) {
245 F* code = static_cast<F*>(erasure.ptr);
249 code_.ptr = new F(std::move(code));
254 Task(std::reference_wrapper<F> code)
256 const static TaskErasureVtable vtable {
258 [](TaskErasure& erasure, Args... args) -> R {
259 F* code = static_cast<F*>(erasure.ptr);
260 (*code)(std::forward<Args>(args)...);
265 code.code_.ptr = code.get();
269 operator bool() const { return vtable_ != nullptr; }
270 bool operator!() const { return vtable_ == nullptr; }
272 R operator()(Args... args)
275 throw std::bad_function_call();
276 const TaskErasureVtable* vtable = vtable_;
278 return vtable->call(code_, std::forward<Args>(args)...);
282 template<class F, class... Args>
286 std::tuple<Args...> args_;
287 typedef decltype(simgrid::xbt::apply(std::move(code_), std::move(args_))) result_type;
289 TaskImpl(F code, std::tuple<Args...> args) :
290 code_(std::move(code)),
291 args_(std::move(args))
293 result_type operator()()
295 return simgrid::xbt::apply(std::move(code_), std::move(args_));
299 template<class F, class... Args>
300 auto makeTask(F code, Args... args)
301 -> Task< decltype(code(std::move(args)...))() >
303 TaskImpl<F, Args...> task(std::move(code), std::make_tuple(std::move(args)...));
304 return std::move(task);