Back to home page

EIC code displayed by LXR

 
 

    


Warning, /include/ThePEG/Interface/ParMap.tcc is written in an unsupported language. File is not indexed.

0001 // -*- C++ -*-
0002 //
0003 // ParMap.tcc is a part of ThePEG - Toolkit for HEP Event Generation
0004 // Copyright (C) 1999-2019 Leif Lonnblad
0005 //
0006 // ThePEG is licenced under version 3 of the GPL, see COPYING for details.
0007 // Please respect the MCnet academic guidelines, see GUIDELINES for details.
0008 //
0009 //
0010 // This is the implementation of the non-inlined templated member
0011 // functions of the ParMap and ParMapTBase classes.
0012 //
0013 
0014 #include "ParMap.xh"
0015 
0016 namespace ThePEG {
0017 
0018 template <typename Type>
0019 string ParMapTBase<Type>::type() const {
0020   if ( std::numeric_limits<Type>::is_integer ) return "Vi";
0021   if ( typeid(Type) == typeid(string) ) return "Vs";
0022   return "Vf";
0023 }
0024 
0025 template <typename Type>
0026 string ParMapTBase<Type>::doxygenType() const {
0027   ostringstream os;
0028   if ( size() <= 0 ) os << "Varying size ";
0029   else os << "Fixed size (" << size() << ") ";
0030   os << "map of ";
0031   string lim = "";
0032   if ( !limited() ) lim = " unlimited";
0033   if ( std::numeric_limits<Type>::is_integer ) os << lim << "integer ";
0034   else if ( typeid(Type) == typeid(string) ) os << "string ";
0035   else os << lim;
0036   os << "parameters";
0037   return os.str();
0038 }
0039 
0040 template <typename Type>
0041 string ParMapTBase<Type>::fullDescription(const InterfacedBase & ib) const {
0042   return ParMapBase::fullDescription(ib) + def() + "\n";
0043 }
0044 
0045 template <typename Type>
0046 void ParMapTBase<Type>::setDef(InterfacedBase & i, int place) const
0047   {
0048   if ( place >= 0 ) tset(i, tdef(i, place), place);
0049   int sz = get(i).size();
0050   for ( int j = 0; j < sz; ++j ) tset(i, tdef(i, j), j);
0051 }
0052 
0053 template <typename Type>
0054 inline void ParMapTBase<Type>::
0055 setImpl(InterfacedBase & i, string newValue, int place, StandardT) 
0056   const {
0057   istringstream is(newValue);
0058   if ( unit() > Type() ) {
0059     double t;
0060     is >> t;
0061     tset(i, Type(t*unit()), place);
0062   } else {
0063     Type t = Type();
0064     is >> t;
0065     tset(i, t, place);
0066   }
0067 }
0068 
0069 template<>
0070 inline void ParMapTBase<bool>::
0071 setImpl(InterfacedBase & i, string newValue, int place, StandardT) 
0072   const {
0073   istringstream is(newValue);
0074   bool t;
0075   is >> t;
0076   tset(i, t, place);
0077 }
0078   
0079 template <typename Type>
0080 inline void ParMapTBase<Type>::
0081 setImpl(InterfacedBase & i, string newValue, int place, DimensionT) 
0082   const {
0083   istringstream is(newValue);
0084   double t;
0085   is >> t;
0086   tset(i, t*unit(), place);
0087 }
0088 
0089 template <typename T>
0090 void ParMapTBase<T>::
0091 set(InterfacedBase & i, string newValue, int place) const 
0092   {
0093   setImpl(i, newValue, place, typename TypeTraits<T>::DimType());
0094 }
0095 
0096 template <typename Type>
0097 inline void ParMapTBase<Type>::
0098 insertImpl(InterfacedBase & i, string newValue, int place, StandardT) 
0099   const {
0100   istringstream is(newValue);
0101   if ( unit() > Type() ) {
0102     double t;
0103     is >> t;
0104     tinsert(i, Type(t*unit()), place);
0105   } else {
0106     Type t = Type();
0107     is >> t;
0108     tinsert(i, t, place);
0109   }
0110 }
0111 
0112 template <>
0113 inline void ParMapTBase<bool>::
0114 insertImpl(InterfacedBase & i, string newValue, int place, StandardT) 
0115   const {
0116   istringstream is(newValue);
0117   bool t;
0118   is >> t;
0119   tinsert(i, t, place);
0120 }
0121 
0122 template <typename Type>
0123 inline void ParMapTBase<Type>::
0124 insertImpl(InterfacedBase & i, string newValue, int place, DimensionT) 
0125   const {
0126   istringstream is(newValue);
0127   double t;
0128   is >> t;
0129   tinsert(i, t*unit(), place);
0130 }
0131 
0132 template <typename T>
0133 void ParMapTBase<T>::
0134 insert(InterfacedBase & i, string newValue, int place) const 
0135   {
0136   insertImpl(i, newValue, place, typename TypeTraits<T>::DimType());
0137 }
0138 
0139 template <typename Type>
0140 typename ParMapTBase<Type>::StringMap ParMapTBase<Type>::
0141 get(const InterfacedBase & i) const {
0142   TypeMap tres = tget(i);
0143   StringMap res;
0144   for ( typename TypeMap::iterator it = tres.begin();
0145         it != tres.end(); ++it ) {
0146     ostringstream os;
0147     putUnit(os, it->second);
0148     res.insert(make_pair(it->first,os.str()));
0149   }
0150   return res;
0151 }
0152 
0153 template <typename Type>
0154 string ParMapTBase<Type>::
0155 minimum(const InterfacedBase & i, int place) const {
0156   ostringstream os;
0157   putUnit(os, tminimum(i,place));
0158   return os.str();
0159 }
0160 
0161 template <typename Type>
0162 string ParMapTBase<Type>::
0163 maximum(const InterfacedBase & i, int place) const {
0164   ostringstream os;
0165   putUnit(os, tmaximum(i, place));
0166   return os.str();
0167 }
0168 
0169 template <typename Type>
0170 string ParMapTBase<Type>::
0171 def(const InterfacedBase & i, int place) const {
0172   ostringstream os;
0173   putUnit(os, tdef(i,place));
0174   return os.str();
0175 }
0176 
0177 template <typename Type>
0178 string ParMapTBase<Type>::def() const {
0179   ostringstream os;
0180   putUnit(os, tdef());
0181   return os.str();
0182 }
0183 
0184 template <typename T, typename Type>
0185 Type ParMap<T,Type>::tdef() const {
0186   return theDef;
0187 }
0188 
0189 template <typename T, typename Type>
0190 void ParMap<T,Type>::tset(InterfacedBase & i, Type newValue, int place) const
0191   {
0192   if ( InterfaceBase::readOnly() ) throw InterExReadOnly(*this, i);
0193   T * t = dynamic_cast<T *>(&i);
0194   if ( !t ) throw InterExClass(*this, i);
0195   if ( ( ParMapBase::lowerLimit() && newValue < tminimum(*t, place) ) ||
0196        ( ParMapBase::upperLimit() && newValue > tmaximum(*t, place) ) )
0197     throw ParMExLimit(*this, i, newValue);
0198   TypeMap oldMap = tget(i);
0199   if ( theSetFn ) {
0200     try { (t->*theSetFn)(newValue, place); }
0201     catch (InterfaceException & e) { throw e; }
0202     catch ( ... ) { throw ParMExUnknown(*this, i, newValue, place, "set"); }
0203   } else {
0204     if ( !theMember ) throw InterExSetup(*this, i);
0205     (t->*theMember)[place] = newValue;
0206   }
0207   if ( !InterfaceBase::dependencySafe() && oldMap != tget(i) ) i.touch();
0208 }
0209 
0210 template <typename T, typename Type>
0211 void ParMap<T,Type>::
0212 tinsert(InterfacedBase & i, Type newValue, int place) const
0213   {
0214   if ( InterfaceBase::readOnly() ) throw InterExReadOnly(*this, i);
0215   if ( ParMapBase::size() > 0 ) throw ParMExFixed(*this, i);
0216   T * t = dynamic_cast<T *>(&i);
0217   if ( !t ) throw InterExClass(*this, i);
0218   if ( ( ParMapBase::lowerLimit() && newValue < tminimum(*t, place) ) ||
0219        ( ParMapBase::upperLimit() && newValue > tmaximum(*t, place) ) )
0220     throw ParMExLimit(*this, i, newValue);
0221   TypeMap oldMap = tget(i);
0222   if ( theInsFn ) {
0223     try { (t->*theInsFn)(newValue, place); }
0224     catch (InterfaceException & e) { throw e; }
0225     catch ( ... ) { throw ParMExUnknown(*this, i, newValue, place, "insert"); }
0226   } else {
0227     if ( !theMember ) throw InterExSetup(*this, i);
0228     (t->*theMember).insert(make_pair(place, newValue));
0229   }
0230   if ( !InterfaceBase::dependencySafe() && oldMap != tget(i) ) i.touch();
0231 }
0232 
0233 template <typename T, typename Type>
0234 void ParMap<T,Type>::
0235 erase(InterfacedBase & i, int place) const {
0236   if ( InterfaceBase::readOnly() ) throw InterExReadOnly(*this, i);
0237   if ( ParMapBase::size() > 0 ) throw ParMExFixed(*this, i);
0238   T * t = dynamic_cast<T *>(&i);
0239   if ( !t ) throw InterExClass(*this, i);
0240   TypeMap oldMap = tget(i);
0241   if ( theDelFn ) {
0242     try { (t->*theDelFn)(place); }
0243     catch (InterfaceException & e) { throw e; }
0244     catch ( ... ) {  throw ParMExDelUnknown(*this, i, place); }
0245   } else {
0246     if ( !theMember ) throw InterExSetup(*this, i);
0247     (t->*theMember).erase(place);
0248   }
0249   if ( !InterfaceBase::dependencySafe() && oldMap != tget(i) ) i.touch();
0250 }
0251 
0252 template <class T, class R>
0253 void ParMap<T,R>::clear(InterfacedBase & i) const
0254   {
0255   if ( ParMapBase::readOnly() ) throw InterExReadOnly(*this, i);
0256   if ( ParMapBase::size() > 0 ) throw ParMExFixed(*this, i);
0257   T * t = dynamic_cast<T *>(&i);
0258   if ( !t ) throw InterExClass(*this, i);
0259   (t->*theMember).clear();
0260   if (  !InterfaceBase::dependencySafe() ) i.touch();
0261 }
0262 
0263 template <typename T, typename Type>
0264 typename ParMap<T,Type>::TypeMap ParMap<T,Type>::
0265 tget(const InterfacedBase & i) const {
0266   const T * t = dynamic_cast<const T *>(&i);
0267   if ( !t ) throw InterExClass(*this, i);
0268   if ( theGetFn ) {
0269     try { return (t->*theGetFn)(); }
0270     catch (InterfaceException & e) { throw e; }
0271     catch ( ... ) { throw ParMExGetUnknown(*this, i, "current"); }
0272   }
0273   if ( theMember ) return t->*theMember;
0274   throw InterExSetup(*this, i);
0275 }
0276 
0277 template <typename T, typename Type>
0278 typename ParMap<T,Type>::StringMap ParMap<T,Type>::
0279 get(const InterfacedBase & i) const {
0280   if ( !theStringGetFn ) return ParMapTBase<Type>::get(i);
0281   const T * t = dynamic_cast<const T *>(&i);
0282   if ( !t ) throw InterExClass(*this, i);
0283   try { return (t->*theStringGetFn)(); }
0284   catch (InterfaceException & e) { throw e; }
0285   catch ( ... ) { throw ParMExGetUnknown(*this, i, "current"); }
0286 }
0287 
0288 
0289 template <typename T, typename Type>
0290 Type ParMap<T,Type>::tdef(const InterfacedBase & i, int place) const
0291   {
0292   if ( !theDefFn ) return theMin;
0293   const T * t = dynamic_cast<const T *>(&i);
0294   if ( !t ) throw InterExClass(*this, i);
0295   try { return (t->*theDefFn)(place); }
0296   catch (InterfaceException & e) { throw e; }
0297   catch ( ... ) { throw ParMExGetUnknown(*this, i, "default"); }
0298 }
0299 
0300 template <typename T, typename Type>
0301 Type ParMap<T,Type>::tminimum(const InterfacedBase & i, int place) const
0302   {
0303   if ( !theMinFn ) return theMin;
0304   const T * t = dynamic_cast<const T *>(&i);
0305   if ( !t ) throw InterExClass(*this, i);
0306   try { return (t->*theMinFn)(place); }
0307   catch (InterfaceException & e) { throw e; }
0308   catch ( ... ) { throw ParMExGetUnknown(*this, i, "minimum"); }
0309 }
0310 
0311 template <typename T, typename Type>
0312 Type ParMap<T,Type>::tmaximum(const InterfacedBase & i, int place) const
0313   {
0314   if ( !theMaxFn ) return theMax;
0315   const T * t = dynamic_cast<const T *>(&i);
0316   if ( !t ) throw InterExClass(*this, i);
0317   try { return (t->*theMaxFn)(place); }
0318   catch (InterfaceException & e) { throw e; }
0319   catch ( ... ) { throw ParMExGetUnknown(*this, i, "maximum"); }
0320 }
0321 
0322 template <typename T, typename Type>
0323 void ParMap<T,Type>::doxygenDescription(ostream & os) const {
0324   ParMapTBase<Type>::doxygenDescription(os);
0325   os << "<b>Default value:</b> ";
0326   this->putUnit(os, theDef);
0327   if ( theDefFn ) os << " (May be changed by member function.)";
0328   if ( ParMapBase::lowerLimit() ) {
0329     os << "<br>\n<b>Minimum value:</b> ";
0330     this->putUnit(os, theMin);
0331     if ( theMinFn ) os << " (May be changed by member function.)";
0332   }
0333   if ( ParMapBase::upperLimit() ) {
0334     os << "<br>\n<b>Maximum value:</b> ";
0335     this->putUnit(os, theMax);
0336     if ( theMaxFn ) os << " (May be changed by member function.)";
0337   }
0338   os << "<br>\n";
0339 }
0340 
0341 namespace {
0342   template <typename T>
0343   inline
0344   void ostreamInsert2(ostream & os, T v, DimensionT) {
0345     os << ounit(v, T::baseunit());
0346   }
0347   
0348   template <typename T>
0349   inline
0350   void ostreamInsert2(ostream & os, T v, StandardT) {
0351     os << v;
0352   }
0353 }
0354 
0355 template <typename T>
0356 ParMExLimit::ParMExLimit(const InterfaceBase & i,
0357                          const InterfacedBase & o, T v) {
0358   theMessage << "Could not set/insert ";
0359   ostreamInsert2(theMessage,v,typename TypeTraits<T>::DimType() );
0360   theMessage << " in the parameter map \""
0361              << i.name() << "\" for the object \"" << o.name()
0362              << "\" because the value is outside the specified limits.";
0363   severity(setuperror);
0364 }
0365 
0366 template <typename T>
0367 ParMExUnknown::ParMExUnknown(const InterfaceBase & i, const InterfacedBase & o,
0368                              T v, int j, const char * s) {
0369   theMessage << "Could not " << s << " the value ";
0370   ostreamInsert2(theMessage,v,typename TypeTraits<T>::DimType() );
0371   theMessage << " at position "
0372              << j << " in the parameter map \"" << i.name()
0373              << "\" for the object \"" << o.name() << "\" because the "
0374              << s << " function threw an unknown exception.";
0375   severity(setuperror);
0376 }
0377 
0378 }