Documentation of MARTY
A Modern ARtificial Theoretical phYsicist
callback.h
Go to the documentation of this file.
1 // This file is part of MARTY.
2 //
3 // MARTY is free software: you can redistribute it and/or modify
4 // it under the terms of the GNU General Public License as published by
5 // the Free Software Foundation, either version 3 of the License, or
6 // (at your option) any later version.
7 //
8 // MARTY is distributed in the hope that it will be useful,
9 // but WITHOUT ANY WARRANTY; without even the implied warranty of
10 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 // GNU General Public License for more details.
12 //
13 // You should have received a copy of the GNU General Public License
14 // along with MARTY. If not, see <https://www.gnu.org/licenses/>.
15 
23 #ifndef CALLBACK_H_INCLUDED
24 #define CALLBACK_H_INCLUDED
25 
26 #include <optional>
27 #include <functional>
28 
29 namespace mty {
30 
31  template<class ReturnType, class ...Args>
32  struct CallBack {
33 
34  std::optional<std::function<ReturnType(Args...)>> func;
35 
36  CallBack() = default;
37  CallBack(CallBack const &other) = default;
38  CallBack &operator=(CallBack const &other) = default;
39  CallBack(std::function<ReturnType(Args ...)> const &t_func)
40  :func(t_func)
41  {}
42 
43  CallBack &operator=(std::function<ReturnType(Args ...)> const &t_func)
44  {
45  func = t_func;
46  return *this;
47  }
48 
49  inline operator bool() const
50  {
51  return bool(func);
52  }
53 
54  void reset()
55  {
56  func.reset();
57  }
58 
59  bool apply(
60  ReturnType &returnValue,
61  Args &&...args
62  ) const
63  {
64  if (!func)
65  return false;
66  returnValue = (*func)(std::forward<Args>(args)...);
67  return true;
68  }
69  };
70 
71  template<class ...Args>
72  struct CallBack<void(Args...)> {
73 
74  std::optional<std::function<void(Args...)>> func;
75 
76  CallBack() = default;
77  CallBack(CallBack const &other) = default;
78  CallBack &operator=(CallBack const &other) = default;
79  CallBack(std::function<void(Args ...)> const &t_func)
80  :func(t_func)
81  {}
82 
83  CallBack &operator=(std::function<void(Args ...)> const &t_func)
84  {
85  func = t_func;
86  return *this;
87  }
88 
89  inline operator bool() const
90  {
91  return bool(func);
92  }
93 
94  void reset()
95  {
96  func.reset();
97  }
98 
99  bool apply(
100  Args &&...args
101  ) const
102  {
103  if (!func)
104  return false;
105  (*func)(std::forward<Args>(args)...);
106  return true;
107  }
108  };
109 
110 }
111 
112 #endif
Definition: callback.h:32
Namespace of MARTY.
Definition: 2HDM.h:31