GammaLib 2.0.0
Loading...
Searching...
No Matches
GModelRegistry.cpp
Go to the documentation of this file.
1/***************************************************************************
2 * GModelRegistry.cpp - Model registry class *
3 * ----------------------------------------------------------------------- *
4 * copyright (C) 2011-2021 by Juergen Knoedlseder *
5 * ----------------------------------------------------------------------- *
6 * *
7 * This program is free software: you can redistribute it and/or modify *
8 * it under the terms of the GNU General Public License as published by *
9 * the Free Software Foundation, either version 3 of the License, or *
10 * (at your option) any later version. *
11 * *
12 * This program is distributed in the hope that it will be useful, *
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of *
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
15 * GNU General Public License for more details. *
16 * *
17 * You should have received a copy of the GNU General Public License *
18 * along with this program. If not, see <http://www.gnu.org/licenses/>. *
19 * *
20 ***************************************************************************/
21/**
22 * @file GModelRegistry.cpp
23 * @brief Model registry class implementation
24 * @author Juergen Knoedlseder
25 */
26
27/* __ Includes ___________________________________________________________ */
28#ifdef HAVE_CONFIG_H
29#include <config.h>
30#endif
31#include "GModelRegistry.hpp"
32#include "GException.hpp"
33#include "GTools.hpp"
34
35/* __ Method name definitions ____________________________________________ */
36#define G_NAME "GModelRegistry::name(int&)"
37
38/* __ Macros _____________________________________________________________ */
39
40/* __ Coding definitions _________________________________________________ */
41
42/* __ Debug definitions __________________________________________________ */
43#define G_DEBUG_REGISTRY 0
44
45
46/*==========================================================================
47 = =
48 = Constructors/destructors =
49 = =
50 ==========================================================================*/
51
52/***********************************************************************//**
53 * @brief Void constructor
54 ***************************************************************************/
56{
57 // Initialise private members for clean destruction
59
60 // Debug option: Show actual registry
61 #if G_DEBUG_REGISTRY
62 std::cout << "GModelRegistry(void): ";
63 for (int i = 0; i < size(); ++i) {
64 std::cout << "\"" << names()[i] << "\" ";
65 }
66 std::cout << std::endl;
67 #endif
68
69 // Return
70 return;
71}
72
73
74/***********************************************************************//**
75 * @brief Model constructor
76 *
77 * @param[in] model Model.
78 *
79 * Construct registry by adding a model to the registry. This is the standard
80 * constructor that is used to register a new model to GammaLib.
81 ***************************************************************************/
83{
84 // Initialise private members for clean destruction
86
87 // Debug option: Notify new registry
88 #if G_DEBUG_REGISTRY
89 std::cout << "GModelRegistry(const GModel*): ";
90 std::cout << "add \"" << model->type() << "\" to registry." << std::endl;
91 #endif
92
93 // Allocate new registry
94 std::string* new_names = new std::string[size()+1];
95 const GModel** new_models = new const GModel*[size()+1];
96
97 // Save old registry
98 for (int i = 0; i < size(); ++i) {
99 new_names[i] = names()[i];
100 new_models[i] = models()[i];
101 }
102
103 // Add new model to registry
104 new_names[size()] = model->type();
105 new_models[size()] = model;
106
107 // Set pointers on new registry
108 names().assign(new_names);
109 models().assign(new_models);
110
111 // Increment number of models in registry
112 number()++;
113
114 // Debug option: Show actual registry
115 #if G_DEBUG_REGISTRY
116 std::cout << "GModelRegistry(const GModel*): ";
117 for (int i = 0; i < size(); ++i) {
118 std::cout << "\"" << names()[i] << "\" ";
119 }
120 std::cout << std::endl;
121 #endif
122
123 // Return
124 return;
125}
126
127
128/***********************************************************************//**
129 * @brief Copy constructor
130 *
131 * @param[in] registry Registry.
132 ***************************************************************************/
134{
135 // Initialise private members
136 init_members();
137
138 // Copy members
139 copy_members(registry);
140
141 // Return
142 return;
143}
144
145
146/***********************************************************************//**
147 * @brief Destructor
148 ***************************************************************************/
150{
151 // Free members
152 free_members();
153
154 // Return
155 return;
156}
157
158
159/*==========================================================================
160 = =
161 = Operators =
162 = =
163 ==========================================================================*/
164
165/***********************************************************************//**
166 * @brief Assignment operator
167 *
168 * @param[in] registry Registry.
169 * @return Reference to registry.
170 ***************************************************************************/
172{
173 // Execute only if object is not identical
174 if (this != &registry) {
175
176 // Free members
177 free_members();
178
179 // Initialise private members
180 init_members();
181
182 // Copy members
183 copy_members(registry);
184
185 } // endif: object was not identical
186
187 // Return
188 return *this;
189}
190
191
192/*==========================================================================
193 = =
194 = Public methods =
195 = =
196 ==========================================================================*/
197
198/***********************************************************************//**
199 * @brief Allocate model of given name
200 *
201 * @param[in] name Model name.
202 * @return Pointer to model (NULL if name is not registered).
203 *
204 * Returns a pointer to a void model instance of the specified name. If the
205 * name has not been found in the registry, a NULL pointer is returned.
206 ***************************************************************************/
207GModel* GModelRegistry::alloc(const std::string& name) const
208{
209 // Initialise model
210 GModel* model = NULL;
211
212 // Search for model in registry
213 for (int i = 0; i < size(); ++i) {
214 if (names()[i] == name) {
215 model = models()[i]->clone();
216 break;
217 }
218 }
219
220 // Return model
221 return model;
222}
223
224
225/***********************************************************************//**
226 * @brief Returns model name
227 *
228 * @param[in] index Model index [0,...,size()[.
229 * @return Model name.
230 *
231 * @exception GException::out_of_range
232 * Model index is out of range.
233 ***************************************************************************/
234std::string GModelRegistry::name(const int& index) const
235{
236 // Compile option: raise exception if index is out of range
237 #if defined(G_RANGE_CHECK)
238 if (index < 0 || index >= size()) {
239 throw GException::out_of_range(G_NAME, "Model index", index, size());
240 }
241 #endif
242
243 // Return name
244 return (names()[index]);
245}
246
247
248/***********************************************************************//**
249 * @brief Print registry information
250 *
251 * @param[in] chatter Chattiness.
252 * @return Registry content.
253 ***************************************************************************/
254std::string GModelRegistry::print(const GChatter& chatter) const
255{
256 // Initialise result string
257 std::string result;
258
259 // Continue only if chatter is not silent
260 if (chatter != SILENT) {
261
262 // Append header
263 result.append("=== GModelRegistry ===");
264
265 // Append information
266 result.append("\n"+gammalib::parformat("Number of models"));
267 result.append(gammalib::str(size()));
268
269 // NORMAL: Append models
270 if (chatter >= NORMAL) {
271 for (int i = 0; i < size(); ++i) {
272 result.append("\n"+gammalib::parformat(names()[i]));
273 result.append(models()[i]->type());
274 }
275 }
276
277 } // endif: chatter was not silent
278
279 // Return result
280 return result;
281}
282
283/*==========================================================================
284 = =
285 = Private methods =
286 = =
287 ==========================================================================*/
288
289/***********************************************************************//**
290 * @brief Initialise class members
291 ***************************************************************************/
293{
294 // Return
295 return;
296}
297
298
299/***********************************************************************//**
300 * @brief Copy class members
301 *
302 * @param[in] registry Registry.
303 ***************************************************************************/
305{
306 // Return
307 return;
308}
309
310
311/***********************************************************************//**
312 * @brief Delete class members
313 ***************************************************************************/
315{
316 // Return
317 return;
318}
Exception handler interface definition.
#define G_NAME
Model registry class definition.
Gammalib tools definition.
GChatter
Definition GTypemaps.hpp:33
@ NORMAL
Definition GTypemaps.hpp:36
@ SILENT
Definition GTypemaps.hpp:34
Interface definition for the model registry class.
std::string print(const GChatter &chatter=NORMAL) const
Print registry information.
GModelRegistry & operator=(const GModelRegistry &registry)
Assignment operator.
void init_members(void)
Initialise class members.
GModelRegistry(void)
Void constructor.
virtual ~GModelRegistry(void)
Destructor.
void free_members(void)
Delete class members.
static int & number()
static GRegistryPointer< const GModel * > & models()
static GRegistryPointer< std::string > & names()
GModel * alloc(const std::string &name) const
Allocate model of given name.
int size(void) const
Return number of registered models.
void copy_members(const GModelRegistry &registry)
Copy class members.
std::string name(const int &index) const
Returns model name.
Abstract model class.
Definition GModel.hpp:100
virtual std::string type(void) const =0
virtual GModel * clone(void) const =0
Clones object.
std::string parformat(const std::string &s, const int &indent=0)
Convert string in parameter format.
Definition GTools.cpp:1143
std::string str(const unsigned short int &value)
Convert unsigned short integer value into string.
Definition GTools.cpp:489