GammaLib  2.0.0
 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-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 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()[.
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, "Projection index",
242  index, size());
243  }
244  #endif
245 
246  // Return code
247  return (codes()[index]);
248 }
249 
250 
251 /***********************************************************************//**
252  * @brief Returns projection name
253  *
254  * @param[in] index Projection index [0,...,size()[.
255  * @return Projection name.
256  *
257  * @exception GException::out_of_range
258  * Projection index is out of range.
259  ***************************************************************************/
260 std::string GWcsRegistry::name(const int& index) const
261 {
262  // Compile option: raise exception if index is out of range
263  #if defined(G_RANGE_CHECK)
264  if (index < 0 || index >= size()) {
265  throw GException::out_of_range(G_CODE, "Projection index",
266  index, size());
267  }
268  #endif
269 
270  // Return name
271  return (names()[index]);
272 }
273 
274 
275 /***********************************************************************//**
276  * @brief Print registry information
277  *
278  * @param[in] chatter Chatter level.
279  * @return Registry content.
280  ***************************************************************************/
281 std::string GWcsRegistry::print(const GChatter& chatter) const
282 {
283  // Initialise result string
284  std::string result;
285 
286  // Continue only if chatter is not silent
287  if (chatter != SILENT) {
288 
289  // Append header
290  result.append("=== GWcsRegistry ===");
291 
292  // Append information
293  result.append("\n"+gammalib::parformat("Number of projections"));
294  result.append(gammalib::str(size()));
295 
296  // NORMAL: Append projections
297  if (chatter >= NORMAL) {
298  for (int i = 0; i < size(); ++i) {
299  result.append("\n"+gammalib::parformat(codes()[i]));
300  result.append(names()[i]);
301  }
302  }
303 
304  } // endif: chatter was not silent
305 
306  // Return result
307  return result;
308 }
309 
310 
311 /*==========================================================================
312  = =
313  = Private methods =
314  = =
315  ==========================================================================*/
316 
317 /***********************************************************************//**
318  * @brief Initialise class members
319  ***************************************************************************/
321 {
322  // Return
323  return;
324 }
325 
326 
327 /***********************************************************************//**
328  * @brief Copy class members
329  *
330  * @param[in] registry Registry.
331  ***************************************************************************/
333 {
334  // Return
335  return;
336 }
337 
338 
339 /***********************************************************************//**
340  * @brief Delete class members
341  ***************************************************************************/
343 {
344  // Return
345  return;
346 }
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.
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()
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:1143
#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:489
static GRegistryPointer< const GWcs * > & projections()