GammaLib  2.0.0
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
GCTAModelRadialRegistry.cpp
Go to the documentation of this file.
1 /***************************************************************************
2  * GCTAModelRadialRegistry.cpp - CTA Radial 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 GCTAModelRadialRegistry.cpp
23  * @brief CTA radial model registry class implementation
24  * @author Juergen Knoedlseder
25  */
26 
27 /* __ Includes ___________________________________________________________ */
28 #ifdef HAVE_CONFIG_H
29 #include <config.h>
30 #endif
32 #include "GException.hpp"
33 #include "GTools.hpp"
34 
35 /* __ Method name definitions ____________________________________________ */
36 #define G_NAME "GCTAModelRadialRegistry::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 members
58  init_members();
59 
60  // Debug option: Show actual registry
61  #if G_DEBUG_REGISTRY
62  std::cout << "GCTAModelRadialRegistry(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  ***************************************************************************/
80 {
81  // Initialise members
82  init_members();
83 
84  // Debug option: Notify new registry
85  #if G_DEBUG_REGISTRY
86  std::cout << "GCTAModelRadialRegistry(const GCTAModelRadial*): ";
87  std::cout << "add \"" << model->type() << "\" to registry." << std::endl;
88  #endif
89 
90  // Allocate new registry
91  std::string* new_names = new std::string[size()+1];
92  const GCTAModelRadial** new_models = new const GCTAModelRadial*[size()+1];
93 
94  // Save old registry
95  for (int i = 0; i < size(); ++i) {
96  new_names[i] = names()[i];
97  new_models[i] = models()[i];
98  }
99 
100  // Add new model to registry
101  new_names[size()] = model->type();
102  new_models[size()] = model;
103 
104  // Set pointers on new registry
105  names().assign(new_names);
106  models().assign(new_models);
107 
108  // Increment number of models in registry
109  number()++;
110 
111  // Debug option: Show actual registry
112  #if G_DEBUG_REGISTRY
113  std::cout << "GCTAModelRadialRegistry(const GCTAModelRadial*): ";
114  for (int i = 0; i < size(); ++i) {
115  std::cout << "\"" << names()[i] << "\" ";
116  }
117  std::cout << std::endl;
118  #endif
119 
120  // Return
121  return;
122 }
123 
124 
125 /***********************************************************************//**
126  * @brief Copy constructor
127  *
128  * @param[in] registry Registry.
129  ***************************************************************************/
131 {
132  // Initialise members
133  init_members();
134 
135  // Copy members
136  copy_members(registry);
137 
138  // Return
139  return;
140 }
141 
142 
143 /***********************************************************************//**
144  * @brief Destructor
145  ***************************************************************************/
147 {
148  // Free members
149  free_members();
150 
151  // Return
152  return;
153 }
154 
155 
156 /*==========================================================================
157  = =
158  = Operators =
159  = =
160  ==========================================================================*/
161 
162 /***********************************************************************//**
163  * @brief Assignment operator
164  *
165  * @param[in] registry Registry.
166  * @return Reference to registry.
167  ***************************************************************************/
169 {
170  // Execute only if object is not identical
171  if (this != &registry) {
172 
173  // Free members
174  free_members();
175 
176  // Initialise members
177  init_members();
178 
179  // Copy members
180  copy_members(registry);
181 
182  } // endif: object was not identical
183 
184  // Return
185  return *this;
186 }
187 
188 
189 /*==========================================================================
190  = =
191  = Public methods =
192  = =
193  ==========================================================================*/
194 
195 /***********************************************************************//**
196  * @brief Allocate radial model of given name
197  *
198  * @param[in] name Radial model name.
199  *
200  * Returns a pointer to a radial model instance of the specified name.
201  * If the name has not been found in the registry, a NULL pointer is
202  * returned.
203  ***************************************************************************/
204 GCTAModelRadial* GCTAModelRadialRegistry::alloc(const std::string& name) const
205 {
206  // Initialise radial model
207  GCTAModelRadial* model = NULL;
208 
209  // Search for model in registry
210  for (int i = 0; i < size(); ++i) {
211  if (names()[i] == name) {
212  model = models()[i]->clone();
213  break;
214  }
215  }
216 
217  // Return radial model
218  return model;
219 }
220 
221 
222 /***********************************************************************//**
223  * @brief Returns model name
224  *
225  * @param[in] index Model index [0,...,size()-1].
226  * @return Model name.
227  *
228  * @exception GException::out_of_range
229  * Model index is out of range.
230  ***************************************************************************/
231 std::string GCTAModelRadialRegistry::name(const int& index) const
232 {
233  // Compile option: raise exception if index is out of range
234  #if defined(G_RANGE_CHECK)
235  if (index < 0 || index >= size()) {
236  throw GException::out_of_range(G_NAME, "Model index", index, size());
237  }
238  #endif
239 
240  // Return name
241  return (names()[index]);
242 }
243 
244 
245 /***********************************************************************//**
246  * @brief Print registry information
247  *
248  * @param[in] chatter Chattiness (defaults to NORMAL).
249  * @return String containing registry information.
250  ***************************************************************************/
251 std::string GCTAModelRadialRegistry::print(const GChatter& chatter) const
252 {
253  // Initialise result string
254  std::string result;
255 
256  // Continue only if chatter is not silent
257  if (chatter != SILENT) {
258 
259  // Append header
260  result.append("=== GCTAModelRadialRegistry ===");
261 
262  // Append information
263  result.append("\n"+gammalib::parformat("Number of models"));
264  result.append(gammalib::str(size()));
265 
266  // NORMAL: Append models
267  if (chatter >= NORMAL) {
268  for (int i = 0; i < size(); ++i) {
269  result.append("\n"+gammalib::parformat(names()[i]));
270  result.append(models()[i]->type());
271  }
272  }
273 
274  } // endif: chatter was not silent
275 
276  // Return result
277  return result;
278 }
279 
280 /*==========================================================================
281  = =
282  = Private methods =
283  = =
284  ==========================================================================*/
285 
286 /***********************************************************************//**
287  * @brief Initialise class members
288  ***************************************************************************/
290 {
291  // Return
292  return;
293 }
294 
295 
296 /***********************************************************************//**
297  * @brief Copy class members
298  *
299  * @param[in] registry Registry.
300  ***************************************************************************/
302 {
303  // Return
304  return;
305 }
306 
307 
308 /***********************************************************************//**
309  * @brief Delete class members
310  ***************************************************************************/
312 {
313  // Return
314  return;
315 }
static GRegistryPointer< const GCTAModelRadial * > & models()
virtual ~GCTAModelRadialRegistry(void)
Destructor.
Gammalib tools definition.
CTA radial model registry class definition.
void init_members(void)
Initialise class members.
#define G_NAME
GCTAModelRadialRegistry(void)
Void constructor.
std::string name(const int &index) const
Returns model name.
GChatter
Definition: GTypemaps.hpp:33
int size(void) const
Return number of registered models.
GCTAModelRadial * alloc(const std::string &name) const
Allocate radial model of given name.
GCTAModelRadialRegistry & operator=(const GCTAModelRadialRegistry &registry)
Assignment operator.
Interface definition for the CTA radial model registry class.
Exception handler interface definition.
Abstract radial acceptance model class.
void free_members(void)
Delete class members.
std::string parformat(const std::string &s, const int &indent=0)
Convert string in parameter format.
Definition: GTools.cpp:1143
virtual std::string type(void) const =0
std::string print(const GChatter &chatter=NORMAL) const
Print registry information.
void copy_members(const GCTAModelRadialRegistry &registry)
Copy class members.
static GRegistryPointer< std::string > & names()
std::string str(const unsigned short int &value)
Convert unsigned short integer value into string.
Definition: GTools.cpp:489