1 /* Copyright (c) 2015-2018. The SimGrid Team. All rights reserved. */
3 /* This program is free software; you can redistribute it and/or modify it
4 * under the terms of the license (GNU LGPL) which comes with this package. */
6 #ifndef XBT_FUNCTIONAL_HPP
7 #define XBT_FUNCTIONAL_HPP
9 #include <xbt/sysdep.h>
10 #include <xbt/utility.hpp>
22 #include <type_traits>
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 void operator()() const
41 const int argc = args_->size();
42 std::vector<std::string> args = *args_;
43 if (not args.empty()) {
44 char noarg[] = {'\0'};
45 std::unique_ptr<char* []> argv(new char*[argc + 1]);
46 for (int i = 0; i != argc; ++i)
47 argv[i] = args[i].empty() ? noarg : &args[i].front();
49 code_(argc, argv.get());
56 inline XBT_ATTRIB_DEPRECATED_v323("Please use wrap_main()") std::function<void()> wrapMain(
57 F code, std::vector<std::string> args)
59 return MainFunction<F>(std::move(code), std::move(args));
62 template <class F> inline std::function<void()> wrap_main(F code, std::vector<std::string> args)
64 return MainFunction<F>(std::move(code), std::move(args));
68 inline XBT_ATTRIB_DEPRECATED_v323("Please use wrap_main()") std::function<void()> wrapMain(F code, int argc,
69 const char* const argv[])
71 std::vector<std::string> args(argv, argv + argc);
72 return MainFunction<F>(std::move(code), std::move(args));
74 template <class F> inline std::function<void()> wrap_main(F code, int argc, const char* const argv[])
76 std::vector<std::string> args(argv, argv + argc);
77 return MainFunction<F>(std::move(code), std::move(args));
81 template <class F, class Tuple, std::size_t... I>
82 constexpr auto apply(F&& f, Tuple&& t, simgrid::xbt::index_sequence<I...>)
83 -> decltype(std::forward<F>(f)(std::get<I>(std::forward<Tuple>(t))...))
85 return std::forward<F>(f)(std::get<I>(std::forward<Tuple>(t))...);
89 /** Call a functional object with the values in the given tuple (from C++17)
92 * int foo(int a, bool b);
94 * auto args = std::make_tuple(1, false);
95 * int res = apply(foo, args);
98 template <class F, class Tuple>
99 constexpr auto apply(F&& f, Tuple&& t)
100 -> decltype(simgrid::xbt::bits::apply(
102 std::forward<Tuple>(t),
103 simgrid::xbt::make_index_sequence<
104 std::tuple_size<typename std::decay<Tuple>::type>::value
107 return simgrid::xbt::bits::apply(
109 std::forward<Tuple>(t),
110 simgrid::xbt::make_index_sequence<
111 std::tuple_size<typename std::decay<Tuple>::type>::value
115 template<class T> class Task;
117 /** Type-erased run-once task
119 * * Like std::function but callable only once.
120 * However, it works with move-only types.
122 * * Like std::packaged_task<> but without the shared state.
124 template<class R, class... Args>
125 class Task<R(Args...)> {
128 // Placeholder for some class type:
131 // Union used for storage:
133 typedef typename std::aligned_union<0,
135 std::pair<void(*)(),void*>,
136 std::pair<void(whatever::*)(), whatever*>
141 std::pair<void(*)(),void*> funcptr;
142 std::pair<void(whatever::*)(), whatever*> memberptr;
143 char any1[sizeof(std::pair<void(*)(),void*>)];
144 char any2[sizeof(std::pair<void(whatever::*)(), whatever*>)];
145 TaskUnion() { /* Nothing to do */}
146 ~TaskUnion() { /* Nothing to do */}
150 // Is F suitable for small buffer optimization?
152 static constexpr bool canSBO()
154 return sizeof(F) <= sizeof(TaskUnion) &&
155 alignof(F) <= alignof(TaskUnion);
158 static_assert(canSBO<std::reference_wrapper<whatever>>(),
159 "SBO not working for reference_wrapper");
161 // Call (and possibly destroy) the function:
162 typedef R (*call_function)(TaskUnion&, Args...);
163 // Destroy the function (of needed):
164 typedef void (*destroy_function)(TaskUnion&);
165 // Move the function (otherwise memcpy):
166 typedef void (*move_function)(TaskUnion& dest, TaskUnion& src);
168 // Vtable of functions for manipulating whatever is in the TaskUnion:
171 destroy_function destroy;
176 const TaskVtable* vtable_ = nullptr;
180 if (vtable_ && vtable_->destroy)
181 vtable_->destroy(buffer_);
185 Task() { /* Nothing to do */}
186 explicit Task(std::nullptr_t) { /* Nothing to do */}
192 Task(Task const&) = delete;
196 if (that.vtable_ && that.vtable_->move)
197 that.vtable_->move(buffer_, that.buffer_);
199 std::memcpy(static_cast<void*>(&buffer_), static_cast<void*>(&that.buffer_), sizeof(buffer_));
201 vtable_ = that.vtable_;
202 that.vtable_ = nullptr;
204 Task& operator=(Task that)
207 if (that.vtable_ && that.vtable_->move)
208 that.vtable_->move(buffer_, that.buffer_);
210 std::memcpy(static_cast<void*>(&buffer_), static_cast<void*>(&that.buffer_), sizeof(buffer_));
211 vtable_ = that.vtable_;
212 that.vtable_ = nullptr;
219 typename std::enable_if<canSBO<F>()>::type
222 const static TaskVtable vtable {
224 [](TaskUnion& buffer, Args... args) {
225 F* src = reinterpret_cast<F*>(&buffer);
226 F code = std::move(*src);
228 return code(std::forward<Args>(args)...);
231 std::is_trivially_destructible<F>::value ?
232 static_cast<destroy_function>(nullptr) :
233 [](TaskUnion& buffer) {
234 F* code = reinterpret_cast<F*>(&buffer);
238 [](TaskUnion& dst, TaskUnion& src) {
239 F* src_code = reinterpret_cast<F*>(&src);
240 F* dst_code = reinterpret_cast<F*>(&dst);
241 new(dst_code) F(std::move(*src_code));
245 new(&buffer_) F(std::move(code));
249 template <class F> typename std::enable_if<not canSBO<F>()>::type init(F code)
251 const static TaskVtable vtable {
253 [](TaskUnion& buffer, Args... args) {
254 // Delete F when we go out of scope:
255 std::unique_ptr<F> code(*reinterpret_cast<F**>(&buffer));
256 return (*code)(std::forward<Args>(args)...);
259 [](TaskUnion& buffer) {
260 F* code = *reinterpret_cast<F**>(&buffer);
266 *reinterpret_cast<F**>(&buffer_) = new F(std::move(code));
271 template <class F> explicit Task(F code) { this->init(std::move(code)); }
273 operator bool() const { return vtable_ != nullptr; }
274 bool operator!() const { return vtable_ == nullptr; }
276 R operator()(Args... args)
278 if (vtable_ == nullptr)
279 throw std::bad_function_call();
280 const TaskVtable* vtable = vtable_;
282 return vtable->call(buffer_, std::forward<Args>(args)...);
286 template<class F, class... Args>
290 std::tuple<Args...> args_;
291 typedef decltype(simgrid::xbt::apply(std::move(code_), std::move(args_))) result_type;
293 TaskImpl(F code, std::tuple<Args...> args) :
294 code_(std::move(code)),
295 args_(std::move(args))
297 result_type operator()()
299 return simgrid::xbt::apply(std::move(code_), std::move(args_));
303 template <class F, class... Args>
304 XBT_ATTRIB_DEPRECATED_v323("Please use make_task()") auto makeTask(F code, Args... args)
305 -> Task<decltype(code(std::move(args)...))()>
307 TaskImpl<F, Args...> task(std::move(code), std::make_tuple(std::move(args)...));
308 return Task<decltype(code(std::move(args)...))()>(std::move(task));
311 template <class F, class... Args> auto make_task(F code, Args... args) -> Task<decltype(code(std::move(args)...))()>
313 TaskImpl<F, Args...> task(std::move(code), std::make_tuple(std::move(args)...));
314 return Task<decltype(code(std::move(args)...))()>(std::move(task));