GammaLib 2.0.0
Loading...
Searching...
No Matches
GObservationRegistry.cpp
Go to the documentation of this file.
1/***************************************************************************
2 * GObservationRegistry.cpp - Observation 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 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
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
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
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 ***************************************************************************/
205GObservation* 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()[.
227 * @return Instrument name.
228 *
229 * @exception GException::out_of_range
230 * Observation index is out of range.
231 ***************************************************************************/
232std::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, "Observation index",
238 index, size());
239 }
240 #endif
241
242 // Return name
243 return (names()[index]);
244}
245
246
247/***********************************************************************//**
248 * @brief Print registry information
249 *
250 * @param[in] chatter Chattiness (defaults to NORMAL).
251 * @return Registry content.
252 ***************************************************************************/
253std::string GObservationRegistry::print(const GChatter& chatter) const
254{
255 // Initialise result string
256 std::string result;
257
258 // Continue only if chatter is not silent
259 if (chatter != SILENT) {
260
261 // Append header
262 result.append("=== GObservationRegistry ===");
263
264 // Append information
265 result.append("\n"+gammalib::parformat("Number of observations"));
266 result.append(gammalib::str(size()));
267
268 // NORMAL: Append observations
269 if (chatter >= NORMAL) {
270 for (int i = 0; i < size(); ++i) {
271 result.append("\n"+gammalib::parformat(names()[i]));
272 result.append(this->obs()[i]->instrument());
273 }
274 }
275
276 } // endif: chatter was not silent
277
278 // Return result
279 return result;
280}
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
Observation 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 observation registry class.
virtual ~GObservationRegistry(void)
Destructor.
std::string name(const int &index) const
Returns instrument name for a specific registered observation.
int size(void) const
Return number of registered observations.
static GRegistryPointer< const GObservation * > & obs()
void init_members(void)
Initialise class members.
GObservation * alloc(const std::string &name) const
Allocate observation of given name.
GObservationRegistry & operator=(const GObservationRegistry &registry)
Assignment operator.
GObservationRegistry(void)
Void constructor.
static GRegistryPointer< std::string > & names()
void copy_members(const GObservationRegistry &registry)
Copy class members.
void free_members(void)
Delete class members.
std::string print(const GChatter &chatter=NORMAL) const
Print registry information.
Abstract observation base class.
virtual GObservation * 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