GammaLib  2.0.0
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
GModelSpatialRegistry.cpp
Go to the documentation of this file.
1 /***************************************************************************
2  * GModelSpatialRegistry.cpp - Spatial 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 GModelSpatialRegistry.cpp
23  * @brief Spatial model registry class implemenation
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 "GModelSpatial.hpp"
34 #include "GException.hpp"
35 #include "GTools.hpp"
36 
37 /* __ Method name definitions ____________________________________________ */
38 #define G_ALLOC "GModelSpatialRegistry::alloc(GXmlElement&)"
39 #define G_NAME "GModelSpatialRegistry::name(int&)"
40 
41 /* __ Macros _____________________________________________________________ */
42 
43 /* __ Coding definitions _________________________________________________ */
44 
45 /* __ Debug definitions __________________________________________________ */
46 //#define G_DEBUG_REGISTRY //!< Dump registry
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 defined(G_DEBUG_REGISTRY)
65  std::cout << "GModelSpatialRegistry(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 defined(G_DEBUG_REGISTRY)
92  std::cout << "GModelSpatialRegistry(const GModelSpatial*): ";
93  std::cout << "add \"" << model->type() << "\" to registry." << std::endl;
94  #endif
95 
96  // Allocate new registry
97  const GModelSpatial** new_models = new const GModelSpatial*[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 defined(G_DEBUG_REGISTRY)
115  std::cout << "GModelSpatialRegistry(const GModelSpatial*): ";
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 spatial model that is found in XML element
199  *
200  * @param[in] xml XML element.
201  * @return Pointer to spatial model.
202  *
203  * @exception GException::invalid_value
204  * No appropriate spatial model found in XML element.
205  *
206  * Returns a pointer to a spatial 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 spatial model
213  GModelSpatial* 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 = "Spatial model of type \""+xml.attribute("type")+
226  "\" not found in registry. Possible spatial 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 spectral 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 GModelSpatialRegistry::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, "Spatial 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 (defaults to NORMAL).
271  * @return Registry content.
272  ***************************************************************************/
273 std::string GModelSpatialRegistry::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("=== GModelSpatialRegistry ===");
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  = Private methods =
312  = =
313  ==========================================================================*/
314 
315 /***********************************************************************//**
316  * @brief Initialise class members
317  ***************************************************************************/
319 {
320  // Return
321  return;
322 }
323 
324 
325 /***********************************************************************//**
326  * @brief Copy class members
327  *
328  * @param[in] registry Registry.
329  ***************************************************************************/
331 {
332  // Return
333  return;
334 }
335 
336 
337 /***********************************************************************//**
338  * @brief Delete class members
339  ***************************************************************************/
341 {
342  // Return
343  return;
344 }
#define G_NAME
virtual void read(const GXmlElement &xml)=0
GModelSpatialRegistry(void)
Void constructor.
XML element node class.
Definition: GXmlElement.hpp:48
Abstract spatial model base class interface definition.
Gammalib tools definition.
Interface definition for the spatial model registry class.
void free_members(void)
Delete class members.
Model parameter class interface definition.
void copy_members(const GModelSpatialRegistry &registry)
Copy class members.
const GXmlAttribute * attribute(const int &index) const
Return attribute.
void init_members(void)
Initialise class members.
int size(void) const
Return number of registered models.
Spatial model registry class definition.
GChatter
Definition: GTypemaps.hpp:33
GModelSpatial * alloc(const GXmlElement &xml) const
Allocate spatial model that is found in XML element.
#define G_ALLOC
std::string type(void) const
Return model type.
static GRegistryPointer< const GModelSpatial * > & models()
virtual ~GModelSpatialRegistry(void)
Destructor.
GModelSpatialRegistry & operator=(const GModelSpatialRegistry &registry)
Assignment operator.
std::string print(const GChatter &chatter=NORMAL) const
Print registry information.
Exception handler interface definition.
Abstract spatial model base class.
std::string parformat(const std::string &s, const int &indent=0)
Convert string in parameter format.
Definition: GTools.cpp:1143
std::string name(const int &index) const
Returns model name.
std::string str(const unsigned short int &value)
Convert unsigned short integer value into string.
Definition: GTools.cpp:489