thepeg is hosted by Hepforge, IPPP Durham
ThePEG  2.2.1
AnyReference.h
1 // -*- C++ -*-
2 //
3 // AnyReference.h is a part of ThePEG - Toolkit for HEP Event Generation
4 // Copyright (C) 1999-2019 Leif Lonnblad
5 // Copyright (C) 2010-2019 Simon Platzer
6 //
7 // ThePEG is licenced under version 3 of the GPL, see COPYING for details.
8 // Please respect the MCnet academic guidelines, see GUIDELINES for details.
9 //
10 #ifndef ThePEG_AnyReference_H
11 #define ThePEG_AnyReference_H
12 
13 #include "ThePEG/Config/ThePEG.h"
14 #include "Exception.h"
15 
16 namespace ThePEG {
17 
22  class AnyReference {
23 
25 
29  virtual ~ReferenceHolderBase() {}
30 
34  virtual const std::type_info& type() const = 0;
35 
39  virtual ReferenceHolderBase* clone() const = 0;
40 
41  };
42 
43  template<class T>
45  : public ReferenceHolderBase {
46 
50  T& value;
51 
55  static T& init() {
56  static T v; return v;
57  }
58 
63  : value(init()) {}
64 
68  explicit ReferenceHolder(T& v)
69  : value(v) {}
70 
74  virtual const std::type_info& type() const {
75  return typeid(T);
76  }
77 
81  virtual ReferenceHolderBase* clone() const {
82  return new ReferenceHolder(*this);
83  }
84 
85  };
86 
91 
92  public:
93 
98  : holder(0) {}
99 
103  template<class T>
105  : holder(new ReferenceHolder<T>(v)) {}
106 
111  : holder(other.holder ? other.holder->clone() : 0) {}
112 
117  delete holder; holder = 0;
118  }
119 
120  public:
121 
125  const std::type_info& type() const { return holder ? holder->type() : typeid(void); }
126 
130  bool empty() const { return !holder; }
131 
132  public:
133 
138  std::swap(holder,other.holder); return *this;
139  }
140 
144  template<class T>
146  AnyReference(v).swap(*this);
147  return *this;
148  }
149 
154  other.swap(*this);
155  return *this;
156  }
157 
161  void reset() {
162  delete holder; holder = 0;
163  }
164 
165  public:
166 
170  template<class T>
171  T& cast() const {
172  if ( !empty() && type() == typeid(T) ) {
173  return static_cast<ReferenceHolder<T>*>(holder)->value;
174  }
175  throw Exception() << "Bad cast in AnyReference" << Exception::abortnow;
176  return ReferenceHolder<T>::init();
177  }
178 
179  };
180 
181 }
182 
183 #endif // ThePEG_AnyReference_H
ReferenceHolder(T &v)
Construct from given reference.
Definition: AnyReference.h:68
~AnyReference()
The destructor.
Definition: AnyReference.h:116
T & cast() const
Extract the held reference.
Definition: AnyReference.h:171
virtual ReferenceHolderBase * clone() const =0
Clone this reference holder.
bool empty() const
Return true, if no reference is held.
Definition: AnyReference.h:130
AnyReference & operator=(AnyReference other)
Assign from AnyReference.
Definition: AnyReference.h:153
const std::type_info & type() const
Return the type held.
Definition: AnyReference.h:125
ReferenceHolderBase * holder
The reference holder used.
Definition: AnyReference.h:90
static T & init()
Static member to initialize the reference.
Definition: AnyReference.h:55
This is the main namespace within which all identifiers in ThePEG are declared.
Definition: FactoryBase.h:28
T & value
The reference held.
Definition: AnyReference.h:50
This is the main config header file for ThePEG.
virtual ~ReferenceHolderBase()
The destructor.
Definition: AnyReference.h:29
virtual ReferenceHolderBase * clone() const
Clone this reference holder.
Definition: AnyReference.h:81
AnyReference & swap(AnyReference &other)
Swap the references held.
Definition: AnyReference.h:137
virtual const std::type_info & type() const =0
Return the type held.
AnyReference()
The default constructor.
Definition: AnyReference.h:97
AnyReference & operator=(T &v)
Assign from definite type.
Definition: AnyReference.h:145
AnyReference(T &v)
The standard constructor.
Definition: AnyReference.h:104
AnyReference is inspired by boost::any to hold a reference to an object of arbitrary type...
Definition: AnyReference.h:22
virtual const std::type_info & type() const
Return the type held.
Definition: AnyReference.h:74
void reset()
Reset to not keep track of any reference.
Definition: AnyReference.h:161
Exception is the base class for all exceptions to be used in ThePEG.
Definition: Exception.h:44
AnyReference(const AnyReference &other)
The copy constructor.
Definition: AnyReference.h:110
ReferenceHolder()
The default constructor.
Definition: AnyReference.h:62
Severe error, (the run is aborted immediately, before the exception is thrown).
Definition: Exception.h:65