Commit 5364bdc3a5ff7040b1e0c7e26528be5e1018a43d
1 parent
c610b5ba
Use static functions instead of static members to avoid initialization order tro…
…uble. A smart pointer is now used for the static memory allocation so that memory gets freed correctly before exit.
Showing
15 changed files
with
381 additions
and
282 deletions
include/GModelRegistry.hpp
| ... | ... | @@ -75,10 +75,25 @@ protected: |
| 75 | 75 | void free_members(void); |
| 76 | 76 | |
| 77 | 77 | private: |
| 78 | - // Pricate members | |
| 79 | - static int m_number; //!< Number of models in registry | |
| 80 | - static std::string* m_names; //!< Model names | |
| 81 | - static const GModel** m_models; //!< Pointer to seed models | |
| 78 | + // Private members (the private members have been implement as static | |
| 79 | + // methods to avoid the static initialization order fiasco of static | |
| 80 | + // members; using static methods we follow the "construct on first use | |
| 81 | + // idiom") | |
| 82 | + // Number of models in registry | |
| 83 | + static int& number() { | |
| 84 | + static int m_number = 0; | |
| 85 | + return m_number; | |
| 86 | + }; | |
| 87 | + // Model names | |
| 88 | + static GRegistryPointer<std::string>& names() { | |
| 89 | + static GRegistryPointer<std::string> m_names; | |
| 90 | + return m_names; | |
| 91 | + }; | |
| 92 | + // Pointer to seed models | |
| 93 | + static GRegistryPointer<const GModel*>& models() { | |
| 94 | + static GRegistryPointer<const GModel*> m_models; | |
| 95 | + return m_models; | |
| 96 | + }; | |
| 82 | 97 | }; |
| 83 | 98 | |
| 84 | 99 | |
| ... | ... | @@ -92,7 +107,7 @@ private: |
| 92 | 107 | inline |
| 93 | 108 | int GModelRegistry::size(void) const |
| 94 | 109 | { |
| 95 | - return m_number; | |
| 110 | + return number(); | |
| 96 | 111 | } |
| 97 | 112 | |
| 98 | 113 | #endif /* GMODELREGISTRY_HPP */ | ... | ... |
include/GModelSpatialRegistry.hpp
| ... | ... | @@ -75,10 +75,25 @@ protected: |
| 75 | 75 | void free_members(void); |
| 76 | 76 | |
| 77 | 77 | private: |
| 78 | - // Pricate members | |
| 79 | - static int m_number; //!< Number of models in registry | |
| 80 | - static std::string* m_names; //!< Model names | |
| 81 | - static const GModelSpatial** m_models; //!< Pointer to seed models | |
| 78 | + // Private members (the private members have been implement as static | |
| 79 | + // methods to avoid the static initialization order fiasco of static | |
| 80 | + // members; using static methods we follow the "construct on first use | |
| 81 | + // idiom") | |
| 82 | + // Number of models in registry | |
| 83 | + static int& number() { | |
| 84 | + static int m_number = 0; | |
| 85 | + return m_number; | |
| 86 | + }; | |
| 87 | + // Model names | |
| 88 | + static GRegistryPointer<std::string>& names() { | |
| 89 | + static GRegistryPointer<std::string> m_names; | |
| 90 | + return m_names; | |
| 91 | + }; | |
| 92 | + // Pointer to seed models | |
| 93 | + static GRegistryPointer<const GModelSpatial*>& models() { | |
| 94 | + static GRegistryPointer<const GModelSpatial*> m_models; | |
| 95 | + return m_models; | |
| 96 | + }; | |
| 82 | 97 | }; |
| 83 | 98 | |
| 84 | 99 | |
| ... | ... | @@ -92,7 +107,7 @@ private: |
| 92 | 107 | inline |
| 93 | 108 | int GModelSpatialRegistry::size(void) const |
| 94 | 109 | { |
| 95 | - return m_number; | |
| 110 | + return number(); | |
| 96 | 111 | } |
| 97 | 112 | |
| 98 | 113 | #endif /* GMODELSPATIALREGISTRY_HPP */ | ... | ... |
include/GModelSpectralRegistry.hpp
| ... | ... | @@ -75,10 +75,25 @@ protected: |
| 75 | 75 | void free_members(void); |
| 76 | 76 | |
| 77 | 77 | private: |
| 78 | - // Pricate members | |
| 79 | - static int m_number; //!< Number of models in registry | |
| 80 | - static std::string* m_names; //!< Model names | |
| 81 | - static const GModelSpectral** m_models; //!< Pointer to seed models | |
| 78 | + // Private members (the private members have been implement as static | |
| 79 | + // methods to avoid the static initialization order fiasco of static | |
| 80 | + // members; using static methods we follow the "construct on first use | |
| 81 | + // idiom") | |
| 82 | + // Number of models in registry | |
| 83 | + static int& number() { | |
| 84 | + static int m_number = 0; | |
| 85 | + return m_number; | |
| 86 | + }; | |
| 87 | + // Model names | |
| 88 | + static GRegistryPointer<std::string>& names() { | |
| 89 | + static GRegistryPointer<std::string> m_names; | |
| 90 | + return m_names; | |
| 91 | + }; | |
| 92 | + // Pointer to seed models | |
| 93 | + static GRegistryPointer<const GModelSpectral*>& models() { | |
| 94 | + static GRegistryPointer<const GModelSpectral*> m_models; | |
| 95 | + return m_models; | |
| 96 | + }; | |
| 82 | 97 | }; |
| 83 | 98 | |
| 84 | 99 | |
| ... | ... | @@ -92,7 +107,7 @@ private: |
| 92 | 107 | inline |
| 93 | 108 | int GModelSpectralRegistry::size(void) const |
| 94 | 109 | { |
| 95 | - return m_number; | |
| 110 | + return number(); | |
| 96 | 111 | } |
| 97 | 112 | |
| 98 | 113 | #endif /* GMODELSPECTRALREGISTRY_HPP */ | ... | ... |
include/GModelTemporalRegistry.hpp
| ... | ... | @@ -79,18 +79,17 @@ private: |
| 79 | 79 | // methods to avoid the static initialization order fiasco of static |
| 80 | 80 | // members; using static methods we follow the "construct on first use |
| 81 | 81 | // idiom") |
| 82 | - //static int m_number; //!< Number of models in registry | |
| 83 | - //static std::string* m_names; //!< Model names | |
| 84 | - //static const GModelTemporal** m_models; //!< Pointer to seed models | |
| 85 | - //static GRegistryPointer<std::string> m_names; | |
| 82 | + // Number of models in registry | |
| 86 | 83 | static int& number() { |
| 87 | 84 | static int m_number = 0; |
| 88 | 85 | return m_number; |
| 89 | 86 | }; |
| 87 | + // Model names | |
| 90 | 88 | static GRegistryPointer<std::string>& names() { |
| 91 | 89 | static GRegistryPointer<std::string> m_names; |
| 92 | 90 | return m_names; |
| 93 | 91 | }; |
| 92 | + // Pointer to seed models | |
| 94 | 93 | static GRegistryPointer<const GModelTemporal*>& models() { |
| 95 | 94 | static GRegistryPointer<const GModelTemporal*> m_models; |
| 96 | 95 | return m_models; | ... | ... |
include/GObservationRegistry.hpp
| ... | ... | @@ -64,7 +64,7 @@ public: |
| 64 | 64 | GObservationRegistry& operator= (const GObservationRegistry& registry); |
| 65 | 65 | |
| 66 | 66 | // Methods |
| 67 | - int size(void) const { return m_number; } | |
| 67 | + int size(void) const; | |
| 68 | 68 | GObservation* alloc(const std::string& name) const; |
| 69 | 69 | std::string name(const int& index) const; |
| 70 | 70 | std::string print(const GChatter& chatter = NORMAL) const; |
| ... | ... | @@ -76,10 +76,39 @@ protected: |
| 76 | 76 | void free_members(void); |
| 77 | 77 | |
| 78 | 78 | private: |
| 79 | - // Private members | |
| 80 | - static int m_number; //!< Number of observations in registry | |
| 81 | - static std::string* m_names; //!< Instrument names | |
| 82 | - static const GObservation** m_obs; //!< Pointer to seed observations | |
| 79 | + // Private members (the private members have been implement as static | |
| 80 | + // methods to avoid the static initialization order fiasco of static | |
| 81 | + // members; using static methods we follow the "construct on first use | |
| 82 | + // idiom") | |
| 83 | + // Number of models in registry | |
| 84 | + static int& number() { | |
| 85 | + static int m_number = 0; | |
| 86 | + return m_number; | |
| 87 | + }; | |
| 88 | + // Model names | |
| 89 | + static GRegistryPointer<std::string>& names() { | |
| 90 | + static GRegistryPointer<std::string> m_names; | |
| 91 | + return m_names; | |
| 92 | + }; | |
| 93 | + // Pointer to seed models | |
| 94 | + static GRegistryPointer<const GObservation*>& obs() { | |
| 95 | + static GRegistryPointer<const GObservation*> m_obs; | |
| 96 | + return m_obs; | |
| 97 | + }; | |
| 83 | 98 | }; |
| 84 | 99 | |
| 100 | + | |
| 101 | +/***********************************************************************//** | |
| 102 | + * @brief Return number of registered observations | |
| 103 | + * | |
| 104 | + * @return Number of registered observations. | |
| 105 | + * | |
| 106 | + * Returns the number of registered observations. | |
| 107 | + ***************************************************************************/ | |
| 108 | +inline | |
| 109 | +int GObservationRegistry::size(void) const | |
| 110 | +{ | |
| 111 | + return number(); | |
| 112 | +} | |
| 113 | + | |
| 85 | 114 | #endif /* GOBSERVATIONREGISTRY_HPP */ | ... | ... |
include/GRegistry.hpp
include/GWcsRegistry.hpp
| ... | ... | @@ -63,7 +63,7 @@ public: |
| 63 | 63 | GWcsRegistry& operator= (const GWcsRegistry& registry); |
| 64 | 64 | |
| 65 | 65 | // Methods |
| 66 | - int size(void) const { return m_number; } | |
| 66 | + int size(void) const; | |
| 67 | 67 | GWcs* alloc(const std::string& code) const; |
| 68 | 68 | std::string code(const int& index) const; |
| 69 | 69 | std::string name(const int& index) const; |
| ... | ... | @@ -77,11 +77,44 @@ protected: |
| 77 | 77 | void free_members(void); |
| 78 | 78 | |
| 79 | 79 | private: |
| 80 | - // Pricate members | |
| 81 | - static int m_number; //!< Number of projections in registry | |
| 82 | - static std::string* m_codes; //!< Projection codes | |
| 83 | - static std::string* m_names; //!< Projection names | |
| 84 | - static const GWcs** m_prjs; //!< Pointer to seed projections | |
| 80 | + // Private members (the private members have been implement as static | |
| 81 | + // methods to avoid the static initialization order fiasco of static | |
| 82 | + // members; using static methods we follow the "construct on first use | |
| 83 | + // idiom") | |
| 84 | + // Number of projections in registry | |
| 85 | + static int& number() { | |
| 86 | + static int m_number = 0; | |
| 87 | + return m_number; | |
| 88 | + }; | |
| 89 | + // Projection codes | |
| 90 | + static GRegistryPointer<std::string>& codes() { | |
| 91 | + static GRegistryPointer<std::string> m_codes; | |
| 92 | + return m_codes; | |
| 93 | + }; | |
| 94 | + // Projection names | |
| 95 | + static GRegistryPointer<std::string>& names() { | |
| 96 | + static GRegistryPointer<std::string> m_names; | |
| 97 | + return m_names; | |
| 98 | + }; | |
| 99 | + // Pointer to seed projections | |
| 100 | + static GRegistryPointer<const GWcs*>& projections() { | |
| 101 | + static GRegistryPointer<const GWcs*> m_projections; | |
| 102 | + return m_projections; | |
| 103 | + }; | |
| 85 | 104 | }; |
| 86 | 105 | |
| 106 | + | |
| 107 | +/***********************************************************************//** | |
| 108 | + * @brief Return number of registered models | |
| 109 | + * | |
| 110 | + * @return Number of registered models. | |
| 111 | + * | |
| 112 | + * Returns the number of registered model. | |
| 113 | + ***************************************************************************/ | |
| 114 | +inline | |
| 115 | +int GWcsRegistry::size(void) const | |
| 116 | +{ | |
| 117 | + return number(); | |
| 118 | +} | |
| 119 | + | |
| 87 | 120 | #endif /* GWCSREGISTRY_HPP */ | ... | ... |
inst/cta/include/GCTAModelRadialRegistry.hpp
| ... | ... | @@ -63,7 +63,7 @@ public: |
| 63 | 63 | GCTAModelRadialRegistry& operator= (const GCTAModelRadialRegistry& registry); |
| 64 | 64 | |
| 65 | 65 | // Methods |
| 66 | - int size(void) const { return m_number; } | |
| 66 | + int size(void) const; | |
| 67 | 67 | GCTAModelRadial* alloc(const std::string& name) const; |
| 68 | 68 | std::string name(const int& index) const; |
| 69 | 69 | std::string print(const GChatter& chatter = NORMAL) const; |
| ... | ... | @@ -75,10 +75,39 @@ protected: |
| 75 | 75 | void free_members(void); |
| 76 | 76 | |
| 77 | 77 | private: |
| 78 | - // Pricate members | |
| 79 | - static int m_number; //!< Number of models in registry | |
| 80 | - static std::string* m_names; //!< Model names | |
| 81 | - static const GCTAModelRadial** m_models; //!< Pointer to seed models | |
| 78 | + // Private members (the private members have been implement as static | |
| 79 | + // methods to avoid the static initialization order fiasco of static | |
| 80 | + // members; using static methods we follow the "construct on first use | |
| 81 | + // idiom") | |
| 82 | + // Number of models in registry | |
| 83 | + static int& number() { | |
| 84 | + static int m_number = 0; | |
| 85 | + return m_number; | |
| 86 | + }; | |
| 87 | + // Model names | |
| 88 | + static GRegistryPointer<std::string>& names() { | |
| 89 | + static GRegistryPointer<std::string> m_names; | |
| 90 | + return m_names; | |
| 91 | + }; | |
| 92 | + // Pointer to seed models | |
| 93 | + static GRegistryPointer<const GCTAModelRadial*>& models() { | |
| 94 | + static GRegistryPointer<const GCTAModelRadial*> m_models; | |
| 95 | + return m_models; | |
| 96 | + }; | |
| 82 | 97 | }; |
| 83 | 98 | |
| 99 | + | |
| 100 | +/***********************************************************************//** | |
| 101 | + * @brief Return number of registered models | |
| 102 | + * | |
| 103 | + * @return Number of registered models. | |
| 104 | + * | |
| 105 | + * Returns the number of registered model. | |
| 106 | + ***************************************************************************/ | |
| 107 | +inline | |
| 108 | +int GCTAModelRadialRegistry::size(void) const | |
| 109 | +{ | |
| 110 | + return number(); | |
| 111 | +} | |
| 112 | + | |
| 84 | 113 | #endif /* GCTAMODELRADIALREGISTRY_HPP */ | ... | ... |
inst/cta/src/GCTAModelRadialRegistry.cpp
| ... | ... | @@ -29,13 +29,9 @@ |
| 29 | 29 | #include <config.h> |
| 30 | 30 | #endif |
| 31 | 31 | #include "GCTAModelRadialRegistry.hpp" |
| 32 | +#include "GException.hpp" | |
| 32 | 33 | #include "GTools.hpp" |
| 33 | 34 | |
| 34 | -/* __ Static members _____________________________________________________ */ | |
| 35 | -int GCTAModelRadialRegistry::m_number(0); | |
| 36 | -std::string* GCTAModelRadialRegistry::m_names(0); | |
| 37 | -const GCTAModelRadial** GCTAModelRadialRegistry::m_models(0); | |
| 38 | - | |
| 39 | 35 | /* __ Method name definitions ____________________________________________ */ |
| 40 | 36 | #define G_NAME "GCTAModelRadialRegistry::name(int&)" |
| 41 | 37 | |
| ... | ... | @@ -64,8 +60,9 @@ GCTAModelRadialRegistry::GCTAModelRadialRegistry(void) |
| 64 | 60 | // Debug option: Show actual registry |
| 65 | 61 | #if G_DEBUG_REGISTRY |
| 66 | 62 | std::cout << "GCTAModelRadialRegistry(void): "; |
| 67 | - for (int i = 0; i < m_number; ++i) | |
| 68 | - std::cout << "\"" << m_names[i] << "\" "; | |
| 63 | + for (int i = 0; i < size(); ++i) { | |
| 64 | + std::cout << "\"" << names()[i] << "\" "; | |
| 65 | + } | |
| 69 | 66 | std::cout << std::endl; |
| 70 | 67 | #endif |
| 71 | 68 | |
| ... | ... | @@ -89,37 +86,33 @@ GCTAModelRadialRegistry::GCTAModelRadialRegistry(const GCTAModelRadial* model) |
| 89 | 86 | std::cout << "GCTAModelRadialRegistry(const GCTAModelRadial*): "; |
| 90 | 87 | std::cout << "add \"" << model->type() << "\" to registry." << std::endl; |
| 91 | 88 | #endif |
| 92 | - | |
| 93 | - // Allocate new old registry | |
| 94 | - std::string* new_names = new std::string[m_number+1]; | |
| 95 | - const GCTAModelRadial** new_models = new const GCTAModelRadial*[m_number+1]; | |
| 89 | + | |
| 90 | + // Allocate new registry | |
| 91 | + std::string* new_names = new std::string[size()+1]; | |
| 92 | + const GCTAModelRadial** new_models = new const GCTAModelRadial*[size()+1]; | |
| 96 | 93 | |
| 97 | 94 | // Save old registry |
| 98 | - for (int i = 0; i < m_number; ++i) { | |
| 99 | - new_names[i] = m_names[i]; | |
| 100 | - new_models[i] = m_models[i]; | |
| 95 | + for (int i = 0; i < size(); ++i) { | |
| 96 | + new_names[i] = names()[i]; | |
| 97 | + new_models[i] = models()[i]; | |
| 101 | 98 | } |
| 102 | 99 | |
| 103 | 100 | // Add new model to registry |
| 104 | - new_names[m_number] = model->type(); | |
| 105 | - new_models[m_number] = model; | |
| 106 | - | |
| 107 | - // Delete old registry | |
| 108 | - if (m_names != NULL) delete [] m_names; | |
| 109 | - if (m_models != NULL) delete [] m_models; | |
| 101 | + new_names[size()] = model->type(); | |
| 102 | + new_models[size()] = model; | |
| 110 | 103 | |
| 111 | 104 | // Set pointers on new registry |
| 112 | - m_names = new_names; | |
| 113 | - m_models = new_models; | |
| 105 | + names().assign(new_names); | |
| 106 | + models().assign(new_models); | |
| 114 | 107 | |
| 115 | 108 | // Increment number of models in registry |
| 116 | - m_number++; | |
| 109 | + number()++; | |
| 117 | 110 | |
| 118 | 111 | // Debug option: Show actual registry |
| 119 | 112 | #if G_DEBUG_REGISTRY |
| 120 | 113 | std::cout << "GCTAModelRadialRegistry(const GCTAModelRadial*): "; |
| 121 | - for (int i = 0; i < m_number; ++i) { | |
| 122 | - std::cout << "\"" << m_names[i] << "\" "; | |
| 114 | + for (int i = 0; i < size(); ++i) { | |
| 115 | + std::cout << "\"" << names()[i] << "\" "; | |
| 123 | 116 | } |
| 124 | 117 | std::cout << std::endl; |
| 125 | 118 | #endif |
| ... | ... | @@ -172,7 +165,7 @@ GCTAModelRadialRegistry::~GCTAModelRadialRegistry(void) |
| 172 | 165 | * @param[in] registry Registry. |
| 173 | 166 | * @return Reference to registry. |
| 174 | 167 | ***************************************************************************/ |
| 175 | -GCTAModelRadialRegistry& GCTAModelRadialRegistry::operator= (const GCTAModelRadialRegistry& registry) | |
| 168 | +GCTAModelRadialRegistry& GCTAModelRadialRegistry::operator=(const GCTAModelRadialRegistry& registry) | |
| 176 | 169 | { |
| 177 | 170 | // Execute only if object is not identical |
| 178 | 171 | if (this != ®istry) { |
| ... | ... | @@ -214,12 +207,12 @@ GCTAModelRadial* GCTAModelRadialRegistry::alloc(const std::string& name) const |
| 214 | 207 | GCTAModelRadial* model = NULL; |
| 215 | 208 | |
| 216 | 209 | // Search for model in registry |
| 217 | - for (int i = 0; i < m_number; ++i) { | |
| 218 | - if (m_names[i] == name) { | |
| 219 | - model = m_models[i]->clone(); | |
| 210 | + for (int i = 0; i < size(); ++i) { | |
| 211 | + if (names()[i] == name) { | |
| 212 | + model = models()[i]->clone(); | |
| 220 | 213 | break; |
| 221 | 214 | } |
| 222 | - } | |
| 215 | + } | |
| 223 | 216 | |
| 224 | 217 | // Return radial model |
| 225 | 218 | return model; |
| ... | ... | @@ -245,7 +238,7 @@ std::string GCTAModelRadialRegistry::name(const int& index) const |
| 245 | 238 | #endif |
| 246 | 239 | |
| 247 | 240 | // Return name |
| 248 | - return (m_names[index]); | |
| 241 | + return (names()[index]); | |
| 249 | 242 | } |
| 250 | 243 | |
| 251 | 244 | |
| ... | ... | @@ -267,13 +260,15 @@ std::string GCTAModelRadialRegistry::print(const GChatter& chatter) const |
| 267 | 260 | result.append("=== GCTAModelRadialRegistry ==="); |
| 268 | 261 | |
| 269 | 262 | // Append information |
| 270 | - result.append("\n"+gammalib::parformat("Number of models") + | |
| 271 | - gammalib::str(m_number)); | |
| 272 | - | |
| 273 | - // Append models | |
| 274 | - for (int i = 0; i < m_number; ++i) { | |
| 275 | - result.append("\n"+gammalib::parformat(m_names[i])); | |
| 276 | - result.append(m_models[i]->type()); | |
| 263 | + result.append("\n"+gammalib::parformat("Number of models")); | |
| 264 | + result.append(gammalib::str(size())); | |
| 265 | + | |
| 266 | + // NORMAL: Append models | |
| 267 | + if (chatter >= NORMAL) { | |
| 268 | + for (int i = 0; i < size(); ++i) { | |
| 269 | + result.append("\n"+gammalib::parformat(names()[i])); | |
| 270 | + result.append(models()[i]->type()); | |
| 271 | + } | |
| 277 | 272 | } |
| 278 | 273 | |
| 279 | 274 | } // endif: chatter was not silent | ... | ... |
src/model/GModelRegistry.cpp
| ... | ... | @@ -32,11 +32,6 @@ |
| 32 | 32 | #include "GException.hpp" |
| 33 | 33 | #include "GTools.hpp" |
| 34 | 34 | |
| 35 | -/* __ Static members _____________________________________________________ */ | |
| 36 | -int GModelRegistry::m_number(0); | |
| 37 | -std::string* GModelRegistry::m_names(0); | |
| 38 | -const GModel** GModelRegistry::m_models(0); | |
| 39 | - | |
| 40 | 35 | /* __ Method name definitions ____________________________________________ */ |
| 41 | 36 | #define G_NAME "GModelRegistry::name(int&)" |
| 42 | 37 | |
| ... | ... | @@ -65,8 +60,8 @@ GModelRegistry::GModelRegistry(void) |
| 65 | 60 | // Debug option: Show actual registry |
| 66 | 61 | #if G_DEBUG_REGISTRY |
| 67 | 62 | std::cout << "GModelRegistry(void): "; |
| 68 | - for (int i = 0; i < m_number; ++i) { | |
| 69 | - std::cout << "\"" << m_names[i] << "\" "; | |
| 63 | + for (int i = 0; i < size(); ++i) { | |
| 64 | + std::cout << "\"" << names()[i] << "\" "; | |
| 70 | 65 | } |
| 71 | 66 | std::cout << std::endl; |
| 72 | 67 | #endif |
| ... | ... | @@ -94,37 +89,33 @@ GModelRegistry::GModelRegistry(const GModel* model) |
| 94 | 89 | std::cout << "GModelRegistry(const GModel*): "; |
| 95 | 90 | std::cout << "add \"" << model->type() << "\" to registry." << std::endl; |
| 96 | 91 | #endif |
| 97 | - | |
| 98 | - // Allocate new old registry | |
| 99 | - std::string* new_names = new std::string[m_number+1]; | |
| 100 | - const GModel** new_models = new const GModel*[m_number+1]; | |
| 92 | + | |
| 93 | + // Allocate new registry | |
| 94 | + std::string* new_names = new std::string[size()+1]; | |
| 95 | + const GModel** new_models = new const GModel*[size()+1]; | |
| 101 | 96 | |
| 102 | 97 | // Save old registry |
| 103 | - for (int i = 0; i < m_number; ++i) { | |
| 104 | - new_names[i] = m_names[i]; | |
| 105 | - new_models[i] = m_models[i]; | |
| 98 | + for (int i = 0; i < size(); ++i) { | |
| 99 | + new_names[i] = names()[i]; | |
| 100 | + new_models[i] = models()[i]; | |
| 106 | 101 | } |
| 107 | 102 | |
| 108 | 103 | // Add new model to registry |
| 109 | - new_names[m_number] = model->type(); | |
| 110 | - new_models[m_number] = model; | |
| 111 | - | |
| 112 | - // Delete old registry | |
| 113 | - if (m_names != NULL) delete [] m_names; | |
| 114 | - if (m_models != NULL) delete [] m_models; | |
| 104 | + new_names[size()] = model->type(); | |
| 105 | + new_models[size()] = model; | |
| 115 | 106 | |
| 116 | 107 | // Set pointers on new registry |
| 117 | - m_names = new_names; | |
| 118 | - m_models = new_models; | |
| 108 | + names().assign(new_names); | |
| 109 | + models().assign(new_models); | |
| 119 | 110 | |
| 120 | 111 | // Increment number of models in registry |
| 121 | - m_number++; | |
| 112 | + number()++; | |
| 122 | 113 | |
| 123 | 114 | // Debug option: Show actual registry |
| 124 | 115 | #if G_DEBUG_REGISTRY |
| 125 | 116 | std::cout << "GModelRegistry(const GModel*): "; |
| 126 | - for (int i = 0; i < m_number; ++i) { | |
| 127 | - std::cout << "\"" << m_names[i] << "\" "; | |
| 117 | + for (int i = 0; i < size(); ++i) { | |
| 118 | + std::cout << "\"" << names()[i] << "\" "; | |
| 128 | 119 | } |
| 129 | 120 | std::cout << std::endl; |
| 130 | 121 | #endif |
| ... | ... | @@ -219,12 +210,12 @@ GModel* GModelRegistry::alloc(const std::string& name) const |
| 219 | 210 | GModel* model = NULL; |
| 220 | 211 | |
| 221 | 212 | // Search for model in registry |
| 222 | - for (int i = 0; i < m_number; ++i) { | |
| 223 | - if (m_names[i] == name) { | |
| 224 | - model = m_models[i]->clone(); | |
| 213 | + for (int i = 0; i < size(); ++i) { | |
| 214 | + if (names()[i] == name) { | |
| 215 | + model = models()[i]->clone(); | |
| 225 | 216 | break; |
| 226 | 217 | } |
| 227 | - } | |
| 218 | + } | |
| 228 | 219 | |
| 229 | 220 | // Return model |
| 230 | 221 | return model; |
| ... | ... | @@ -250,7 +241,7 @@ std::string GModelRegistry::name(const int& index) const |
| 250 | 241 | #endif |
| 251 | 242 | |
| 252 | 243 | // Return name |
| 253 | - return (m_names[index]); | |
| 244 | + return (names()[index]); | |
| 254 | 245 | } |
| 255 | 246 | |
| 256 | 247 | |
| ... | ... | @@ -273,12 +264,14 @@ std::string GModelRegistry::print(const GChatter& chatter) const |
| 273 | 264 | |
| 274 | 265 | // Append information |
| 275 | 266 | result.append("\n"+gammalib::parformat("Number of models")); |
| 276 | - result.append(gammalib::str(m_number)); | |
| 277 | - | |
| 278 | - // Append models | |
| 279 | - for (int i = 0; i < m_number; ++i) { | |
| 280 | - result.append("\n"+gammalib::parformat(m_names[i])); | |
| 281 | - result.append(m_models[i]->type()); | |
| 267 | + result.append(gammalib::str(size())); | |
| 268 | + | |
| 269 | + // NORMAL: Append models | |
| 270 | + if (chatter >= NORMAL) { | |
| 271 | + for (int i = 0; i < size(); ++i) { | |
| 272 | + result.append("\n"+gammalib::parformat(names()[i])); | |
| 273 | + result.append(models()[i]->type()); | |
| 274 | + } | |
| 282 | 275 | } |
| 283 | 276 | |
| 284 | 277 | } // endif: chatter was not silent | ... | ... |
src/model/GModelSpatialRegistry.cpp
| ... | ... | @@ -29,13 +29,9 @@ |
| 29 | 29 | #include <config.h> |
| 30 | 30 | #endif |
| 31 | 31 | #include "GModelSpatialRegistry.hpp" |
| 32 | +#include "GException.hpp" | |
| 32 | 33 | #include "GTools.hpp" |
| 33 | 34 | |
| 34 | -/* __ Static members _____________________________________________________ */ | |
| 35 | -int GModelSpatialRegistry::m_number(0); | |
| 36 | -std::string* GModelSpatialRegistry::m_names(0); | |
| 37 | -const GModelSpatial** GModelSpatialRegistry::m_models(0); | |
| 38 | - | |
| 39 | 35 | /* __ Method name definitions ____________________________________________ */ |
| 40 | 36 | #define G_NAME "GModelSpatialRegistry::name(int&)" |
| 41 | 37 | |
| ... | ... | @@ -64,8 +60,8 @@ GModelSpatialRegistry::GModelSpatialRegistry(void) |
| 64 | 60 | // Debug option: Show actual registry |
| 65 | 61 | #if G_DEBUG_REGISTRY |
| 66 | 62 | std::cout << "GModelSpatialRegistry(void): "; |
| 67 | - for (int i = 0; i < m_number; ++i) { | |
| 68 | - std::cout << "\"" << m_names[i] << "\" "; | |
| 63 | + for (int i = 0; i < size(); ++i) { | |
| 64 | + std::cout << "\"" << names()[i] << "\" "; | |
| 69 | 65 | } |
| 70 | 66 | std::cout << std::endl; |
| 71 | 67 | #endif |
| ... | ... | @@ -93,37 +89,33 @@ GModelSpatialRegistry::GModelSpatialRegistry(const GModelSpatial* model) |
| 93 | 89 | std::cout << "GModelSpatialRegistry(const GModelSpatial*): "; |
| 94 | 90 | std::cout << "add \"" << model->type() << "\" to registry." << std::endl; |
| 95 | 91 | #endif |
| 96 | - | |
| 97 | - // Allocate new old registry | |
| 98 | - std::string* new_names = new std::string[m_number+1]; | |
| 99 | - const GModelSpatial** new_models = new const GModelSpatial*[m_number+1]; | |
| 92 | + | |
| 93 | + // Allocate new registry | |
| 94 | + std::string* new_names = new std::string[size()+1]; | |
| 95 | + const GModelSpatial** new_models = new const GModelSpatial*[size()+1]; | |
| 100 | 96 | |
| 101 | 97 | // Save old registry |
| 102 | - for (int i = 0; i < m_number; ++i) { | |
| 103 | - new_names[i] = m_names[i]; | |
| 104 | - new_models[i] = m_models[i]; | |
| 98 | + for (int i = 0; i < size(); ++i) { | |
| 99 | + new_names[i] = names()[i]; | |
| 100 | + new_models[i] = models()[i]; | |
| 105 | 101 | } |
| 106 | 102 | |
| 107 | 103 | // Add new model to registry |
| 108 | - new_names[m_number] = model->type(); | |
| 109 | - new_models[m_number] = model; | |
| 110 | - | |
| 111 | - // Delete old registry | |
| 112 | - if (m_names != NULL) delete [] m_names; | |
| 113 | - if (m_models != NULL) delete [] m_models; | |
| 104 | + new_names[size()] = model->type(); | |
| 105 | + new_models[size()] = model; | |
| 114 | 106 | |
| 115 | 107 | // Set pointers on new registry |
| 116 | - m_names = new_names; | |
| 117 | - m_models = new_models; | |
| 108 | + names().assign(new_names); | |
| 109 | + models().assign(new_models); | |
| 118 | 110 | |
| 119 | 111 | // Increment number of models in registry |
| 120 | - m_number++; | |
| 112 | + number()++; | |
| 121 | 113 | |
| 122 | 114 | // Debug option: Show actual registry |
| 123 | 115 | #if G_DEBUG_REGISTRY |
| 124 | 116 | std::cout << "GModelSpatialRegistry(const GModelSpatial*): "; |
| 125 | - for (int i = 0; i < m_number; ++i) { | |
| 126 | - std::cout << "\"" << m_names[i] << "\" "; | |
| 117 | + for (int i = 0; i < size(); ++i) { | |
| 118 | + std::cout << "\"" << names()[i] << "\" "; | |
| 127 | 119 | } |
| 128 | 120 | std::cout << std::endl; |
| 129 | 121 | #endif |
| ... | ... | @@ -219,12 +211,12 @@ GModelSpatial* GModelSpatialRegistry::alloc(const std::string& name) const |
| 219 | 211 | GModelSpatial* model = NULL; |
| 220 | 212 | |
| 221 | 213 | // Search for model in registry |
| 222 | - for (int i = 0; i < m_number; ++i) { | |
| 223 | - if (m_names[i] == name) { | |
| 224 | - model = m_models[i]->clone(); | |
| 214 | + for (int i = 0; i < size(); ++i) { | |
| 215 | + if (names()[i] == name) { | |
| 216 | + model = models()[i]->clone(); | |
| 225 | 217 | break; |
| 226 | 218 | } |
| 227 | - } | |
| 219 | + } | |
| 228 | 220 | |
| 229 | 221 | // Return spatial model |
| 230 | 222 | return model; |
| ... | ... | @@ -250,7 +242,7 @@ std::string GModelSpatialRegistry::name(const int& index) const |
| 250 | 242 | #endif |
| 251 | 243 | |
| 252 | 244 | // Return name |
| 253 | - return (m_names[index]); | |
| 245 | + return (names()[index]); | |
| 254 | 246 | } |
| 255 | 247 | |
| 256 | 248 | |
| ... | ... | @@ -273,12 +265,14 @@ std::string GModelSpatialRegistry::print(const GChatter& chatter) const |
| 273 | 265 | |
| 274 | 266 | // Append information |
| 275 | 267 | result.append("\n"+gammalib::parformat("Number of models")); |
| 276 | - result.append(gammalib::str(m_number)); | |
| 277 | - | |
| 278 | - // Append models | |
| 279 | - for (int i = 0; i < m_number; ++i) { | |
| 280 | - result.append("\n"+gammalib::parformat(m_names[i])); | |
| 281 | - result.append(m_models[i]->type()); | |
| 268 | + result.append(gammalib::str(size())); | |
| 269 | + | |
| 270 | + // NORMAL: Append models | |
| 271 | + if (chatter >= NORMAL) { | |
| 272 | + for (int i = 0; i < size(); ++i) { | |
| 273 | + result.append("\n"+gammalib::parformat(names()[i])); | |
| 274 | + result.append(models()[i]->type()); | |
| 275 | + } | |
| 282 | 276 | } |
| 283 | 277 | |
| 284 | 278 | } // endif: chatter was not silent | ... | ... |
src/model/GModelSpectralRegistry.cpp
| ... | ... | @@ -32,11 +32,6 @@ |
| 32 | 32 | #include "GException.hpp" |
| 33 | 33 | #include "GTools.hpp" |
| 34 | 34 | |
| 35 | -/* __ Static members _____________________________________________________ */ | |
| 36 | -int GModelSpectralRegistry::m_number(0); | |
| 37 | -std::string* GModelSpectralRegistry::m_names(0); | |
| 38 | -const GModelSpectral** GModelSpectralRegistry::m_models(0); | |
| 39 | - | |
| 40 | 35 | /* __ Method name definitions ____________________________________________ */ |
| 41 | 36 | #define G_NAME "GModelSpectralRegistry::name(int&)" |
| 42 | 37 | |
| ... | ... | @@ -65,8 +60,9 @@ GModelSpectralRegistry::GModelSpectralRegistry(void) |
| 65 | 60 | // Debug option: Show actual registry |
| 66 | 61 | #if G_DEBUG_REGISTRY |
| 67 | 62 | std::cout << "GModelSpectralRegistry(void): "; |
| 68 | - for (int i = 0; i < m_number; ++i) | |
| 69 | - std::cout << "\"" << m_names[i] << "\" "; | |
| 63 | + for (int i = 0; i < size(); ++i) { | |
| 64 | + std::cout << "\"" << names()[i] << "\" "; | |
| 65 | + } | |
| 70 | 66 | std::cout << std::endl; |
| 71 | 67 | #endif |
| 72 | 68 | |
| ... | ... | @@ -90,37 +86,33 @@ GModelSpectralRegistry::GModelSpectralRegistry(const GModelSpectral* model) |
| 90 | 86 | std::cout << "GModelSpectralRegistry(const GModelSpatial*): "; |
| 91 | 87 | std::cout << "add \"" << model->type() << "\" to registry." << std::endl; |
| 92 | 88 | #endif |
| 93 | - | |
| 94 | - // Allocate new old registry | |
| 95 | - std::string* new_names = new std::string[m_number+1]; | |
| 96 | - const GModelSpectral** new_models = new const GModelSpectral*[m_number+1]; | |
| 89 | + | |
| 90 | + // Allocate new registry | |
| 91 | + std::string* new_names = new std::string[size()+1]; | |
| 92 | + const GModelSpectral** new_models = new const GModelSpectral*[size()+1]; | |
| 97 | 93 | |
| 98 | 94 | // Save old registry |
| 99 | - for (int i = 0; i < m_number; ++i) { | |
| 100 | - new_names[i] = m_names[i]; | |
| 101 | - new_models[i] = m_models[i]; | |
| 95 | + for (int i = 0; i < size(); ++i) { | |
| 96 | + new_names[i] = names()[i]; | |
| 97 | + new_models[i] = models()[i]; | |
| 102 | 98 | } |
| 103 | 99 | |
| 104 | 100 | // Add new model to registry |
| 105 | - new_names[m_number] = model->type(); | |
| 106 | - new_models[m_number] = model; | |
| 107 | - | |
| 108 | - // Delete old registry | |
| 109 | - if (m_names != NULL) delete [] m_names; | |
| 110 | - if (m_models != NULL) delete [] m_models; | |
| 101 | + new_names[size()] = model->type(); | |
| 102 | + new_models[size()] = model; | |
| 111 | 103 | |
| 112 | 104 | // Set pointers on new registry |
| 113 | - m_names = new_names; | |
| 114 | - m_models = new_models; | |
| 105 | + names().assign(new_names); | |
| 106 | + models().assign(new_models); | |
| 115 | 107 | |
| 116 | 108 | // Increment number of models in registry |
| 117 | - m_number++; | |
| 109 | + number()++; | |
| 118 | 110 | |
| 119 | 111 | // Debug option: Show actual registry |
| 120 | 112 | #if G_DEBUG_REGISTRY |
| 121 | 113 | std::cout << "GModelSpectralRegistry(const GModelSpatial*): "; |
| 122 | - for (int i = 0; i < m_number; ++i) { | |
| 123 | - std::cout << "\"" << m_names[i] << "\" "; | |
| 114 | + for (int i = 0; i < size(); ++i) { | |
| 115 | + std::cout << "\"" << names()[i] << "\" "; | |
| 124 | 116 | } |
| 125 | 117 | std::cout << std::endl; |
| 126 | 118 | #endif |
| ... | ... | @@ -216,12 +208,12 @@ GModelSpectral* GModelSpectralRegistry::alloc(const std::string& name) const |
| 216 | 208 | GModelSpectral* model = NULL; |
| 217 | 209 | |
| 218 | 210 | // Search for model in registry |
| 219 | - for (int i = 0; i < m_number; ++i) { | |
| 220 | - if (m_names[i] == name) { | |
| 221 | - model = m_models[i]->clone(); | |
| 211 | + for (int i = 0; i < size(); ++i) { | |
| 212 | + if (names()[i] == name) { | |
| 213 | + model = models()[i]->clone(); | |
| 222 | 214 | break; |
| 223 | 215 | } |
| 224 | - } | |
| 216 | + } | |
| 225 | 217 | |
| 226 | 218 | // Return spectral model |
| 227 | 219 | return model; |
| ... | ... | @@ -247,7 +239,7 @@ std::string GModelSpectralRegistry::name(const int& index) const |
| 247 | 239 | #endif |
| 248 | 240 | |
| 249 | 241 | // Return name |
| 250 | - return (m_names[index]); | |
| 242 | + return (names()[index]); | |
| 251 | 243 | } |
| 252 | 244 | |
| 253 | 245 | |
| ... | ... | @@ -268,12 +260,14 @@ std::string GModelSpectralRegistry::print(const GChatter& chatter) const |
| 268 | 260 | // Append header |
| 269 | 261 | result.append("=== GModelSpectralRegistry ==="); |
| 270 | 262 | result.append("\n"+gammalib::parformat("Number of models")); |
| 271 | - result.append(gammalib::str(m_number)); | |
| 272 | - | |
| 273 | - // Append models | |
| 274 | - for (int i = 0; i < m_number; ++i) { | |
| 275 | - result.append("\n"+gammalib::parformat(m_names[i])); | |
| 276 | - result.append(m_models[i]->type()); | |
| 263 | + result.append(gammalib::str(size())); | |
| 264 | + | |
| 265 | + // NORMAL: Append models | |
| 266 | + if (chatter >= NORMAL) { | |
| 267 | + for (int i = 0; i < size(); ++i) { | |
| 268 | + result.append("\n"+gammalib::parformat(names()[i])); | |
| 269 | + result.append(models()[i]->type()); | |
| 270 | + } | |
| 277 | 271 | } |
| 278 | 272 | |
| 279 | 273 | } // endif: chatter was not silent | ... | ... |
src/model/GModelTemporalRegistry.cpp
| ... | ... | @@ -60,7 +60,7 @@ GModelTemporalRegistry::GModelTemporalRegistry(void) |
| 60 | 60 | // Debug option: Show actual registry |
| 61 | 61 | #if G_DEBUG_REGISTRY |
| 62 | 62 | std::cout << "GModelTemporalRegistry(void): "; |
| 63 | - for (int i = 0; i < number(); ++i) { | |
| 63 | + for (int i = 0; i < size(); ++i) { | |
| 64 | 64 | std::cout << "\"" << names()[i] << "\" "; |
| 65 | 65 | } |
| 66 | 66 | std::cout << std::endl; |
| ... | ... | @@ -88,18 +88,18 @@ GModelTemporalRegistry::GModelTemporalRegistry(const GModelTemporal* model) |
| 88 | 88 | #endif |
| 89 | 89 | |
| 90 | 90 | // Allocate new registry |
| 91 | - std::string* new_names = new std::string[number()+1]; | |
| 92 | - const GModelTemporal** new_models = new const GModelTemporal*[number()+1]; | |
| 91 | + std::string* new_names = new std::string[size()+1]; | |
| 92 | + const GModelTemporal** new_models = new const GModelTemporal*[size()+1]; | |
| 93 | 93 | |
| 94 | 94 | // Save old registry |
| 95 | - for (int i = 0; i < number(); ++i) { | |
| 95 | + for (int i = 0; i < size(); ++i) { | |
| 96 | 96 | new_names[i] = names()[i]; |
| 97 | 97 | new_models[i] = models()[i]; |
| 98 | 98 | } |
| 99 | 99 | |
| 100 | 100 | // Add new model to registry |
| 101 | - new_names[number()] = model->type(); | |
| 102 | - new_models[number()] = model; | |
| 101 | + new_names[size()] = model->type(); | |
| 102 | + new_models[size()] = model; | |
| 103 | 103 | |
| 104 | 104 | // Set pointers on new registry |
| 105 | 105 | names().assign(new_names); |
| ... | ... | @@ -111,7 +111,7 @@ GModelTemporalRegistry::GModelTemporalRegistry(const GModelTemporal* model) |
| 111 | 111 | // Debug option: Show actual registry |
| 112 | 112 | #if G_DEBUG_REGISTRY |
| 113 | 113 | std::cout << "GModelTemporalRegistry(const GModelTemporal*): "; |
| 114 | - for (int i = 0; i < number(); ++i) { | |
| 114 | + for (int i = 0; i < size(); ++i) { | |
| 115 | 115 | std::cout << "\"" << names()[i] << "\" "; |
| 116 | 116 | } |
| 117 | 117 | std::cout << std::endl; |
| ... | ... | @@ -165,7 +165,7 @@ GModelTemporalRegistry::~GModelTemporalRegistry(void) |
| 165 | 165 | * @param[in] registry Registry. |
| 166 | 166 | * @return Reference to registry. |
| 167 | 167 | ***************************************************************************/ |
| 168 | -GModelTemporalRegistry& GModelTemporalRegistry::operator= (const GModelTemporalRegistry& registry) | |
| 168 | +GModelTemporalRegistry& GModelTemporalRegistry::operator=(const GModelTemporalRegistry& registry) | |
| 169 | 169 | { |
| 170 | 170 | // Execute only if object is not identical |
| 171 | 171 | if (this != ®istry) { |
| ... | ... | @@ -208,7 +208,7 @@ GModelTemporal* GModelTemporalRegistry::alloc(const std::string& name) const |
| 208 | 208 | GModelTemporal* model = NULL; |
| 209 | 209 | |
| 210 | 210 | // Search for model in registry |
| 211 | - for (int i = 0; i < number(); ++i) { | |
| 211 | + for (int i = 0; i < size(); ++i) { | |
| 212 | 212 | if (names()[i] == name) { |
| 213 | 213 | model = models()[i]->clone(); |
| 214 | 214 | break; |
| ... | ... | @@ -262,12 +262,14 @@ std::string GModelTemporalRegistry::print(const GChatter& chatter) const |
| 262 | 262 | |
| 263 | 263 | // Append information |
| 264 | 264 | result.append("\n"+gammalib::parformat("Number of models")); |
| 265 | - result.append(gammalib::str(number())); | |
| 266 | - | |
| 267 | - // Append models | |
| 268 | - for (int i = 0; i < number(); ++i) { | |
| 269 | - result.append("\n"+gammalib::parformat(names()[i])); | |
| 270 | - result.append(models()[i]->type()); | |
| 265 | + result.append(gammalib::str(size())); | |
| 266 | + | |
| 267 | + // NORMAL: Append models | |
| 268 | + if (chatter >= NORMAL) { | |
| 269 | + for (int i = 0; i < size(); ++i) { | |
| 270 | + result.append("\n"+gammalib::parformat(names()[i])); | |
| 271 | + result.append(models()[i]->type()); | |
| 272 | + } | |
| 271 | 273 | } |
| 272 | 274 | |
| 273 | 275 | } // endif: chatter was not silent | ... | ... |
src/obs/GObservationRegistry.cpp
| 1 | 1 | /*************************************************************************** |
| 2 | 2 | * GObservationRegistry.cpp - Observation registry class * |
| 3 | 3 | * ----------------------------------------------------------------------- * |
| 4 | - * copyright (C) 2011-2012 by Juergen Knoedlseder * | |
| 4 | + * copyright (C) 2011-2013 by Juergen Knoedlseder * | |
| 5 | 5 | * ----------------------------------------------------------------------- * |
| 6 | 6 | * * |
| 7 | 7 | * This program is free software: you can redistribute it and/or modify * |
| ... | ... | @@ -29,15 +29,11 @@ |
| 29 | 29 | #include <config.h> |
| 30 | 30 | #endif |
| 31 | 31 | #include "GObservationRegistry.hpp" |
| 32 | +#include "GException.hpp" | |
| 32 | 33 | #include "GTools.hpp" |
| 33 | 34 | |
| 34 | -/* __ Static members _____________________________________________________ */ | |
| 35 | -int GObservationRegistry::m_number(0); | |
| 36 | -std::string* GObservationRegistry::m_names(0); | |
| 37 | -const GObservation** GObservationRegistry::m_obs(0); | |
| 38 | - | |
| 39 | 35 | /* __ Method name definitions ____________________________________________ */ |
| 40 | -#define G_INSTRUMENT "GObservationRegistry::instrument(int&)" | |
| 36 | +#define G_NAME "GObservationRegistry::name(int&)" | |
| 41 | 37 | |
| 42 | 38 | /* __ Macros _____________________________________________________________ */ |
| 43 | 39 | |
| ... | ... | @@ -64,8 +60,8 @@ GObservationRegistry::GObservationRegistry(void) |
| 64 | 60 | // Debug option: Show actual registry |
| 65 | 61 | #if G_DEBUG_REGISTRY |
| 66 | 62 | std::cout << "GObservationRegistry(void): "; |
| 67 | - for (int i = 0; i < m_number; ++i) { | |
| 68 | - std::cout << "\"" << m_names[i] << "\" "; | |
| 63 | + for (int i = 0; i < size(); ++i) { | |
| 64 | + std::cout << "\"" << names()[i] << "\" "; | |
| 69 | 65 | } |
| 70 | 66 | std::cout << std::endl; |
| 71 | 67 | #endif |
| ... | ... | @@ -90,37 +86,33 @@ GObservationRegistry::GObservationRegistry(const GObservation* obs) |
| 90 | 86 | std::cout << "GObservationRegistry(const GObservation*): "; |
| 91 | 87 | std::cout << "add \"" << obs->instrument() << "\" to registry." << std::endl; |
| 92 | 88 | #endif |
| 93 | - | |
| 89 | + | |
| 94 | 90 | // Allocate new registry |
| 95 | - std::string* new_names = new std::string[m_number+1]; | |
| 96 | - const GObservation** new_obs = new const GObservation*[m_number+1]; | |
| 91 | + std::string* new_names = new std::string[size()+1]; | |
| 92 | + const GObservation** new_obs = new const GObservation*[size()+1]; | |
| 97 | 93 | |
| 98 | 94 | // Save old registry |
| 99 | - for (int i = 0; i < m_number; ++i) { | |
| 100 | - new_names[i] = m_names[i]; | |
| 101 | - new_obs[i] = m_obs[i]; | |
| 95 | + for (int i = 0; i < size(); ++i) { | |
| 96 | + new_names[i] = names()[i]; | |
| 97 | + new_obs[i] = this->obs()[i]; | |
| 102 | 98 | } |
| 103 | 99 | |
| 104 | 100 | // Add new observation to registry |
| 105 | - new_names[m_number] = obs->instrument(); | |
| 106 | - new_obs[m_number] = obs; | |
| 107 | - | |
| 108 | - // Delete old registry | |
| 109 | - if (m_names != NULL) delete [] m_names; | |
| 110 | - if (m_obs != NULL) delete [] m_obs; | |
| 101 | + new_names[size()] = obs->instrument(); | |
| 102 | + new_obs[size()] = obs; | |
| 111 | 103 | |
| 112 | 104 | // Set pointers on new registry |
| 113 | - m_names = new_names; | |
| 114 | - m_obs = new_obs; | |
| 105 | + names().assign(new_names); | |
| 106 | + this->obs().assign(new_obs); | |
| 115 | 107 | |
| 116 | 108 | // Increment number of observations in registry |
| 117 | - m_number++; | |
| 109 | + number()++; | |
| 118 | 110 | |
| 119 | 111 | // Debug option: Show actual registry |
| 120 | 112 | #if G_DEBUG_REGISTRY |
| 121 | 113 | std::cout << "GObservationRegistry(const GObservation*): "; |
| 122 | - for (int i = 0; i < m_number; ++i) { | |
| 123 | - std::cout << "\"" << m_names[i] << "\" "; | |
| 114 | + for (int i = 0; i < size(); ++i) { | |
| 115 | + std::cout << "\"" << names()[i] << "\" "; | |
| 124 | 116 | } |
| 125 | 117 | std::cout << std::endl; |
| 126 | 118 | #endif |
| ... | ... | @@ -216,12 +208,12 @@ GObservation* GObservationRegistry::alloc(const std::string& name) const |
| 216 | 208 | GObservation* obs = NULL; |
| 217 | 209 | |
| 218 | 210 | // Search for observation in registry |
| 219 | - for (int i = 0; i < m_number; ++i) { | |
| 220 | - if (m_names[i] == name) { | |
| 221 | - obs = m_obs[i]->clone(); | |
| 211 | + for (int i = 0; i < size(); ++i) { | |
| 212 | + if (names()[i] == name) { | |
| 213 | + obs = this->obs()[i]->clone(); | |
| 222 | 214 | break; |
| 223 | 215 | } |
| 224 | - } | |
| 216 | + } | |
| 225 | 217 | |
| 226 | 218 | // Return observation |
| 227 | 219 | return obs; |
| ... | ... | @@ -242,12 +234,12 @@ std::string GObservationRegistry::name(const int& index) const |
| 242 | 234 | // Compile option: raise exception if index is out of range |
| 243 | 235 | #if defined(G_RANGE_CHECK) |
| 244 | 236 | if (index < 0 || index >= size()) { |
| 245 | - throw GException::out_of_range(G_INSTRUMENT, index, 0, size()-1); | |
| 237 | + throw GException::out_of_range(G_NAME, index, 0, size()-1); | |
| 246 | 238 | } |
| 247 | 239 | #endif |
| 248 | 240 | |
| 249 | 241 | // Return name |
| 250 | - return (m_names[index]); | |
| 242 | + return (names()[index]); | |
| 251 | 243 | } |
| 252 | 244 | |
| 253 | 245 | |
| ... | ... | @@ -270,13 +262,13 @@ std::string GObservationRegistry::print(const GChatter& chatter) const |
| 270 | 262 | |
| 271 | 263 | // Append information |
| 272 | 264 | result.append("\n"+gammalib::parformat("Number of observations")); |
| 273 | - result.append(gammalib::str(m_number)); | |
| 265 | + result.append(gammalib::str(size())); | |
| 274 | 266 | |
| 275 | 267 | // NORMAL: Append observations |
| 276 | 268 | if (chatter >= NORMAL) { |
| 277 | - for (int i = 0; i < m_number; ++i) { | |
| 278 | - result.append("\n"+gammalib::parformat(m_names[i])); | |
| 279 | - result.append(m_obs[i]->instrument()); | |
| 269 | + for (int i = 0; i < size(); ++i) { | |
| 270 | + result.append("\n"+gammalib::parformat(names()[i])); | |
| 271 | + result.append(this->obs()[i]->instrument()); | |
| 280 | 272 | } |
| 281 | 273 | } |
| 282 | 274 | |
| ... | ... | @@ -286,6 +278,7 @@ std::string GObservationRegistry::print(const GChatter& chatter) const |
| 286 | 278 | return result; |
| 287 | 279 | } |
| 288 | 280 | |
| 281 | + | |
| 289 | 282 | /*========================================================================== |
| 290 | 283 | = = |
| 291 | 284 | = Private methods = | ... | ... |
src/sky/GWcsRegistry.cpp
| ... | ... | @@ -29,14 +29,9 @@ |
| 29 | 29 | #include <config.h> |
| 30 | 30 | #endif |
| 31 | 31 | #include "GWcsRegistry.hpp" |
| 32 | +#include "GException.hpp" | |
| 32 | 33 | #include "GTools.hpp" |
| 33 | 34 | |
| 34 | -/* __ Static members _____________________________________________________ */ | |
| 35 | -int GWcsRegistry::m_number(0); | |
| 36 | -std::string* GWcsRegistry::m_codes(0); | |
| 37 | -std::string* GWcsRegistry::m_names(0); | |
| 38 | -const GWcs** GWcsRegistry::m_prjs(0); | |
| 39 | - | |
| 40 | 35 | /* __ Method name definitions ____________________________________________ */ |
| 41 | 36 | #define G_CODE "GWcsRegistry::code(int&)" |
| 42 | 37 | #define G_NAME "GWcsRegistry::name(int&)" |
| ... | ... | @@ -66,8 +61,9 @@ GWcsRegistry::GWcsRegistry(void) |
| 66 | 61 | // Debug option: Show actual registry |
| 67 | 62 | #if G_DEBUG_REGISTRY |
| 68 | 63 | std::cout << "GWcsRegistry(void): "; |
| 69 | - for (int i = 0; i < m_number; ++i) | |
| 70 | - std::cout << "\"" << m_codes[i] << "\" "; | |
| 64 | + for (int i = 0; i < size(); ++i) { | |
| 65 | + std::cout << "\"" << codes()[i] << "\" "; | |
| 66 | + } | |
| 71 | 67 | std::cout << std::endl; |
| 72 | 68 | #endif |
| 73 | 69 | |
| ... | ... | @@ -91,42 +87,37 @@ GWcsRegistry::GWcsRegistry(const GWcs* prj) |
| 91 | 87 | std::cout << "GWcsRegistry(const GWcs*): "; |
| 92 | 88 | std::cout << "add \"" << prj->code() << "\" to registry." << std::endl; |
| 93 | 89 | #endif |
| 94 | - | |
| 95 | - // Allocate new old registry | |
| 96 | - std::string* new_codes = new std::string[m_number+1]; | |
| 97 | - std::string* new_names = new std::string[m_number+1]; | |
| 98 | - const GWcs** new_prjs = new const GWcs*[m_number+1]; | |
| 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]; | |
| 99 | 95 | |
| 100 | 96 | // Save old registry |
| 101 | - for (int i = 0; i < m_number; ++i) { | |
| 102 | - new_codes[i] = m_codes[i]; | |
| 103 | - new_names[i] = m_names[i]; | |
| 104 | - new_prjs[i] = m_prjs[i]; | |
| 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]; | |
| 105 | 101 | } |
| 106 | 102 | |
| 107 | - // Add new model to registry | |
| 108 | - new_codes[m_number] = prj->code(); | |
| 109 | - new_names[m_number] = prj->name(); | |
| 110 | - new_prjs[m_number] = prj; | |
| 111 | - | |
| 112 | - // Delete old registry | |
| 113 | - if (m_codes != NULL) delete [] m_codes; | |
| 114 | - if (m_names != NULL) delete [] m_names; | |
| 115 | - if (m_prjs != NULL) delete [] m_prjs; | |
| 103 | + // Add new projection to registry | |
| 104 | + new_codes[size()] = prj->code(); | |
| 105 | + new_names[size()] = prj->name(); | |
| 106 | + new_projections[size()] = prj; | |
| 116 | 107 | |
| 117 | 108 | // Set pointers on new registry |
| 118 | - m_codes = new_codes; | |
| 119 | - m_names = new_names; | |
| 120 | - m_prjs = new_prjs; | |
| 109 | + codes().assign(new_codes); | |
| 110 | + names().assign(new_names); | |
| 111 | + projections().assign(new_projections); | |
| 121 | 112 | |
| 122 | 113 | // Increment number of projections in registry |
| 123 | - m_number++; | |
| 114 | + number()++; | |
| 124 | 115 | |
| 125 | 116 | // Debug option: Show actual registry |
| 126 | 117 | #if G_DEBUG_REGISTRY |
| 127 | 118 | std::cout << "GWcsRegistry(const GWcs*): "; |
| 128 | - for (int i = 0; i < m_number; ++i) { | |
| 129 | - std::cout << "\"" << m_codes[i] << "\" "; | |
| 119 | + for (int i = 0; i < size(); ++i) { | |
| 120 | + std::cout << "\"" << codes()[i] << "\" "; | |
| 130 | 121 | } |
| 131 | 122 | std::cout << std::endl; |
| 132 | 123 | #endif |
| ... | ... | @@ -179,7 +170,7 @@ GWcsRegistry::~GWcsRegistry(void) |
| 179 | 170 | * @param[in] registry Registry. |
| 180 | 171 | * @return Reference to registry. |
| 181 | 172 | ***************************************************************************/ |
| 182 | -GWcsRegistry& GWcsRegistry::operator= (const GWcsRegistry& registry) | |
| 173 | +GWcsRegistry& GWcsRegistry::operator=(const GWcsRegistry& registry) | |
| 183 | 174 | { |
| 184 | 175 | // Execute only if object is not identical |
| 185 | 176 | if (this != ®istry) { |
| ... | ... | @@ -218,18 +209,18 @@ GWcsRegistry& GWcsRegistry::operator= (const GWcsRegistry& registry) |
| 218 | 209 | GWcs* GWcsRegistry::alloc(const std::string& code) const |
| 219 | 210 | { |
| 220 | 211 | // Initialise projection |
| 221 | - GWcs* prj = NULL; | |
| 212 | + GWcs* projection = NULL; | |
| 222 | 213 | |
| 223 | 214 | // Search for projection in registry |
| 224 | - for (int i = 0; i < m_number; ++i) { | |
| 225 | - if (m_codes[i] == code) { | |
| 226 | - prj = m_prjs[i]->clone(); | |
| 215 | + for (int i = 0; i < size(); ++i) { | |
| 216 | + if (codes()[i] == code) { | |
| 217 | + projection = projections()[i]->clone(); | |
| 227 | 218 | break; |
| 228 | 219 | } |
| 229 | - } | |
| 220 | + } | |
| 230 | 221 | |
| 231 | 222 | // Return projection |
| 232 | - return prj; | |
| 223 | + return projection; | |
| 233 | 224 | } |
| 234 | 225 | |
| 235 | 226 | |
| ... | ... | @@ -252,7 +243,7 @@ std::string GWcsRegistry::code(const int& index) const |
| 252 | 243 | #endif |
| 253 | 244 | |
| 254 | 245 | // Return code |
| 255 | - return (m_codes[index]); | |
| 246 | + return (codes()[index]); | |
| 256 | 247 | } |
| 257 | 248 | |
| 258 | 249 | |
| ... | ... | @@ -275,7 +266,7 @@ std::string GWcsRegistry::name(const int& index) const |
| 275 | 266 | #endif |
| 276 | 267 | |
| 277 | 268 | // Return name |
| 278 | - return (m_names[index]); | |
| 269 | + return (names()[index]); | |
| 279 | 270 | } |
| 280 | 271 | |
| 281 | 272 | |
| ... | ... | @@ -292,11 +283,11 @@ std::string GWcsRegistry::list(void) const |
| 292 | 283 | std::string result; |
| 293 | 284 | |
| 294 | 285 | // Append projections |
| 295 | - for (int i = 0; i < m_number; ++i) { | |
| 286 | + for (int i = 0; i < size(); ++i) { | |
| 296 | 287 | if (i > 0) { |
| 297 | 288 | result.append("/"); |
| 298 | 289 | } |
| 299 | - result.append(m_codes[i]); | |
| 290 | + result.append(codes()[i]); | |
| 300 | 291 | } |
| 301 | 292 | |
| 302 | 293 | // Return result |
| ... | ... | @@ -322,12 +313,14 @@ std::string GWcsRegistry::print(const GChatter& chatter) const |
| 322 | 313 | |
| 323 | 314 | // Append information |
| 324 | 315 | result.append("\n"+gammalib::parformat("Number of projections")); |
| 325 | - result.append(gammalib::str(m_number)); | |
| 326 | - | |
| 327 | - // Append projections | |
| 328 | - for (int i = 0; i < m_number; ++i) { | |
| 329 | - result.append("\n"+gammalib::parformat(m_codes[i])); | |
| 330 | - result.append(m_names[i]); | |
| 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 | + } | |
| 331 | 324 | } |
| 332 | 325 | |
| 333 | 326 | } // endif: chatter was not silent | ... | ... |