thepeg
is hosted by
Hepforge
,
IPPP Durham
ThePEG
2.3.0
Utilities
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
24
struct
ReferenceHolderBase
{
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>
44
struct
ReferenceHolder
45
:
public
ReferenceHolderBase
{
46
50
T&
value
;
51
55
static
T&
init
() {
56
static
T v;
return
v;
57
}
58
62
ReferenceHolder
()
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
90
ReferenceHolderBase
*
holder
;
91
92
public
:
93
97
AnyReference
()
98
:
holder
(0) {}
99
103
template
<
class
T>
104
AnyReference
(T& v)
105
:
holder
(new
ReferenceHolder
<T>(v)) {}
106
110
AnyReference
(
const
AnyReference
& other)
111
:
holder
(other.
holder
? other.
holder
->clone() : 0) {}
112
116
~AnyReference
() {
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
137
AnyReference
&
swap
(
AnyReference
& other) {
138
std::swap
(
holder
,other.
holder
);
return
*
this
;
139
}
140
144
template
<
class
T>
145
AnyReference
&
operator=
(T& v) {
146
AnyReference
(v).swap(*
this
);
147
return
*
this
;
148
}
149
153
AnyReference
&
operator=
(
AnyReference
other) {
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
ThePEG.h
This is the main config header file for ThePEG.
ThePEG::AnyReference
AnyReference is inspired by boost::any to hold a reference to an object of arbitrary type.
Definition:
AnyReference.h:22
ThePEG::AnyReference::operator=
AnyReference & operator=(AnyReference other)
Assign from AnyReference.
Definition:
AnyReference.h:153
ThePEG::AnyReference::cast
T & cast() const
Extract the held reference.
Definition:
AnyReference.h:171
ThePEG::AnyReference::operator=
AnyReference & operator=(T &v)
Assign from definite type.
Definition:
AnyReference.h:145
ThePEG::AnyReference::~AnyReference
~AnyReference()
The destructor.
Definition:
AnyReference.h:116
ThePEG::AnyReference::type
const std::type_info & type() const
Return the type held.
Definition:
AnyReference.h:125
ThePEG::AnyReference::empty
bool empty() const
Return true, if no reference is held.
Definition:
AnyReference.h:130
ThePEG::AnyReference::AnyReference
AnyReference()
The default constructor.
Definition:
AnyReference.h:97
ThePEG::AnyReference::AnyReference
AnyReference(T &v)
The standard constructor.
Definition:
AnyReference.h:104
ThePEG::AnyReference::AnyReference
AnyReference(const AnyReference &other)
The copy constructor.
Definition:
AnyReference.h:110
ThePEG::AnyReference::reset
void reset()
Reset to not keep track of any reference.
Definition:
AnyReference.h:161
ThePEG::AnyReference::holder
ReferenceHolderBase * holder
The reference holder used.
Definition:
AnyReference.h:90
ThePEG::AnyReference::swap
AnyReference & swap(AnyReference &other)
Swap the references held.
Definition:
AnyReference.h:137
ThePEG::Exception
Exception is the base class for all exceptions to be used in ThePEG.
Definition:
Exception.h:44
ThePEG::Exception::abortnow
@ abortnow
Severe error, (the run is aborted immediately, before the exception is thrown).
Definition:
Exception.h:65
ThePEG
This is the main namespace within which all identifiers in ThePEG are declared.
Definition:
FactoryBase.h:28
std::swap
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
ThePEG::AnyReference::ReferenceHolderBase
Definition:
AnyReference.h:24
ThePEG::AnyReference::ReferenceHolderBase::type
virtual const std::type_info & type() const =0
Return the type held.
ThePEG::AnyReference::ReferenceHolderBase::~ReferenceHolderBase
virtual ~ReferenceHolderBase()
The destructor.
Definition:
AnyReference.h:29
ThePEG::AnyReference::ReferenceHolderBase::clone
virtual ReferenceHolderBase * clone() const =0
Clone this reference holder.
ThePEG::AnyReference::ReferenceHolder
Definition:
AnyReference.h:45
ThePEG::AnyReference::ReferenceHolder::type
virtual const std::type_info & type() const
Return the type held.
Definition:
AnyReference.h:74
ThePEG::AnyReference::ReferenceHolder::value
T & value
The reference held.
Definition:
AnyReference.h:50
ThePEG::AnyReference::ReferenceHolder::init
static T & init()
Static member to initialize the reference.
Definition:
AnyReference.h:55
ThePEG::AnyReference::ReferenceHolder::clone
virtual ReferenceHolderBase * clone() const
Clone this reference holder.
Definition:
AnyReference.h:81
ThePEG::AnyReference::ReferenceHolder::ReferenceHolder
ReferenceHolder()
The default constructor.
Definition:
AnyReference.h:62
ThePEG::AnyReference::ReferenceHolder::ReferenceHolder
ReferenceHolder(T &v)
Construct from given reference.
Definition:
AnyReference.h:68
Generated on Thu Jun 20 2024 14:47:00 for ThePEG by
1.9.6