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
21 #include <xbt/sysdep.h>
22 #include <xbt/utility.hpp>
31 std::shared_ptr<const std::vector<std::string>> args_;
33 MainFunction(F code, std::vector<std::string> args) :
34 code_(std::move(code)),
35 args_(std::make_shared<const std::vector<std::string>>(std::move(args)))
37 int operator()() const
39 const int argc = args_->size();
40 std::vector<std::string> args = *args_;
41 std::unique_ptr<char*[]> argv(new char*[argc + 1]);
42 for (int i = 0; i != argc; ++i)
43 argv[i] = args[i].empty() ? const_cast<char*>(""): &args[i].front();
45 return code_(argc, argv.get());
49 template<class F> inline
50 std::function<void()> wrapMain(F code, std::vector<std::string> args)
52 return MainFunction<F>(std::move(code), std::move(args));
55 template<class F> inline
56 std::function<void()> wrapMain(F code, int argc, const char*const argv[])
58 std::vector<std::string> args(argv, argv + argc);
59 return MainFunction<F>(std::move(code), std::move(args));
63 template <class F, class Tuple, std::size_t... I>
64 constexpr auto apply(F&& f, Tuple&& t, simgrid::xbt::index_sequence<I...>)
65 -> decltype(std::forward<F>(f)(std::get<I>(std::forward<Tuple>(t))...))
67 return std::forward<F>(f)(std::get<I>(std::forward<Tuple>(t))...);
71 /** Call a functional object with the values in the given tuple (from C++17)
74 * int foo(int a, bool b);
76 * auto args = std::make_tuple(1, false);
77 * int res = apply(foo, args);
80 template <class F, class Tuple>
81 constexpr auto apply(F&& f, Tuple&& t)
82 -> decltype(simgrid::xbt::bits::apply(
84 std::forward<Tuple>(t),
85 simgrid::xbt::make_index_sequence<
86 std::tuple_size<typename std::decay<Tuple>::type>::value
89 return 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
97 template<class T> class Task;
99 /** Type-erased run-once task
101 * * Like std::function but callable only once.
102 * However, it works with move-only types.
104 * * Like std::packaged_task<> but without the shared state.
106 template<class R, class... Args>
107 class Task<R(Args...)> {
109 // Type-erasure for the code:
113 virtual R operator()(Args...) = 0;
116 class Impl : public Base {
118 Impl(F&& code) : code_(std::move(code)) {}
119 Impl(F const& code) : code_(code) {}
121 R operator()(Args... args) override
123 return code_(std::forward<Args>(args)...);
128 std::unique_ptr<Base> code_;
131 Task(std::nullptr_t) {}
135 code_(new Impl<F>(std::forward<F>(code))) {}
137 operator bool() const { return code_ != nullptr; }
138 bool operator!() const { return code_ == nullptr; }
140 template<class... OtherArgs>
141 R operator()(OtherArgs&&... args)
143 std::unique_ptr<Base> code = std::move(code_);
144 return (*code)(std::forward<OtherArgs>(args)...);
148 template<class F, class... Args>
152 std::tuple<Args...> args_;
153 typedef decltype(simgrid::xbt::apply(std::move(code_), std::move(args_))) result_type;
155 TaskImpl(F code, std::tuple<Args...> args) :
156 code_(std::move(code)),
157 args_(std::move(args))
159 result_type operator()()
161 return simgrid::xbt::apply(std::move(code_), std::move(args_));
165 template<class F, class... Args>
166 auto makeTask(F code, Args... args)
167 -> Task< decltype(code(std::move(args)...))() >
169 TaskImpl<F, Args...> task(std::move(code), std::make_tuple(std::move(args)...));
170 return std::move(task);