GammaLib  2.1.0.dev
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
GModelTemporalRegistry.cpp
Go to the documentation of this file.
1 /***************************************************************************
2  * GModelTemporalRegistry.cpp - Temporal model registry class *
3  * ----------------------------------------------------------------------- *
4  * copyright (C) 2011-2016 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 GModelTemporalRegistry.cpp
23  * @brief Temporal 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 "GModelPar.hpp"
32 #include "GModelTemporal.hpp"
34 #include "GException.hpp"
35 #include "GTools.hpp"
36 
37 /* __ Method name definitions ____________________________________________ */
38 #define G_ALLOC "GModelTemporalRegistry::alloc(GXmlElement&)"
39 #define G_NAME "GModelTemporalRegistry::name(int&)"
40 
41 /* __ Macros _____________________________________________________________ */
42 
43 /* __ Coding definitions _________________________________________________ */
44 
45 /* __ Debug definitions __________________________________________________ */
46 #define G_DEBUG_REGISTRY 0
47 
48 
49 /*==========================================================================
50  = =
51  = Constructors/destructors =
52  = =
53  ==========================================================================*/
54 
55 /***********************************************************************//**
56  * @brief Void constructor
57  ***************************************************************************/
59 {
60  // Initialise private members for clean destruction
61  init_members();
62 
63  // Debug option: Show actual registry
64  #if G_DEBUG_REGISTRY
65  std::cout << "GModelTemporalRegistry(void): ";
66  for (int i = 0; i < size(); ++i) {
67  std::cout << "\"" << models()[i]->type() << "\" ";
68  }
69  std::cout << std::endl;
70  #endif
71 
72  // Return
73  return;
74 }
75 
76 
77 /***********************************************************************//**
78  * @brief Model constructor
79  *
80  * @param[in] model Model.
81  *
82  * Construct registry by adding a model to the registry. This is the standard
83  * constructor that is used to register a new model to GammaLib.
84  ***************************************************************************/
86 {
87  // Initialise private members for clean destruction
88  init_members();
89 
90  // Debug option: Notify new registry
91  #if G_DEBUG_REGISTRY
92  std::cout << "GModelTemporalRegistry(const GModelTemporal*): ";
93  std::cout << "add \"" << model->type() << "\" to registry." << std::endl;
94  #endif
95 
96  // Allocate new registry
97  const GModelTemporal** new_models = new const GModelTemporal*[size()+1];
98 
99  // Save old registry
100  for (int i = 0; i < size(); ++i) {
101  new_models[i] = models()[i];
102  }
103 
104  // Add new model to registry
105  new_models[size()] = model;
106 
107  // Set pointers on new registry
108  models().assign(new_models);
109 
110  // Increment number of models in registry
111  number()++;
112 
113  // Debug option: Show actual registry
114  #if G_DEBUG_REGISTRY
115  std::cout << "GModelTemporalRegistry(const GModelTemporal*): ";
116  for (int i = 0; i < size(); ++i) {
117  std::cout << "\"" << models()[i]->type() << "\" ";
118  }
119  std::cout << std::endl;
120  #endif
121 
122  // Return
123  return;
124 }
125 
126 
127 /***********************************************************************//**
128  * @brief Copy constructor
129  *
130  * @param[in] registry Registry.
131  ***************************************************************************/
133 {
134  // Initialise private members
135  init_members();
136 
137  // Copy members
138  copy_members(registry);
139 
140  // Return
141  return;
142 }
143 
144 
145 /***********************************************************************//**
146  * @brief Destructor
147  ***************************************************************************/
149 {
150  // Free members
151  free_members();
152 
153  // Return
154  return;
155 }
156 
157 
158 /*==========================================================================
159  = =
160  = Operators =
161  = =
162  ==========================================================================*/
163 
164 /***********************************************************************//**
165  * @brief Assignment operator
166  *
167  * @param[in] registry Registry.
168  * @return Reference to registry.
169  ***************************************************************************/
171 {
172  // Execute only if object is not identical
173  if (this != &registry) {
174 
175  // Free members
176  free_members();
177 
178  // Initialise private members
179  init_members();
180 
181  // Copy members
182  copy_members(registry);
183 
184  } // endif: object was not identical
185 
186  // Return
187  return *this;
188 }
189 
190 
191 /*==========================================================================
192  = =
193  = Public methods =
194  = =
195  ==========================================================================*/
196 
197 /***********************************************************************//**
198  * @brief Allocate temporal model that is found in XML element
199  *
200  * @param[in] xml XML element.
201  * @return Pointer to temporal model.
202  *
203  * @exception GException::invalid_value
204  * No appropriate temporal model found in XML element.
205  *
206  * Returns a pointer to a temporal model instance that corresponds to the
207  * type found in an XML element. If no appropriate model is found the method
208  * will throw an exception.
209  ***************************************************************************/
211 {
212  // Initialise temporal model
213  GModelTemporal* model = NULL;
214 
215  // Search for model type in registry
216  for (int i = 0; i < size(); ++i) {
217  if (models()[i]->type() == xml.attribute("type")) {
218  model = models()[i]->clone();
219  break;
220  }
221  }
222 
223  // If no model has been found then throw an exception
224  if (model == NULL) {
225  std::string msg = "Temporal model of type \""+xml.attribute("type")+
226  "\" not found in registry. Possible temporal model "
227  "types are:";
228  for (int i = 0; i < size(); ++i) {
229  msg += " \""+models()[i]->type()+"\"";
230  }
231  msg += ".";
233  }
234 
235  // Read model from XML element
236  model->read(xml);
237 
238  // Return temporal model
239  return model;
240 }
241 
242 
243 /***********************************************************************//**
244  * @brief Returns model name
245  *
246  * @param[in] index Model index [0,...,size()-1].
247  * @return Model name.
248  *
249  * @exception GException::out_of_range
250  * Model index is out of range.
251  ***************************************************************************/
252 std::string GModelTemporalRegistry::name(const int& index) const
253 {
254  // Compile option: raise exception if index is out of range
255  #if defined(G_RANGE_CHECK)
256  if (index < 0 || index >= size()) {
257  throw GException::out_of_range(G_NAME, "Temporal model index", index,
258  size());
259  }
260  #endif
261 
262  // Return name
263  return (models()[index]->type());
264 }
265 
266 
267 /***********************************************************************//**
268  * @brief Print registry information
269  *
270  * @param[in] chatter Chattiness.
271  * @return Registry content.
272  ***************************************************************************/
273 std::string GModelTemporalRegistry::print(const GChatter& chatter) const
274 {
275  // Initialise result string
276  std::string result;
277 
278  // Continue only if chatter is not silent
279  if (chatter != SILENT) {
280 
281  // Append header
282  result.append("=== GModelTemporalRegistry ===");
283 
284  // Append information
285  result.append("\n"+gammalib::parformat("Number of models"));
286  result.append(gammalib::str(size()));
287 
288  // NORMAL: Append models
289  if (chatter >= NORMAL) {
290  for (int i = 0; i < size(); ++i) {
291  result.append("\n"+gammalib::parformat(models()[i]->type()));
292  for (int k = 0; k < models()[i]->size(); ++k) {
293  if (k > 0) {
294  result.append(", ");
295  }
296  result.append("\"");
297  result.append((*(models()[i]))[k].name());
298  result.append("\"");
299  }
300  }
301  }
302 
303  } // endif: chatter was not silent
304 
305  // Return result
306  return result;
307 }
308 
309 
310 /*==========================================================================
311  = =
312  = Private methods =
313  = =
314  ==========================================================================*/
315 
316 /***********************************************************************//**
317  * @brief Initialise class members
318  ***************************************************************************/
320 {
321  // Return
322  return;
323 }
324 
325 
326 /***********************************************************************//**
327  * @brief Copy class members
328  *
329  * @param[in] registry Registry.
330  ***************************************************************************/
332 {
333  // Return
334  return;
335 }
336 
337 
338 /***********************************************************************//**
339  * @brief Delete class members
340  ***************************************************************************/
342 {
343  // Return
344  return;
345 }
GModelTemporalRegistry(void)
Void constructor.
Abstract temporal model base class.
XML element node class.
Definition: GXmlElement.hpp:48
std::string name(const int &index) const
Returns model name.
Gammalib tools definition.
std::string print(const GChatter &chatter=NORMAL) const
Print registry information.
void init_members(void)
Initialise class members.
Model parameter class interface definition.
Abstract temporal model base class interface definition.
const GXmlAttribute * attribute(const int &index) const
Return attribute.
void free_members(void)
Delete class members.
GModelTemporalRegistry & operator=(const GModelTemporalRegistry &registry)
Assignment operator.
GModelTemporal * alloc(const GXmlElement &xml) const
Allocate temporal model that is found in XML element.
int size(void) const
Return number of registered models.
GChatter
Definition: GTypemaps.hpp:33
#define G_NAME
virtual void read(const GXmlElement &xml)=0
void copy_members(const GModelTemporalRegistry &registry)
Copy class members.
Interface definition for the temporal model registry class.
virtual std::string type(void) const =0
Temporal model registry class definition.
#define G_ALLOC
Exception handler interface definition.
std::string parformat(const std::string &s, const int &indent=0)
Convert string in parameter format.
Definition: GTools.cpp:1143
virtual ~GModelTemporalRegistry(void)
Destructor.
static GRegistryPointer< const GModelTemporal * > & models()
std::string str(const unsigned short int &value)
Convert unsigned short integer value into string.
Definition: GTools.cpp:489