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
18 #include <type_traits>
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 void operator()() const
40 char noarg[] = {'\0'};
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() ? noarg : &args[i].front();
47 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;
101 /** Type-erased run-once task
103 * * Like std::function but callable only once.
104 * However, it works with move-only types.
106 * * Like std::packaged_task<> but without the shared state.
108 template<class R, class... Args>
109 class Task<R(Args...)> {
112 // Placeholder for some class type:
115 // Union used for storage:
117 typedef typename std::aligned_union<0,
119 std::pair<void(*)(),void*>,
120 std::pair<void(whatever::*)(), whatever*>
125 std::pair<void(*)(),void*> funcptr;
126 std::pair<void(whatever::*)(), whatever*> memberptr;
127 char any1[sizeof(std::pair<void(*)(),void*>)];
128 char any2[sizeof(std::pair<void(whatever::*)(), whatever*>)];
134 // Is F suitable for small buffer optimization?
136 static constexpr bool canSBO()
138 return sizeof(F) <= sizeof(TaskUnion) &&
139 alignof(F) <= alignof(TaskUnion);
142 static_assert(canSBO<std::reference_wrapper<whatever>>(),
143 "SBO not working for reference_wrapper");
145 // Call (and possibly destroy) the function:
146 typedef R (*call_function)(TaskUnion&, Args...);
147 // Destroy the function (of needed):
148 typedef void (*destroy_function)(TaskUnion&);
149 // Move the function (otherwise memcpy):
150 typedef void (*move_function)(TaskUnion& dest, TaskUnion& src);
152 // Vtable of functions for manipulating whatever is in the TaskUnion:
155 destroy_function destroy;
160 const TaskVtable* vtable_ = nullptr;
164 if (vtable_ && vtable_->destroy)
165 vtable_->destroy(buffer_);
171 Task(std::nullptr_t) {}
177 Task(Task const&) = delete;
181 if (that.vtable_ && that.vtable_->move)
182 that.vtable_->move(buffer_, that.buffer_);
184 std::memcpy(&buffer_, &that.buffer_, sizeof(buffer_));
185 vtable_ = that.vtable_;
186 that.vtable_ = nullptr;
188 Task& operator=(Task that)
191 if (that.vtable_ && that.vtable_->move)
192 that.vtable_->move(buffer_, that.buffer_);
194 std::memcpy(&buffer_, &that.buffer_, sizeof(buffer_));
195 vtable_ = that.vtable_;
196 that.vtable_ = nullptr;
203 typename std::enable_if<canSBO<F>()>::type
206 const static TaskVtable vtable {
208 [](TaskUnion& buffer, Args... args) -> R {
209 F* src = reinterpret_cast<F*>(&buffer);
210 F code = std::move(*src);
212 code(std::forward<Args>(args)...);
215 std::is_trivially_destructible<F>::value ?
216 static_cast<destroy_function>(nullptr) :
217 [](TaskUnion& buffer) {
218 F* code = reinterpret_cast<F*>(&buffer);
222 [](TaskUnion& dst, TaskUnion& src) {
223 F* src_code = reinterpret_cast<F*>(&src);
224 F* dst_code = reinterpret_cast<F*>(&dst);
225 new(dst_code) F(std::move(*src_code));
229 new(&buffer_) F(std::move(code));
233 template <class F> typename std::enable_if<not canSBO<F>()>::type init(F code)
235 const static TaskVtable vtable {
237 [](TaskUnion& buffer, Args... args) -> R {
238 // Delete F when we go out of scope:
239 std::unique_ptr<F> code(*reinterpret_cast<F**>(&buffer));
240 return (*code)(std::forward<Args>(args)...);
243 [](TaskUnion& buffer) {
244 F* code = *reinterpret_cast<F**>(&buffer);
250 *reinterpret_cast<F**>(&buffer_) = new F(std::move(code));
259 this->init(std::move(code));
262 operator bool() const { return vtable_ != nullptr; }
263 bool operator!() const { return vtable_ == nullptr; }
265 R operator()(Args... args)
267 if (vtable_ == nullptr)
268 throw std::bad_function_call();
269 const TaskVtable* vtable = vtable_;
271 return vtable->call(buffer_, std::forward<Args>(args)...);
275 template<class F, class... Args>
279 std::tuple<Args...> args_;
280 typedef decltype(simgrid::xbt::apply(std::move(code_), std::move(args_))) result_type;
282 TaskImpl(F code, std::tuple<Args...> args) :
283 code_(std::move(code)),
284 args_(std::move(args))
286 result_type operator()()
288 return simgrid::xbt::apply(std::move(code_), std::move(args_));
292 template<class F, class... Args>
293 auto makeTask(F code, Args... args)
294 -> Task< decltype(code(std::move(args)...))() >
296 TaskImpl<F, Args...> task(std::move(code), std::make_tuple(std::move(args)...));
297 return std::move(task);