2 * This file is part of the GROMACS molecular simulation package.
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.
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.
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.
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.
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.
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.
35 /*! \libinternal \file
37 * Declares gmx::MDModulesNotifier.
39 * \author Christian Blau <blau@kth.se>
41 * \ingroup module_utility
44 #ifndef GMX_UTILITY_MDMODULENOTIFICATION_IMPL_H
45 #define GMX_UTILITY_MDMODULENOTIFICATION_IMPL_H
54 * \brief Organizes notifications about an event of interest to modules.
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.
65 * See gmx::MDModulesNotifiers for sequence diagrams for an example.
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.
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.
85 * \tparam CallParameter of the function to be notified
86 * \tparam MDModulesNotifierBase class to be extended with a notification
89 * \note All added subscribers are required to out-live the MDModulesNotifier
92 template<class CallParameter, class MDModulesNotifierBase>
93 class MDModulesNotifier : public MDModulesNotifierBase
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;
101 /*! \brief Notifies subscribers of the event described by \c
104 * \param[in] callParameter of the function to be called back
106 void notify(CallParameter callParameter) const
108 for (auto& callBack : callBackFunctions_)
110 callBack(callParameter);
115 * Add callback function to be called when \c notify() is called
117 * \param[in] callBackFunction to be called
119 void subscribe(std::function<void(CallParameter)> callBackFunction)
121 callBackFunctions_.emplace_back(callBackFunction);
125 std::vector<std::function<void(CallParameter)>> callBackFunctions_;
129 * \brief Aide to avoid nested MDModulesNotifier definition.
132 * MDModulesNotifier<CallParameterA, MDModulesNotifier<CallParameterB, etc ... >>
133 * this allows to write
134 * BuildMDModulesNotifier<CallParameterA, CallParameterB, ...>::type
136 * \tparam CallParameter all the callback types to be registered
138 template<class... CallParameter>
139 struct BuildMDModulesNotifier;
141 /*! \internal \brief Template specialization to end parameter unpacking recursion.
144 struct BuildMDModulesNotifier<>
147 * \brief Do nothing but be base class of MDModulesNotifier.
149 * Required so that using MDModulesNotifierBase::notify and
150 * MDModulesNotifierBase::subscribe are valid in derived class.
152 class NoCallParameter
155 //! Do nothing but provide MDModulesNotifier::notify to derived class
157 //! Do nothing but provide MDModulesNotifier::subscribe to derived class
160 /*! \brief Defines a type if no notifications are managed.
162 * This ensures that code works with MDModuleCallParameterManagement that
163 * does not manage any notifications.
165 using type = NoCallParameter;
169 * \brief Template specialization to assemble MDModulesNotifier.
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.
175 * \tparam CurrentCallParameter front of the template parameter pack
176 * \tparam CallParameter rest of the callback types
178 template<class CurrentCallParameter, class... CallParameter>
179 struct BuildMDModulesNotifier<CurrentCallParameter, CallParameter...>
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>;