Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Update copyright lines for 2022.
[simgrid.git] / include / xbt / functional.hpp
1 /* Copyright (c) 2015-2022. The SimGrid Team. All rights reserved.          */
2
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. */
5
6 #ifndef XBT_FUNCTIONAL_HPP
7 #define XBT_FUNCTIONAL_HPP
8
9 #include <xbt/sysdep.h>
10
11 #include <cstddef>
12 #include <cstdlib>
13 #include <cstring>
14
15 #include <algorithm>
16 #include <array>
17 #include <exception>
18 #include <functional>
19 #include <memory>
20 #include <string>
21 #include <tuple>
22 #include <type_traits>
23 #include <utility>
24 #include <vector>
25
26 namespace simgrid {
27 namespace xbt {
28
29 template <class F> class MainFunction {
30   F code_;
31   std::shared_ptr<const std::vector<std::string>> args_;
32
33 public:
34   MainFunction(F code, std::vector<std::string>&& args)
35       : code_(std::move(code)), args_(std::make_shared<const std::vector<std::string>>(std::move(args)))
36   {
37   }
38   void operator()() const
39   {
40     const int argc                = args_->size();
41     std::vector<std::string> args = *args_;
42     std::vector<char*> argv(args.size() + 1); // argv[argc] is nullptr
43     std::transform(begin(args), end(args), begin(argv), [](std::string& s) { return &s.front(); });
44     code_(argc, argv.data());
45   }
46 };
47
48 template <class F> inline std::function<void()> wrap_main(F code, std::vector<std::string>&& args)
49 {
50   return MainFunction<F>(std::move(code), std::move(args));
51 }
52
53 template <class F> inline std::function<void()> wrap_main(F code, int argc, const char* const argv[])
54 {
55   std::vector<std::string> args(argv, argv + argc);
56   return MainFunction<F>(std::move(code), std::move(args));
57 }
58
59 namespace bits {
60 template <class F, class Tuple, std::size_t... I>
61 constexpr auto apply(F&& f, Tuple&& t, std::index_sequence<I...>)
62     -> decltype(std::forward<F>(f)(std::get<I>(std::forward<Tuple>(t))...))
63 {
64   return std::forward<F>(f)(std::get<I>(std::forward<Tuple>(t))...);
65 }
66 }
67
68 /** Call a functional object with the values in the given tuple (from C++17)
69  *
70  *  @code{.cpp}
71  *  int foo(int a, bool b);
72  *
73  *  auto args = std::make_tuple(1, false);
74  *  int res = apply(foo, args);
75  *  @endcode
76  **/
77 template <class F, class Tuple>
78 constexpr auto apply(F&& f, Tuple&& t) -> decltype(
79     simgrid::xbt::bits::apply(std::forward<F>(f), std::forward<Tuple>(t),
80                               std::make_index_sequence<std::tuple_size<typename std::decay_t<Tuple>>::value>()))
81 {
82   return simgrid::xbt::bits::apply(std::forward<F>(f), std::forward<Tuple>(t),
83                                    std::make_index_sequence<std::tuple_size<typename std::decay_t<Tuple>>::value>());
84 }
85
86 template<class T> class Task;
87
88 /** Type-erased run-once task
89  *
90  *  * Like std::function but callable only once.
91  *    However, it works with move-only types.
92  *
93  *  * Like std::packaged_task<> but without the shared state.
94  */
95 template<class R, class... Args>
96 class Task<R(Args...)> {
97   // Placeholder for some class type:
98   struct whatever {};
99
100   // Union used for storage:
101   using TaskUnion =
102       typename std::aligned_union_t<0, void*, std::pair<void (*)(), void*>, std::pair<void (whatever::*)(), whatever*>>;
103
104   // Is F suitable for small buffer optimization?
105   template<class F>
106   static constexpr bool canSBO()
107   {
108     return sizeof(F) <= sizeof(TaskUnion) &&
109       alignof(F) <= alignof(TaskUnion);
110   }
111
112   static_assert(canSBO<std::reference_wrapper<whatever>>(),
113     "SBO not working for reference_wrapper");
114
115   // Call (and possibly destroy) the function:
116   using call_function = R (*)(TaskUnion&, Args...);
117   // Destroy the function (of needed):
118   using destroy_function = void (*)(TaskUnion&);
119   // Move the function (otherwise memcpy):
120   using move_function = void (*)(TaskUnion& dest, TaskUnion& src);
121
122   // Vtable of functions for manipulating whatever is in the TaskUnion:
123   struct TaskVtable {
124     call_function call;
125     destroy_function destroy;
126     move_function move;
127   };
128
129   TaskUnion buffer_         = {};
130   const TaskVtable* vtable_ = nullptr;
131
132   void clear()
133   {
134     if (vtable_ && vtable_->destroy)
135       vtable_->destroy(buffer_);
136   }
137
138 public:
139   Task() = default;
140   explicit Task(std::nullptr_t) { /* Nothing to do */}
141   ~Task()
142   {
143     this->clear();
144   }
145
146   Task(Task const&) = delete;
147
148   Task(Task&& that) noexcept
149   {
150     if (that.vtable_ && that.vtable_->move)
151       that.vtable_->move(buffer_, that.buffer_);
152     else
153       std::memcpy(&buffer_, &that.buffer_, sizeof(buffer_));
154     vtable_      = std::move(that.vtable_);
155     that.vtable_ = nullptr;
156   }
157   Task& operator=(Task const& that) = delete;
158   Task& operator=(Task&& that) noexcept
159   {
160     this->clear();
161     if (that.vtable_ && that.vtable_->move)
162       that.vtable_->move(buffer_, that.buffer_);
163     else
164       std::memcpy(&buffer_, &that.buffer_, sizeof(buffer_));
165     vtable_      = std::move(that.vtable_);
166     that.vtable_ = nullptr;
167     return *this;
168   }
169
170 private:
171   template <class F> typename std::enable_if_t<canSBO<F>()> init(F code)
172   {
173     const static TaskVtable vtable {
174       // Call:
175       [](TaskUnion& buffer, Args... args) {
176         auto* src = reinterpret_cast<F*>(&buffer);
177         F code = std::move(*src);
178         src->~F();
179         // NOTE: std::forward<Args>(args)... is correct.
180         return code(std::forward<Args>(args)...);
181       },
182       // Destroy:
183       std::is_trivially_destructible<F>::value ?
184       static_cast<destroy_function>(nullptr) :
185       [](TaskUnion& buffer) {
186         auto* code = reinterpret_cast<F*>(&buffer);
187         code->~F();
188       },
189       // Move:
190       [](TaskUnion& dst, TaskUnion& src) {
191         auto* src_code = reinterpret_cast<F*>(&src);
192         auto* dst_code = reinterpret_cast<F*>(&dst);
193         new(dst_code) F(std::move(*src_code));
194         src_code->~F();
195       }
196     };
197     new(&buffer_) F(std::move(code));
198     vtable_ = &vtable;
199   }
200
201   template <class F> typename std::enable_if_t<not canSBO<F>()> init(F code)
202   {
203     const static TaskVtable vtable {
204       // Call:
205       [](TaskUnion& buffer, Args... args) {
206         // Delete F when we go out of scope:
207         std::unique_ptr<F> code(*reinterpret_cast<F**>(&buffer));
208         // NOTE: std::forward<Args>(args)... is correct.
209         return (*code)(std::forward<Args>(args)...);
210       },
211       // Destroy:
212       [](TaskUnion& buffer) {
213         F* code = *reinterpret_cast<F**>(&buffer);
214         delete code;
215       },
216       // Move:
217       nullptr
218     };
219     *reinterpret_cast<F**>(&buffer_) = new F(std::move(code));
220     vtable_ = &vtable;
221   }
222
223 public:
224   template <class F> explicit Task(F code) { this->init(std::move(code)); }
225
226   operator bool() const { return vtable_ != nullptr; }
227   bool operator!() const { return vtable_ == nullptr; }
228
229   R operator()(Args... args)
230   {
231     if (vtable_ == nullptr)
232       throw std::bad_function_call();
233     const TaskVtable* vtable = vtable_;
234     vtable_ = nullptr;
235     // NOTE: std::forward<Args>(args)... is correct.
236     // see C++ [func.wrap.func.inv] for an example
237     return vtable->call(buffer_, std::forward<Args>(args)...);
238   }
239 };
240
241 template<class F, class... Args>
242 class TaskImpl {
243   F code_;
244   std::tuple<Args...> args_;
245   using result_type = decltype(simgrid::xbt::apply(std::move(code_), std::move(args_)));
246
247 public:
248   TaskImpl(F code, std::tuple<Args...> args) :
249     code_(std::move(code)),
250     args_(std::move(args))
251   {}
252   result_type operator()()
253   {
254     return simgrid::xbt::apply(std::move(code_), std::move(args_));
255   }
256 };
257
258 template <class F, class... Args> auto make_task(F code, Args... args) -> Task<decltype(code(std::move(args)...))()>
259 {
260   TaskImpl<F, Args...> task(std::move(code), std::make_tuple(std::move(args)...));
261   return Task<decltype(code(std::move(args)...))()>(std::move(task));
262 }
263
264 } // namespace xbt
265 } // namespace simgrid
266 #endif