Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
1f8abfbb200ab889d6ee6eda2c8546c7f23f7ae1
[simgrid.git] / include / xbt / functional.hpp
1 /* Copyright (c) 2015-2016. The SimGrid Team.
2  * All rights reserved.                                                     */
3
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. */
6
7 #ifndef XBT_FUNCTIONAL_HPP
8 #define XBT_FUNCTIONAL_HPP
9
10 #include <cstdlib>
11
12 #include <exception>
13 #include <functional>
14 #include <future>
15 #include <utility>
16 #include <tuple>
17
18 #include <xbt/sysdep.h>
19 #include <xbt/utility.hpp>
20
21 namespace simgrid {
22 namespace xbt {
23
24 class args {
25 private:
26   int argc_ = 0;
27   char** argv_ = nullptr;
28 public:
29
30   // Main constructors
31   args() {}
32
33   void assign(int argc, const char*const* argv)
34   {
35     clear();
36     char** new_argv = xbt_new(char*,argc + 1);
37     for (int i = 0; i < argc; i++)
38       new_argv[i] = xbt_strdup(argv[i]);
39     new_argv[argc] = nullptr;
40     this->argc_ = argc;
41     this->argv_ = new_argv;
42   }
43   args(int argc, const char*const* argv)
44   {
45     this->assign(argc, argv);
46   }
47
48   char** to_argv() const
49   {
50     const int argc = argc_;
51     char** argv = xbt_new(char*, argc + 1);
52     for (int i=0; i< argc; i++)
53       argv[i] = xbt_strdup(argv_[i]);
54     argv[argc] = nullptr;
55     return argv;
56   }
57
58   // Free
59   void clear()
60   {
61     for (int i = 0; i < this->argc_; i++)
62       std::free(this->argv_[i]);
63     std::free(this->argv_);
64     this->argc_ = 0;
65     this->argv_ = nullptr;
66   }
67   ~args() { clear(); }
68
69   // Copy
70   args(args const& that)
71   {
72     this->assign(that.argc(), that.argv());
73   }
74   args& operator=(args const& that)
75   {
76     this->assign(that.argc(), that.argv());
77     return *this;
78   }
79
80   // Move:
81   args(args&& that) : argc_(that.argc_), argv_(that.argv_)
82   {
83     that.argc_ = 0;
84     that.argv_ = nullptr;
85   }
86   args& operator=(args&& that)
87   {
88     this->argc_ = that.argc_;
89     this->argv_ = that.argv_;
90     that.argc_ = 0;
91     that.argv_ = nullptr;
92     return *this;
93   }
94
95   int    argc()            const { return argc_; }
96   char** argv()                  { return argv_; }
97   const char*const* argv() const { return argv_; }
98   char* operator[](std::size_t i) { return argv_[i]; }
99 };
100
101 template<class F> inline
102 std::function<void()> wrapMain(F code, std::shared_ptr<simgrid::xbt::args> args)
103 {
104   return [=]() {
105     code(args->argc(), args->argv());
106   };
107 }
108
109 template<class F> inline
110 std::function<void()> wrapMain(F code, simgrid::xbt::args args)
111 {
112   return wrapMain(std::move(code),
113     std::unique_ptr<simgrid::xbt::args>(new simgrid::xbt::args(std::move(args))));
114 }
115
116 template<class F> inline
117 std::function<void()> wrapMain(F code, int argc, const char*const* argv)
118 {
119   return wrapMain(std::move(code), args(argc, argv));
120 }
121
122 namespace bits {
123 template <class F, class Tuple, std::size_t... I>
124 constexpr auto apply(F&& f, Tuple&& t, simgrid::xbt::index_sequence<I...>)
125   -> decltype(std::forward<F>(f)(std::get<I>(std::forward<Tuple>(t))...))
126 {
127   return std::forward<F>(f)(std::get<I>(std::forward<Tuple>(t))...);
128 }
129 }
130
131 /** Call a functional object with the values in the given tuple (from C++17)
132  *
133  *  @code{.cpp}
134  *  int foo(int a, bool b);
135  *
136  *  auto args = std::make_tuple(1, false);
137  *  int res = apply(foo, args);
138  *  @encode
139  **/
140 template <class F, class Tuple>
141 constexpr auto apply(F&& f, Tuple&& t)
142   -> decltype(simgrid::xbt::bits::apply(
143     std::forward<F>(f),
144     std::forward<Tuple>(t),
145     simgrid::xbt::make_index_sequence<
146       std::tuple_size<typename std::decay<Tuple>::type>::value
147     >()))
148 {
149   return simgrid::xbt::bits::apply(
150     std::forward<F>(f),
151     std::forward<Tuple>(t),
152     simgrid::xbt::make_index_sequence<
153       std::tuple_size<typename std::decay<Tuple>::type>::value
154     >());
155 }
156
157 template<class T> class Task;
158
159 /** Type-erased run-once task
160  *
161  *  * Like std::function but callable only once.
162  *    However, it works with move-only types.
163  *
164  *  * Like std::packaged_task<> but without the shared state.
165  */
166 template<class R, class... Args>
167 class Task<R(Args...)> {
168 private:
169   // Type-erasure for the code:
170   class Base {
171   public:
172     virtual ~Base() {}
173     virtual R operator()(Args...) = 0;
174   };
175   template<class F>
176   class Impl : public Base {
177   public:
178     Impl(F&& code) : code_(std::move(code)) {}
179     Impl(F const& code) : code_(code) {}
180     ~Impl() override {}
181     R operator()(Args... args)
182     {
183       return code_(std::forward<Args>(args)...);
184     }
185   private:
186     F code_;
187   };
188   std::unique_ptr<Base> code_;
189 public:
190   Task() {}
191   Task(std::nullptr_t) {}
192
193   template<class F>
194   Task(F&& code) :
195     code_(new Impl<F>(std::forward<F>(code))) {}
196
197   operator bool() const { return code_ != nullptr; }
198   bool operator!() const { return code_ == nullptr; }
199
200   template<class... OtherArgs>
201   R operator()(OtherArgs&&... args)
202   {
203     std::unique_ptr<Base> code = std::move(code_);
204     return (*code)(std::forward<OtherArgs>(args)...);
205   }
206 };
207
208 template<class F, class... Args>
209 auto makeTask(F code, Args... args)
210 -> Task< decltype(code(std::move(args)...))() >
211 {
212   typedef decltype(code(std::move(args)...)) result_type;
213
214   class Impl {
215   private:
216     F code_;
217     std::tuple<Args...> args_;
218   public:
219     Impl(F code, std::tuple<Args...> args) :
220       code_(std::move(code)),
221       args_(std::move(args)) {}
222     result_type operator()()
223     {
224       return simgrid::xbt::apply(std::move(code_), std::move(args_));
225     }
226   };
227
228   return Impl(std::move(code), std::make_tuple(std::move(args)...));
229 }
230
231 }
232 }
233
234 #endif