Callback.hpp
Go to the documentation of this file.
1 // Distributed under the MIT License.
2 // See LICENSE.txt for details.
3 
4 /// \file
5 /// Defines Parallel::Callback.
6 
7 #pragma once
8 
9 #include <pup.h>
10 #include <tuple>
11 #include <utility>
12 
14 #include "Parallel/Invoke.hpp"
15 
16 namespace Parallel {
17 /// An abstract base class, whose derived class holds a function that
18 /// can be invoked at a later time. The function is intended to be
19 /// invoked only once.
20 class Callback : public PUP::able {
21  public:
22  WRAPPED_PUPable_abstract(Callback); // NOLINT
23  Callback() = default;
24  Callback(const Callback&) = default;
25  Callback& operator=(const Callback&) = default;
26  Callback(Callback&&) = default;
27  Callback& operator=(Callback&&) = default;
28  virtual ~Callback() = default;
29  virtual void invoke() noexcept = 0;
30 };
31 
32 /// Wraps a call to a simple action and its arguments.
33 /// Can be invoked only once.
34 template <typename SimpleAction, typename Proxy, typename... Args>
36  public:
37  WRAPPED_PUPable_decl_template(SimpleActionCallback); // NOLINT
38  SimpleActionCallback(Proxy proxy, Args&&... args)
39  : proxy_(proxy), args_(std::make_tuple<Args...>(args...)) {}
40  void invoke() noexcept override {
41  std::apply(
42  [this](auto&&... args) noexcept {
43  Parallel::simple_action<SimpleAction>(proxy_, args...);
44  },
45  std::move(args_));
46  }
47  void pup(PUP::er& p) noexcept override {
48  p | proxy_;
49  p | args_;
50  }
51 
52  private:
53  Proxy proxy_;
54  std::tuple<Args...> args_;
55 };
56 
57 /// Wraps a call to a simple action without arguments.
58 template <typename SimpleAction, typename Proxy>
59 class SimpleActionCallback<SimpleAction, Proxy> : public Callback {
60  public:
61  WRAPPED_PUPable_decl_template(SimpleActionCallback); // NOLINT
62  SimpleActionCallback(Proxy proxy) : proxy_(proxy) {}
63  void invoke() noexcept override {
64  Parallel::simple_action<SimpleAction>(proxy_);
65  }
66 
67  void pup(PUP::er& p) noexcept override { p | proxy_; }
68 
69  private:
70  Proxy proxy_;
71 };
72 
73 /// Wraps a call to perform_algorithm.
74 template <typename Proxy>
76  public:
77  WRAPPED_PUPable_decl_template(PerformAlgorithmCallback); // NOLINT
78  PerformAlgorithmCallback(Proxy proxy) : proxy_(proxy) {}
79  void invoke() noexcept override { proxy_.perform_algorithm(); }
80  void pup(PUP::er& p) noexcept override { p | proxy_; }
81 
82  private:
83  Proxy proxy_;
84 };
85 
86 /// \cond
87 template <typename Proxy>
88 PUP::able::PUP_ID PerformAlgorithmCallback<Proxy>::my_PUP_ID = 0; // NOLINT
89 template <typename SimpleAction, typename Proxy, typename... Args>
90 PUP::able::PUP_ID
91  SimpleActionCallback<SimpleAction, Proxy, Args...>::my_PUP_ID =
92  0; // NOLINT
93 template <typename SimpleAction, typename Proxy>
95  0; // NOLINT
96 /// \endcond
97 
98 } // namespace Parallel
CharmPupable.hpp
utility
tuple
Parallel::PerformAlgorithmCallback
Wraps a call to perform_algorithm.
Definition: Callback.hpp:75
Parallel::Callback
An abstract base class, whose derived class holds a function that can be invoked at a later time....
Definition: Callback.hpp:20
Parallel::SimpleActionCallback
Wraps a call to a simple action and its arguments. Can be invoked only once.
Definition: Callback.hpp:35
Parallel
Functionality for parallelization.
Definition: ElementReceiveInterpPoints.hpp:13