GammaLib  1.7.0.dev
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
GObservationRegistry.cpp
Go to the documentation of this file.
1 /***************************************************************************
2  * GObservationRegistry.cpp - Observation registry class *
3  * ----------------------------------------------------------------------- *
4  * copyright (C) 2011-2013 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 GObservationRegistry.cpp
23  * @brief Observation registry class definition
24  * @author Juergen Knoedlseder
25  */
26 
27 /* __ Includes ___________________________________________________________ */
28 #ifdef HAVE_CONFIG_H
29 #include <config.h>
30 #endif
31 #include "GObservationRegistry.hpp"
32 #include "GException.hpp"
33 #include "GTools.hpp"
34 
35 /* __ Method name definitions ____________________________________________ */
36 #define G_NAME "GObservationRegistry::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
58  init_members();
59 
60  // Debug option: Show actual registry
61  #if G_DEBUG_REGISTRY
62  std::cout << "GObservationRegistry(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 Observation constructor
76  *
77  * @param[in] obs Observation.
78  ***************************************************************************/
80 {
81  // Initialise private members for clean destruction
82  init_members();
83 
84  // Debug option: Notify new registry
85  #if G_DEBUG_REGISTRY
86  std::cout << "GObservationRegistry(const GObservation*): ";
87  std::cout << "add \"" << obs->instrument() << "\" to registry." << std::endl;
88  #endif
89 
90  // Allocate new registry
91  std::string* new_names = new std::string[size()+1];
92  const GObservation** new_obs = new const GObservation*[size()+1];
93 
94  // Save old registry
95  for (int i = 0; i < size(); ++i) {
96  new_names[i] = names()[i];
97  new_obs[i] = this->obs()[i];
98  }
99 
100  // Add new observation to registry
101  new_names[size()] = obs->instrument();
102  new_obs[size()] = obs;
103 
104  // Set pointers on new registry
105  names().assign(new_names);
106  this->obs().assign(new_obs);
107 
108  // Increment number of observations in registry
109  number()++;
110 
111  // Debug option: Show actual registry
112  #if G_DEBUG_REGISTRY
113  std::cout << "GObservationRegistry(const GObservation*): ";
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 private 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 private 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 observation of given name
197  *
198  * @param[in] name Instrument name.
199  * @return Pointer to observation (NULL if name is not registered).
200  *
201  * Returns a pointer to an observation instance for a specific name.
202  * If the instrument name has not been found in the registry, a NULL
203  * pointer is returned.
204  ***************************************************************************/
205 GObservation* GObservationRegistry::alloc(const std::string& name) const
206 {
207  // Initialise observation
208  GObservation* obs = NULL;
209 
210  // Search for observation in registry
211  for (int i = 0; i < size(); ++i) {
212  if (names()[i] == name) {
213  obs = this->obs()[i]->clone();
214  break;
215  }
216  }
217 
218  // Return observation
219  return obs;
220 }
221 
222 
223 /***********************************************************************//**
224  * @brief Returns instrument name for a specific registered observation
225  *
226  * @param[in] index Observation index [0,...,size()-1].
227  * @return Instrument name.
228  *
229  * @exception GException::out_of_range
230  * Observation index is out of range.
231  ***************************************************************************/
232 std::string GObservationRegistry::name(const int& index) const
233 {
234  // Compile option: raise exception if index is out of range
235  #if defined(G_RANGE_CHECK)
236  if (index < 0 || index >= size()) {
237  throw GException::out_of_range(G_NAME, index, 0, size()-1);
238  }
239  #endif
240 
241  // Return name
242  return (names()[index]);
243 }
244 
245 
246 /***********************************************************************//**
247  * @brief Print registry information
248  *
249  * @param[in] chatter Chattiness (defaults to NORMAL).
250  * @return Registry content.
251  ***************************************************************************/
252 std::string GObservationRegistry::print(const GChatter& chatter) const
253 {
254  // Initialise result string
255  std::string result;
256 
257  // Continue only if chatter is not silent
258  if (chatter != SILENT) {
259 
260  // Append header
261  result.append("=== GObservationRegistry ===");
262 
263  // Append information
264  result.append("\n"+gammalib::parformat("Number of observations"));
265  result.append(gammalib::str(size()));
266 
267  // NORMAL: Append observations
268  if (chatter >= NORMAL) {
269  for (int i = 0; i < size(); ++i) {
270  result.append("\n"+gammalib::parformat(names()[i]));
271  result.append(this->obs()[i]->instrument());
272  }
273  }
274 
275  } // endif: chatter was not silent
276 
277  // Return result
278  return result;
279 }
280 
281 
282 /*==========================================================================
283  = =
284  = Private methods =
285  = =
286  ==========================================================================*/
287 
288 /***********************************************************************//**
289  * @brief Initialise class members
290  ***************************************************************************/
292 {
293  // Return
294  return;
295 }
296 
297 
298 /***********************************************************************//**
299  * @brief Copy class members
300  *
301  * @param[in] registry Registry.
302  ***************************************************************************/
304 {
305  // Return
306  return;
307 }
308 
309 
310 /***********************************************************************//**
311  * @brief Delete class members
312  ***************************************************************************/
314 {
315  // Return
316  return;
317 }
virtual ~GObservationRegistry(void)
Destructor.
GObservation * alloc(const std::string &name) const
Allocate observation of given name.
virtual std::string instrument(void) const =0
#define G_NAME
GObservationRegistry & operator=(const GObservationRegistry &registry)
Assignment operator.
void free_members(void)
Delete class members.
void init_members(void)
Initialise class members.
Gammalib tools definition.
int size(void) const
Return number of registered observations.
static GRegistryPointer< const GObservation * > & obs()
Interface definition for the observation registry class.
Observation registry class definition.
std::string name(const int &index) const
Returns instrument name for a specific registered observation.
GChatter
Definition: GTypemaps.hpp:33
Abstract observation base class.
GObservationRegistry(void)
Void constructor.
void copy_members(const GObservationRegistry &registry)
Copy class members.
Exception handler interface definition.
std::string print(const GChatter &chatter=NORMAL) const
Print registry information.
std::string parformat(const std::string &s, const int &indent=0)
Convert string in parameter format.
Definition: GTools.cpp:1022
static GRegistryPointer< std::string > & names()
std::string str(const unsigned short int &value)
Convert unsigned short integer value into string.
Definition: GTools.cpp:413