00001 // -*- C++ -*- 00002 // 00003 // Rebinder.h is a part of ThePEG - Toolkit for HEP Event Generation 00004 // Copyright (C) 1999-2007 Leif Lonnblad 00005 // 00006 // ThePEG is licenced under version 2 of the GPL, see COPYING for details. 00007 // Please respect the MCnet academic guidelines, see GUIDELINES for details. 00008 // 00009 #ifndef ThePEG_Rebinder_H 00010 #define ThePEG_Rebinder_H 00011 00012 #include "ThePEG/Config/ThePEG.h" 00013 #include "Rebinder.fh" 00014 #include <stdexcept> 00015 00016 namespace ThePEG { 00017 00026 template <typename T> 00027 class Rebinder { 00028 00029 public: 00030 00032 Rebinder() {} 00033 00034 public: 00035 00036 ThePEG_DECLARE_TEMPLATE_POINTERS(T,TPtr); 00037 00039 typedef std::map<cTPtr,TPtr> MapType; 00040 00042 typedef typename MapType::const_iterator const_iterator; 00043 00044 public: 00045 00049 TPtr & operator[](tcTPtr t) { return theMap[t]; } 00050 00057 template <typename R> 00058 R translate(const R & r) const { 00059 const_iterator it = theMap.find(r); 00060 return it == theMap.end()? R(): dynamic_ptr_cast<R>(it->second); 00061 } 00062 00069 template <typename OutputIterator, typename InputIterator> 00070 void translate(OutputIterator r, 00071 InputIterator first, InputIterator last) const { 00072 while ( first != last ) *r++ = translate(*first++); 00073 } 00074 00080 template <typename R> 00081 R alwaysTranslate(const R & r) const throw(std::runtime_error) { 00082 R ret; 00083 if ( !r ) return ret; 00084 const_iterator it = theMap.find(r); 00085 ret = (it == theMap.end()? R(): dynamic_ptr_cast<R>(it->second)); 00086 if ( !ret ) throw std::runtime_error("Rebinder exception"); 00087 return ret; 00088 } 00089 00096 template <typename OutputIterator, typename InputIterator> 00097 void alwaysTranslate(OutputIterator r, InputIterator first, InputIterator last) 00098 const throw(std::runtime_error) { 00099 while ( first != last ) *r++ = alwaysTranslate(*first++); 00100 } 00101 00105 const MapType & map() const { return theMap; } 00106 00107 private: 00108 00109 00113 MapType theMap; 00114 00115 private: 00116 00118 Rebinder(const Rebinder &); 00119 00121 Rebinder & operator=(const Rebinder &); 00122 00123 }; 00124 00125 00126 } 00127 00128 #endif /* ThePEG_Rebinder_H */