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 char noarg[] = {'\0'};
43 const int argc = args_->size();
44 std::vector<std::string> args = *args_;
45 if (not args.empty()) {
46 std::unique_ptr<char* []> argv(new char*[argc + 1]);
47 for (int i = 0; i != argc; ++i)
48 argv[i] = args[i].empty() ? noarg : &args[i].front();
50 code_(argc, argv.get());
56 template<class F> inline
57 std::function<void()> wrapMain(F code, std::vector<std::string> args)
59 return MainFunction<F>(std::move(code), std::move(args));
62 template<class F> inline
63 std::function<void()> wrapMain(F code, int argc, const char*const argv[])
65 std::vector<std::string> args(argv, argv + argc);
66 return MainFunction<F>(std::move(code), std::move(args));
70 template <class F, class Tuple, std::size_t... I>
71 constexpr auto apply(F&& f, Tuple&& t, simgrid::xbt::index_sequence<I...>)
72 -> decltype(std::forward<F>(f)(std::get<I>(std::forward<Tuple>(t))...))
74 return std::forward<F>(f)(std::get<I>(std::forward<Tuple>(t))...);
78 /** Call a functional object with the values in the given tuple (from C++17)
81 * int foo(int a, bool b);
83 * auto args = std::make_tuple(1, false);
84 * int res = apply(foo, args);
87 template <class F, class Tuple>
88 constexpr auto apply(F&& f, Tuple&& t)
89 -> decltype(simgrid::xbt::bits::apply(
91 std::forward<Tuple>(t),
92 simgrid::xbt::make_index_sequence<
93 std::tuple_size<typename std::decay<Tuple>::type>::value
96 return simgrid::xbt::bits::apply(
98 std::forward<Tuple>(t),
99 simgrid::xbt::make_index_sequence<
100 std::tuple_size<typename std::decay<Tuple>::type>::value
104 template<class T> class Task;
106 /** Type-erased run-once task
108 * * Like std::function but callable only once.
109 * However, it works with move-only types.
111 * * Like std::packaged_task<> but without the shared state.
113 template<class R, class... Args>
114 class Task<R(Args...)> {
117 // Placeholder for some class type:
120 // Union used for storage:
122 typedef typename std::aligned_union<0,
124 std::pair<void(*)(),void*>,
125 std::pair<void(whatever::*)(), whatever*>
130 std::pair<void(*)(),void*> funcptr;
131 std::pair<void(whatever::*)(), whatever*> memberptr;
132 char any1[sizeof(std::pair<void(*)(),void*>)];
133 char any2[sizeof(std::pair<void(whatever::*)(), whatever*>)];
139 // Is F suitable for small buffer optimization?
141 static constexpr bool canSBO()
143 return sizeof(F) <= sizeof(TaskUnion) &&
144 alignof(F) <= alignof(TaskUnion);
147 static_assert(canSBO<std::reference_wrapper<whatever>>(),
148 "SBO not working for reference_wrapper");
150 // Call (and possibly destroy) the function:
151 typedef R (*call_function)(TaskUnion&, Args...);
152 // Destroy the function (of needed):
153 typedef void (*destroy_function)(TaskUnion&);
154 // Move the function (otherwise memcpy):
155 typedef void (*move_function)(TaskUnion& dest, TaskUnion& src);
157 // Vtable of functions for manipulating whatever is in the TaskUnion:
160 destroy_function destroy;
165 const TaskVtable* vtable_ = nullptr;
169 if (vtable_ && vtable_->destroy)
170 vtable_->destroy(buffer_);
176 Task(std::nullptr_t) {}
182 Task(Task const&) = delete;
186 if (that.vtable_ && that.vtable_->move)
187 that.vtable_->move(buffer_, that.buffer_);
189 std::memcpy(&buffer_, &that.buffer_, sizeof(buffer_));
190 vtable_ = that.vtable_;
191 that.vtable_ = nullptr;
193 Task& operator=(Task that)
196 if (that.vtable_ && that.vtable_->move)
197 that.vtable_->move(buffer_, that.buffer_);
199 std::memcpy(&buffer_, &that.buffer_, sizeof(buffer_));
200 vtable_ = that.vtable_;
201 that.vtable_ = nullptr;
208 typename std::enable_if<canSBO<F>()>::type
211 const static TaskVtable vtable {
213 [](TaskUnion& buffer, Args... args) -> R {
214 F* src = reinterpret_cast<F*>(&buffer);
215 F code = std::move(*src);
217 code(std::forward<Args>(args)...);
220 std::is_trivially_destructible<F>::value ?
221 static_cast<destroy_function>(nullptr) :
222 [](TaskUnion& buffer) {
223 F* code = reinterpret_cast<F*>(&buffer);
227 [](TaskUnion& dst, TaskUnion& src) {
228 F* src_code = reinterpret_cast<F*>(&src);
229 F* dst_code = reinterpret_cast<F*>(&dst);
230 new(dst_code) F(std::move(*src_code));
234 new(&buffer_) F(std::move(code));
238 template <class F> typename std::enable_if<not canSBO<F>()>::type init(F code)
240 const static TaskVtable vtable {
242 [](TaskUnion& buffer, Args... args) -> R {
243 // Delete F when we go out of scope:
244 std::unique_ptr<F> code(*reinterpret_cast<F**>(&buffer));
245 return (*code)(std::forward<Args>(args)...);
248 [](TaskUnion& buffer) {
249 F* code = *reinterpret_cast<F**>(&buffer);
255 *reinterpret_cast<F**>(&buffer_) = new F(std::move(code));
264 this->init(std::move(code));
267 operator bool() const { return vtable_ != nullptr; }
268 bool operator!() const { return vtable_ == nullptr; }
270 R operator()(Args... args)
272 if (vtable_ == nullptr)
273 throw std::bad_function_call();
274 const TaskVtable* vtable = vtable_;
276 return vtable->call(buffer_, std::forward<Args>(args)...);
280 template<class F, class... Args>
284 std::tuple<Args...> args_;
285 typedef decltype(simgrid::xbt::apply(std::move(code_), std::move(args_))) result_type;
287 TaskImpl(F code, std::tuple<Args...> args) :
288 code_(std::move(code)),
289 args_(std::move(args))
291 result_type operator()()
293 return simgrid::xbt::apply(std::move(code_), std::move(args_));
297 template<class F, class... Args>
298 auto makeTask(F code, Args... args)
299 -> Task< decltype(code(std::move(args)...))() >
301 TaskImpl<F, Args...> task(std::move(code), std::make_tuple(std::move(args)...));
302 return std::move(task);