thepeg is hosted by Hepforge, IPPP Durham
ThePEG 2.3.0
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
14#include "Exception.h"
15
16namespace ThePEG {
17
23
25
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
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;
177 }
178
179 };
180
181}
182
183#endif // ThePEG_AnyReference_H
This is the main config header file for ThePEG.
AnyReference is inspired by boost::any to hold a reference to an object of arbitrary type.
Definition: AnyReference.h:22
AnyReference & operator=(AnyReference other)
Assign from AnyReference.
Definition: AnyReference.h:153
T & cast() const
Extract the held reference.
Definition: AnyReference.h:171
AnyReference & operator=(T &v)
Assign from definite type.
Definition: AnyReference.h:145
~AnyReference()
The destructor.
Definition: AnyReference.h:116
const std::type_info & type() const
Return the type held.
Definition: AnyReference.h:125
bool empty() const
Return true, if no reference is held.
Definition: AnyReference.h:130
AnyReference()
The default constructor.
Definition: AnyReference.h:97
AnyReference(T &v)
The standard constructor.
Definition: AnyReference.h:104
AnyReference(const AnyReference &other)
The copy constructor.
Definition: AnyReference.h:110
void reset()
Reset to not keep track of any reference.
Definition: AnyReference.h:161
ReferenceHolderBase * holder
The reference holder used.
Definition: AnyReference.h:90
AnyReference & swap(AnyReference &other)
Swap the references held.
Definition: AnyReference.h:137
Exception is the base class for all exceptions to be used in ThePEG.
Definition: Exception.h:44
@ abortnow
Severe error, (the run is aborted immediately, before the exception is thrown).
Definition: Exception.h:65
This is the main namespace within which all identifiers in ThePEG are declared.
Definition: FactoryBase.h:28
void swap(ThePEG::Pointer::RCPtr< T > &t1, ThePEG::Pointer::RCPtr< T > &t2)
Specialization of std::swap to avoid unnecessary (in/de)crements of the reference count.
Definition: RCPtr.h:1154
virtual const std::type_info & type() const =0
Return the type held.
virtual ~ReferenceHolderBase()
The destructor.
Definition: AnyReference.h:29
virtual ReferenceHolderBase * clone() const =0
Clone this reference holder.
virtual const std::type_info & type() const
Return the type held.
Definition: AnyReference.h:74
static T & init()
Static member to initialize the reference.
Definition: AnyReference.h:55
virtual ReferenceHolderBase * clone() const
Clone this reference holder.
Definition: AnyReference.h:81
ReferenceHolder()
The default constructor.
Definition: AnyReference.h:62
ReferenceHolder(T &v)
Construct from given reference.
Definition: AnyReference.h:68