Back to home page

EIC code displayed by LXR

 
 

    


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

0001 // -*- C++ -*-
0002 //
0003 // Current.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_Current_H
0010 #define ThePEG_Current_H
0011 // This is the declaration of the Current class.
0012 
0013 namespace ThePEG {
0014 
0015 /**
0016  * The Current class keeps a static stack of objects of the templated
0017  * class, which can be used anywhere by any class. When an object is
0018  * active it adds itself to the stack which can be used by any other
0019  * object through the static functions of the Current class. If
0020  * someone needs to use an alternative object a new Current object can
0021  * be constructed with a pointer to the desired object
0022  * as argument and that object will the be used by the static Current
0023  * functions until the Current object is destructed.
0024  *
0025  * Default-contructed objects of the Current class can be used as a
0026  * pointer to the currently chosen object on the stack.
0027  *
0028  * The typical use case for this class is a handler class which uses a
0029  * number of objects which do not have a reference back to the
0030  * handler, but still need to acces some member functions. In a member
0031  * function the handler class will construct a Current object:
0032  * <code>Current&lt;Handler&gt; current(this);</code> in any following
0033  * function called in this member function, any object can then access
0034  * the handlers methods as
0035  * <code>Current&lt;Handler&gt;()-&gt;memfun();</code>.
0036  *
0037  */
0038 template <typename T>
0039 class Current {
0040 
0041 public:
0042 
0043   /**
0044    * Default constructor does nothing.
0045    */
0046   Current() : pushed(false) {}
0047 
0048   /**
0049    * Copy-constructor does nothing.
0050    */
0051   Current(const Current<T> &)
0052     : pushed(false) {}
0053 
0054   /**
0055    * Construct a new object specifying a new object, \a o, to be used
0056    * during this objects lifetime. The object must not be deleted
0057    * until the Current object us destroyed.
0058    */
0059   Current(T * t) : pushed(false) {
0060     if ( t ) {
0061       theStack.push_back(t);
0062       pushed = true;
0063     }
0064   }
0065 
0066   /**
0067    * The destructor removing the object specified in the constructor
0068    * from the stack.
0069    */
0070   ~Current() {
0071     if ( pushed ) theStack.pop_back();
0072   }
0073 
0074 public:
0075 
0076   /**
0077    * Returns true if there is no currently chosen object.
0078    */
0079   static bool isVoid() {
0080     return theStack.empty() || !(theStack.back());
0081   }
0082 
0083   /**
0084    * Return a reference to the currently chosen object.
0085    */
0086   static T & current() {
0087     return *theStack.back();
0088   }
0089 
0090   /**
0091    * Return a reference to the currently chosen object.
0092    */
0093   T & operator*() const {
0094     return *theStack.back();
0095   }
0096 
0097   /**
0098    * Return a pointer to the currently chosen object.
0099    */
0100   T * operator->() const {
0101     return theStack.back();
0102   }
0103 
0104   /**
0105    *  Pointer to the stack
0106    */
0107   static T * ptr() {
0108     return theStack.back();
0109   }
0110 
0111   /**
0112    * Test for existance
0113    */
0114   operator bool() const {
0115     return ptr();
0116   }
0117 
0118   /**
0119    * Test for existance
0120    */
0121   bool operator!() const {
0122     return !ptr();
0123   }
0124 
0125 private:
0126 
0127   /**
0128    * The stack of objects requested.
0129    */
0130   static vector<T *> theStack;
0131 
0132   /**
0133    * True if this object is responsible for pushing an object
0134    * onto the stack.
0135    */
0136   bool pushed;
0137 
0138 private:
0139 
0140   /**
0141    *  Private and non-existent assignment operator.
0142    */
0143   Current<T> & operator=(const Current<T> &) = delete;
0144 
0145 };
0146 
0147 template <typename T>
0148 std::vector<T *> ThePEG::Current<T>::theStack;
0149 
0150 }
0151 
0152 #endif /* ThePEG_Current_H */