Improve MessageStringCollector
[alexxy/gromacs.git] / src / gromacs / utility / mdmodulesnotifier.h
1 /*
2  * This file is part of the GROMACS molecular simulation package.
3  *
4  * Copyright (c) 2020,2021, 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::MDModulesNotifier and builder.
38  *
39  * \author Christian Blau <blau@kth.se>
40  * \inlibraryapi
41  * \ingroup module_utility
42  */
43
44 #ifndef GMX_UTILITY_MDMODULESNOTIFIER_H
45 #define GMX_UTILITY_MDMODULESNOTIFIER_H
46
47 #include <functional>
48 #include <vector>
49
50 namespace gmx
51 {
52
53 /*! \libinternal
54  * \brief Organizes notifications about an event of interest to modules.
55  *
56  * An object of this type permits modules to subscribe to the
57  * corresponding event. The template types of this type encode what
58  * information is available when the event occurs. Modules \c
59  * subscribe() by providing a callback function that accepts a single
60  * parameter of such an event type. The code that handles that event
61  * has the responsibilty to call \c notify() afterwards. The
62  * subscribed modules then receive the callback with the requested
63  * event type as an argument.
64  *
65  * See gmx::MDModulesNotifiers for sequence diagrams for an example.
66  *
67  * This suits scenarios where several objects are built (or re-built)
68  * and one or more modules need to know when one or more of such
69  * objects are available (or updated), so they can adapt their
70  * internal state accordingly. Examples include responding to loading
71  * input data, or to changes related to a recurring process like
72  * checkpointing or partitioning. The coupling between these modules
73  * is now expressed indirectly. This improves the modularity and
74  * testability of those modules.
75  *
76  * The implementation provides the necessary flexibility to be
77  * parameterized with multiple event types and provide \c callback()
78  * and \b notify() methods corresponding to each related event. This
79  * is done by inheriting from a series of base classes, each of which
80  * handles a single type of event. BuildMDModulesNotifier implements
81  * the details. To create a class of this type that provides two
82  * events with callbacks that receive respectively types TypeA and
83  * TypeB, use BuildMDModulesNotifier<TypeA, TypeB>::type.
84  *
85  * \tparam CallParameter of the function to be notified
86  * \tparam MDModulesNotifierBase class to be extended with a notification
87  *                                  with CallParameter
88  *
89  * \note All added subscribers are required to out-live the MDModulesNotifier
90  *
91  */
92 template<class CallParameter, class MDModulesNotifierBase>
93 class MDModulesNotifier : public MDModulesNotifierBase
94 {
95 public:
96     //! Make base class notification trigger available to this class
97     using MDModulesNotifierBase::notify;
98     //! Make base class subscription available to this class
99     using MDModulesNotifierBase::subscribe;
100
101     /*! \brief Notifies subscribers of the event described by \c
102      * callbackParameter.
103      *
104      * \param[in] callParameter of the function to be called back
105      */
106     void notify(CallParameter callParameter) const
107     {
108         for (auto& callBack : callBackFunctions_)
109         {
110             callBack(callParameter);
111         }
112     }
113
114     /*! \brief
115      * Add callback function to be called when \c notify() is called
116      *
117      * \param[in] callBackFunction to be called
118      */
119     void subscribe(std::function<void(CallParameter)> callBackFunction)
120     {
121         callBackFunctions_.emplace_back(callBackFunction);
122     }
123
124 private:
125     std::vector<std::function<void(CallParameter)>> callBackFunctions_;
126 };
127
128 /*! \internal
129  * \brief Aide to avoid nested MDModulesNotifier definition.
130  *
131  * Instead of
132  * MDModulesNotifier<CallParameterA, MDModulesNotifier<CallParameterB, etc ... >>
133  * this allows to write
134  * BuildMDModulesNotifier<CallParameterA, CallParameterB, ...>::type
135  *
136  * \tparam CallParameter all the callback types to be registered
137  */
138 template<class... CallParameter>
139 struct BuildMDModulesNotifier;
140
141 /*! \internal \brief Template specialization to end parameter unpacking recursion.
142  */
143 template<>
144 struct BuildMDModulesNotifier<>
145 {
146     /*! \internal
147      * \brief Do nothing but be base class of MDModulesNotifier.
148      *
149      * Required so that using MDModulesNotifierBase::notify and
150      * MDModulesNotifierBase::subscribe are valid in derived class.
151      */
152     class NoCallParameter
153     {
154     public:
155         //! Do nothing but provide MDModulesNotifier::notify to derived class
156         void notify() {}
157         //! Do nothing but provide MDModulesNotifier::subscribe to derived class
158         void subscribe() {}
159     };
160     /*! \brief Defines a type if no notifications are managed.
161      *
162      * This ensures that code works with MDModuleCallParameterManagement that
163      * does not manage any notifications.
164      */
165     using type = NoCallParameter;
166 };
167
168 /*! \libinternal
169  * \brief Template specialization to assemble MDModulesNotifier.
170  *
171  * Assembly of MDModulesNotifier is performed by recursively taking off the
172  * front of the CallParameter parameter pack and constructing the nested type
173  * definition of MDModulesNotifier base classes.
174  *
175  * \tparam CurrentCallParameter front of the template parameter pack
176  * \tparam CallParameter rest of the callback types
177  */
178 template<class CurrentCallParameter, class... CallParameter>
179 struct BuildMDModulesNotifier<CurrentCallParameter, CallParameter...>
180 {
181     // private:
182     //! The next type with rest of the arguments with the front parameter removed.
183     using next_type = typename BuildMDModulesNotifier<CallParameter...>::type;
184     //! The type of the MDModulesNotifier
185     using type = MDModulesNotifier<CurrentCallParameter, next_type>;
186 };
187
188 } // namespace gmx
189
190 #endif