00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032
00033
00034
00035
00036
00037
00038
00039
00040
00041 #include <typeinfo>
00042 #include "LOCA_Parameter_Entry.H"
00043 #include "LOCA_GlobalData.H"
00044 #include "LOCA_ErrorCheck.H"
00045
00046 #include "Teuchos_TestForException.hpp"
00047
00048 template <class ValueType>
00049 void
00050 LOCA::Parameter::Library::setValue(const string& name,
00051 const ValueType& value) {
00052
00053 LOCA::Parameter::Entry<ValueType>& entry = getEntry<ValueType>(name);
00054
00055
00056 entry.setValue(value);
00057 }
00058
00059 template <class ValueType>
00060 ValueType
00061 LOCA::Parameter::Library::getValue(const string& name) const {
00062
00063
00064 const LOCA::Parameter::Entry<ValueType>& entry = getEntry<ValueType>(name);
00065
00066
00067 return entry.getValue();
00068 }
00069
00070 template <class ObjectType, class ValueType>
00071 bool
00072 LOCA::Parameter::Library::addParameterEntry(
00073 const string& name,
00074 ObjectType& object,
00075 ValueType ObjectType::* object_val_ptr) {
00076
00077 typedef LOCA::Parameter::DefaultFunctor<ObjectType, ValueType> FunctorType;
00078
00079
00080 FunctorType *fctr = new FunctorType(object, object_val_ptr);
00081
00082
00083 LOCA::Parameter::StandardEntry<FunctorType, ValueType> *entry =
00084 new LOCA::Parameter::StandardEntry<FunctorType, ValueType>(fctr);
00085
00086
00087 bool success = addParameterEntry(name, entry);
00088
00089
00090 if (!success) {
00091 delete fctr;
00092 delete entry;
00093 }
00094
00095 return success;
00096 }
00097
00098 template <class FunctorType, class ValueType>
00099 bool
00100 LOCA::Parameter::Library::addParameterEntry(const string& name,
00101 FunctorType* fctr) {
00102
00103
00104 LOCA::Parameter::StandardEntry<FunctorType, ValueType> *entry =
00105 new LOCA::Parameter::StandardEntry<FunctorType, ValueType>(fctr);
00106
00107
00108 bool success = addParameterEntry(name, entry);
00109
00110
00111 if (!success)
00112 delete entry;
00113
00114 return success;
00115 }
00116
00117 template <class ValueType>
00118 bool
00119 LOCA::Parameter::Library::addParameterEntry(
00120 const string& name,
00121 LOCA::Parameter::Entry<ValueType>* entry)
00122 {
00123
00124 string valueTypeString = getTypeName<ValueType>();
00125
00126
00127 ParameterMapIterator paramIterator = getEntryMapIterator(name);
00128
00129
00130 if (paramIterator == library.end()) {
00131 ValueTypeMap* paramMap = new ValueTypeMap;
00132 entry->setIsInLibrary();
00133 (*paramMap)[valueTypeString] = entry;
00134 library[name] = paramMap;
00135 }
00136
00137 else {
00138
00139
00140 ValueTypeMapIterator valueIterator = getEntryIterator(valueTypeString,
00141 paramIterator);
00142
00143
00144 if (valueIterator == (*paramIterator).second->end()) {
00145 entry->setIsInLibrary();
00146 (*paramIterator).second->insert(pair<string, AbstractEntry*>(name, entry));
00147 }
00148
00149 else {
00150 return false;
00151 }
00152
00153 }
00154
00155 return true;
00156 }
00157
00158 template <class ValueType>
00159 string
00160 LOCA::Parameter::Library::getTypeName() const {
00161 return typeid(ValueType).name();
00162 }
00163
00164 template <class ValueType>
00165 LOCA::Parameter::Entry<ValueType>&
00166 LOCA::Parameter::Library::getEntry(const string& name) {
00167
00168 const char *func = "LOCA::Parameter::Library::getEntry(): ";
00169
00170
00171 ParameterMapIterator paramIterator = getEntryMapIterator(name);
00172 TEST_FOR_EXCEPTION(paramIterator == library.end(),
00173 std::invalid_argument,
00174 func << "Parameter library has no set of entries for " <<
00175 "parameter " << name);
00176
00177
00178 string valueTypeString = getTypeName<ValueType>();
00179
00180
00181 ValueTypeMapIterator valueIterator = getEntryIterator(valueTypeString,
00182 paramIterator);
00183 TEST_FOR_EXCEPTION(valueIterator == (*paramIterator).second->end(),
00184 std::invalid_argument,
00185 func << "Parameter library has no entry for parameter " <<
00186 name << " of type " << valueTypeString);
00187
00188
00189 LOCA::Parameter::Entry<ValueType>* entry =
00190 dynamic_cast<LOCA::Parameter::Entry<ValueType>*>((*valueIterator).second);
00191 TEST_FOR_EXCEPTION(entry == NULL,
00192 std::invalid_argument,
00193 func << "Parameter entry for parameter " << name <<
00194 " and type " << valueTypeString <<
00195 " is not of the right type!");
00196
00197 return *entry;
00198 }
00199
00200 template <class ValueType>
00201 const LOCA::Parameter::Entry<ValueType>&
00202 LOCA::Parameter::Library::getEntry(const string& name) const {
00203
00204 const char *func = "LOCA::Parameter::Library::getEntry(): ";
00205
00206
00207 ParameterMapConstIterator paramIterator = getEntryMapIterator(name);
00208 TEST_FOR_EXCEPTION(paramIterator == library.end(),
00209 std::invalid_argument,
00210 func << "Parameter library has no set of entries for " <<
00211 "parameter " << name);
00212
00213
00214 string valueTypeString = getTypeName<ValueType>();
00215
00216
00217 ValueTypeMapConstIterator valueIterator = getEntryIterator(valueTypeString,
00218 paramIterator);
00219 TEST_FOR_EXCEPTION(valueIterator == (*paramIterator).second->end(),
00220 std::invalid_argument,
00221 func << "Parameter library has no entry for parameter " <<
00222 name << " of type " << valueTypeString);
00223
00224
00225 const LOCA::Parameter::Entry<ValueType>* entry =
00226 dynamic_cast<const LOCA::Parameter::Entry<ValueType>*>((*valueIterator).second);
00227 TEST_FOR_EXCEPTION(entry == NULL,
00228 std::invalid_argument,
00229 func << "Parameter entry for parameter " << name <<
00230 " and type " << valueTypeString <<
00231 " is not of the right type!");
00232
00233 return *entry;
00234 }