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