Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2026-08-06 09:38:32

0001 // -*- C++ -*-
0002 //
0003 // Rebinder.h 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 #ifndef ThePEG_Rebinder_H
0010 #define ThePEG_Rebinder_H
0011 
0012 #include "ThePEG/Config/ThePEG.h"
0013 #include "Rebinder.fh"
0014 #include <stdexcept>
0015 
0016 namespace ThePEG {
0017 
0018 /**
0019  * Rebinder is a class associating pairs of pointers to objects. It is
0020  * typically used when cloning a set of objects which have pointers to
0021  * eachother. The Rebinder is then set up so that a cloned object can
0022  * easily be retrieved given a pointer to the original one. The cloned
0023  * objects can then use the Rebinder to change their pointers so that
0024  * they henceforth points to the cloned copies.
0025  */
0026 template <typename T>
0027 class Rebinder {
0028 
0029 public:
0030   
0031   /** Default constructor. */
0032   Rebinder() {}
0033 
0034 public:
0035 
0036   ThePEG_DECLARE_TEMPLATE_POINTERS(T,TPtr);
0037 
0038   /** Map associating pairs of objects. */
0039   typedef std::map<cTPtr,TPtr> MapType;
0040 
0041   /** The iterator of the underlying map. */
0042   typedef typename MapType::const_iterator const_iterator;
0043 
0044 public:
0045 
0046   /**
0047    * Return a pointer to the object associated with the argument.
0048    */
0049   TPtr & operator[](tcTPtr t) { return theMap[t]; }
0050 
0051   /**
0052    * Return a pointer to the object associated with the argument. If
0053    * no corresponding object is found a null pointer given by R() is
0054    * returned.
0055    * @param r a pointer to an object of a type which is derived from T.
0056    */
0057   template <typename R>
0058   R translate(const R & r) const {
0059     const_iterator it = theMap.find(r);
0060     return it == theMap.end()? R(): dynamic_ptr_cast<R>(it->second);
0061   }
0062 
0063   /**
0064    * Insert pointers to objects into the output iterator, pointers to
0065    * objects corresponding to the ones given by the range of input
0066    * iterators. If a given object in the input iterator range does not
0067    * exists, a null pointer will be inserted in the output iterator.
0068    */
0069   template <typename OutputIterator, typename InputIterator>
0070   void translate(OutputIterator r,
0071          InputIterator first, InputIterator last) const {
0072     while ( first != last ) *r++ = translate(*first++);
0073   }
0074 
0075   /**
0076    * Return a pointer to the object associated with the argument. If
0077    * no corresponding object is found an exception is thrown.
0078    * @param r a pointer to an object of a type which is derived from T.
0079    */
0080   template <typename R>
0081   R alwaysTranslate(const R & r) const {
0082     R ret;
0083     if ( !r ) return ret;
0084     const_iterator it = theMap.find(r);
0085     ret = (it == theMap.end()? R(): dynamic_ptr_cast<R>(it->second));
0086     if ( !ret ) throw std::runtime_error("Rebinder exception");
0087     return ret;
0088   }
0089 
0090   /**
0091    * Insert pointers to objects into the output iterator, pointers to
0092    * objects corresponding to the ones given by the range of input
0093    * iterators. If a given object in the input iterator range does not
0094    * exists, an exception will be thrown.
0095    */
0096   template <typename OutputIterator, typename InputIterator>
0097   void alwaysTranslate(OutputIterator r, InputIterator first, InputIterator last)
0098     const {
0099     while ( first != last ) *r++ = alwaysTranslate(*first++);
0100   }
0101 
0102   /**
0103    * Acces the underlying map representation.
0104    */
0105   const MapType & map() const { return theMap; }
0106 
0107 private:
0108 
0109 
0110   /**
0111    * The underlying map representation.
0112    */
0113   MapType theMap;
0114 
0115 private:
0116 
0117   /** The copy constructor is private and not implemented */
0118   Rebinder(const Rebinder &);
0119 
0120   /** The assignment operator is private and not implemented */
0121   Rebinder & operator=(const Rebinder &) = delete;
0122 
0123 };
0124 
0125 
0126 }
0127 
0128 #endif /* ThePEG_Rebinder_H */