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
20 #include <type_traits>
24 #include <xbt/sysdep.h>
25 #include <xbt/utility.hpp>
34 std::shared_ptr<const std::vector<std::string>> args_;
36 MainFunction(F code, std::vector<std::string> args) :
37 code_(std::move(code)),
38 args_(std::make_shared<const std::vector<std::string>>(std::move(args)))
40 void operator()() const
42 const int argc = args_->size();
43 std::vector<std::string> args = *args_;
44 std::unique_ptr<char*[]> argv(new char*[argc + 1]);
45 for (int i = 0; i != argc; ++i)
46 argv[i] = args[i].empty() ? const_cast<char*>(""): &args[i].front();
48 code_(argc, argv.get());
52 template<class F> inline
53 std::function<void()> wrapMain(F code, std::vector<std::string> args)
55 return MainFunction<F>(std::move(code), std::move(args));
58 template<class F> inline
59 std::function<void()> wrapMain(F code, int argc, const char*const argv[])
61 std::vector<std::string> args(argv, argv + argc);
62 return MainFunction<F>(std::move(code), std::move(args));
66 template <class F, class Tuple, std::size_t... I>
67 constexpr auto apply(F&& f, Tuple&& t, simgrid::xbt::index_sequence<I...>)
68 -> decltype(std::forward<F>(f)(std::get<I>(std::forward<Tuple>(t))...))
70 return std::forward<F>(f)(std::get<I>(std::forward<Tuple>(t))...);
74 /** Call a functional object with the values in the given tuple (from C++17)
77 * int foo(int a, bool b);
79 * auto args = std::make_tuple(1, false);
80 * int res = apply(foo, args);
83 template <class F, class Tuple>
84 constexpr auto apply(F&& f, Tuple&& t)
85 -> decltype(simgrid::xbt::bits::apply(
87 std::forward<Tuple>(t),
88 simgrid::xbt::make_index_sequence<
89 std::tuple_size<typename std::decay<Tuple>::type>::value
92 return simgrid::xbt::bits::apply(
94 std::forward<Tuple>(t),
95 simgrid::xbt::make_index_sequence<
96 std::tuple_size<typename std::decay<Tuple>::type>::value
100 template<class T> class Task;
102 /** Type-erased run-once task
104 * * Like std::function but callable only once.
105 * However, it works with move-only types.
107 * * Like std::packaged_task<> but without the shared state.
109 template<class R, class... Args>
110 class Task<R(Args...)> {
113 // Placeholder for some class type:
116 // Union used for storage:
118 typedef typename std::aligned_union<0,
120 std::pair<void(*)(),void*>,
121 std::pair<void(whatever::*)(), whatever*>
126 std::pair<void(*)(),void*> funcptr;
127 std::pair<void(whatever::*)(), whatever*> memberptr;
128 char any1[sizeof(std::pair<void(*)(),void*>)];
129 char any2[sizeof(std::pair<void(whatever::*)(), whatever*>)];
135 // Is F suitable for small buffer optimization?
137 static constexpr bool canSBO()
139 return sizeof(F) <= sizeof(TaskUnion) &&
140 alignof(F) <= alignof(TaskUnion);
143 static_assert(canSBO<std::reference_wrapper<whatever>>(),
144 "SBO not working for reference_wrapper");
146 // Call (and possibly destroy) the function:
147 typedef R (*call_function)(TaskUnion&, Args...);
148 // Destroy the function (of needed):
149 typedef void (*destroy_function)(TaskUnion&);
150 // Move the function (otherwise memcpy):
151 typedef void (*move_function)(TaskUnion& dest, TaskUnion& src);
153 // Vtable of functions for manipulating whatever is in the TaskUnion:
156 destroy_function destroy;
161 const TaskVtable* vtable_ = nullptr;
165 if (vtable_ && vtable_->destroy)
166 vtable_->destroy(buffer_);
172 Task(std::nullptr_t) {}
178 Task(Task const&) = delete;
182 if (that.vtable_ && that.vtable_->move)
183 that.vtable_->move(buffer_, that.buffer_);
185 std::memcpy(&buffer_, &that.buffer_, sizeof(buffer_));
186 vtable_ = that.vtable_;
187 that.vtable_ = nullptr;
189 Task& operator=(Task that)
192 if (that.vtable_ && that.vtable_->move)
193 that.vtable_->move(buffer_, that.buffer_);
195 std::memcpy(&buffer_, &that.buffer_, sizeof(buffer_));
196 vtable_ = that.vtable_;
197 that.vtable_ = nullptr;
204 typename std::enable_if<canSBO<F>()>::type
207 const static TaskVtable vtable {
209 [](TaskUnion& buffer, Args... args) -> R {
210 F* src = reinterpret_cast<F*>(&buffer);
211 F code = std::move(*src);
213 code(std::forward<Args>(args)...);
216 std::is_trivially_destructible<F>::value ?
217 static_cast<destroy_function>(nullptr) :
218 [](TaskUnion& buffer) {
219 F* code = reinterpret_cast<F*>(&buffer);
223 [](TaskUnion& dst, TaskUnion& src) {
224 F* src_code = reinterpret_cast<F*>(&src);
225 F* dst_code = reinterpret_cast<F*>(&dst);
226 new(dst_code) F(std::move(*src_code));
230 new(&buffer_) F(std::move(code));
235 typename std::enable_if<!canSBO<F>()>::type
238 const static TaskVtable vtable {
240 [](TaskUnion& buffer, Args... args) -> R {
241 // Delete F when we go out of scope:
242 std::unique_ptr<F> code(*reinterpret_cast<F**>(&buffer));
243 return (*code)(std::forward<Args>(args)...);
246 [](TaskUnion& buffer) {
247 F* code = *reinterpret_cast<F**>(&buffer);
253 *reinterpret_cast<F**>(&buffer_) = new F(std::move(code));
262 this->init(std::move(code));
265 operator bool() const { return vtable_ != nullptr; }
266 bool operator!() const { return vtable_ == nullptr; }
268 R operator()(Args... args)
270 if (vtable_ == nullptr)
271 throw std::bad_function_call();
272 const TaskVtable* vtable = vtable_;
274 return vtable->call(buffer_, std::forward<Args>(args)...);
278 template<class F, class... Args>
282 std::tuple<Args...> args_;
283 typedef decltype(simgrid::xbt::apply(std::move(code_), std::move(args_))) result_type;
285 TaskImpl(F code, std::tuple<Args...> args) :
286 code_(std::move(code)),
287 args_(std::move(args))
289 result_type operator()()
291 return simgrid::xbt::apply(std::move(code_), std::move(args_));
295 template<class F, class... Args>
296 auto makeTask(F code, Args... args)
297 -> Task< decltype(code(std::move(args)...))() >
299 TaskImpl<F, Args...> task(std::move(code), std::make_tuple(std::move(args)...));
300 return std::move(task);