ModuleRegistry.h
1 /*
2  * MoMEMta: a modular implementation of the Matrix Element Method
3  * Copyright (C) 2017 Universite catholique de Louvain (UCL), Belgium
4  *
5  * This program is free software: you can redistribute it and/or modify
6  * it under the terms of the GNU General Public License as published by
7  * the Free Software Foundation, either version 3 of the License, or
8  * (at your option) any later version.
9  *
10  * This program is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13  * GNU General Public License for more details.
14  *
15  * You should have received a copy of the GNU General Public License
16  * along with this program. If not, see <http://www.gnu.org/licenses/>.
17  */
18 
19 #pragma once
20 
21 #include <mutex>
22 
23 #include <momemta/ModuleFactory.h>
24 #include <momemta/ModuleDefBuilder.h>
25 
26 namespace momemta {
27 
29 public:
30  virtual ~ModuleRegistryInterface();
31 
33  virtual const ModuleRegistrationData& find(const std::string& module_name) const = 0;
34 };
35 
43 
44 public:
45  typedef std::function<ModuleRegistrationData()> RegisterOp;
46 
48  virtual ~ModuleRegistry() {}
49 
51  static ModuleRegistry& get();
52 
53  void registerModule(RegisterOp registration_op);
54 
60  void deregisterModule(const std::string& module_name);
61 
67  const ModuleRegistrationData& find(const std::string& module_name) const override;
68 
75  void exportList(bool ignore_internal, ModuleList& list) const;
76 
80  void processRegistrations();
81 
82 private:
83 
84  void callDeferred() const;
85  void registerModuleWithLock(RegisterOp registration_op) const;
86 
87  mutable std::mutex mutex_;
88 
93  mutable std::vector<RegisterOp> deferred_; // Guarded by mutex_
94  mutable std::unordered_map<std::string, const ModuleRegistrationData> registry_; // Guarded by mutex_
95 
96  mutable bool initialized_ = false; // Guarded by mutex_
97 
98  // Exceptions
99  class module_already_exists_error: public std::runtime_error {
100  using std::runtime_error::runtime_error;
101  };
102 
103  class module_not_found_error: public std::runtime_error {
104  using std::runtime_error::runtime_error;
105  };
106 };
107 
108 namespace registration {
109 
111  // To call ModuleRegistry::get()->register(...), used by the
112  // REGISTER_MODULE macro below.
113  // Note: These are implicitly converting constructors.
115 
117 
119  std::string name_;
120 };
121 
122 }
123 
124 #define REGISTER_MODULE(type) \
125  REGISTER_MODULE_UNIQ_HELPER(__LINE__, #type, type)
126 #define REGISTER_MODULE_NAME(name, type) \
127  REGISTER_MODULE_UNIQ_HELPER(__LINE__, name, type)
128 
129 #define REGISTER_MODULE_UNIQ_HELPER(ctr, name, type) REGISTER_MODULE_UNIQ(ctr, name, type)
130 #define REGISTER_MODULE_UNIQ(ctr, name, type) \
131  static const ::momemta::ModuleFactory::PMaker<type> register_module_factory##ctr(name); \
132  static const ::momemta::registration::ModuleDefBuilderReceiver register_module##ctr = \
133  ::momemta::registration::ModuleDefBuilder(name).Type<type>()
134 }
virtual const ModuleRegistrationData & find(const std::string &module_name) const =0
Returns the registration data for a given module. Throws an exception if no such module exists...
Definition: Graph.h:21