bec57d4a22fb4b103e45ec863f8973764e85a039
[alexxy/gromacs.git] / src / gromacs / utility / mdmodulenotification.h
1 /*
2  * This file is part of the GROMACS molecular simulation package.
3  *
4  * Copyright (c) 2019, 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_MDRUNUTILITY_MDMODULENOTIFICATION_H
45 #define GMX_MDRUNUTILITY_MDMODULENOTIFICATION_H
46
47 #include <functional>
48 #include <vector>
49
50 struct t_commrec;
51
52 namespace gmx
53 {
54
55 /*! \libinternal \brief
56  * Subscribe and trigger notification functions.
57  *
58  * Extends MdModuleNotificationBase with new notification function and routine
59  * to subscribe new listeners.
60  *
61  * To create a class of this type that provides callbacks, e.g., for events
62  * EventA, and EventB use registerMdModuleNotification<EventA, EventB>::type.
63  *
64  * \tparam CallParameter of the function to be notified
65  * \tparam MdModuleNotificationBase class to be extended with a notification
66  *                                  with CallParameter
67  *
68    \msc
69    wordwraparcs=true,
70    hscale="2";
71
72    runner [label="runner:\nMdrunner"],
73    CallParameter [label = "eventA:\nCallParameter"],
74    MOD [label = "mdModules_:\nMdModules"],
75    ModuleA [label="moduleA"],
76    ModuleB [label="moduleB"],
77    MdModuleNotification [label="notifier_:\nMdModuleNotification"];
78
79    MOD box MdModuleNotification [label = "mdModules_ owns notifier_ and moduleA/B"];
80    MOD =>> ModuleA [label="instantiates(notifier_)"];
81    ModuleA =>> MdModuleNotification [label="subscribe(otherfunc)"];
82    ModuleA =>> MOD;
83    MOD =>> ModuleB [label="instantiates(notifier_)"];
84    ModuleB =>> MdModuleNotification [label="subscribe(func)"];
85    ModuleB =>> MOD;
86    runner =>> CallParameter [label="instantiate"];
87    CallParameter =>> runner ;
88    runner =>> MOD [label="notify(eventA)"];
89    MOD =>> MdModuleNotification [label="notify(eventA)"];
90    MdModuleNotification =>> ModuleA [label="notify(eventA)"];
91    ModuleA -> ModuleA [label="func(eventA)"];
92    MdModuleNotification =>> ModuleB [label="notify(eventA)"];
93    ModuleB -> ModuleB [label="otherfunc(eventA)"];
94
95    \endmsc
96  *
97  * \note All added subscribers are required to out-live the MdModuleNotification
98  *
99  */
100 template <class CallParameter, class MdModuleNotificationBase>
101 class MdModuleNotification : public MdModuleNotificationBase
102 {
103     public:
104         //! Make base class notification trigger available to this class
105         using MdModuleNotificationBase::notify;
106         //! Make base class subscription available to this class
107         using MdModuleNotificationBase::subscribe;
108
109         /*! \brief Trigger the subscribed notifications.
110          * \param[in] callParameter of the function to be called back
111          */
112         void notify(CallParameter callParameter) const
113         {
114             for (auto &callBack : callBackFunctions_)
115             {
116                 callBack(callParameter);
117             }
118         }
119
120         /*! \brief
121          * Add callback function to be called when notification is triggered.
122          *
123          * Notifications are distinguished by their call signature.
124          *
125          * \param[in] callBackFunction to be called from this class
126          */
127         void subscribe(std::function<void(CallParameter)> callBackFunction)
128         {
129             callBackFunctions_.emplace_back(callBackFunction);
130         }
131
132     private:
133         std::vector < std::function < void(CallParameter)>> callBackFunctions_;
134 };
135
136 /*! \internal
137  * \brief Aide to avoid nested MdModuleNotification definition.
138  *
139  * Instead of
140  * MdModuleNotification<CallParameterA, MdModuleNotification<CallParameterB, etc ... >>
141  * this allows to write
142  * registerMdModuleNotification<CallParameterA, CallParameterB, ...>::type
143  *
144  * \tparam CallParameter all the event types to be registered
145  */
146 template <class ... CallParameter> struct registerMdModuleNotification;
147
148 /*! \internal \brief Template specialization to end parameter unpacking recursion.
149  */
150 template <>
151 struct registerMdModuleNotification<>
152 {
153     /*! \internal
154      * \brief Do nothing but be base class of MdModuleNotification.
155      *
156      * Required so that using MdModuleNotificationBase::notify and
157      * MdModuleNotificationBase::subscribe are valid in derived class.
158      */
159     class NoCallParameter
160     {
161         public:
162             //! Do nothing but provide MdModuleNotification::notify to derived class
163             void notify() {}
164             //! Do nothing but provide MdModuleNotification::subscribe to derived class
165             void subscribe() {}
166     };
167     /*! \brief Defines a type if no notifications are managed.
168      *
169      * This ensures that code works with MdModuleCallParameterManagement that
170      * does not manage any notifications.
171      */
172     using type = NoCallParameter;
173 };
174
175 /*! \libinternal
176  * \brief Template specialization to assemble MdModuleNotification.
177  *
178  * Assembly of MdModuleNotification is performed by recursively taking off the
179  * front of the CallParameter parameter pack and constructing the nested type
180  * definition of MdModuleNotification base classes.
181  *
182  * \tparam CurrentCallParameter front of the template parameter pack
183  * \tparam CallParameter rest of the event types
184  */
185 template <class CurrentCallParameter, class ... CallParameter>
186 struct registerMdModuleNotification<CurrentCallParameter, CallParameter...>
187 {
188     // private:
189     //! The next type with rest of the arguments with the front parameter removed.
190     using next_type = typename registerMdModuleNotification<CallParameter...>::type;
191     //! The type of the MdModuleNotification
192     using type = MdModuleNotification<CurrentCallParameter, next_type>;
193 };
194
195 class KeyValueTreeObject;
196 class KeyValueTreeObjectBuilder;
197 class LocalAtomSetManager;
198 class IndexGroupsAndNames;
199 struct MdModulesCheckpointReadingDataOnMaster;
200 struct MdModulesCheckpointReadingBroadcast;
201 struct MdModulesWriteCheckpointData;
202 struct PeriodicBoundaryConditionType
203 {
204     int pbcType;
205 };
206
207 struct MdModulesNotifier
208 {
209 //! Register callback function types for MdModule
210     registerMdModuleNotification<
211         const t_commrec &,
212         IndexGroupsAndNames,
213         KeyValueTreeObjectBuilder,
214         const KeyValueTreeObject &,
215         LocalAtomSetManager *,
216         MdModulesCheckpointReadingDataOnMaster,
217         MdModulesCheckpointReadingBroadcast,
218         MdModulesWriteCheckpointData,
219         PeriodicBoundaryConditionType>::type notifier_;
220 };
221
222 } // namespace gmx
223
224 #endif