From 90a3343672c3c745efa698db810ef7beda72926f Mon Sep 17 00:00:00 2001 From: Jürgen Knödlseder Date: Sat, 23 Jul 2016 22:12:59 +0200 Subject: [PATCH] Simplify model registry classes --- include/GModelSpatialRegistry.hpp | 18 ++++++++---------- include/GModelSpectralRegistry.hpp | 18 ++++++++---------- include/GModelTemporalRegistry.hpp | 18 ++++++++---------- inst/cta/src/GCTAModelAeffBackground.cpp | 50 ++++++++++---------------------------------------- inst/cta/src/GCTAModelCubeBackground.cpp | 50 ++++++++++---------------------------------------- inst/cta/src/GCTAModelIrfBackground.cpp | 50 ++++++++++---------------------------------------- inst/cta/src/GCTAModelRadialAcceptance.cpp | 50 ++++++++++---------------------------------------- pyext/GModelSpatialRegistry.i | 4 ++-- pyext/GModelSpectralRegistry.i | 4 ++-- pyext/GModelTemporalRegistry.i | 4 ++-- src/model/GModelSky.cpp | 66 ++++++++++++------------------------------------------------------ src/model/GModelSpatialRegistry.cpp | 65 +++++++++++++++++++++++++++++++++++++++++++++-------------------- src/model/GModelSpectralRegistry.cpp | 75 ++++++++++++++++++++++++++++++++++++++++++++++++++++----------------------- src/model/GModelTemporalRegistry.cpp | 70 +++++++++++++++++++++++++++++++++++++++++++++++++--------------------- test/test_GModel.cpp | 111 ++++++++++++++++++--------------------------------------------------------------------------------------------- 15 files changed, 246 insertions(+), 407 deletions(-) diff --git a/include/GModelSpatialRegistry.hpp b/include/GModelSpatialRegistry.hpp index e2073a1..52b0e5f 100644 --- a/include/GModelSpatialRegistry.hpp +++ b/include/GModelSpatialRegistry.hpp @@ -1,7 +1,7 @@ /*************************************************************************** * GModelSpatialRegistry.hpp - Spatial model registry class * * ----------------------------------------------------------------------- * - * copyright (C) 2011-2014 by Juergen Knoedlseder * + * copyright (C) 2011-2016 by Juergen Knoedlseder * * ----------------------------------------------------------------------- * * * * This program is free software: you can redistribute it and/or modify * @@ -30,7 +30,10 @@ /* __ Includes ___________________________________________________________ */ #include #include "GRegistry.hpp" -#include "GModelSpatial.hpp" + +/* __ Forward declarations _______________________________________________ */ +class GXmlElement; +class GModelSpatial; /***********************************************************************//** @@ -40,8 +43,8 @@ * * The registry class allows the registration of spatial models that are not * necessarily compiled into the GammaLib. It uses the static members - * m_number, m_names, and m_models which are allocated globally to keep track - * of spatial models that are available throughout all linked libraries. To + * m_number and m_models which are allocated globally to keep track of + * spatial models that are available throughout all linked libraries. To * register a spatial model it is sufficient to add * * const GModelSpatialXXX g_spatial_XXX_seed; @@ -65,7 +68,7 @@ public: // Methods std::string classname(void) const; int size(void) const; - GModelSpatial* alloc(const std::string& name) const; + GModelSpatial* alloc(const GXmlElement& xml) const; std::string name(const int& index) const; std::string print(const GChatter& chatter = NORMAL) const; @@ -85,11 +88,6 @@ private: static int m_number = 0; return m_number; }; - // Model names - static GRegistryPointer& names() { - static GRegistryPointer m_names; - return m_names; - }; // Pointer to seed models static GRegistryPointer& models() { static GRegistryPointer m_models; diff --git a/include/GModelSpectralRegistry.hpp b/include/GModelSpectralRegistry.hpp index a7e6577..0813bc7 100644 --- a/include/GModelSpectralRegistry.hpp +++ b/include/GModelSpectralRegistry.hpp @@ -1,7 +1,7 @@ /*************************************************************************** * GModelSpectralRegistry.hpp - Spectral model registry class * * ----------------------------------------------------------------------- * - * copyright (C) 2011-2014 by Juergen Knoedlseder * + * copyright (C) 2011-2016 by Juergen Knoedlseder * * ----------------------------------------------------------------------- * * * * This program is free software: you can redistribute it and/or modify * @@ -30,7 +30,10 @@ /* __ Includes ___________________________________________________________ */ #include #include "GRegistry.hpp" -#include "GModelSpectral.hpp" + +/* __ Forward declarations _______________________________________________ */ +class GXmlElement; +class GModelSpectral; /***********************************************************************//** @@ -40,8 +43,8 @@ * * The registry class allows the registration of spectral models that are not * necessarily compiled into the GammaLib. It uses the static members - * m_number, m_names, and m_models which are allocated globally to keep track - * of spectral models that are available throughout all linked libraries. To + * m_number and m_models which are allocated globally to keep track of + * spectral models that are available throughout all linked libraries. To * register a spectral model it is sufficient to add * * const GModelSpectralXXX g_spectral_XXX_seed; @@ -65,7 +68,7 @@ public: // Methods std::string classname(void) const; int size(void) const; - GModelSpectral* alloc(const std::string& name) const; + GModelSpectral* alloc(const GXmlElement& xml) const; std::string name(const int& index) const; std::string print(const GChatter& chatter = NORMAL) const; @@ -85,11 +88,6 @@ private: static int m_number = 0; return m_number; }; - // Model names - static GRegistryPointer& names() { - static GRegistryPointer m_names; - return m_names; - }; // Pointer to seed models static GRegistryPointer& models() { static GRegistryPointer m_models; diff --git a/include/GModelTemporalRegistry.hpp b/include/GModelTemporalRegistry.hpp index a1247d9..674852a 100644 --- a/include/GModelTemporalRegistry.hpp +++ b/include/GModelTemporalRegistry.hpp @@ -1,7 +1,7 @@ /*************************************************************************** * GModelTemporalRegistry.hpp - Temporal model registry class * * ----------------------------------------------------------------------- * - * copyright (C) 2011-2014 by Juergen Knoedlseder * + * copyright (C) 2011-2016 by Juergen Knoedlseder * * ----------------------------------------------------------------------- * * * * This program is free software: you can redistribute it and/or modify * @@ -30,7 +30,10 @@ /* __ Includes ___________________________________________________________ */ #include #include "GRegistry.hpp" -#include "GModelTemporal.hpp" + +/* __ Forward declarations _______________________________________________ */ +class GXmlElement; +class GModelTemporal; /***********************************************************************//** @@ -40,8 +43,8 @@ * * The registry class allows the registration of temporal models that are not * necessarily compiled into the GammaLib. It uses the static members - * m_number, m_names, and m_models which are allocated globally to keep track - * of temporal models that are available throughout all linked libraries. To + * m_number and m_models which are allocated globally to keep track of + * temporal models that are available throughout all linked libraries. To * register a temporal model it is sufficient to add * * const GModelTemporalXXX g_temporal_XXX_seed; @@ -65,7 +68,7 @@ public: // Methods std::string classname(void) const; int size(void) const; - GModelTemporal* alloc(const std::string& name) const; + GModelTemporal* alloc(const GXmlElement& xml) const; std::string name(const int& index) const; std::string print(const GChatter& chatter = NORMAL) const; @@ -85,11 +88,6 @@ private: static int m_number = 0; return m_number; }; - // Model names - static GRegistryPointer& names() { - static GRegistryPointer m_names; - return m_names; - }; // Pointer to seed models static GRegistryPointer& models() { static GRegistryPointer m_models; diff --git a/inst/cta/src/GCTAModelAeffBackground.cpp b/inst/cta/src/GCTAModelAeffBackground.cpp index 5615bd6..ccb4daa 100644 --- a/inst/cta/src/GCTAModelAeffBackground.cpp +++ b/inst/cta/src/GCTAModelAeffBackground.cpp @@ -1107,33 +1107,18 @@ bool GCTAModelAeffBackground::valid_model(void) const /***********************************************************************//** - * @brief Construct spectral model from XML element + * @brief Return pointer to spectral model from XML element * - * @param[in] spectral XML element containing spectral model information. + * @param[in] spectral XML element. + * @return Pointer to spectral model. * - * @exception GException::model_invalid_spectral - * Invalid spectral model type encountered. - * - * Returns pointer to a spectral model that is defined in an XML element. + * Returns pointer to spectral model that is defined in an XML element. ***************************************************************************/ GModelSpectral* GCTAModelAeffBackground::xml_spectral(const GXmlElement& spectral) const { - // Get spectral model type - std::string type = spectral.attribute("type"); - // Get spectral model GModelSpectralRegistry registry; - GModelSpectral* ptr = registry.alloc(type); - - // If model if valid then read model from XML file - if (ptr != NULL) { - ptr->read(spectral); - } - - // ... otherwise throw an exception - else { - throw GException::model_invalid_spectral(G_XML_SPECTRAL, type); - } + GModelSpectral* ptr = registry.alloc(spectral); // Return pointer return ptr; @@ -1141,33 +1126,18 @@ GModelSpectral* GCTAModelAeffBackground::xml_spectral(const GXmlElement& spectra /***********************************************************************//** - * @brief Construct temporal model from XML element + * @brief Return pointer to temporal model from XML element * - * @param[in] temporal XML element containing temporal model information. + * @param[in] temporal XML element. + * @return Pointer to temporal model. * - * @exception GException::model_invalid_temporal - * Invalid temporal model type encountered. - * - * Returns pointer to a temporal model that is defined in an XML element. + * Returns pointer to temporal model that is defined in an XML element. ***************************************************************************/ GModelTemporal* GCTAModelAeffBackground::xml_temporal(const GXmlElement& temporal) const { - // Get temporal model type - std::string type = temporal.attribute("type"); - // Get temporal model GModelTemporalRegistry registry; - GModelTemporal* ptr = registry.alloc(type); - - // If model if valid then read model from XML file - if (ptr != NULL) { - ptr->read(temporal); - } - - // ... otherwise throw an exception - else { - throw GException::model_invalid_temporal(G_XML_TEMPORAL, type); - } + GModelTemporal* ptr = registry.alloc(temporal); // Return pointer return ptr; diff --git a/inst/cta/src/GCTAModelCubeBackground.cpp b/inst/cta/src/GCTAModelCubeBackground.cpp index 9ffef12..d8b1715 100644 --- a/inst/cta/src/GCTAModelCubeBackground.cpp +++ b/inst/cta/src/GCTAModelCubeBackground.cpp @@ -841,33 +841,18 @@ bool GCTAModelCubeBackground::valid_model(void) const /***********************************************************************//** - * @brief Construct spectral model from XML element + * @brief Return pointer to spectral model from XML element * - * @param[in] spectral XML element containing spectral model information. + * @param[in] spectral XML element. + * @return Pointer to spectral model. * - * @exception GException::model_invalid_spectral - * Invalid spectral model type encountered. - * - * Returns pointer to a spectral model that is defined in an XML element. + * Returns pointer to spectral model that is defined in an XML element. ***************************************************************************/ GModelSpectral* GCTAModelCubeBackground::xml_spectral(const GXmlElement& spectral) const { - // Get spectral model type - std::string type = spectral.attribute("type"); - // Get spectral model GModelSpectralRegistry registry; - GModelSpectral* ptr = registry.alloc(type); - - // If model if valid then read model from XML file - if (ptr != NULL) { - ptr->read(spectral); - } - - // ... otherwise throw an exception - else { - throw GException::model_invalid_spectral(G_XML_SPECTRAL, type); - } + GModelSpectral* ptr = registry.alloc(spectral); // Return pointer return ptr; @@ -875,33 +860,18 @@ GModelSpectral* GCTAModelCubeBackground::xml_spectral(const GXmlElement& spectra /***********************************************************************//** - * @brief Construct temporal model from XML element + * @brief Return pointer to temporal model from XML element * - * @param[in] temporal XML element containing temporal model information. + * @param[in] temporal XML element. + * @return Pointer to temporal model. * - * @exception GException::model_invalid_temporal - * Invalid temporal model type encountered. - * - * Returns pointer to a temporal model that is defined in an XML element. + * Returns pointer to temporal model that is defined in an XML element. ***************************************************************************/ GModelTemporal* GCTAModelCubeBackground::xml_temporal(const GXmlElement& temporal) const { - // Get temporal model type - std::string type = temporal.attribute("type"); - // Get temporal model GModelTemporalRegistry registry; - GModelTemporal* ptr = registry.alloc(type); - - // If model if valid then read model from XML file - if (ptr != NULL) { - ptr->read(temporal); - } - - // ... otherwise throw an exception - else { - throw GException::model_invalid_temporal(G_XML_TEMPORAL, type); - } + GModelTemporal* ptr = registry.alloc(temporal); // Return pointer return ptr; diff --git a/inst/cta/src/GCTAModelIrfBackground.cpp b/inst/cta/src/GCTAModelIrfBackground.cpp index fd6737c..58c9ded 100644 --- a/inst/cta/src/GCTAModelIrfBackground.cpp +++ b/inst/cta/src/GCTAModelIrfBackground.cpp @@ -1098,33 +1098,18 @@ bool GCTAModelIrfBackground::valid_model(void) const /***********************************************************************//** - * @brief Construct spectral model from XML element + * @brief Return pointer to spectral model from XML element * - * @param[in] spectral XML element containing spectral model information. + * @param[in] spectral XML element. + * @return Pointer to spectral model. * - * @exception GException::model_invalid_spectral - * Invalid spectral model type encountered. - * - * Returns pointer to a spectral model that is defined in an XML element. + * Returns pointer to spectral model that is defined in an XML element. ***************************************************************************/ GModelSpectral* GCTAModelIrfBackground::xml_spectral(const GXmlElement& spectral) const { - // Get spectral model type - std::string type = spectral.attribute("type"); - // Get spectral model GModelSpectralRegistry registry; - GModelSpectral* ptr = registry.alloc(type); - - // If model if valid then read model from XML file - if (ptr != NULL) { - ptr->read(spectral); - } - - // ... otherwise throw an exception - else { - throw GException::model_invalid_spectral(G_XML_SPECTRAL, type); - } + GModelSpectral* ptr = registry.alloc(spectral); // Return pointer return ptr; @@ -1132,33 +1117,18 @@ GModelSpectral* GCTAModelIrfBackground::xml_spectral(const GXmlElement& spectral /***********************************************************************//** - * @brief Construct temporal model from XML element + * @brief Return pointer to temporal model from XML element * - * @param[in] temporal XML element containing temporal model information. + * @param[in] temporal XML element. + * @return Pointer to temporal model. * - * @exception GException::model_invalid_temporal - * Invalid temporal model type encountered. - * - * Returns pointer to a temporal model that is defined in an XML element. + * Returns pointer to temporal model that is defined in an XML element. ***************************************************************************/ GModelTemporal* GCTAModelIrfBackground::xml_temporal(const GXmlElement& temporal) const { - // Get temporal model type - std::string type = temporal.attribute("type"); - // Get temporal model GModelTemporalRegistry registry; - GModelTemporal* ptr = registry.alloc(type); - - // If model if valid then read model from XML file - if (ptr != NULL) { - ptr->read(temporal); - } - - // ... otherwise throw an exception - else { - throw GException::model_invalid_temporal(G_XML_TEMPORAL, type); - } + GModelTemporal* ptr = registry.alloc(temporal); // Return pointer return ptr; diff --git a/inst/cta/src/GCTAModelRadialAcceptance.cpp b/inst/cta/src/GCTAModelRadialAcceptance.cpp index e7654ba..ecea7ba 100644 --- a/inst/cta/src/GCTAModelRadialAcceptance.cpp +++ b/inst/cta/src/GCTAModelRadialAcceptance.cpp @@ -979,33 +979,18 @@ GCTAModelRadial* GCTAModelRadialAcceptance::xml_radial(const GXmlElement& radial /***********************************************************************//** - * @brief Construct spectral model from XML element + * @brief Return pointer to spectral model from XML element * - * @param[in] spectral XML element containing spectral model information. + * @param[in] spectral XML element. + * @return Pointer to spectral model. * - * @exception GException::model_invalid_spectral - * Invalid spectral model type encountered. - * - * Returns pointer to a spectral model that is defined in an XML element. + * Returns pointer to spectral model that is defined in an XML element. ***************************************************************************/ GModelSpectral* GCTAModelRadialAcceptance::xml_spectral(const GXmlElement& spectral) const { - // Get spectral model type - std::string type = spectral.attribute("type"); - // Get spectral model GModelSpectralRegistry registry; - GModelSpectral* ptr = registry.alloc(type); - - // If model if valid then read model from XML file - if (ptr != NULL) { - ptr->read(spectral); - } - - // ... otherwise throw an exception - else { - throw GException::model_invalid_spectral(G_XML_SPECTRAL, type); - } + GModelSpectral* ptr = registry.alloc(spectral); // Return pointer return ptr; @@ -1013,33 +998,18 @@ GModelSpectral* GCTAModelRadialAcceptance::xml_spectral(const GXmlElement& spect /***********************************************************************//** - * @brief Construct temporal model from XML element + * @brief Return pointer to temporal model from XML element * - * @param[in] temporal XML element containing temporal model information. + * @param[in] temporal XML element. + * @return Pointer to temporal model. * - * @exception GException::model_invalid_temporal - * Invalid temporal model type encountered. - * - * Returns pointer to a temporal model that is defined in an XML element. + * Returns pointer to temporal model that is defined in an XML element. ***************************************************************************/ GModelTemporal* GCTAModelRadialAcceptance::xml_temporal(const GXmlElement& temporal) const { - // Get temporal model type - std::string type = temporal.attribute("type"); - // Get temporal model GModelTemporalRegistry registry; - GModelTemporal* ptr = registry.alloc(type); - - // If model if valid then read model from XML file - if (ptr != NULL) { - ptr->read(temporal); - } - - // ... otherwise throw an exception - else { - throw GException::model_invalid_temporal(G_XML_TEMPORAL, type); - } + GModelTemporal* ptr = registry.alloc(temporal); // Return pointer return ptr; diff --git a/pyext/GModelSpatialRegistry.i b/pyext/GModelSpatialRegistry.i index e26bb12..0eda463 100644 --- a/pyext/GModelSpatialRegistry.i +++ b/pyext/GModelSpatialRegistry.i @@ -1,7 +1,7 @@ /*************************************************************************** * GModelSpatialRegistry.i - Spatial model registry class * * ----------------------------------------------------------------------- * - * copyright (C) 2011-2015 by Juergen Knoedlseder * + * copyright (C) 2011-2016 by Juergen Knoedlseder * * ----------------------------------------------------------------------- * * * * This program is free software: you can redistribute it and/or modify * @@ -46,7 +46,7 @@ public: // Methods std::string classname(void) const; int size(void) const; - GModelSpatial* alloc(const std::string& name) const; + GModelSpatial* alloc(const GXmlElement& xml) const; std::string name(const int& index) const; }; diff --git a/pyext/GModelSpectralRegistry.i b/pyext/GModelSpectralRegistry.i index 9187a19..a19988d 100644 --- a/pyext/GModelSpectralRegistry.i +++ b/pyext/GModelSpectralRegistry.i @@ -1,7 +1,7 @@ /*************************************************************************** * GModelSpectralRegistry.i - Spectral model registry class * * ----------------------------------------------------------------------- * - * copyright (C) 2011-2015 by Juergen Knoedlseder * + * copyright (C) 2011-2016 by Juergen Knoedlseder * * ----------------------------------------------------------------------- * * * * This program is free software: you can redistribute it and/or modify * @@ -46,7 +46,7 @@ public: // Methods std::string classname(void) const; int size(void) const; - GModelSpectral* alloc(const std::string& name) const; + GModelSpectral* alloc(const GXmlElement& xml) const; std::string name(const int& index) const; }; diff --git a/pyext/GModelTemporalRegistry.i b/pyext/GModelTemporalRegistry.i index eff9646..f60dfb0 100644 --- a/pyext/GModelTemporalRegistry.i +++ b/pyext/GModelTemporalRegistry.i @@ -1,7 +1,7 @@ /*************************************************************************** * GModelTemporalRegistry.i - Temporal model registry class * * ----------------------------------------------------------------------- * - * copyright (C) 2011-2015 by Juergen Knoedlseder * + * copyright (C) 2011-2016 by Juergen Knoedlseder * * ----------------------------------------------------------------------- * * * * This program is free software: you can redistribute it and/or modify * @@ -46,7 +46,7 @@ public: // Methods std::string classname(void) const; int size(void) const; - GModelTemporal* alloc(const std::string& name) const; + GModelTemporal* alloc(const GXmlElement& xml) const; std::string name(const int& index) const; }; diff --git a/src/model/GModelSky.cpp b/src/model/GModelSky.cpp index ac11d51..3276e60 100644 --- a/src/model/GModelSky.cpp +++ b/src/model/GModelSky.cpp @@ -1170,32 +1170,18 @@ void GModelSky::set_type(void) /***********************************************************************//** - * @brief Construct spatial model from XML element + * @brief Return pointer to spatial model from XML element * - * @param[in] spatial XML element containing spatial model information. + * @param[in] spatial XML element. * @return Pointer to spatial model. * - * @exception GException::model_invalid_spatial - * Invalid spatial model type encountered. + * Returns pointer to spatial model that is defined in an XML element. ***************************************************************************/ GModelSpatial* GModelSky::xml_spatial(const GXmlElement& spatial) const { - // Get spatial model type - std::string type = spatial.attribute("type"); - // Get spatial model GModelSpatialRegistry registry; - GModelSpatial* ptr = registry.alloc(type); - - // If model if valid then read model from XML file - if (ptr != NULL) { - ptr->read(spatial); - } - - // ... otherwise throw an exception - else { - throw GException::model_invalid_spatial(G_XML_SPATIAL, type); - } + GModelSpatial* ptr = registry.alloc(spatial); // Return pointer return ptr; @@ -1203,32 +1189,18 @@ GModelSpatial* GModelSky::xml_spatial(const GXmlElement& spatial) const /***********************************************************************//** - * @brief Construct spectral model from XML element + * @brief Return pointer to spectral model from XML element * - * @param[in] spectral XML element containing spectral model information. + * @param[in] spectral XML element. * @return Pointer to spectral model. * - * @exception GException::model_invalid_spectral - * Invalid spatial model type encountered. + * Returns pointer to spectral model that is defined in an XML element. ***************************************************************************/ GModelSpectral* GModelSky::xml_spectral(const GXmlElement& spectral) const { - // Get spectral model type - std::string type = spectral.attribute("type"); - // Get spectral model GModelSpectralRegistry registry; - GModelSpectral* ptr = registry.alloc(type); - - // If model if valid then read model from XML file - if (ptr != NULL) { - ptr->read(spectral); - } - - // ... otherwise throw an exception - else { - throw GException::model_invalid_spectral(G_XML_SPECTRAL, type); - } + GModelSpectral* ptr = registry.alloc(spectral); // Return pointer return ptr; @@ -1236,32 +1208,18 @@ GModelSpectral* GModelSky::xml_spectral(const GXmlElement& spectral) const /***********************************************************************//** - * @brief Construct temporal model from XML element + * @brief Return pointer to temporal model from XML element * - * @param[in] temporal XML element containing temporal model information. + * @param[in] temporal XML element. * @return Pointer to temporal model. * - * @exception GException::model_invalid_temporal - * Invalid spatial model type encountered. + * Returns pointer to temporal model that is defined in an XML element. ***************************************************************************/ GModelTemporal* GModelSky::xml_temporal(const GXmlElement& temporal) const { - // Get temporal model type - std::string type = temporal.attribute("type"); - // Get temporal model GModelTemporalRegistry registry; - GModelTemporal* ptr = registry.alloc(type); - - // If model if valid then read model from XML file - if (ptr != NULL) { - ptr->read(temporal); - } - - // ... otherwise throw an exception - else { - throw GException::model_invalid_temporal(G_XML_TEMPORAL, type); - } + GModelTemporal* ptr = registry.alloc(temporal); // Return pointer return ptr; diff --git a/src/model/GModelSpatialRegistry.cpp b/src/model/GModelSpatialRegistry.cpp index b48ef0f..e2298e7 100644 --- a/src/model/GModelSpatialRegistry.cpp +++ b/src/model/GModelSpatialRegistry.cpp @@ -28,11 +28,14 @@ #ifdef HAVE_CONFIG_H #include #endif +#include "GModelPar.hpp" +#include "GModelSpatial.hpp" #include "GModelSpatialRegistry.hpp" #include "GException.hpp" #include "GTools.hpp" /* __ Method name definitions ____________________________________________ */ +#define G_ALLOC "GModelSpatialRegistry::alloc(GXmlElement&)" #define G_NAME "GModelSpatialRegistry::name(int&)" /* __ Macros _____________________________________________________________ */ @@ -61,7 +64,7 @@ GModelSpatialRegistry::GModelSpatialRegistry(void) #if defined(G_DEBUG_REGISTRY) std::cout << "GModelSpatialRegistry(void): "; for (int i = 0; i < size(); ++i) { - std::cout << "\"" << names()[i] << "\" "; + std::cout << "\"" << models()[i]->type() << "\" "; } std::cout << std::endl; #endif @@ -91,21 +94,17 @@ GModelSpatialRegistry::GModelSpatialRegistry(const GModelSpatial* model) #endif // Allocate new registry - std::string* new_names = new std::string[size()+1]; const GModelSpatial** new_models = new const GModelSpatial*[size()+1]; // Save old registry for (int i = 0; i < size(); ++i) { - new_names[i] = names()[i]; new_models[i] = models()[i]; } // Add new model to registry - new_names[size()] = model->type(); new_models[size()] = model; // Set pointers on new registry - names().assign(new_names); models().assign(new_models); // Increment number of models in registry @@ -115,7 +114,7 @@ GModelSpatialRegistry::GModelSpatialRegistry(const GModelSpatial* model) #if defined(G_DEBUG_REGISTRY) std::cout << "GModelSpatialRegistry(const GModelSpatial*): "; for (int i = 0; i < size(); ++i) { - std::cout << "\"" << names()[i] << "\" "; + std::cout << "\"" << models()[i]->type() << "\" "; } std::cout << std::endl; #endif @@ -196,29 +195,47 @@ GModelSpatialRegistry& GModelSpatialRegistry::operator=(const GModelSpatialRegis ==========================================================================*/ /***********************************************************************//** - * @brief Allocate spatial model of given name + * @brief Allocate spatial model that is found in XML element * - * @param[in] name Model name. - * @return Pointer to model (NULL if name is not registered). + * @param[in] xml XML element. + * @return Pointer to spatial model. * - * Returns a pointer to a void spatial model instance of the specified - * name. If the name has not been found in the registry, a NULL pointer is - * returned. + * @exception GException::invalid_value + * No appropriate spatial model found in XML element. + * + * Returns a pointer to a spatial model instance that corresponds to the + * type found in an XML element. If no appropriate model is found the method + * will throw an exception. ***************************************************************************/ -GModelSpatial* GModelSpatialRegistry::alloc(const std::string& name) const +GModelSpatial* GModelSpatialRegistry::alloc(const GXmlElement& xml) const { // Initialise spatial model GModelSpatial* model = NULL; - // Search for model in registry + // Search for model type in registry for (int i = 0; i < size(); ++i) { - if (names()[i] == name) { + if (models()[i]->type() == xml.attribute("type")) { model = models()[i]->clone(); break; } } - // Return spatial model + // If no model has been found then throw an exception + if (model == NULL) { + std::string msg = "Spatial model of type \""+xml.attribute("type")+ + "\" not found in registry. Possible spatial model " + "types are:"; + for (int i = 0; i < size(); ++i) { + msg += " \""+models()[i]->type()+"\""; + } + msg += "."; + throw GException::invalid_value(G_ALLOC, msg); + } + + // Read model from XML element + model->read(xml); + + // Return spectral model return model; } @@ -237,12 +254,13 @@ std::string GModelSpatialRegistry::name(const int& index) const // Compile option: raise exception if index is out of range #if defined(G_RANGE_CHECK) if (index < 0 || index >= size()) { - throw GException::out_of_range(G_NAME, index, 0, size()-1); + throw GException::out_of_range(G_NAME, "Spatial model index", index, + size()); } #endif // Return name - return (names()[index]); + return (models()[index]->type()); } @@ -270,8 +288,15 @@ std::string GModelSpatialRegistry::print(const GChatter& chatter) const // NORMAL: Append models if (chatter >= NORMAL) { for (int i = 0; i < size(); ++i) { - result.append("\n"+gammalib::parformat(names()[i])); - result.append(models()[i]->type()); + result.append("\n"+gammalib::parformat(models()[i]->type())); + for (int k = 0; k < models()[i]->size(); ++k) { + if (k > 0) { + result.append(", "); + } + result.append("\""); + result.append((*(models()[i]))[k].name()); + result.append("\""); + } } } diff --git a/src/model/GModelSpectralRegistry.cpp b/src/model/GModelSpectralRegistry.cpp index 7e72ec5..373aea7 100644 --- a/src/model/GModelSpectralRegistry.cpp +++ b/src/model/GModelSpectralRegistry.cpp @@ -1,7 +1,7 @@ /*************************************************************************** * GModelSpectralRegistry.cpp - Spectral model registry class * * ----------------------------------------------------------------------- * - * copyright (C) 2011-2013 by Juergen Knoedlseder * + * copyright (C) 2011-2016 by Juergen Knoedlseder * * ----------------------------------------------------------------------- * * * * This program is free software: you can redistribute it and/or modify * @@ -28,11 +28,14 @@ #ifdef HAVE_CONFIG_H #include #endif +#include "GModelPar.hpp" +#include "GModelSpectral.hpp" #include "GModelSpectralRegistry.hpp" #include "GException.hpp" #include "GTools.hpp" /* __ Method name definitions ____________________________________________ */ +#define G_ALLOC "GModelSpectralRegistry::alloc(GXmlElement&)" #define G_NAME "GModelSpectralRegistry::name(int&)" /* __ Macros _____________________________________________________________ */ @@ -61,7 +64,7 @@ GModelSpectralRegistry::GModelSpectralRegistry(void) #if G_DEBUG_REGISTRY std::cout << "GModelSpectralRegistry(void): "; for (int i = 0; i < size(); ++i) { - std::cout << "\"" << names()[i] << "\" "; + std::cout << "\"" << models()[i]->type() << "\" "; } std::cout << std::endl; #endif @@ -75,6 +78,9 @@ GModelSpectralRegistry::GModelSpectralRegistry(void) * @brief Model constructor * * @param[in] model Model. + * + * Construct registry by adding a model to the registry. This is the standard + * constructor that is used to register a new model to GammaLib. ***************************************************************************/ GModelSpectralRegistry::GModelSpectralRegistry(const GModelSpectral* model) { @@ -88,21 +94,17 @@ GModelSpectralRegistry::GModelSpectralRegistry(const GModelSpectral* model) #endif // Allocate new registry - std::string* new_names = new std::string[size()+1]; const GModelSpectral** new_models = new const GModelSpectral*[size()+1]; // Save old registry for (int i = 0; i < size(); ++i) { - new_names[i] = names()[i]; new_models[i] = models()[i]; } // Add new model to registry - new_names[size()] = model->type(); new_models[size()] = model; // Set pointers on new registry - names().assign(new_names); models().assign(new_models); // Increment number of models in registry @@ -110,9 +112,9 @@ GModelSpectralRegistry::GModelSpectralRegistry(const GModelSpectral* model) // Debug option: Show actual registry #if G_DEBUG_REGISTRY - std::cout << "GModelSpectralRegistry(const GModelSpatial*): "; + std::cout << "GModelSpectralRegistry(const GModelSpectral*): "; for (int i = 0; i < size(); ++i) { - std::cout << "\"" << names()[i] << "\" "; + std::cout << "\"" << models()[i]->type() << "\" "; } std::cout << std::endl; #endif @@ -165,7 +167,7 @@ GModelSpectralRegistry::~GModelSpectralRegistry(void) * @param[in] registry Registry. * @return Reference to registry. ***************************************************************************/ -GModelSpectralRegistry& GModelSpectralRegistry::operator= (const GModelSpectralRegistry& registry) +GModelSpectralRegistry& GModelSpectralRegistry::operator=(const GModelSpectralRegistry& registry) { // Execute only if object is not identical if (this != ®istry) { @@ -193,28 +195,46 @@ GModelSpectralRegistry& GModelSpectralRegistry::operator= (const GModelSpectralR ==========================================================================*/ /***********************************************************************//** - * @brief Allocate spectral model of given name + * @brief Allocate spectral model that is found in XML element * - * @param[in] name Spectral model name. - * @return Pointer to spectral model (NULL if name is not registered). + * @param[in] xml XML element. + * @return Pointer to spectral model. * - * Returns a pointer to a spectral model instance of the specified name. - * If the name has not been found in the registry, a NULL pointer is - * returned. + * @exception GException::invalid_value + * No appropriate spectral model found in XML element. + * + * Returns a pointer to a spectral model instance that corresponds to the + * type and parameters found in an XML element. If no appropriate model is + * found the method will throw an exception. ***************************************************************************/ -GModelSpectral* GModelSpectralRegistry::alloc(const std::string& name) const +GModelSpectral* GModelSpectralRegistry::alloc(const GXmlElement& xml) const { // Initialise spectral model GModelSpectral* model = NULL; - // Search for model in registry + // Search for model type in registry for (int i = 0; i < size(); ++i) { - if (names()[i] == name) { + if (models()[i]->type() == xml.attribute("type")) { model = models()[i]->clone(); break; } } + // If no model has been found then throw an exception + if (model == NULL) { + std::string msg = "Spectral model of type \""+xml.attribute("type")+ + "\" not found in registry. Possible spectral model " + "types are:"; + for (int i = 0; i < size(); ++i) { + msg += " \""+models()[i]->type()+"\""; + } + msg += "."; + throw GException::invalid_value(G_ALLOC, msg); + } + + // Read model from XML element + model->read(xml); + // Return spectral model return model; } @@ -234,19 +254,20 @@ std::string GModelSpectralRegistry::name(const int& index) const // Compile option: raise exception if index is out of range #if defined(G_RANGE_CHECK) if (index < 0 || index >= size()) { - throw GException::out_of_range(G_NAME, index, 0, size()-1); + throw GException::out_of_range(G_NAME, "Spectral model index", index, + size()); } #endif // Return name - return (names()[index]); + return (models()[index]->type()); } /***********************************************************************//** * @brief Print registry information * - * @param[in] chatter Chattiness (defaults to NORMAL). + * @param[in] chatter Chattiness. * @return Registry content. ***************************************************************************/ std::string GModelSpectralRegistry::print(const GChatter& chatter) const @@ -265,8 +286,15 @@ std::string GModelSpectralRegistry::print(const GChatter& chatter) const // NORMAL: Append models if (chatter >= NORMAL) { for (int i = 0; i < size(); ++i) { - result.append("\n"+gammalib::parformat(names()[i])); - result.append(models()[i]->type()); + result.append("\n"+gammalib::parformat(models()[i]->type())); + for (int k = 0; k < models()[i]->size(); ++k) { + if (k > 0) { + result.append(", "); + } + result.append("\""); + result.append((*(models()[i]))[k].name()); + result.append("\""); + } } } @@ -276,6 +304,7 @@ std::string GModelSpectralRegistry::print(const GChatter& chatter) const return result; } + /*========================================================================== = = = Private methods = diff --git a/src/model/GModelTemporalRegistry.cpp b/src/model/GModelTemporalRegistry.cpp index 07c1720..a4b3746 100644 --- a/src/model/GModelTemporalRegistry.cpp +++ b/src/model/GModelTemporalRegistry.cpp @@ -1,7 +1,7 @@ /*************************************************************************** * GModelTemporalRegistry.cpp - Temporal model registry class * * ----------------------------------------------------------------------- * - * copyright (C) 2011-2013 by Juergen Knoedlseder * + * copyright (C) 2011-2016 by Juergen Knoedlseder * * ----------------------------------------------------------------------- * * * * This program is free software: you can redistribute it and/or modify * @@ -28,11 +28,14 @@ #ifdef HAVE_CONFIG_H #include #endif +#include "GModelPar.hpp" +#include "GModelTemporal.hpp" #include "GModelTemporalRegistry.hpp" #include "GException.hpp" #include "GTools.hpp" /* __ Method name definitions ____________________________________________ */ +#define G_ALLOC "GModelTemporalRegistry::alloc(GXmlElement&)" #define G_NAME "GModelTemporalRegistry::name(int&)" /* __ Macros _____________________________________________________________ */ @@ -61,7 +64,7 @@ GModelTemporalRegistry::GModelTemporalRegistry(void) #if G_DEBUG_REGISTRY std::cout << "GModelTemporalRegistry(void): "; for (int i = 0; i < size(); ++i) { - std::cout << "\"" << names()[i] << "\" "; + std::cout << "\"" << models()[i]->type() << "\" "; } std::cout << std::endl; #endif @@ -75,6 +78,9 @@ GModelTemporalRegistry::GModelTemporalRegistry(void) * @brief Model constructor * * @param[in] model Model. + * + * Construct registry by adding a model to the registry. This is the standard + * constructor that is used to register a new model to GammaLib. ***************************************************************************/ GModelTemporalRegistry::GModelTemporalRegistry(const GModelTemporal* model) { @@ -88,21 +94,17 @@ GModelTemporalRegistry::GModelTemporalRegistry(const GModelTemporal* model) #endif // Allocate new registry - std::string* new_names = new std::string[size()+1]; const GModelTemporal** new_models = new const GModelTemporal*[size()+1]; // Save old registry for (int i = 0; i < size(); ++i) { - new_names[i] = names()[i]; new_models[i] = models()[i]; } // Add new model to registry - new_names[size()] = model->type(); new_models[size()] = model; // Set pointers on new registry - names().assign(new_names); models().assign(new_models); // Increment number of models in registry @@ -112,7 +114,7 @@ GModelTemporalRegistry::GModelTemporalRegistry(const GModelTemporal* model) #if G_DEBUG_REGISTRY std::cout << "GModelTemporalRegistry(const GModelTemporal*): "; for (int i = 0; i < size(); ++i) { - std::cout << "\"" << names()[i] << "\" "; + std::cout << "\"" << models()[i]->type() << "\" "; } std::cout << std::endl; #endif @@ -193,28 +195,46 @@ GModelTemporalRegistry& GModelTemporalRegistry::operator=(const GModelTemporalRe ==========================================================================*/ /***********************************************************************//** - * @brief Allocate temporal model of given name + * @brief Allocate temporal model that is found in XML element + * + * @param[in] xml XML element. + * @return Pointer to temporal model. * - * @param[in] name Temporal model name. - * @return Pointer to temporal model (NULL if name is not registered). + * @exception GException::invalid_value + * No appropriate temporal model found in XML element. * - * Returns a pointer to a temporal model instance of the specified name. - * If the name has not been found in the registry, a NULL pointer is - * returned. + * Returns a pointer to a temporal model instance that corresponds to the + * type found in an XML element. If no appropriate model is found the method + * will throw an exception. ***************************************************************************/ -GModelTemporal* GModelTemporalRegistry::alloc(const std::string& name) const +GModelTemporal* GModelTemporalRegistry::alloc(const GXmlElement& xml) const { // Initialise temporal model GModelTemporal* model = NULL; - // Search for model in registry + // Search for model type in registry for (int i = 0; i < size(); ++i) { - if (names()[i] == name) { + if (models()[i]->type() == xml.attribute("type")) { model = models()[i]->clone(); break; } } + // If no model has been found then throw an exception + if (model == NULL) { + std::string msg = "Temporal model of type \""+xml.attribute("type")+ + "\" not found in registry. Possible temporal model " + "types are:"; + for (int i = 0; i < size(); ++i) { + msg += " \""+models()[i]->type()+"\""; + } + msg += "."; + throw GException::invalid_value(G_ALLOC, msg); + } + + // Read model from XML element + model->read(xml); + // Return temporal model return model; } @@ -234,19 +254,20 @@ std::string GModelTemporalRegistry::name(const int& index) const // Compile option: raise exception if index is out of range #if defined(G_RANGE_CHECK) if (index < 0 || index >= size()) { - throw GException::out_of_range(G_NAME, index, 0, size()-1); + throw GException::out_of_range(G_NAME, "Temporal model index", index, + size()); } #endif // Return name - return (names()[index]); + return (models()[index]->type()); } /***********************************************************************//** * @brief Print registry information * - * @param[in] chatter Chattiness (defaults to NORMAL). + * @param[in] chatter Chattiness. * @return Registry content. ***************************************************************************/ std::string GModelTemporalRegistry::print(const GChatter& chatter) const @@ -267,8 +288,15 @@ std::string GModelTemporalRegistry::print(const GChatter& chatter) const // NORMAL: Append models if (chatter >= NORMAL) { for (int i = 0; i < size(); ++i) { - result.append("\n"+gammalib::parformat(names()[i])); - result.append(models()[i]->type()); + result.append("\n"+gammalib::parformat(models()[i]->type())); + for (int k = 0; k < models()[i]->size(); ++k) { + if (k > 0) { + result.append(", "); + } + result.append("\""); + result.append((*(models()[i]))[k].name()); + result.append("\""); + } } } diff --git a/test/test_GModel.cpp b/test/test_GModel.cpp index d14639b..c87e047 100644 --- a/test/test_GModel.cpp +++ b/test/test_GModel.cpp @@ -2604,16 +2604,6 @@ void TestGModel::test_models(void) ***************************************************************************/ void TestGModel::test_model_registry(void) { - // Test GModelRegistry void constructor - test_try("Test GModelRegistry void constructor"); - try { - GModelRegistry registry; - test_try_success(); - } - catch (std::exception &e) { - test_try_failure(e); - } - // Test GModelRegistry allocators test_try("Test GModelRegistry model allocation"); try { @@ -2641,110 +2631,45 @@ void TestGModel::test_model_registry(void) test_try_failure(e); } - // Test GModelSpatialRegistry void constructor - test_try("Test GModelSpatialRegistry void constructor"); + // Test GModelSpatialRegistry allocator for invalid XML element + test_try("Test GModelSpatialRegistry allocator for invalid XML element"); try { + GXmlElement xml; GModelSpatialRegistry registry; - test_try_success(); + GModelSpatial* ptr = registry.alloc(xml); + test_try_failure("Invalid XML element shall throw an exception"); } - catch (std::exception &e) { - test_try_failure(e); - } - - // Test GModelSpatialRegistry allocators - test_try("Test GModelSpatialRegistry model allocation"); - try { - // Loop over all models in registry - GModelSpatialRegistry registry; - int num = registry.size(); - for (int i = 0; i < num; ++i) { - GModelSpatial* ptr = registry.alloc(registry.name(i)); - test_assert(ptr != NULL, - "Model pointer for \""+ registry.name(i)+"\" is NULL"); - if (ptr != NULL) { - - // Test model type - test_assert(ptr->type() == registry.name(i), - "Expected \""+registry.name(i)+"\" instead" - " of \""+ptr->type()+"\"."); - - // Free model - delete ptr; - } - } // endfor: looped over all models + catch (GException::invalid_value &e) { test_try_success(); } catch (std::exception &e) { test_try_failure(e); } - // Test GModelSpectralRegistry void constructor - test_try("Test GModelSpectralRegistry void constructor"); + // Test GModelSpectralRegistry allocator for invalid XML element + test_try("Test GModelSpectralRegistry allocator for invalid XML element"); try { + GXmlElement xml; GModelSpectralRegistry registry; - test_try_success(); - } - catch (std::exception &e) { - test_try_failure(e); + GModelSpectral* ptr = registry.alloc(xml); + test_try_failure("Invalid XML element shall throw an exception"); } - - // Test GModelSpectralRegistry allocators - test_try("Test GModelSpectralRegistry model allocation"); - try { - // Loop over all models in registry - GModelSpectralRegistry registry; - int num = registry.size(); - for (int i = 0; i < num; ++i) { - GModelSpectral* ptr = registry.alloc(registry.name(i)); - test_assert(ptr != NULL, "Model pointer for \""+registry.name(i)+"\" is NULL"); - if (ptr != NULL) { - - // Test model type - test_assert(ptr->type() == registry.name(i), - "Expected \""+registry.name(i)+"\" instead" - " of \""+ptr->type()+"\"."); - - // Free model - delete ptr; - } - } // endfor: looped over all models + catch (GException::invalid_value &e) { test_try_success(); } catch (std::exception &e) { test_try_failure(e); } - // Test GModelTemporalRegistry void constructor - test_try("Test GModelTemporalRegistry void constructor"); + // Test GModelTemporalRegistry allocator for invalid XML element + test_try("Test GModelTemporalRegistry allocator for invalid XML element"); try { + GXmlElement xml; GModelTemporalRegistry registry; - test_try_success(); + GModelTemporal* ptr = registry.alloc(xml); + test_try_failure("Invalid XML element shall throw an exception"); } - catch (std::exception &e) { - test_try_failure(e); - } - - // Test GModelTemporalRegistry allocators - test_try("Test GModelTemporalRegistry model allocation"); - try { - // Loop over all models in registry - GModelTemporalRegistry registry; - int num = registry.size(); - for (int i = 0; i < num; ++i) { - GModelTemporal* ptr = registry.alloc(registry.name(i)); - test_assert(ptr != NULL, "Model pointer for \""+ \ - registry.name(i)+"\" is NULL"); - if (ptr != NULL) { - - // Test model type - test_assert(ptr->type() == registry.name(i), - "Expected \""+registry.name(i)+"\" instead" - " of \""+ptr->type()+"\"."); - - // Free model - delete ptr; - } - } // endfor: looped over all models + catch (GException::invalid_value &e) { test_try_success(); } catch (std::exception &e) { -- libgit2 0.21.4