Commit 90a3343672c3c745efa698db810ef7beda72926f
1 parent
24794521
Simplify model registry classes
Remove the m_names members from the spatial, spectral and temporal registry classes and directly use the model type to find the appropriate model. The alloc() method now throws an exception if the model is not found. The new logic helped to reduced the code in the client classes (#1824)
Showing
15 changed files
with
246 additions
and
407 deletions
include/GModelSpatialRegistry.hpp
| 1 | 1 | /*************************************************************************** |
| 2 | 2 | * GModelSpatialRegistry.hpp - Spatial model registry class * |
| 3 | 3 | * ----------------------------------------------------------------------- * |
| 4 | - * copyright (C) 2011-2014 by Juergen Knoedlseder * | |
| 4 | + * copyright (C) 2011-2016 by Juergen Knoedlseder * | |
| 5 | 5 | * ----------------------------------------------------------------------- * |
| 6 | 6 | * * |
| 7 | 7 | * This program is free software: you can redistribute it and/or modify * |
| ... | ... | @@ -30,7 +30,10 @@ |
| 30 | 30 | /* __ Includes ___________________________________________________________ */ |
| 31 | 31 | #include <string> |
| 32 | 32 | #include "GRegistry.hpp" |
| 33 | -#include "GModelSpatial.hpp" | |
| 33 | + | |
| 34 | +/* __ Forward declarations _______________________________________________ */ | |
| 35 | +class GXmlElement; | |
| 36 | +class GModelSpatial; | |
| 34 | 37 | |
| 35 | 38 | |
| 36 | 39 | /***********************************************************************//** |
| ... | ... | @@ -40,8 +43,8 @@ |
| 40 | 43 | * |
| 41 | 44 | * The registry class allows the registration of spatial models that are not |
| 42 | 45 | * necessarily compiled into the GammaLib. It uses the static members |
| 43 | - * m_number, m_names, and m_models which are allocated globally to keep track | |
| 44 | - * of spatial models that are available throughout all linked libraries. To | |
| 46 | + * m_number and m_models which are allocated globally to keep track of | |
| 47 | + * spatial models that are available throughout all linked libraries. To | |
| 45 | 48 | * register a spatial model it is sufficient to add |
| 46 | 49 | * |
| 47 | 50 | * const GModelSpatialXXX g_spatial_XXX_seed; |
| ... | ... | @@ -65,7 +68,7 @@ public: |
| 65 | 68 | // Methods |
| 66 | 69 | std::string classname(void) const; |
| 67 | 70 | int size(void) const; |
| 68 | - GModelSpatial* alloc(const std::string& name) const; | |
| 71 | + GModelSpatial* alloc(const GXmlElement& xml) const; | |
| 69 | 72 | std::string name(const int& index) const; |
| 70 | 73 | std::string print(const GChatter& chatter = NORMAL) const; |
| 71 | 74 | |
| ... | ... | @@ -85,11 +88,6 @@ private: |
| 85 | 88 | static int m_number = 0; |
| 86 | 89 | return m_number; |
| 87 | 90 | }; |
| 88 | - // Model names | |
| 89 | - static GRegistryPointer<std::string>& names() { | |
| 90 | - static GRegistryPointer<std::string> m_names; | |
| 91 | - return m_names; | |
| 92 | - }; | |
| 93 | 91 | // Pointer to seed models |
| 94 | 92 | static GRegistryPointer<const GModelSpatial*>& models() { |
| 95 | 93 | static GRegistryPointer<const GModelSpatial*> m_models; | ... | ... |
include/GModelSpectralRegistry.hpp
| 1 | 1 | /*************************************************************************** |
| 2 | 2 | * GModelSpectralRegistry.hpp - Spectral model registry class * |
| 3 | 3 | * ----------------------------------------------------------------------- * |
| 4 | - * copyright (C) 2011-2014 by Juergen Knoedlseder * | |
| 4 | + * copyright (C) 2011-2016 by Juergen Knoedlseder * | |
| 5 | 5 | * ----------------------------------------------------------------------- * |
| 6 | 6 | * * |
| 7 | 7 | * This program is free software: you can redistribute it and/or modify * |
| ... | ... | @@ -30,7 +30,10 @@ |
| 30 | 30 | /* __ Includes ___________________________________________________________ */ |
| 31 | 31 | #include <string> |
| 32 | 32 | #include "GRegistry.hpp" |
| 33 | -#include "GModelSpectral.hpp" | |
| 33 | + | |
| 34 | +/* __ Forward declarations _______________________________________________ */ | |
| 35 | +class GXmlElement; | |
| 36 | +class GModelSpectral; | |
| 34 | 37 | |
| 35 | 38 | |
| 36 | 39 | /***********************************************************************//** |
| ... | ... | @@ -40,8 +43,8 @@ |
| 40 | 43 | * |
| 41 | 44 | * The registry class allows the registration of spectral models that are not |
| 42 | 45 | * necessarily compiled into the GammaLib. It uses the static members |
| 43 | - * m_number, m_names, and m_models which are allocated globally to keep track | |
| 44 | - * of spectral models that are available throughout all linked libraries. To | |
| 46 | + * m_number and m_models which are allocated globally to keep track of | |
| 47 | + * spectral models that are available throughout all linked libraries. To | |
| 45 | 48 | * register a spectral model it is sufficient to add |
| 46 | 49 | * |
| 47 | 50 | * const GModelSpectralXXX g_spectral_XXX_seed; |
| ... | ... | @@ -65,7 +68,7 @@ public: |
| 65 | 68 | // Methods |
| 66 | 69 | std::string classname(void) const; |
| 67 | 70 | int size(void) const; |
| 68 | - GModelSpectral* alloc(const std::string& name) const; | |
| 71 | + GModelSpectral* alloc(const GXmlElement& xml) const; | |
| 69 | 72 | std::string name(const int& index) const; |
| 70 | 73 | std::string print(const GChatter& chatter = NORMAL) const; |
| 71 | 74 | |
| ... | ... | @@ -85,11 +88,6 @@ private: |
| 85 | 88 | static int m_number = 0; |
| 86 | 89 | return m_number; |
| 87 | 90 | }; |
| 88 | - // Model names | |
| 89 | - static GRegistryPointer<std::string>& names() { | |
| 90 | - static GRegistryPointer<std::string> m_names; | |
| 91 | - return m_names; | |
| 92 | - }; | |
| 93 | 91 | // Pointer to seed models |
| 94 | 92 | static GRegistryPointer<const GModelSpectral*>& models() { |
| 95 | 93 | static GRegistryPointer<const GModelSpectral*> m_models; | ... | ... |
include/GModelTemporalRegistry.hpp
| 1 | 1 | /*************************************************************************** |
| 2 | 2 | * GModelTemporalRegistry.hpp - Temporal model registry class * |
| 3 | 3 | * ----------------------------------------------------------------------- * |
| 4 | - * copyright (C) 2011-2014 by Juergen Knoedlseder * | |
| 4 | + * copyright (C) 2011-2016 by Juergen Knoedlseder * | |
| 5 | 5 | * ----------------------------------------------------------------------- * |
| 6 | 6 | * * |
| 7 | 7 | * This program is free software: you can redistribute it and/or modify * |
| ... | ... | @@ -30,7 +30,10 @@ |
| 30 | 30 | /* __ Includes ___________________________________________________________ */ |
| 31 | 31 | #include <string> |
| 32 | 32 | #include "GRegistry.hpp" |
| 33 | -#include "GModelTemporal.hpp" | |
| 33 | + | |
| 34 | +/* __ Forward declarations _______________________________________________ */ | |
| 35 | +class GXmlElement; | |
| 36 | +class GModelTemporal; | |
| 34 | 37 | |
| 35 | 38 | |
| 36 | 39 | /***********************************************************************//** |
| ... | ... | @@ -40,8 +43,8 @@ |
| 40 | 43 | * |
| 41 | 44 | * The registry class allows the registration of temporal models that are not |
| 42 | 45 | * necessarily compiled into the GammaLib. It uses the static members |
| 43 | - * m_number, m_names, and m_models which are allocated globally to keep track | |
| 44 | - * of temporal models that are available throughout all linked libraries. To | |
| 46 | + * m_number and m_models which are allocated globally to keep track of | |
| 47 | + * temporal models that are available throughout all linked libraries. To | |
| 45 | 48 | * register a temporal model it is sufficient to add |
| 46 | 49 | * |
| 47 | 50 | * const GModelTemporalXXX g_temporal_XXX_seed; |
| ... | ... | @@ -65,7 +68,7 @@ public: |
| 65 | 68 | // Methods |
| 66 | 69 | std::string classname(void) const; |
| 67 | 70 | int size(void) const; |
| 68 | - GModelTemporal* alloc(const std::string& name) const; | |
| 71 | + GModelTemporal* alloc(const GXmlElement& xml) const; | |
| 69 | 72 | std::string name(const int& index) const; |
| 70 | 73 | std::string print(const GChatter& chatter = NORMAL) const; |
| 71 | 74 | |
| ... | ... | @@ -85,11 +88,6 @@ private: |
| 85 | 88 | static int m_number = 0; |
| 86 | 89 | return m_number; |
| 87 | 90 | }; |
| 88 | - // Model names | |
| 89 | - static GRegistryPointer<std::string>& names() { | |
| 90 | - static GRegistryPointer<std::string> m_names; | |
| 91 | - return m_names; | |
| 92 | - }; | |
| 93 | 91 | // Pointer to seed models |
| 94 | 92 | static GRegistryPointer<const GModelTemporal*>& models() { |
| 95 | 93 | static GRegistryPointer<const GModelTemporal*> m_models; | ... | ... |
inst/cta/src/GCTAModelAeffBackground.cpp
| ... | ... | @@ -1107,33 +1107,18 @@ bool GCTAModelAeffBackground::valid_model(void) const |
| 1107 | 1107 | |
| 1108 | 1108 | |
| 1109 | 1109 | /***********************************************************************//** |
| 1110 | - * @brief Construct spectral model from XML element | |
| 1110 | + * @brief Return pointer to spectral model from XML element | |
| 1111 | 1111 | * |
| 1112 | - * @param[in] spectral XML element containing spectral model information. | |
| 1112 | + * @param[in] spectral XML element. | |
| 1113 | + * @return Pointer to spectral model. | |
| 1113 | 1114 | * |
| 1114 | - * @exception GException::model_invalid_spectral | |
| 1115 | - * Invalid spectral model type encountered. | |
| 1116 | - * | |
| 1117 | - * Returns pointer to a spectral model that is defined in an XML element. | |
| 1115 | + * Returns pointer to spectral model that is defined in an XML element. | |
| 1118 | 1116 | ***************************************************************************/ |
| 1119 | 1117 | GModelSpectral* GCTAModelAeffBackground::xml_spectral(const GXmlElement& spectral) const |
| 1120 | 1118 | { |
| 1121 | - // Get spectral model type | |
| 1122 | - std::string type = spectral.attribute("type"); | |
| 1123 | - | |
| 1124 | 1119 | // Get spectral model |
| 1125 | 1120 | GModelSpectralRegistry registry; |
| 1126 | - GModelSpectral* ptr = registry.alloc(type); | |
| 1127 | - | |
| 1128 | - // If model if valid then read model from XML file | |
| 1129 | - if (ptr != NULL) { | |
| 1130 | - ptr->read(spectral); | |
| 1131 | - } | |
| 1132 | - | |
| 1133 | - // ... otherwise throw an exception | |
| 1134 | - else { | |
| 1135 | - throw GException::model_invalid_spectral(G_XML_SPECTRAL, type); | |
| 1136 | - } | |
| 1121 | + GModelSpectral* ptr = registry.alloc(spectral); | |
| 1137 | 1122 | |
| 1138 | 1123 | // Return pointer |
| 1139 | 1124 | return ptr; |
| ... | ... | @@ -1141,33 +1126,18 @@ GModelSpectral* GCTAModelAeffBackground::xml_spectral(const GXmlElement& spectra |
| 1141 | 1126 | |
| 1142 | 1127 | |
| 1143 | 1128 | /***********************************************************************//** |
| 1144 | - * @brief Construct temporal model from XML element | |
| 1129 | + * @brief Return pointer to temporal model from XML element | |
| 1145 | 1130 | * |
| 1146 | - * @param[in] temporal XML element containing temporal model information. | |
| 1131 | + * @param[in] temporal XML element. | |
| 1132 | + * @return Pointer to temporal model. | |
| 1147 | 1133 | * |
| 1148 | - * @exception GException::model_invalid_temporal | |
| 1149 | - * Invalid temporal model type encountered. | |
| 1150 | - * | |
| 1151 | - * Returns pointer to a temporal model that is defined in an XML element. | |
| 1134 | + * Returns pointer to temporal model that is defined in an XML element. | |
| 1152 | 1135 | ***************************************************************************/ |
| 1153 | 1136 | GModelTemporal* GCTAModelAeffBackground::xml_temporal(const GXmlElement& temporal) const |
| 1154 | 1137 | { |
| 1155 | - // Get temporal model type | |
| 1156 | - std::string type = temporal.attribute("type"); | |
| 1157 | - | |
| 1158 | 1138 | // Get temporal model |
| 1159 | 1139 | GModelTemporalRegistry registry; |
| 1160 | - GModelTemporal* ptr = registry.alloc(type); | |
| 1161 | - | |
| 1162 | - // If model if valid then read model from XML file | |
| 1163 | - if (ptr != NULL) { | |
| 1164 | - ptr->read(temporal); | |
| 1165 | - } | |
| 1166 | - | |
| 1167 | - // ... otherwise throw an exception | |
| 1168 | - else { | |
| 1169 | - throw GException::model_invalid_temporal(G_XML_TEMPORAL, type); | |
| 1170 | - } | |
| 1140 | + GModelTemporal* ptr = registry.alloc(temporal); | |
| 1171 | 1141 | |
| 1172 | 1142 | // Return pointer |
| 1173 | 1143 | return ptr; | ... | ... |
inst/cta/src/GCTAModelCubeBackground.cpp
| ... | ... | @@ -841,33 +841,18 @@ bool GCTAModelCubeBackground::valid_model(void) const |
| 841 | 841 | |
| 842 | 842 | |
| 843 | 843 | /***********************************************************************//** |
| 844 | - * @brief Construct spectral model from XML element | |
| 844 | + * @brief Return pointer to spectral model from XML element | |
| 845 | 845 | * |
| 846 | - * @param[in] spectral XML element containing spectral model information. | |
| 846 | + * @param[in] spectral XML element. | |
| 847 | + * @return Pointer to spectral model. | |
| 847 | 848 | * |
| 848 | - * @exception GException::model_invalid_spectral | |
| 849 | - * Invalid spectral model type encountered. | |
| 850 | - * | |
| 851 | - * Returns pointer to a spectral model that is defined in an XML element. | |
| 849 | + * Returns pointer to spectral model that is defined in an XML element. | |
| 852 | 850 | ***************************************************************************/ |
| 853 | 851 | GModelSpectral* GCTAModelCubeBackground::xml_spectral(const GXmlElement& spectral) const |
| 854 | 852 | { |
| 855 | - // Get spectral model type | |
| 856 | - std::string type = spectral.attribute("type"); | |
| 857 | - | |
| 858 | 853 | // Get spectral model |
| 859 | 854 | GModelSpectralRegistry registry; |
| 860 | - GModelSpectral* ptr = registry.alloc(type); | |
| 861 | - | |
| 862 | - // If model if valid then read model from XML file | |
| 863 | - if (ptr != NULL) { | |
| 864 | - ptr->read(spectral); | |
| 865 | - } | |
| 866 | - | |
| 867 | - // ... otherwise throw an exception | |
| 868 | - else { | |
| 869 | - throw GException::model_invalid_spectral(G_XML_SPECTRAL, type); | |
| 870 | - } | |
| 855 | + GModelSpectral* ptr = registry.alloc(spectral); | |
| 871 | 856 | |
| 872 | 857 | // Return pointer |
| 873 | 858 | return ptr; |
| ... | ... | @@ -875,33 +860,18 @@ GModelSpectral* GCTAModelCubeBackground::xml_spectral(const GXmlElement& spectra |
| 875 | 860 | |
| 876 | 861 | |
| 877 | 862 | /***********************************************************************//** |
| 878 | - * @brief Construct temporal model from XML element | |
| 863 | + * @brief Return pointer to temporal model from XML element | |
| 879 | 864 | * |
| 880 | - * @param[in] temporal XML element containing temporal model information. | |
| 865 | + * @param[in] temporal XML element. | |
| 866 | + * @return Pointer to temporal model. | |
| 881 | 867 | * |
| 882 | - * @exception GException::model_invalid_temporal | |
| 883 | - * Invalid temporal model type encountered. | |
| 884 | - * | |
| 885 | - * Returns pointer to a temporal model that is defined in an XML element. | |
| 868 | + * Returns pointer to temporal model that is defined in an XML element. | |
| 886 | 869 | ***************************************************************************/ |
| 887 | 870 | GModelTemporal* GCTAModelCubeBackground::xml_temporal(const GXmlElement& temporal) const |
| 888 | 871 | { |
| 889 | - // Get temporal model type | |
| 890 | - std::string type = temporal.attribute("type"); | |
| 891 | - | |
| 892 | 872 | // Get temporal model |
| 893 | 873 | GModelTemporalRegistry registry; |
| 894 | - GModelTemporal* ptr = registry.alloc(type); | |
| 895 | - | |
| 896 | - // If model if valid then read model from XML file | |
| 897 | - if (ptr != NULL) { | |
| 898 | - ptr->read(temporal); | |
| 899 | - } | |
| 900 | - | |
| 901 | - // ... otherwise throw an exception | |
| 902 | - else { | |
| 903 | - throw GException::model_invalid_temporal(G_XML_TEMPORAL, type); | |
| 904 | - } | |
| 874 | + GModelTemporal* ptr = registry.alloc(temporal); | |
| 905 | 875 | |
| 906 | 876 | // Return pointer |
| 907 | 877 | return ptr; | ... | ... |
inst/cta/src/GCTAModelIrfBackground.cpp
| ... | ... | @@ -1098,33 +1098,18 @@ bool GCTAModelIrfBackground::valid_model(void) const |
| 1098 | 1098 | |
| 1099 | 1099 | |
| 1100 | 1100 | /***********************************************************************//** |
| 1101 | - * @brief Construct spectral model from XML element | |
| 1101 | + * @brief Return pointer to spectral model from XML element | |
| 1102 | 1102 | * |
| 1103 | - * @param[in] spectral XML element containing spectral model information. | |
| 1103 | + * @param[in] spectral XML element. | |
| 1104 | + * @return Pointer to spectral model. | |
| 1104 | 1105 | * |
| 1105 | - * @exception GException::model_invalid_spectral | |
| 1106 | - * Invalid spectral model type encountered. | |
| 1107 | - * | |
| 1108 | - * Returns pointer to a spectral model that is defined in an XML element. | |
| 1106 | + * Returns pointer to spectral model that is defined in an XML element. | |
| 1109 | 1107 | ***************************************************************************/ |
| 1110 | 1108 | GModelSpectral* GCTAModelIrfBackground::xml_spectral(const GXmlElement& spectral) const |
| 1111 | 1109 | { |
| 1112 | - // Get spectral model type | |
| 1113 | - std::string type = spectral.attribute("type"); | |
| 1114 | - | |
| 1115 | 1110 | // Get spectral model |
| 1116 | 1111 | GModelSpectralRegistry registry; |
| 1117 | - GModelSpectral* ptr = registry.alloc(type); | |
| 1118 | - | |
| 1119 | - // If model if valid then read model from XML file | |
| 1120 | - if (ptr != NULL) { | |
| 1121 | - ptr->read(spectral); | |
| 1122 | - } | |
| 1123 | - | |
| 1124 | - // ... otherwise throw an exception | |
| 1125 | - else { | |
| 1126 | - throw GException::model_invalid_spectral(G_XML_SPECTRAL, type); | |
| 1127 | - } | |
| 1112 | + GModelSpectral* ptr = registry.alloc(spectral); | |
| 1128 | 1113 | |
| 1129 | 1114 | // Return pointer |
| 1130 | 1115 | return ptr; |
| ... | ... | @@ -1132,33 +1117,18 @@ GModelSpectral* GCTAModelIrfBackground::xml_spectral(const GXmlElement& spectral |
| 1132 | 1117 | |
| 1133 | 1118 | |
| 1134 | 1119 | /***********************************************************************//** |
| 1135 | - * @brief Construct temporal model from XML element | |
| 1120 | + * @brief Return pointer to temporal model from XML element | |
| 1136 | 1121 | * |
| 1137 | - * @param[in] temporal XML element containing temporal model information. | |
| 1122 | + * @param[in] temporal XML element. | |
| 1123 | + * @return Pointer to temporal model. | |
| 1138 | 1124 | * |
| 1139 | - * @exception GException::model_invalid_temporal | |
| 1140 | - * Invalid temporal model type encountered. | |
| 1141 | - * | |
| 1142 | - * Returns pointer to a temporal model that is defined in an XML element. | |
| 1125 | + * Returns pointer to temporal model that is defined in an XML element. | |
| 1143 | 1126 | ***************************************************************************/ |
| 1144 | 1127 | GModelTemporal* GCTAModelIrfBackground::xml_temporal(const GXmlElement& temporal) const |
| 1145 | 1128 | { |
| 1146 | - // Get temporal model type | |
| 1147 | - std::string type = temporal.attribute("type"); | |
| 1148 | - | |
| 1149 | 1129 | // Get temporal model |
| 1150 | 1130 | GModelTemporalRegistry registry; |
| 1151 | - GModelTemporal* ptr = registry.alloc(type); | |
| 1152 | - | |
| 1153 | - // If model if valid then read model from XML file | |
| 1154 | - if (ptr != NULL) { | |
| 1155 | - ptr->read(temporal); | |
| 1156 | - } | |
| 1157 | - | |
| 1158 | - // ... otherwise throw an exception | |
| 1159 | - else { | |
| 1160 | - throw GException::model_invalid_temporal(G_XML_TEMPORAL, type); | |
| 1161 | - } | |
| 1131 | + GModelTemporal* ptr = registry.alloc(temporal); | |
| 1162 | 1132 | |
| 1163 | 1133 | // Return pointer |
| 1164 | 1134 | return ptr; | ... | ... |
inst/cta/src/GCTAModelRadialAcceptance.cpp
| ... | ... | @@ -979,33 +979,18 @@ GCTAModelRadial* GCTAModelRadialAcceptance::xml_radial(const GXmlElement& radial |
| 979 | 979 | |
| 980 | 980 | |
| 981 | 981 | /***********************************************************************//** |
| 982 | - * @brief Construct spectral model from XML element | |
| 982 | + * @brief Return pointer to spectral model from XML element | |
| 983 | 983 | * |
| 984 | - * @param[in] spectral XML element containing spectral model information. | |
| 984 | + * @param[in] spectral XML element. | |
| 985 | + * @return Pointer to spectral model. | |
| 985 | 986 | * |
| 986 | - * @exception GException::model_invalid_spectral | |
| 987 | - * Invalid spectral model type encountered. | |
| 988 | - * | |
| 989 | - * Returns pointer to a spectral model that is defined in an XML element. | |
| 987 | + * Returns pointer to spectral model that is defined in an XML element. | |
| 990 | 988 | ***************************************************************************/ |
| 991 | 989 | GModelSpectral* GCTAModelRadialAcceptance::xml_spectral(const GXmlElement& spectral) const |
| 992 | 990 | { |
| 993 | - // Get spectral model type | |
| 994 | - std::string type = spectral.attribute("type"); | |
| 995 | - | |
| 996 | 991 | // Get spectral model |
| 997 | 992 | GModelSpectralRegistry registry; |
| 998 | - GModelSpectral* ptr = registry.alloc(type); | |
| 999 | - | |
| 1000 | - // If model if valid then read model from XML file | |
| 1001 | - if (ptr != NULL) { | |
| 1002 | - ptr->read(spectral); | |
| 1003 | - } | |
| 1004 | - | |
| 1005 | - // ... otherwise throw an exception | |
| 1006 | - else { | |
| 1007 | - throw GException::model_invalid_spectral(G_XML_SPECTRAL, type); | |
| 1008 | - } | |
| 993 | + GModelSpectral* ptr = registry.alloc(spectral); | |
| 1009 | 994 | |
| 1010 | 995 | // Return pointer |
| 1011 | 996 | return ptr; |
| ... | ... | @@ -1013,33 +998,18 @@ GModelSpectral* GCTAModelRadialAcceptance::xml_spectral(const GXmlElement& spect |
| 1013 | 998 | |
| 1014 | 999 | |
| 1015 | 1000 | /***********************************************************************//** |
| 1016 | - * @brief Construct temporal model from XML element | |
| 1001 | + * @brief Return pointer to temporal model from XML element | |
| 1017 | 1002 | * |
| 1018 | - * @param[in] temporal XML element containing temporal model information. | |
| 1003 | + * @param[in] temporal XML element. | |
| 1004 | + * @return Pointer to temporal model. | |
| 1019 | 1005 | * |
| 1020 | - * @exception GException::model_invalid_temporal | |
| 1021 | - * Invalid temporal model type encountered. | |
| 1022 | - * | |
| 1023 | - * Returns pointer to a temporal model that is defined in an XML element. | |
| 1006 | + * Returns pointer to temporal model that is defined in an XML element. | |
| 1024 | 1007 | ***************************************************************************/ |
| 1025 | 1008 | GModelTemporal* GCTAModelRadialAcceptance::xml_temporal(const GXmlElement& temporal) const |
| 1026 | 1009 | { |
| 1027 | - // Get temporal model type | |
| 1028 | - std::string type = temporal.attribute("type"); | |
| 1029 | - | |
| 1030 | 1010 | // Get temporal model |
| 1031 | 1011 | GModelTemporalRegistry registry; |
| 1032 | - GModelTemporal* ptr = registry.alloc(type); | |
| 1033 | - | |
| 1034 | - // If model if valid then read model from XML file | |
| 1035 | - if (ptr != NULL) { | |
| 1036 | - ptr->read(temporal); | |
| 1037 | - } | |
| 1038 | - | |
| 1039 | - // ... otherwise throw an exception | |
| 1040 | - else { | |
| 1041 | - throw GException::model_invalid_temporal(G_XML_TEMPORAL, type); | |
| 1042 | - } | |
| 1012 | + GModelTemporal* ptr = registry.alloc(temporal); | |
| 1043 | 1013 | |
| 1044 | 1014 | // Return pointer |
| 1045 | 1015 | return ptr; | ... | ... |
pyext/GModelSpatialRegistry.i
| 1 | 1 | /*************************************************************************** |
| 2 | 2 | * GModelSpatialRegistry.i - Spatial model registry class * |
| 3 | 3 | * ----------------------------------------------------------------------- * |
| 4 | - * copyright (C) 2011-2015 by Juergen Knoedlseder * | |
| 4 | + * copyright (C) 2011-2016 by Juergen Knoedlseder * | |
| 5 | 5 | * ----------------------------------------------------------------------- * |
| 6 | 6 | * * |
| 7 | 7 | * This program is free software: you can redistribute it and/or modify * |
| ... | ... | @@ -46,7 +46,7 @@ public: |
| 46 | 46 | // Methods |
| 47 | 47 | std::string classname(void) const; |
| 48 | 48 | int size(void) const; |
| 49 | - GModelSpatial* alloc(const std::string& name) const; | |
| 49 | + GModelSpatial* alloc(const GXmlElement& xml) const; | |
| 50 | 50 | std::string name(const int& index) const; |
| 51 | 51 | }; |
| 52 | 52 | ... | ... |
pyext/GModelSpectralRegistry.i
| 1 | 1 | /*************************************************************************** |
| 2 | 2 | * GModelSpectralRegistry.i - Spectral model registry class * |
| 3 | 3 | * ----------------------------------------------------------------------- * |
| 4 | - * copyright (C) 2011-2015 by Juergen Knoedlseder * | |
| 4 | + * copyright (C) 2011-2016 by Juergen Knoedlseder * | |
| 5 | 5 | * ----------------------------------------------------------------------- * |
| 6 | 6 | * * |
| 7 | 7 | * This program is free software: you can redistribute it and/or modify * |
| ... | ... | @@ -46,7 +46,7 @@ public: |
| 46 | 46 | // Methods |
| 47 | 47 | std::string classname(void) const; |
| 48 | 48 | int size(void) const; |
| 49 | - GModelSpectral* alloc(const std::string& name) const; | |
| 49 | + GModelSpectral* alloc(const GXmlElement& xml) const; | |
| 50 | 50 | std::string name(const int& index) const; |
| 51 | 51 | }; |
| 52 | 52 | ... | ... |
pyext/GModelTemporalRegistry.i
| 1 | 1 | /*************************************************************************** |
| 2 | 2 | * GModelTemporalRegistry.i - Temporal model registry class * |
| 3 | 3 | * ----------------------------------------------------------------------- * |
| 4 | - * copyright (C) 2011-2015 by Juergen Knoedlseder * | |
| 4 | + * copyright (C) 2011-2016 by Juergen Knoedlseder * | |
| 5 | 5 | * ----------------------------------------------------------------------- * |
| 6 | 6 | * * |
| 7 | 7 | * This program is free software: you can redistribute it and/or modify * |
| ... | ... | @@ -46,7 +46,7 @@ public: |
| 46 | 46 | // Methods |
| 47 | 47 | std::string classname(void) const; |
| 48 | 48 | int size(void) const; |
| 49 | - GModelTemporal* alloc(const std::string& name) const; | |
| 49 | + GModelTemporal* alloc(const GXmlElement& xml) const; | |
| 50 | 50 | std::string name(const int& index) const; |
| 51 | 51 | }; |
| 52 | 52 | ... | ... |
src/model/GModelSky.cpp
| ... | ... | @@ -1170,32 +1170,18 @@ void GModelSky::set_type(void) |
| 1170 | 1170 | |
| 1171 | 1171 | |
| 1172 | 1172 | /***********************************************************************//** |
| 1173 | - * @brief Construct spatial model from XML element | |
| 1173 | + * @brief Return pointer to spatial model from XML element | |
| 1174 | 1174 | * |
| 1175 | - * @param[in] spatial XML element containing spatial model information. | |
| 1175 | + * @param[in] spatial XML element. | |
| 1176 | 1176 | * @return Pointer to spatial model. |
| 1177 | 1177 | * |
| 1178 | - * @exception GException::model_invalid_spatial | |
| 1179 | - * Invalid spatial model type encountered. | |
| 1178 | + * Returns pointer to spatial model that is defined in an XML element. | |
| 1180 | 1179 | ***************************************************************************/ |
| 1181 | 1180 | GModelSpatial* GModelSky::xml_spatial(const GXmlElement& spatial) const |
| 1182 | 1181 | { |
| 1183 | - // Get spatial model type | |
| 1184 | - std::string type = spatial.attribute("type"); | |
| 1185 | - | |
| 1186 | 1182 | // Get spatial model |
| 1187 | 1183 | GModelSpatialRegistry registry; |
| 1188 | - GModelSpatial* ptr = registry.alloc(type); | |
| 1189 | - | |
| 1190 | - // If model if valid then read model from XML file | |
| 1191 | - if (ptr != NULL) { | |
| 1192 | - ptr->read(spatial); | |
| 1193 | - } | |
| 1194 | - | |
| 1195 | - // ... otherwise throw an exception | |
| 1196 | - else { | |
| 1197 | - throw GException::model_invalid_spatial(G_XML_SPATIAL, type); | |
| 1198 | - } | |
| 1184 | + GModelSpatial* ptr = registry.alloc(spatial); | |
| 1199 | 1185 | |
| 1200 | 1186 | // Return pointer |
| 1201 | 1187 | return ptr; |
| ... | ... | @@ -1203,32 +1189,18 @@ GModelSpatial* GModelSky::xml_spatial(const GXmlElement& spatial) const |
| 1203 | 1189 | |
| 1204 | 1190 | |
| 1205 | 1191 | /***********************************************************************//** |
| 1206 | - * @brief Construct spectral model from XML element | |
| 1192 | + * @brief Return pointer to spectral model from XML element | |
| 1207 | 1193 | * |
| 1208 | - * @param[in] spectral XML element containing spectral model information. | |
| 1194 | + * @param[in] spectral XML element. | |
| 1209 | 1195 | * @return Pointer to spectral model. |
| 1210 | 1196 | * |
| 1211 | - * @exception GException::model_invalid_spectral | |
| 1212 | - * Invalid spatial model type encountered. | |
| 1197 | + * Returns pointer to spectral model that is defined in an XML element. | |
| 1213 | 1198 | ***************************************************************************/ |
| 1214 | 1199 | GModelSpectral* GModelSky::xml_spectral(const GXmlElement& spectral) const |
| 1215 | 1200 | { |
| 1216 | - // Get spectral model type | |
| 1217 | - std::string type = spectral.attribute("type"); | |
| 1218 | - | |
| 1219 | 1201 | // Get spectral model |
| 1220 | 1202 | GModelSpectralRegistry registry; |
| 1221 | - GModelSpectral* ptr = registry.alloc(type); | |
| 1222 | - | |
| 1223 | - // If model if valid then read model from XML file | |
| 1224 | - if (ptr != NULL) { | |
| 1225 | - ptr->read(spectral); | |
| 1226 | - } | |
| 1227 | - | |
| 1228 | - // ... otherwise throw an exception | |
| 1229 | - else { | |
| 1230 | - throw GException::model_invalid_spectral(G_XML_SPECTRAL, type); | |
| 1231 | - } | |
| 1203 | + GModelSpectral* ptr = registry.alloc(spectral); | |
| 1232 | 1204 | |
| 1233 | 1205 | // Return pointer |
| 1234 | 1206 | return ptr; |
| ... | ... | @@ -1236,32 +1208,18 @@ GModelSpectral* GModelSky::xml_spectral(const GXmlElement& spectral) const |
| 1236 | 1208 | |
| 1237 | 1209 | |
| 1238 | 1210 | /***********************************************************************//** |
| 1239 | - * @brief Construct temporal model from XML element | |
| 1211 | + * @brief Return pointer to temporal model from XML element | |
| 1240 | 1212 | * |
| 1241 | - * @param[in] temporal XML element containing temporal model information. | |
| 1213 | + * @param[in] temporal XML element. | |
| 1242 | 1214 | * @return Pointer to temporal model. |
| 1243 | 1215 | * |
| 1244 | - * @exception GException::model_invalid_temporal | |
| 1245 | - * Invalid spatial model type encountered. | |
| 1216 | + * Returns pointer to temporal model that is defined in an XML element. | |
| 1246 | 1217 | ***************************************************************************/ |
| 1247 | 1218 | GModelTemporal* GModelSky::xml_temporal(const GXmlElement& temporal) const |
| 1248 | 1219 | { |
| 1249 | - // Get temporal model type | |
| 1250 | - std::string type = temporal.attribute("type"); | |
| 1251 | - | |
| 1252 | 1220 | // Get temporal model |
| 1253 | 1221 | GModelTemporalRegistry registry; |
| 1254 | - GModelTemporal* ptr = registry.alloc(type); | |
| 1255 | - | |
| 1256 | - // If model if valid then read model from XML file | |
| 1257 | - if (ptr != NULL) { | |
| 1258 | - ptr->read(temporal); | |
| 1259 | - } | |
| 1260 | - | |
| 1261 | - // ... otherwise throw an exception | |
| 1262 | - else { | |
| 1263 | - throw GException::model_invalid_temporal(G_XML_TEMPORAL, type); | |
| 1264 | - } | |
| 1222 | + GModelTemporal* ptr = registry.alloc(temporal); | |
| 1265 | 1223 | |
| 1266 | 1224 | // Return pointer |
| 1267 | 1225 | return ptr; | ... | ... |
src/model/GModelSpatialRegistry.cpp
| ... | ... | @@ -28,11 +28,14 @@ |
| 28 | 28 | #ifdef HAVE_CONFIG_H |
| 29 | 29 | #include <config.h> |
| 30 | 30 | #endif |
| 31 | +#include "GModelPar.hpp" | |
| 32 | +#include "GModelSpatial.hpp" | |
| 31 | 33 | #include "GModelSpatialRegistry.hpp" |
| 32 | 34 | #include "GException.hpp" |
| 33 | 35 | #include "GTools.hpp" |
| 34 | 36 | |
| 35 | 37 | /* __ Method name definitions ____________________________________________ */ |
| 38 | +#define G_ALLOC "GModelSpatialRegistry::alloc(GXmlElement&)" | |
| 36 | 39 | #define G_NAME "GModelSpatialRegistry::name(int&)" |
| 37 | 40 | |
| 38 | 41 | /* __ Macros _____________________________________________________________ */ |
| ... | ... | @@ -61,7 +64,7 @@ GModelSpatialRegistry::GModelSpatialRegistry(void) |
| 61 | 64 | #if defined(G_DEBUG_REGISTRY) |
| 62 | 65 | std::cout << "GModelSpatialRegistry(void): "; |
| 63 | 66 | for (int i = 0; i < size(); ++i) { |
| 64 | - std::cout << "\"" << names()[i] << "\" "; | |
| 67 | + std::cout << "\"" << models()[i]->type() << "\" "; | |
| 65 | 68 | } |
| 66 | 69 | std::cout << std::endl; |
| 67 | 70 | #endif |
| ... | ... | @@ -91,21 +94,17 @@ GModelSpatialRegistry::GModelSpatialRegistry(const GModelSpatial* model) |
| 91 | 94 | #endif |
| 92 | 95 | |
| 93 | 96 | // Allocate new registry |
| 94 | - std::string* new_names = new std::string[size()+1]; | |
| 95 | 97 | const GModelSpatial** new_models = new const GModelSpatial*[size()+1]; |
| 96 | 98 | |
| 97 | 99 | // Save old registry |
| 98 | 100 | for (int i = 0; i < size(); ++i) { |
| 99 | - new_names[i] = names()[i]; | |
| 100 | 101 | new_models[i] = models()[i]; |
| 101 | 102 | } |
| 102 | 103 | |
| 103 | 104 | // Add new model to registry |
| 104 | - new_names[size()] = model->type(); | |
| 105 | 105 | new_models[size()] = model; |
| 106 | 106 | |
| 107 | 107 | // Set pointers on new registry |
| 108 | - names().assign(new_names); | |
| 109 | 108 | models().assign(new_models); |
| 110 | 109 | |
| 111 | 110 | // Increment number of models in registry |
| ... | ... | @@ -115,7 +114,7 @@ GModelSpatialRegistry::GModelSpatialRegistry(const GModelSpatial* model) |
| 115 | 114 | #if defined(G_DEBUG_REGISTRY) |
| 116 | 115 | std::cout << "GModelSpatialRegistry(const GModelSpatial*): "; |
| 117 | 116 | for (int i = 0; i < size(); ++i) { |
| 118 | - std::cout << "\"" << names()[i] << "\" "; | |
| 117 | + std::cout << "\"" << models()[i]->type() << "\" "; | |
| 119 | 118 | } |
| 120 | 119 | std::cout << std::endl; |
| 121 | 120 | #endif |
| ... | ... | @@ -196,29 +195,47 @@ GModelSpatialRegistry& GModelSpatialRegistry::operator=(const GModelSpatialRegis |
| 196 | 195 | ==========================================================================*/ |
| 197 | 196 | |
| 198 | 197 | /***********************************************************************//** |
| 199 | - * @brief Allocate spatial model of given name | |
| 198 | + * @brief Allocate spatial model that is found in XML element | |
| 200 | 199 | * |
| 201 | - * @param[in] name Model name. | |
| 202 | - * @return Pointer to model (NULL if name is not registered). | |
| 200 | + * @param[in] xml XML element. | |
| 201 | + * @return Pointer to spatial model. | |
| 203 | 202 | * |
| 204 | - * Returns a pointer to a void spatial model instance of the specified | |
| 205 | - * name. If the name has not been found in the registry, a NULL pointer is | |
| 206 | - * returned. | |
| 203 | + * @exception GException::invalid_value | |
| 204 | + * No appropriate spatial model found in XML element. | |
| 205 | + * | |
| 206 | + * Returns a pointer to a spatial model instance that corresponds to the | |
| 207 | + * type found in an XML element. If no appropriate model is found the method | |
| 208 | + * will throw an exception. | |
| 207 | 209 | ***************************************************************************/ |
| 208 | -GModelSpatial* GModelSpatialRegistry::alloc(const std::string& name) const | |
| 210 | +GModelSpatial* GModelSpatialRegistry::alloc(const GXmlElement& xml) const | |
| 209 | 211 | { |
| 210 | 212 | // Initialise spatial model |
| 211 | 213 | GModelSpatial* model = NULL; |
| 212 | 214 | |
| 213 | - // Search for model in registry | |
| 215 | + // Search for model type in registry | |
| 214 | 216 | for (int i = 0; i < size(); ++i) { |
| 215 | - if (names()[i] == name) { | |
| 217 | + if (models()[i]->type() == xml.attribute("type")) { | |
| 216 | 218 | model = models()[i]->clone(); |
| 217 | 219 | break; |
| 218 | 220 | } |
| 219 | 221 | } |
| 220 | 222 | |
| 221 | - // Return spatial model | |
| 223 | + // If no model has been found then throw an exception | |
| 224 | + if (model == NULL) { | |
| 225 | + std::string msg = "Spatial model of type \""+xml.attribute("type")+ | |
| 226 | + "\" not found in registry. Possible spatial model " | |
| 227 | + "types are:"; | |
| 228 | + for (int i = 0; i < size(); ++i) { | |
| 229 | + msg += " \""+models()[i]->type()+"\""; | |
| 230 | + } | |
| 231 | + msg += "."; | |
| 232 | + throw GException::invalid_value(G_ALLOC, msg); | |
| 233 | + } | |
| 234 | + | |
| 235 | + // Read model from XML element | |
| 236 | + model->read(xml); | |
| 237 | + | |
| 238 | + // Return spectral model | |
| 222 | 239 | return model; |
| 223 | 240 | } |
| 224 | 241 | |
| ... | ... | @@ -237,12 +254,13 @@ std::string GModelSpatialRegistry::name(const int& index) const |
| 237 | 254 | // Compile option: raise exception if index is out of range |
| 238 | 255 | #if defined(G_RANGE_CHECK) |
| 239 | 256 | if (index < 0 || index >= size()) { |
| 240 | - throw GException::out_of_range(G_NAME, index, 0, size()-1); | |
| 257 | + throw GException::out_of_range(G_NAME, "Spatial model index", index, | |
| 258 | + size()); | |
| 241 | 259 | } |
| 242 | 260 | #endif |
| 243 | 261 | |
| 244 | 262 | // Return name |
| 245 | - return (names()[index]); | |
| 263 | + return (models()[index]->type()); | |
| 246 | 264 | } |
| 247 | 265 | |
| 248 | 266 | |
| ... | ... | @@ -270,8 +288,15 @@ std::string GModelSpatialRegistry::print(const GChatter& chatter) const |
| 270 | 288 | // NORMAL: Append models |
| 271 | 289 | if (chatter >= NORMAL) { |
| 272 | 290 | for (int i = 0; i < size(); ++i) { |
| 273 | - result.append("\n"+gammalib::parformat(names()[i])); | |
| 274 | - result.append(models()[i]->type()); | |
| 291 | + result.append("\n"+gammalib::parformat(models()[i]->type())); | |
| 292 | + for (int k = 0; k < models()[i]->size(); ++k) { | |
| 293 | + if (k > 0) { | |
| 294 | + result.append(", "); | |
| 295 | + } | |
| 296 | + result.append("\""); | |
| 297 | + result.append((*(models()[i]))[k].name()); | |
| 298 | + result.append("\""); | |
| 299 | + } | |
| 275 | 300 | } |
| 276 | 301 | } |
| 277 | 302 | ... | ... |
src/model/GModelSpectralRegistry.cpp
| 1 | 1 | /*************************************************************************** |
| 2 | 2 | * GModelSpectralRegistry.cpp - Spectral model registry class * |
| 3 | 3 | * ----------------------------------------------------------------------- * |
| 4 | - * copyright (C) 2011-2013 by Juergen Knoedlseder * | |
| 4 | + * copyright (C) 2011-2016 by Juergen Knoedlseder * | |
| 5 | 5 | * ----------------------------------------------------------------------- * |
| 6 | 6 | * * |
| 7 | 7 | * This program is free software: you can redistribute it and/or modify * |
| ... | ... | @@ -28,11 +28,14 @@ |
| 28 | 28 | #ifdef HAVE_CONFIG_H |
| 29 | 29 | #include <config.h> |
| 30 | 30 | #endif |
| 31 | +#include "GModelPar.hpp" | |
| 32 | +#include "GModelSpectral.hpp" | |
| 31 | 33 | #include "GModelSpectralRegistry.hpp" |
| 32 | 34 | #include "GException.hpp" |
| 33 | 35 | #include "GTools.hpp" |
| 34 | 36 | |
| 35 | 37 | /* __ Method name definitions ____________________________________________ */ |
| 38 | +#define G_ALLOC "GModelSpectralRegistry::alloc(GXmlElement&)" | |
| 36 | 39 | #define G_NAME "GModelSpectralRegistry::name(int&)" |
| 37 | 40 | |
| 38 | 41 | /* __ Macros _____________________________________________________________ */ |
| ... | ... | @@ -61,7 +64,7 @@ GModelSpectralRegistry::GModelSpectralRegistry(void) |
| 61 | 64 | #if G_DEBUG_REGISTRY |
| 62 | 65 | std::cout << "GModelSpectralRegistry(void): "; |
| 63 | 66 | for (int i = 0; i < size(); ++i) { |
| 64 | - std::cout << "\"" << names()[i] << "\" "; | |
| 67 | + std::cout << "\"" << models()[i]->type() << "\" "; | |
| 65 | 68 | } |
| 66 | 69 | std::cout << std::endl; |
| 67 | 70 | #endif |
| ... | ... | @@ -75,6 +78,9 @@ GModelSpectralRegistry::GModelSpectralRegistry(void) |
| 75 | 78 | * @brief Model constructor |
| 76 | 79 | * |
| 77 | 80 | * @param[in] model Model. |
| 81 | + * | |
| 82 | + * Construct registry by adding a model to the registry. This is the standard | |
| 83 | + * constructor that is used to register a new model to GammaLib. | |
| 78 | 84 | ***************************************************************************/ |
| 79 | 85 | GModelSpectralRegistry::GModelSpectralRegistry(const GModelSpectral* model) |
| 80 | 86 | { |
| ... | ... | @@ -88,21 +94,17 @@ GModelSpectralRegistry::GModelSpectralRegistry(const GModelSpectral* model) |
| 88 | 94 | #endif |
| 89 | 95 | |
| 90 | 96 | // Allocate new registry |
| 91 | - std::string* new_names = new std::string[size()+1]; | |
| 92 | 97 | const GModelSpectral** new_models = new const GModelSpectral*[size()+1]; |
| 93 | 98 | |
| 94 | 99 | // Save old registry |
| 95 | 100 | for (int i = 0; i < size(); ++i) { |
| 96 | - new_names[i] = names()[i]; | |
| 97 | 101 | new_models[i] = models()[i]; |
| 98 | 102 | } |
| 99 | 103 | |
| 100 | 104 | // Add new model to registry |
| 101 | - new_names[size()] = model->type(); | |
| 102 | 105 | new_models[size()] = model; |
| 103 | 106 | |
| 104 | 107 | // Set pointers on new registry |
| 105 | - names().assign(new_names); | |
| 106 | 108 | models().assign(new_models); |
| 107 | 109 | |
| 108 | 110 | // Increment number of models in registry |
| ... | ... | @@ -110,9 +112,9 @@ GModelSpectralRegistry::GModelSpectralRegistry(const GModelSpectral* model) |
| 110 | 112 | |
| 111 | 113 | // Debug option: Show actual registry |
| 112 | 114 | #if G_DEBUG_REGISTRY |
| 113 | - std::cout << "GModelSpectralRegistry(const GModelSpatial*): "; | |
| 115 | + std::cout << "GModelSpectralRegistry(const GModelSpectral*): "; | |
| 114 | 116 | for (int i = 0; i < size(); ++i) { |
| 115 | - std::cout << "\"" << names()[i] << "\" "; | |
| 117 | + std::cout << "\"" << models()[i]->type() << "\" "; | |
| 116 | 118 | } |
| 117 | 119 | std::cout << std::endl; |
| 118 | 120 | #endif |
| ... | ... | @@ -165,7 +167,7 @@ GModelSpectralRegistry::~GModelSpectralRegistry(void) |
| 165 | 167 | * @param[in] registry Registry. |
| 166 | 168 | * @return Reference to registry. |
| 167 | 169 | ***************************************************************************/ |
| 168 | -GModelSpectralRegistry& GModelSpectralRegistry::operator= (const GModelSpectralRegistry& registry) | |
| 170 | +GModelSpectralRegistry& GModelSpectralRegistry::operator=(const GModelSpectralRegistry& registry) | |
| 169 | 171 | { |
| 170 | 172 | // Execute only if object is not identical |
| 171 | 173 | if (this != ®istry) { |
| ... | ... | @@ -193,28 +195,46 @@ GModelSpectralRegistry& GModelSpectralRegistry::operator= (const GModelSpectralR |
| 193 | 195 | ==========================================================================*/ |
| 194 | 196 | |
| 195 | 197 | /***********************************************************************//** |
| 196 | - * @brief Allocate spectral model of given name | |
| 198 | + * @brief Allocate spectral model that is found in XML element | |
| 197 | 199 | * |
| 198 | - * @param[in] name Spectral model name. | |
| 199 | - * @return Pointer to spectral model (NULL if name is not registered). | |
| 200 | + * @param[in] xml XML element. | |
| 201 | + * @return Pointer to spectral model. | |
| 200 | 202 | * |
| 201 | - * Returns a pointer to a spectral model instance of the specified name. | |
| 202 | - * If the name has not been found in the registry, a NULL pointer is | |
| 203 | - * returned. | |
| 203 | + * @exception GException::invalid_value | |
| 204 | + * No appropriate spectral model found in XML element. | |
| 205 | + * | |
| 206 | + * Returns a pointer to a spectral model instance that corresponds to the | |
| 207 | + * type and parameters found in an XML element. If no appropriate model is | |
| 208 | + * found the method will throw an exception. | |
| 204 | 209 | ***************************************************************************/ |
| 205 | -GModelSpectral* GModelSpectralRegistry::alloc(const std::string& name) const | |
| 210 | +GModelSpectral* GModelSpectralRegistry::alloc(const GXmlElement& xml) const | |
| 206 | 211 | { |
| 207 | 212 | // Initialise spectral model |
| 208 | 213 | GModelSpectral* model = NULL; |
| 209 | 214 | |
| 210 | - // Search for model in registry | |
| 215 | + // Search for model type in registry | |
| 211 | 216 | for (int i = 0; i < size(); ++i) { |
| 212 | - if (names()[i] == name) { | |
| 217 | + if (models()[i]->type() == xml.attribute("type")) { | |
| 213 | 218 | model = models()[i]->clone(); |
| 214 | 219 | break; |
| 215 | 220 | } |
| 216 | 221 | } |
| 217 | 222 | |
| 223 | + // If no model has been found then throw an exception | |
| 224 | + if (model == NULL) { | |
| 225 | + std::string msg = "Spectral model of type \""+xml.attribute("type")+ | |
| 226 | + "\" not found in registry. Possible spectral model " | |
| 227 | + "types are:"; | |
| 228 | + for (int i = 0; i < size(); ++i) { | |
| 229 | + msg += " \""+models()[i]->type()+"\""; | |
| 230 | + } | |
| 231 | + msg += "."; | |
| 232 | + throw GException::invalid_value(G_ALLOC, msg); | |
| 233 | + } | |
| 234 | + | |
| 235 | + // Read model from XML element | |
| 236 | + model->read(xml); | |
| 237 | + | |
| 218 | 238 | // Return spectral model |
| 219 | 239 | return model; |
| 220 | 240 | } |
| ... | ... | @@ -234,19 +254,20 @@ std::string GModelSpectralRegistry::name(const int& index) const |
| 234 | 254 | // Compile option: raise exception if index is out of range |
| 235 | 255 | #if defined(G_RANGE_CHECK) |
| 236 | 256 | if (index < 0 || index >= size()) { |
| 237 | - throw GException::out_of_range(G_NAME, index, 0, size()-1); | |
| 257 | + throw GException::out_of_range(G_NAME, "Spectral model index", index, | |
| 258 | + size()); | |
| 238 | 259 | } |
| 239 | 260 | #endif |
| 240 | 261 | |
| 241 | 262 | // Return name |
| 242 | - return (names()[index]); | |
| 263 | + return (models()[index]->type()); | |
| 243 | 264 | } |
| 244 | 265 | |
| 245 | 266 | |
| 246 | 267 | /***********************************************************************//** |
| 247 | 268 | * @brief Print registry information |
| 248 | 269 | * |
| 249 | - * @param[in] chatter Chattiness (defaults to NORMAL). | |
| 270 | + * @param[in] chatter Chattiness. | |
| 250 | 271 | * @return Registry content. |
| 251 | 272 | ***************************************************************************/ |
| 252 | 273 | std::string GModelSpectralRegistry::print(const GChatter& chatter) const |
| ... | ... | @@ -265,8 +286,15 @@ std::string GModelSpectralRegistry::print(const GChatter& chatter) const |
| 265 | 286 | // NORMAL: Append models |
| 266 | 287 | if (chatter >= NORMAL) { |
| 267 | 288 | for (int i = 0; i < size(); ++i) { |
| 268 | - result.append("\n"+gammalib::parformat(names()[i])); | |
| 269 | - result.append(models()[i]->type()); | |
| 289 | + result.append("\n"+gammalib::parformat(models()[i]->type())); | |
| 290 | + for (int k = 0; k < models()[i]->size(); ++k) { | |
| 291 | + if (k > 0) { | |
| 292 | + result.append(", "); | |
| 293 | + } | |
| 294 | + result.append("\""); | |
| 295 | + result.append((*(models()[i]))[k].name()); | |
| 296 | + result.append("\""); | |
| 297 | + } | |
| 270 | 298 | } |
| 271 | 299 | } |
| 272 | 300 | |
| ... | ... | @@ -276,6 +304,7 @@ std::string GModelSpectralRegistry::print(const GChatter& chatter) const |
| 276 | 304 | return result; |
| 277 | 305 | } |
| 278 | 306 | |
| 307 | + | |
| 279 | 308 | /*========================================================================== |
| 280 | 309 | = = |
| 281 | 310 | = Private methods = | ... | ... |
src/model/GModelTemporalRegistry.cpp
| 1 | 1 | /*************************************************************************** |
| 2 | 2 | * GModelTemporalRegistry.cpp - Temporal model registry class * |
| 3 | 3 | * ----------------------------------------------------------------------- * |
| 4 | - * copyright (C) 2011-2013 by Juergen Knoedlseder * | |
| 4 | + * copyright (C) 2011-2016 by Juergen Knoedlseder * | |
| 5 | 5 | * ----------------------------------------------------------------------- * |
| 6 | 6 | * * |
| 7 | 7 | * This program is free software: you can redistribute it and/or modify * |
| ... | ... | @@ -28,11 +28,14 @@ |
| 28 | 28 | #ifdef HAVE_CONFIG_H |
| 29 | 29 | #include <config.h> |
| 30 | 30 | #endif |
| 31 | +#include "GModelPar.hpp" | |
| 32 | +#include "GModelTemporal.hpp" | |
| 31 | 33 | #include "GModelTemporalRegistry.hpp" |
| 32 | 34 | #include "GException.hpp" |
| 33 | 35 | #include "GTools.hpp" |
| 34 | 36 | |
| 35 | 37 | /* __ Method name definitions ____________________________________________ */ |
| 38 | +#define G_ALLOC "GModelTemporalRegistry::alloc(GXmlElement&)" | |
| 36 | 39 | #define G_NAME "GModelTemporalRegistry::name(int&)" |
| 37 | 40 | |
| 38 | 41 | /* __ Macros _____________________________________________________________ */ |
| ... | ... | @@ -61,7 +64,7 @@ GModelTemporalRegistry::GModelTemporalRegistry(void) |
| 61 | 64 | #if G_DEBUG_REGISTRY |
| 62 | 65 | std::cout << "GModelTemporalRegistry(void): "; |
| 63 | 66 | for (int i = 0; i < size(); ++i) { |
| 64 | - std::cout << "\"" << names()[i] << "\" "; | |
| 67 | + std::cout << "\"" << models()[i]->type() << "\" "; | |
| 65 | 68 | } |
| 66 | 69 | std::cout << std::endl; |
| 67 | 70 | #endif |
| ... | ... | @@ -75,6 +78,9 @@ GModelTemporalRegistry::GModelTemporalRegistry(void) |
| 75 | 78 | * @brief Model constructor |
| 76 | 79 | * |
| 77 | 80 | * @param[in] model Model. |
| 81 | + * | |
| 82 | + * Construct registry by adding a model to the registry. This is the standard | |
| 83 | + * constructor that is used to register a new model to GammaLib. | |
| 78 | 84 | ***************************************************************************/ |
| 79 | 85 | GModelTemporalRegistry::GModelTemporalRegistry(const GModelTemporal* model) |
| 80 | 86 | { |
| ... | ... | @@ -88,21 +94,17 @@ GModelTemporalRegistry::GModelTemporalRegistry(const GModelTemporal* model) |
| 88 | 94 | #endif |
| 89 | 95 | |
| 90 | 96 | // Allocate new registry |
| 91 | - std::string* new_names = new std::string[size()+1]; | |
| 92 | 97 | const GModelTemporal** new_models = new const GModelTemporal*[size()+1]; |
| 93 | 98 | |
| 94 | 99 | // Save old registry |
| 95 | 100 | for (int i = 0; i < size(); ++i) { |
| 96 | - new_names[i] = names()[i]; | |
| 97 | 101 | new_models[i] = models()[i]; |
| 98 | 102 | } |
| 99 | 103 | |
| 100 | 104 | // Add new model to registry |
| 101 | - new_names[size()] = model->type(); | |
| 102 | 105 | new_models[size()] = model; |
| 103 | 106 | |
| 104 | 107 | // Set pointers on new registry |
| 105 | - names().assign(new_names); | |
| 106 | 108 | models().assign(new_models); |
| 107 | 109 | |
| 108 | 110 | // Increment number of models in registry |
| ... | ... | @@ -112,7 +114,7 @@ GModelTemporalRegistry::GModelTemporalRegistry(const GModelTemporal* model) |
| 112 | 114 | #if G_DEBUG_REGISTRY |
| 113 | 115 | std::cout << "GModelTemporalRegistry(const GModelTemporal*): "; |
| 114 | 116 | for (int i = 0; i < size(); ++i) { |
| 115 | - std::cout << "\"" << names()[i] << "\" "; | |
| 117 | + std::cout << "\"" << models()[i]->type() << "\" "; | |
| 116 | 118 | } |
| 117 | 119 | std::cout << std::endl; |
| 118 | 120 | #endif |
| ... | ... | @@ -193,28 +195,46 @@ GModelTemporalRegistry& GModelTemporalRegistry::operator=(const GModelTemporalRe |
| 193 | 195 | ==========================================================================*/ |
| 194 | 196 | |
| 195 | 197 | /***********************************************************************//** |
| 196 | - * @brief Allocate temporal model of given name | |
| 198 | + * @brief Allocate temporal model that is found in XML element | |
| 199 | + * | |
| 200 | + * @param[in] xml XML element. | |
| 201 | + * @return Pointer to temporal model. | |
| 197 | 202 | * |
| 198 | - * @param[in] name Temporal model name. | |
| 199 | - * @return Pointer to temporal model (NULL if name is not registered). | |
| 203 | + * @exception GException::invalid_value | |
| 204 | + * No appropriate temporal model found in XML element. | |
| 200 | 205 | * |
| 201 | - * Returns a pointer to a temporal model instance of the specified name. | |
| 202 | - * If the name has not been found in the registry, a NULL pointer is | |
| 203 | - * returned. | |
| 206 | + * Returns a pointer to a temporal model instance that corresponds to the | |
| 207 | + * type found in an XML element. If no appropriate model is found the method | |
| 208 | + * will throw an exception. | |
| 204 | 209 | ***************************************************************************/ |
| 205 | -GModelTemporal* GModelTemporalRegistry::alloc(const std::string& name) const | |
| 210 | +GModelTemporal* GModelTemporalRegistry::alloc(const GXmlElement& xml) const | |
| 206 | 211 | { |
| 207 | 212 | // Initialise temporal model |
| 208 | 213 | GModelTemporal* model = NULL; |
| 209 | 214 | |
| 210 | - // Search for model in registry | |
| 215 | + // Search for model type in registry | |
| 211 | 216 | for (int i = 0; i < size(); ++i) { |
| 212 | - if (names()[i] == name) { | |
| 217 | + if (models()[i]->type() == xml.attribute("type")) { | |
| 213 | 218 | model = models()[i]->clone(); |
| 214 | 219 | break; |
| 215 | 220 | } |
| 216 | 221 | } |
| 217 | 222 | |
| 223 | + // If no model has been found then throw an exception | |
| 224 | + if (model == NULL) { | |
| 225 | + std::string msg = "Temporal model of type \""+xml.attribute("type")+ | |
| 226 | + "\" not found in registry. Possible temporal model " | |
| 227 | + "types are:"; | |
| 228 | + for (int i = 0; i < size(); ++i) { | |
| 229 | + msg += " \""+models()[i]->type()+"\""; | |
| 230 | + } | |
| 231 | + msg += "."; | |
| 232 | + throw GException::invalid_value(G_ALLOC, msg); | |
| 233 | + } | |
| 234 | + | |
| 235 | + // Read model from XML element | |
| 236 | + model->read(xml); | |
| 237 | + | |
| 218 | 238 | // Return temporal model |
| 219 | 239 | return model; |
| 220 | 240 | } |
| ... | ... | @@ -234,19 +254,20 @@ std::string GModelTemporalRegistry::name(const int& index) const |
| 234 | 254 | // Compile option: raise exception if index is out of range |
| 235 | 255 | #if defined(G_RANGE_CHECK) |
| 236 | 256 | if (index < 0 || index >= size()) { |
| 237 | - throw GException::out_of_range(G_NAME, index, 0, size()-1); | |
| 257 | + throw GException::out_of_range(G_NAME, "Temporal model index", index, | |
| 258 | + size()); | |
| 238 | 259 | } |
| 239 | 260 | #endif |
| 240 | 261 | |
| 241 | 262 | // Return name |
| 242 | - return (names()[index]); | |
| 263 | + return (models()[index]->type()); | |
| 243 | 264 | } |
| 244 | 265 | |
| 245 | 266 | |
| 246 | 267 | /***********************************************************************//** |
| 247 | 268 | * @brief Print registry information |
| 248 | 269 | * |
| 249 | - * @param[in] chatter Chattiness (defaults to NORMAL). | |
| 270 | + * @param[in] chatter Chattiness. | |
| 250 | 271 | * @return Registry content. |
| 251 | 272 | ***************************************************************************/ |
| 252 | 273 | std::string GModelTemporalRegistry::print(const GChatter& chatter) const |
| ... | ... | @@ -267,8 +288,15 @@ std::string GModelTemporalRegistry::print(const GChatter& chatter) const |
| 267 | 288 | // NORMAL: Append models |
| 268 | 289 | if (chatter >= NORMAL) { |
| 269 | 290 | for (int i = 0; i < size(); ++i) { |
| 270 | - result.append("\n"+gammalib::parformat(names()[i])); | |
| 271 | - result.append(models()[i]->type()); | |
| 291 | + result.append("\n"+gammalib::parformat(models()[i]->type())); | |
| 292 | + for (int k = 0; k < models()[i]->size(); ++k) { | |
| 293 | + if (k > 0) { | |
| 294 | + result.append(", "); | |
| 295 | + } | |
| 296 | + result.append("\""); | |
| 297 | + result.append((*(models()[i]))[k].name()); | |
| 298 | + result.append("\""); | |
| 299 | + } | |
| 272 | 300 | } |
| 273 | 301 | } |
| 274 | 302 | ... | ... |
test/test_GModel.cpp
| ... | ... | @@ -2604,16 +2604,6 @@ void TestGModel::test_models(void) |
| 2604 | 2604 | ***************************************************************************/ |
| 2605 | 2605 | void TestGModel::test_model_registry(void) |
| 2606 | 2606 | { |
| 2607 | - // Test GModelRegistry void constructor | |
| 2608 | - test_try("Test GModelRegistry void constructor"); | |
| 2609 | - try { | |
| 2610 | - GModelRegistry registry; | |
| 2611 | - test_try_success(); | |
| 2612 | - } | |
| 2613 | - catch (std::exception &e) { | |
| 2614 | - test_try_failure(e); | |
| 2615 | - } | |
| 2616 | - | |
| 2617 | 2607 | // Test GModelRegistry allocators |
| 2618 | 2608 | test_try("Test GModelRegistry model allocation"); |
| 2619 | 2609 | try { |
| ... | ... | @@ -2641,110 +2631,45 @@ void TestGModel::test_model_registry(void) |
| 2641 | 2631 | test_try_failure(e); |
| 2642 | 2632 | } |
| 2643 | 2633 | |
| 2644 | - // Test GModelSpatialRegistry void constructor | |
| 2645 | - test_try("Test GModelSpatialRegistry void constructor"); | |
| 2634 | + // Test GModelSpatialRegistry allocator for invalid XML element | |
| 2635 | + test_try("Test GModelSpatialRegistry allocator for invalid XML element"); | |
| 2646 | 2636 | try { |
| 2637 | + GXmlElement xml; | |
| 2647 | 2638 | GModelSpatialRegistry registry; |
| 2648 | - test_try_success(); | |
| 2639 | + GModelSpatial* ptr = registry.alloc(xml); | |
| 2640 | + test_try_failure("Invalid XML element shall throw an exception"); | |
| 2649 | 2641 | } |
| 2650 | - catch (std::exception &e) { | |
| 2651 | - test_try_failure(e); | |
| 2652 | - } | |
| 2653 | - | |
| 2654 | - // Test GModelSpatialRegistry allocators | |
| 2655 | - test_try("Test GModelSpatialRegistry model allocation"); | |
| 2656 | - try { | |
| 2657 | - // Loop over all models in registry | |
| 2658 | - GModelSpatialRegistry registry; | |
| 2659 | - int num = registry.size(); | |
| 2660 | - for (int i = 0; i < num; ++i) { | |
| 2661 | - GModelSpatial* ptr = registry.alloc(registry.name(i)); | |
| 2662 | - test_assert(ptr != NULL, | |
| 2663 | - "Model pointer for \""+ registry.name(i)+"\" is NULL"); | |
| 2664 | - if (ptr != NULL) { | |
| 2665 | - | |
| 2666 | - // Test model type | |
| 2667 | - test_assert(ptr->type() == registry.name(i), | |
| 2668 | - "Expected \""+registry.name(i)+"\" instead" | |
| 2669 | - " of \""+ptr->type()+"\"."); | |
| 2670 | - | |
| 2671 | - // Free model | |
| 2672 | - delete ptr; | |
| 2673 | - } | |
| 2674 | - } // endfor: looped over all models | |
| 2642 | + catch (GException::invalid_value &e) { | |
| 2675 | 2643 | test_try_success(); |
| 2676 | 2644 | } |
| 2677 | 2645 | catch (std::exception &e) { |
| 2678 | 2646 | test_try_failure(e); |
| 2679 | 2647 | } |
| 2680 | 2648 | |
| 2681 | - // Test GModelSpectralRegistry void constructor | |
| 2682 | - test_try("Test GModelSpectralRegistry void constructor"); | |
| 2649 | + // Test GModelSpectralRegistry allocator for invalid XML element | |
| 2650 | + test_try("Test GModelSpectralRegistry allocator for invalid XML element"); | |
| 2683 | 2651 | try { |
| 2652 | + GXmlElement xml; | |
| 2684 | 2653 | GModelSpectralRegistry registry; |
| 2685 | - test_try_success(); | |
| 2686 | - } | |
| 2687 | - catch (std::exception &e) { | |
| 2688 | - test_try_failure(e); | |
| 2654 | + GModelSpectral* ptr = registry.alloc(xml); | |
| 2655 | + test_try_failure("Invalid XML element shall throw an exception"); | |
| 2689 | 2656 | } |
| 2690 | - | |
| 2691 | - // Test GModelSpectralRegistry allocators | |
| 2692 | - test_try("Test GModelSpectralRegistry model allocation"); | |
| 2693 | - try { | |
| 2694 | - // Loop over all models in registry | |
| 2695 | - GModelSpectralRegistry registry; | |
| 2696 | - int num = registry.size(); | |
| 2697 | - for (int i = 0; i < num; ++i) { | |
| 2698 | - GModelSpectral* ptr = registry.alloc(registry.name(i)); | |
| 2699 | - test_assert(ptr != NULL, "Model pointer for \""+registry.name(i)+"\" is NULL"); | |
| 2700 | - if (ptr != NULL) { | |
| 2701 | - | |
| 2702 | - // Test model type | |
| 2703 | - test_assert(ptr->type() == registry.name(i), | |
| 2704 | - "Expected \""+registry.name(i)+"\" instead" | |
| 2705 | - " of \""+ptr->type()+"\"."); | |
| 2706 | - | |
| 2707 | - // Free model | |
| 2708 | - delete ptr; | |
| 2709 | - } | |
| 2710 | - } // endfor: looped over all models | |
| 2657 | + catch (GException::invalid_value &e) { | |
| 2711 | 2658 | test_try_success(); |
| 2712 | 2659 | } |
| 2713 | 2660 | catch (std::exception &e) { |
| 2714 | 2661 | test_try_failure(e); |
| 2715 | 2662 | } |
| 2716 | 2663 | |
| 2717 | - // Test GModelTemporalRegistry void constructor | |
| 2718 | - test_try("Test GModelTemporalRegistry void constructor"); | |
| 2664 | + // Test GModelTemporalRegistry allocator for invalid XML element | |
| 2665 | + test_try("Test GModelTemporalRegistry allocator for invalid XML element"); | |
| 2719 | 2666 | try { |
| 2667 | + GXmlElement xml; | |
| 2720 | 2668 | GModelTemporalRegistry registry; |
| 2721 | - test_try_success(); | |
| 2669 | + GModelTemporal* ptr = registry.alloc(xml); | |
| 2670 | + test_try_failure("Invalid XML element shall throw an exception"); | |
| 2722 | 2671 | } |
| 2723 | - catch (std::exception &e) { | |
| 2724 | - test_try_failure(e); | |
| 2725 | - } | |
| 2726 | - | |
| 2727 | - // Test GModelTemporalRegistry allocators | |
| 2728 | - test_try("Test GModelTemporalRegistry model allocation"); | |
| 2729 | - try { | |
| 2730 | - // Loop over all models in registry | |
| 2731 | - GModelTemporalRegistry registry; | |
| 2732 | - int num = registry.size(); | |
| 2733 | - for (int i = 0; i < num; ++i) { | |
| 2734 | - GModelTemporal* ptr = registry.alloc(registry.name(i)); | |
| 2735 | - test_assert(ptr != NULL, "Model pointer for \""+ \ | |
| 2736 | - registry.name(i)+"\" is NULL"); | |
| 2737 | - if (ptr != NULL) { | |
| 2738 | - | |
| 2739 | - // Test model type | |
| 2740 | - test_assert(ptr->type() == registry.name(i), | |
| 2741 | - "Expected \""+registry.name(i)+"\" instead" | |
| 2742 | - " of \""+ptr->type()+"\"."); | |
| 2743 | - | |
| 2744 | - // Free model | |
| 2745 | - delete ptr; | |
| 2746 | - } | |
| 2747 | - } // endfor: looped over all models | |
| 2672 | + catch (GException::invalid_value &e) { | |
| 2748 | 2673 | test_try_success(); |
| 2749 | 2674 | } |
| 2750 | 2675 | catch (std::exception &e) { | ... | ... |