a4025046704bf2345c2fbf05c2a4d5960f2378e6
[alexxy/gromacs.git] / src / gromacs / utility / mdmodulenotification-impl.h
1 /*
2  * This file is part of the GROMACS molecular simulation package.
3  *
4  * Copyright (c) 2020, by the GROMACS development team, led by
5  * Mark Abraham, David van der Spoel, Berk Hess, and Erik Lindahl,
6  * and including many others, as listed in the AUTHORS file in the
7  * top-level source directory and at http://www.gromacs.org.
8  *
9  * GROMACS is free software; you can redistribute it and/or
10  * modify it under the terms of the GNU Lesser General Public License
11  * as published by the Free Software Foundation; either version 2.1
12  * of the License, or (at your option) any later version.
13  *
14  * GROMACS is distributed in the hope that it will be useful,
15  * but WITHOUT ANY WARRANTY; without even the implied warranty of
16  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
17  * Lesser General Public License for more details.
18  *
19  * You should have received a copy of the GNU Lesser General Public
20  * License along with GROMACS; if not, see
21  * http://www.gnu.org/licenses, or write to the Free Software Foundation,
22  * Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA.
23  *
24  * If you want to redistribute modifications to GROMACS, please
25  * consider that scientific software is very special. Version
26  * control is crucial - bugs must be traceable. We will be happy to
27  * consider code for inclusion in the official distribution, but
28  * derived work must not be called official GROMACS. Details are found
29  * in the README & COPYING files - if they are missing, get the
30  * official version at http://www.gromacs.org.
31  *
32  * To help us fund GROMACS development, we humbly ask that you cite
33  * the research papers on the package. Check out http://www.gromacs.org.
34  */
35 /*! \libinternal \file
36  * \brief
37  * Declares gmx::MdModuleNotification.
38  *
39  * \author Christian Blau <blau@kth.se>
40  * \inlibraryapi
41  * \ingroup module_utility
42  */
43
44 #ifndef GMX_UTILITY_MDMODULENOTIFICATION_IMPL_H
45 #define GMX_UTILITY_MDMODULENOTIFICATION_IMPL_H
46
47 #include <functional>
48 #include <vector>
49
50 namespace gmx
51 {
52
53 /*! \libinternal \brief
54  * Subscribe and trigger notification functions.
55  *
56  * Extends MdModuleNotificationBase with new notification function and routine
57  * to subscribe new listeners.
58  *
59  * To create a class of this type that provides callbacks, e.g., for events
60  * EventA, and EventB use registerMdModuleNotification<EventA, EventB>::type.
61  *
62  * \tparam CallParameter of the function to be notified
63  * \tparam MdModuleNotificationBase class to be extended with a notification
64  *                                  with CallParameter
65  *
66  * \note All added subscribers are required to out-live the MdModuleNotification
67  *
68  */
69 template<class CallParameter, class MdModuleNotificationBase>
70 class MdModuleNotification : public MdModuleNotificationBase
71 {
72 public:
73     //! Make base class notification trigger available to this class
74     using MdModuleNotificationBase::notify;
75     //! Make base class subscription available to this class
76     using MdModuleNotificationBase::subscribe;
77
78     /*! \brief Trigger the subscribed notifications.
79      * \param[in] callParameter of the function to be called back
80      */
81     void notify(CallParameter callParameter) const
82     {
83         for (auto& callBack : callBackFunctions_)
84         {
85             callBack(callParameter);
86         }
87     }
88
89     /*! \brief
90      * Add callback function to be called when notification is triggered.
91      *
92      * Notifications are distinguished by their call signature.
93      *
94      * \param[in] callBackFunction to be called from this class
95      */
96     void subscribe(std::function<void(CallParameter)> callBackFunction)
97     {
98         callBackFunctions_.emplace_back(callBackFunction);
99     }
100
101 private:
102     std::vector<std::function<void(CallParameter)>> callBackFunctions_;
103 };
104
105 /*! \internal
106  * \brief Aide to avoid nested MdModuleNotification definition.
107  *
108  * Instead of
109  * MdModuleNotification<CallParameterA, MdModuleNotification<CallParameterB, etc ... >>
110  * this allows to write
111  * registerMdModuleNotification<CallParameterA, CallParameterB, ...>::type
112  *
113  * \tparam CallParameter all the event types to be registered
114  */
115 template<class... CallParameter>
116 struct registerMdModuleNotification;
117
118 /*! \internal \brief Template specialization to end parameter unpacking recursion.
119  */
120 template<>
121 struct registerMdModuleNotification<>
122 {
123     /*! \internal
124      * \brief Do nothing but be base class of MdModuleNotification.
125      *
126      * Required so that using MdModuleNotificationBase::notify and
127      * MdModuleNotificationBase::subscribe are valid in derived class.
128      */
129     class NoCallParameter
130     {
131     public:
132         //! Do nothing but provide MdModuleNotification::notify to derived class
133         void notify() {}
134         //! Do nothing but provide MdModuleNotification::subscribe to derived class
135         void subscribe() {}
136     };
137     /*! \brief Defines a type if no notifications are managed.
138      *
139      * This ensures that code works with MdModuleCallParameterManagement that
140      * does not manage any notifications.
141      */
142     using type = NoCallParameter;
143 };
144
145 /*! \libinternal
146  * \brief Template specialization to assemble MdModuleNotification.
147  *
148  * Assembly of MdModuleNotification is performed by recursively taking off the
149  * front of the CallParameter parameter pack and constructing the nested type
150  * definition of MdModuleNotification base classes.
151  *
152  * \tparam CurrentCallParameter front of the template parameter pack
153  * \tparam CallParameter rest of the event types
154  */
155 template<class CurrentCallParameter, class... CallParameter>
156 struct registerMdModuleNotification<CurrentCallParameter, CallParameter...>
157 {
158     // private:
159     //! The next type with rest of the arguments with the front parameter removed.
160     using next_type = typename registerMdModuleNotification<CallParameter...>::type;
161     //! The type of the MdModuleNotification
162     using type = MdModuleNotification<CurrentCallParameter, next_type>;
163 };
164
165 } // namespace gmx
166
167 #endif