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 #include <functional>
23 
24 #include <momemta/ModuleFactory.h>
25 #include <momemta/ModuleDefBuilder.h>
26 
27 namespace momemta {
28 
30 public:
31  virtual ~ModuleRegistryInterface();
32 
34  virtual const ModuleRegistrationData& find(const std::string& module_name) const = 0;
35 };
36 
44 
45 public:
46  typedef std::function<ModuleRegistrationData()> RegisterOp;
47 
49  virtual ~ModuleRegistry() {}
50 
52  static ModuleRegistry& get();
53 
54  void registerModule(RegisterOp registration_op);
55 
61  void deregisterModule(const std::string& module_name);
62 
68  const ModuleRegistrationData& find(const std::string& module_name) const override;
69 
76  void exportList(bool ignore_internal, ModuleList& list) const;
77 
81  void processRegistrations();
82 
83 private:
84 
85  void callDeferred() const;
86  void registerModuleWithLock(RegisterOp registration_op) const;
87 
88  mutable std::mutex mutex_;
89 
94  mutable std::vector<RegisterOp> deferred_; // Guarded by mutex_
95  mutable std::unordered_map<std::string, const ModuleRegistrationData> registry_; // Guarded by mutex_
96 
97  mutable bool initialized_ = false; // Guarded by mutex_
98 
99  // Exceptions
100  class module_already_exists_error: public std::runtime_error {
101  using std::runtime_error::runtime_error;
102  };
103 
104  class module_not_found_error: public std::runtime_error {
105  using std::runtime_error::runtime_error;
106  };
107 };
108 
109 namespace registration {
110 
112  // To call ModuleRegistry::get()->register(...), used by the
113  // REGISTER_MODULE macro below.
114  // Note: These are implicitly converting constructors.
116 
118 
120  std::string name_;
121 };
122 
123 }
124 
125 #define REGISTER_MODULE(type) \
126  REGISTER_MODULE_UNIQ_HELPER(__LINE__, #type, type)
127 #define REGISTER_MODULE_NAME(name, type) \
128  REGISTER_MODULE_UNIQ_HELPER(__LINE__, name, type)
129 
130 #define REGISTER_MODULE_UNIQ_HELPER(ctr, name, type) REGISTER_MODULE_UNIQ(ctr, name, type)
131 #define REGISTER_MODULE_UNIQ(ctr, name, type) \
132  static const ::momemta::ModuleFactory::PMaker<type> register_module_factory##ctr(name); \
133  static const ::momemta::registration::ModuleDefBuilderReceiver register_module##ctr = \
134  ::momemta::registration::ModuleDefBuilder(name).Type<type>()
135 }
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