ThePEG  1.8.0
Rebinder.h
1 // -*- C++ -*-
2 //
3 // Rebinder.h is a part of ThePEG - Toolkit for HEP Event Generation
4 // Copyright (C) 1999-2011 Leif Lonnblad
5 //
6 // ThePEG is licenced under version 2 of the GPL, see COPYING for details.
7 // Please respect the MCnet academic guidelines, see GUIDELINES for details.
8 //
9 #ifndef ThePEG_Rebinder_H
10 #define ThePEG_Rebinder_H
11 
12 #include "ThePEG/Config/ThePEG.h"
13 #include "Rebinder.fh"
14 #include <stdexcept>
15 
16 namespace ThePEG {
17 
26 template <typename T>
27 class Rebinder {
28 
29 public:
30 
32  Rebinder() {}
33 
34 public:
35 
37 
39  typedef std::map<cTPtr,TPtr> MapType;
40 
42  typedef typename MapType::const_iterator const_iterator;
43 
44 public:
45 
49  TPtr & operator[](tcTPtr t) { return theMap[t]; }
50 
57  template <typename R>
58  R translate(const R & r) const {
59  const_iterator it = theMap.find(r);
60  return it == theMap.end()? R(): dynamic_ptr_cast<R>(it->second);
61  }
62 
69  template <typename OutputIterator, typename InputIterator>
70  void translate(OutputIterator r,
71  InputIterator first, InputIterator last) const {
72  while ( first != last ) *r++ = translate(*first++);
73  }
74 
80  template <typename R>
81  R alwaysTranslate(const R & r) const throw(std::runtime_error) {
82  R ret;
83  if ( !r ) return ret;
84  const_iterator it = theMap.find(r);
85  ret = (it == theMap.end()? R(): dynamic_ptr_cast<R>(it->second));
86  if ( !ret ) throw std::runtime_error("Rebinder exception");
87  return ret;
88  }
89 
96  template <typename OutputIterator, typename InputIterator>
97  void alwaysTranslate(OutputIterator r, InputIterator first, InputIterator last)
98  const throw(std::runtime_error) {
99  while ( first != last ) *r++ = alwaysTranslate(*first++);
100  }
101 
105  const MapType & map() const { return theMap; }
106 
107 private:
108 
109 
114 
115 private:
116 
118  Rebinder(const Rebinder &);
119 
121  Rebinder & operator=(const Rebinder &);
122 
123 };
124 
125 
126 }
127 
128 #endif /* ThePEG_Rebinder_H */