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 
113  MapType theMap;
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 */
void alwaysTranslate(OutputIterator r, InputIterator first, InputIterator last) const
Insert pointers to objects into the output iterator, pointers to objects corresponding to the ones gi...
Definition: Rebinder.h:97
MapType::const_iterator const_iterator
The iterator of the underlying map.
Definition: Rebinder.h:42
R alwaysTranslate(const R &r) const
Return a pointer to the object associated with the argument.
Definition: Rebinder.h:81
TransientConstRCPtr is a simple wrapper around a bare const pointer which can be assigned to and from...
Definition: RCPtr.h:681
Rebinder is a class associating pairs of pointers to objects.
Definition: Rebinder.h:27
T1 dynamic_ptr_cast(const T2 &t2)
Replacement for the standard dynamic_cast.
Definition: PtrTraits.h:168
This is the main namespace within which all identifiers in ThePEG are declared.
Definition: FactoryBase.h:28
#define ThePEG_DECLARE_TEMPLATE_POINTERS(full, abbrev)
This macro helps us to declare pointers and stuff to standard classes.
Definition: Pointers.h:36
TPtr & operator[](tcTPtr t)
Return a pointer to the object associated with the argument.
Definition: Rebinder.h:49
Rebinder()
Default constructor.
Definition: Rebinder.h:32
const MapType & map() const
Acces the underlying map representation.
Definition: Rebinder.h:105
MapType theMap
The underlying map representation.
Definition: Rebinder.h:113
RCPtr is a reference counted (smart) pointer.
Definition: RCPtr.h:60
R translate(const R &r) const
Return a pointer to the object associated with the argument.
Definition: Rebinder.h:58
void translate(OutputIterator r, InputIterator first, InputIterator last) const
Insert pointers to objects into the output iterator, pointers to objects corresponding to the ones gi...
Definition: Rebinder.h:70
std::map< cTPtr, TPtr > MapType
Map associating pairs of objects.
Definition: Rebinder.h:39
Rebinder & operator=(const Rebinder &)
The assignment operator is private and not implemented.