Warning, /include/ThePEG/Interface/ParVector.tcc is written in an unsupported language. File is not indexed.
0001 // -*- C++ -*-
0002 //
0003 // ParVector.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 ParVector and ParVectorTBase classes.
0012 //
0013
0014 #include "ParVector.xh"
0015
0016 namespace ThePEG {
0017
0018 template <typename Type>
0019 string ParVectorTBase<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 ParVectorTBase<Type>::doxygenType() const {
0027 ostringstream os;
0028 if ( size() <= 0 ) os << "Varying size ";
0029 else os << "Fixed size (" << size() << ") ";
0030 os << "vector 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 ParVectorTBase<Type>::fullDescription(const InterfacedBase & ib) const {
0042 return ParVectorBase::fullDescription(ib) + def() + "\n";
0043 }
0044
0045 template <typename Type>
0046 void ParVectorTBase<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 ParVectorTBase<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 ParVectorTBase<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 ParVectorTBase<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 ParVectorTBase<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 ParVectorTBase<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 ParVectorTBase<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 ParVectorTBase<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 ParVectorTBase<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 ParVectorTBase<Type>::StringVector ParVectorTBase<Type>::
0141 get(const InterfacedBase & i) const {
0142 TypeVector tres = tget(i);
0143 StringVector res;
0144 for ( typename TypeVector::iterator it = tres.begin();
0145 it != tres.end(); ++it ) {
0146 ostringstream os;
0147 putUnit(os, *it);
0148 res.push_back(os.str());
0149 }
0150 return res;
0151 }
0152
0153 template <typename Type>
0154 string ParVectorTBase<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 ParVectorTBase<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 ParVectorTBase<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 ParVectorTBase<Type>::def() const {
0179 ostringstream os;
0180 putUnit(os, tdef());
0181 return os.str();
0182 }
0183
0184 template <typename T, typename Type>
0185 Type ParVector<T,Type>::tdef() const {
0186 return theDef;
0187 }
0188
0189 template <typename T, typename Type>
0190 void ParVector<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 ( ( ParVectorBase::lowerLimit() && newValue < tminimum(*t, place) ) ||
0196 ( ParVectorBase::upperLimit() && newValue > tmaximum(*t, place) ) )
0197 throw ParVExLimit(*this, i, newValue);
0198 TypeVector oldVector = tget(i);
0199 if ( theSetFn ) {
0200 try { (t->*theSetFn)(newValue, place); }
0201 catch (InterfaceException & e) { throw e; }
0202 catch ( ... ) { throw ParVExUnknown(*this, i, newValue, place, "set"); }
0203 } else {
0204 if ( !theMember ) throw InterExSetup(*this, i);
0205 if ( place < 0 || unsigned(place) >= (t->*theMember).size() )
0206 throw ParVExIndex(*this, i, place);
0207 (t->*theMember)[place] = newValue;
0208 }
0209 if ( !InterfaceBase::dependencySafe() && oldVector != tget(i) ) i.touch();
0210 }
0211
0212 template <typename T, typename Type>
0213 void ParVector<T,Type>::
0214 tinsert(InterfacedBase & i, Type newValue, int place) const
0215 {
0216 if ( InterfaceBase::readOnly() ) throw InterExReadOnly(*this, i);
0217 if ( ParVectorBase::size() > 0 ) throw ParVExFixed(*this, i);
0218 T * t = dynamic_cast<T *>(&i);
0219 if ( !t ) throw InterExClass(*this, i);
0220 if ( ( ParVectorBase::lowerLimit() && newValue < tminimum(*t, place) ) ||
0221 ( ParVectorBase::upperLimit() && newValue > tmaximum(*t, place) ) )
0222 throw ParVExLimit(*this, i, newValue);
0223 TypeVector oldVector = tget(i);
0224 if ( theInsFn ) {
0225 try { (t->*theInsFn)(newValue, place); }
0226 catch (InterfaceException & e) { throw e; }
0227 catch ( ... ) { throw ParVExUnknown(*this, i, newValue, place, "insert"); }
0228 } else {
0229 if ( !theMember ) throw InterExSetup(*this, i);
0230 if ( place < 0 || unsigned(place) > (t->*theMember).size() )
0231 throw ParVExIndex(*this, i, place);
0232 (t->*theMember).insert((t->*theMember).begin()+place, newValue);
0233 }
0234 if ( !InterfaceBase::dependencySafe() && oldVector != tget(i) ) i.touch();
0235 }
0236
0237 template <typename T, typename Type>
0238 void ParVector<T,Type>::
0239 erase(InterfacedBase & i, int place) const {
0240 if ( InterfaceBase::readOnly() ) throw InterExReadOnly(*this, i);
0241 if ( ParVectorBase::size() > 0 ) throw ParVExFixed(*this, i);
0242 T * t = dynamic_cast<T *>(&i);
0243 if ( !t ) throw InterExClass(*this, i);
0244 TypeVector oldVector = tget(i);
0245 if ( theDelFn ) {
0246 try { (t->*theDelFn)(place); }
0247 catch (InterfaceException & e) { throw e; }
0248 catch ( ... ) { throw ParVExDelUnknown(*this, i, place); }
0249 } else {
0250 if ( !theMember ) throw InterExSetup(*this, i);
0251 if ( place < 0 || unsigned(place) >= (t->*theMember).size() )
0252 throw ParVExIndex(*this, i, place);
0253 (t->*theMember).erase((t->*theMember).begin()+place);
0254 }
0255 if ( !InterfaceBase::dependencySafe() && oldVector != tget(i) ) i.touch();
0256 }
0257
0258 template <class T, class R>
0259 void ParVector<T,R>::clear(InterfacedBase & i) const
0260 {
0261 if ( ParVectorBase::readOnly() ) throw InterExReadOnly(*this, i);
0262 if ( ParVectorBase::size() > 0 ) throw ParVExFixed(*this, i);
0263 T * t = dynamic_cast<T *>(&i);
0264 if ( !t ) throw InterExClass(*this, i);
0265 (t->*theMember).clear();
0266 if ( !InterfaceBase::dependencySafe() ) i.touch();
0267 }
0268
0269 template <typename T, typename Type>
0270 typename ParVector<T,Type>::TypeVector ParVector<T,Type>::
0271 tget(const InterfacedBase & i) const {
0272 const T * t = dynamic_cast<const T *>(&i);
0273 if ( !t ) throw InterExClass(*this, i);
0274 if ( theGetFn ) {
0275 try { return (t->*theGetFn)(); }
0276 catch (InterfaceException & e) { throw e; }
0277 catch ( ... ) { throw ParVExGetUnknown(*this, i, "current"); }
0278 }
0279 if ( theMember ) return t->*theMember;
0280 throw InterExSetup(*this, i);
0281 }
0282
0283 template <typename T, typename Type>
0284 typename ParVector<T,Type>::StringVector ParVector<T,Type>::
0285 get(const InterfacedBase & i) const {
0286 if ( !theStringGetFn ) return ParVectorTBase<Type>::get(i);
0287 const T * t = dynamic_cast<const T *>(&i);
0288 if ( !t ) throw InterExClass(*this, i);
0289 try { return (t->*theStringGetFn)(); }
0290 catch (InterfaceException & e) { throw e; }
0291 catch ( ... ) { throw ParVExGetUnknown(*this, i, "current"); }
0292 }
0293
0294
0295 template <typename T, typename Type>
0296 Type ParVector<T,Type>::tdef(const InterfacedBase & i, int place) const
0297 {
0298 if ( place < 0 || !theDefFn ) return theMin;
0299 const T * t = dynamic_cast<const T *>(&i);
0300 if ( !t ) throw InterExClass(*this, i);
0301 try { return (t->*theDefFn)(place); }
0302 catch (InterfaceException & e) { throw e; }
0303 catch ( ... ) { throw ParVExGetUnknown(*this, i, "default"); }
0304 }
0305
0306 template <typename T, typename Type>
0307 Type ParVector<T,Type>::tminimum(const InterfacedBase & i, int place) const
0308 {
0309 if ( place < 0 || !theMinFn ) return theMin;
0310 const T * t = dynamic_cast<const T *>(&i);
0311 if ( !t ) throw InterExClass(*this, i);
0312 try { return (t->*theMinFn)(place); }
0313 catch (InterfaceException & e) { throw e; }
0314 catch ( ... ) { throw ParVExGetUnknown(*this, i, "minimum"); }
0315 }
0316
0317 template <typename T, typename Type>
0318 Type ParVector<T,Type>::tmaximum(const InterfacedBase & i, int place) const
0319 {
0320 if ( place < 0 || !theMaxFn ) return theMax;
0321 const T * t = dynamic_cast<const T *>(&i);
0322 if ( !t ) throw InterExClass(*this, i);
0323 try { return (t->*theMaxFn)(place); }
0324 catch (InterfaceException & e) { throw e; }
0325 catch ( ... ) { throw ParVExGetUnknown(*this, i, "maximum"); }
0326 }
0327
0328 template <typename T, typename Type>
0329 void ParVector<T,Type>::doxygenDescription(ostream & os) const {
0330 ParVectorTBase<Type>::doxygenDescription(os);
0331 os << "<b>Default value:</b> ";
0332 this->putUnit(os, theDef);
0333 if ( theDefFn ) os << " (May be changed by member function.)";
0334 if ( ParVectorBase::lowerLimit() ) {
0335 os << "<br>\n<b>Minimum value:</b> ";
0336 this->putUnit(os, theMin);
0337 if ( theMinFn ) os << " (May be changed by member function.)";
0338 }
0339 if ( ParVectorBase::upperLimit() ) {
0340 os << "<br>\n<b>Maximum value:</b> ";
0341 this->putUnit(os, theMax);
0342 if ( theMaxFn ) os << " (May be changed by member function.)";
0343 }
0344 os << "<br>\n";
0345 }
0346
0347 namespace {
0348 template <typename T>
0349 inline
0350 void ostreamInsert2(ostream & os, T v, DimensionT) {
0351 os << ounit(v, T::baseunit());
0352 }
0353
0354 template <typename T>
0355 inline
0356 void ostreamInsert2(ostream & os, T v, StandardT) {
0357 os << v;
0358 }
0359 }
0360
0361 template <typename T>
0362 ParVExLimit::ParVExLimit(const InterfaceBase & i,
0363 const InterfacedBase & o, T v) {
0364 theMessage << "Could not set/insert ";
0365 ostreamInsert2(theMessage,v,typename TypeTraits<T>::DimType() );
0366 theMessage << " in the parameter vector \""
0367 << i.name() << "\" for the object \"" << o.name()
0368 << "\" because the value is outside the specified limits.";
0369 severity(setuperror);
0370 }
0371
0372 template <typename T>
0373 ParVExUnknown::ParVExUnknown(const InterfaceBase & i, const InterfacedBase & o,
0374 T v, int j, const char * s) {
0375 theMessage << "Could not " << s << " the value ";
0376 ostreamInsert2(theMessage,v,typename TypeTraits<T>::DimType() );
0377 theMessage << " at position "
0378 << j << " in the parameter vector \"" << i.name()
0379 << "\" for the object \"" << o.name() << "\" because the "
0380 << s << " function threw an unknown exception.";
0381 severity(setuperror);
0382 }
0383
0384 }