GammaLib  1.7.0.dev
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
GWcsRegistry.cpp
Go to the documentation of this file.
1 /***************************************************************************
2  * GWcsRegistry.cpp - World Coordinate Projection 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 GWcsRegistry.cpp
23  * @brief World Coordinate Projection registry class interface implementation
24  * @author Juergen Knoedlseder
25  */
26 
27 /* __ Includes ___________________________________________________________ */
28 #ifdef HAVE_CONFIG_H
29 #include <config.h>
30 #endif
31 #include "GWcsRegistry.hpp"
32 #include "GException.hpp"
33 #include "GTools.hpp"
34 
35 /* __ Method name definitions ____________________________________________ */
36 #define G_CODE "GWcsRegistry::code(int&)"
37 #define G_NAME "GWcsRegistry::name(int&)"
38 
39 /* __ Macros _____________________________________________________________ */
40 
41 /* __ Coding definitions _________________________________________________ */
42 
43 /* __ Debug definitions __________________________________________________ */
44 #define G_DEBUG_REGISTRY 0
45 
46 
47 /*==========================================================================
48  = =
49  = Constructors/destructors =
50  = =
51  ==========================================================================*/
52 
53 /***********************************************************************//**
54  * @brief Void constructor
55  ***************************************************************************/
57 {
58  // Initialise private members for clean destruction
59  init_members();
60 
61  // Debug option: Show actual registry
62  #if G_DEBUG_REGISTRY
63  std::cout << "GWcsRegistry(void): ";
64  for (int i = 0; i < size(); ++i) {
65  std::cout << "\"" << codes()[i] << "\" ";
66  }
67  std::cout << std::endl;
68  #endif
69 
70  // Return
71  return;
72 }
73 
74 
75 /***********************************************************************//**
76  * @brief Projection constructor
77  *
78  * @param[in] wcs World Coordinate System.
79  ***************************************************************************/
81 {
82  // Initialise private members for clean destruction
83  init_members();
84 
85  // Debug option: Notify new registry
86  #if G_DEBUG_REGISTRY
87  std::cout << "GWcsRegistry(const GWcs*): ";
88  std::cout << "add \"" << wcs->code() << "\" to registry." << std::endl;
89  #endif
90 
91  // Allocate new registry
92  std::string* new_codes = new std::string[size()+1];
93  std::string* new_names = new std::string[size()+1];
94  const GWcs** new_projections = new const GWcs*[size()+1];
95 
96  // Save old registry
97  for (int i = 0; i < size(); ++i) {
98  new_codes[i] = codes()[i];
99  new_names[i] = names()[i];
100  new_projections[i] = projections()[i];
101  }
102 
103  // Add new projection to registry
104  new_codes[size()] = wcs->code();
105  new_names[size()] = wcs->name();
106  new_projections[size()] = wcs;
107 
108  // Set pointers on new registry
109  codes().assign(new_codes);
110  names().assign(new_names);
111  projections().assign(new_projections);
112 
113  // Increment number of projections in registry
114  number()++;
115 
116  // Debug option: Show actual registry
117  #if G_DEBUG_REGISTRY
118  std::cout << "GWcsRegistry(const GWcs*): ";
119  for (int i = 0; i < size(); ++i) {
120  std::cout << "\"" << codes()[i] << "\" ";
121  }
122  std::cout << std::endl;
123  #endif
124 
125  // Return
126  return;
127 }
128 
129 
130 /***********************************************************************//**
131  * @brief Copy constructor
132  *
133  * @param[in] registry Registry.
134  ***************************************************************************/
136 {
137  // Initialise private members
138  init_members();
139 
140  // Copy members
141  copy_members(registry);
142 
143  // Return
144  return;
145 }
146 
147 
148 /***********************************************************************//**
149  * @brief Destructor
150  ***************************************************************************/
152 {
153  // Free members
154  free_members();
155 
156  // Return
157  return;
158 }
159 
160 
161 /*==========================================================================
162  = =
163  = Operators =
164  = =
165  ==========================================================================*/
166 
167 /***********************************************************************//**
168  * @brief Assignment operator
169  *
170  * @param[in] registry Registry.
171  * @return Reference to registry.
172  ***************************************************************************/
174 {
175  // Execute only if object is not identical
176  if (this != &registry) {
177 
178  // Free members
179  free_members();
180 
181  // Initialise private members
182  init_members();
183 
184  // Copy members
185  copy_members(registry);
186 
187  } // endif: object was not identical
188 
189  // Return
190  return *this;
191 }
192 
193 
194 /*==========================================================================
195  = =
196  = Public methods =
197  = =
198  ==========================================================================*/
199 
200 /***********************************************************************//**
201  * @brief Allocate World Coordinate System of given code
202  *
203  * @param[in] code Sky projection code.
204  * @return Pointer to World Coordinate System (NULL if code is not registered).
205  *
206  * Returns a pointer to a sky projection instance of the specified code. If
207  * the code has not been found in the registry, a NULL pointer is returned.
208  ***************************************************************************/
209 GWcs* GWcsRegistry::alloc(const std::string& code) const
210 {
211  // Initialise projection
212  GWcs* projection = NULL;
213 
214  // Search for projection in registry
215  for (int i = 0; i < size(); ++i) {
216  if (codes()[i] == code) {
217  projection = projections()[i]->clone();
218  break;
219  }
220  }
221 
222  // Return projection
223  return projection;
224 }
225 
226 
227 /***********************************************************************//**
228  * @brief Returns projection code
229  *
230  * @param[in] index Projection index [0,...,size()-1].
231  * @return Projection code.
232  *
233  * @exception GException::out_of_range
234  * Projection index is out of range.
235  ***************************************************************************/
236 std::string GWcsRegistry::code(const int& index) const
237 {
238  // Compile option: raise exception if index is out of range
239  #if defined(G_RANGE_CHECK)
240  if (index < 0 || index >= size()) {
241  throw GException::out_of_range(G_CODE, index, 0, size()-1);
242  }
243  #endif
244 
245  // Return code
246  return (codes()[index]);
247 }
248 
249 
250 /***********************************************************************//**
251  * @brief Returns projection name
252  *
253  * @param[in] index Projection index [0,...,size()-1].
254  * @return Projection name.
255  *
256  * @exception GException::out_of_range
257  * Projection index is out of range.
258  ***************************************************************************/
259 std::string GWcsRegistry::name(const int& index) const
260 {
261  // Compile option: raise exception if index is out of range
262  #if defined(G_RANGE_CHECK)
263  if (index < 0 || index >= size()) {
264  throw GException::out_of_range(G_NAME, index, 0, size()-1);
265  }
266  #endif
267 
268  // Return name
269  return (names()[index]);
270 }
271 
272 
273 /***********************************************************************//**
274  * @brief Return list string of projection codes
275  *
276  * @return Projection code list.
277  *
278  * Returns a list of the projection codes in the format 'xxx/yyy/zzz'.
279  ***************************************************************************/
280 std::string GWcsRegistry::list(void) const
281 {
282  // Initialise result string
283  std::string result;
284 
285  // Append projections
286  for (int i = 0; i < size(); ++i) {
287  if (i > 0) {
288  result.append("/");
289  }
290  result.append(codes()[i]);
291  }
292 
293  // Return result
294  return result;
295 }
296 
297 
298 /***********************************************************************//**
299  * @brief Print registry information
300  *
301  * @return Registry content.
302  ***************************************************************************/
303 std::string GWcsRegistry::print(const GChatter& chatter) const
304 {
305  // Initialise result string
306  std::string result;
307 
308  // Continue only if chatter is not silent
309  if (chatter != SILENT) {
310 
311  // Append header
312  result.append("=== GWcsRegistry ===");
313 
314  // Append information
315  result.append("\n"+gammalib::parformat("Number of projections"));
316  result.append(gammalib::str(size()));
317 
318  // NORMAL: Append projections
319  if (chatter >= NORMAL) {
320  for (int i = 0; i < size(); ++i) {
321  result.append("\n"+gammalib::parformat(codes()[i]));
322  result.append(names()[i]);
323  }
324  }
325 
326  } // endif: chatter was not silent
327 
328  // Return result
329  return result;
330 }
331 
332 
333 /*==========================================================================
334  = =
335  = Private methods =
336  = =
337  ==========================================================================*/
338 
339 /***********************************************************************//**
340  * @brief Initialise class members
341  ***************************************************************************/
343 {
344  // Return
345  return;
346 }
347 
348 
349 /***********************************************************************//**
350  * @brief Copy class members
351  *
352  * @param[in] registry Registry.
353  ***************************************************************************/
355 {
356  // Return
357  return;
358 }
359 
360 
361 /***********************************************************************//**
362  * @brief Delete class members
363  ***************************************************************************/
365 {
366  // Return
367  return;
368 }
std::string name(const int &index) const
Returns projection name.
void free_members(void)
Delete class members.
Interface definition for the WCS registry class.
std::string print(const GChatter &chatter=NORMAL) const
Print registry information.
GWcsRegistry(void)
Void constructor.
virtual ~GWcsRegistry(void)
Destructor.
Gammalib tools definition.
virtual std::string code(void) const =0
World Coordinate Projection registry class interface definition.
std::string list(void) const
Return list string of projection codes.
GWcs * alloc(const std::string &code) const
Allocate World Coordinate System of given code.
void copy_members(const GWcsRegistry &registry)
Copy class members.
GChatter
Definition: GTypemaps.hpp:33
static GRegistryPointer< std::string > & codes()
static int & number()
void init_members(void)
Initialise class members.
Abstract world coordinate system base class.
Definition: GWcs.hpp:51
static GRegistryPointer< std::string > & names()
#define G_NAME
Exception handler interface definition.
GWcsRegistry & operator=(const GWcsRegistry &registry)
Assignment operator.
std::string parformat(const std::string &s, const int &indent=0)
Convert string in parameter format.
Definition: GTools.cpp:1022
#define G_CODE
std::string code(const int &index) const
Returns projection code.
virtual std::string name(void) const =0
int size(void) const
Return number of registered models.
std::string str(const unsigned short int &value)
Convert unsigned short integer value into string.
Definition: GTools.cpp:413
static GRegistryPointer< const GWcs * > & projections()