1 |
#ifndef Tracker_ProxyBase_H
|
2 |
#define Tracker_ProxyBase_H
|
3 |
|
4 |
#include "TrackingTools/TrajectoryParametrization/interface/TrajectoryStateExceptions.h"
|
5 |
#include "TrackingTools/TrajectoryState/interface/CopyUsingNew.h"
|
6 |
|
7 |
/** A base class for reference counting proxies.
|
8 |
* The class to which this one is proxy must inherit from
|
9 |
* ReferenceCounted.
|
10 |
* A ProxyBase has value semantics, in contrast to ReferenceCountingPointer,
|
11 |
* which has pointer semantics.
|
12 |
* The Proxy class inheriting from ProxyBase must duplicate the
|
13 |
* part of the interface of the reference counted class that it whiches to expose.
|
14 |
*/
|
15 |
|
16 |
template <class T, class Cloner >
|
17 |
class ProxyBase {
|
18 |
protected:
|
19 |
|
20 |
ProxyBase() : theData(0) {}
|
21 |
|
22 |
explicit ProxyBase( T* p) : theData(p) {if (theData) theData->addReference();}
|
23 |
|
24 |
ProxyBase( const ProxyBase& other) {
|
25 |
theData = other.theData;
|
26 |
if (theData) theData->addReference();
|
27 |
}
|
28 |
|
29 |
~ProxyBase() {
|
30 |
destroy();
|
31 |
}
|
32 |
|
33 |
ProxyBase& operator=( const ProxyBase& other) {
|
34 |
if ( theData != other.theData) {
|
35 |
destroy();
|
36 |
theData = other.theData;
|
37 |
if (theData) theData->addReference();
|
38 |
}
|
39 |
return *this;
|
40 |
}
|
41 |
|
42 |
|
43 |
void swap(ProxyBase& other) {
|
44 |
std::swap(theData,other.theData);
|
45 |
}
|
46 |
|
47 |
|
48 |
#if defined( __GXX_EXPERIMENTAL_CXX0X__)
|
49 |
ProxyBase(ProxyBase&& other) {
|
50 |
theData = other.theData;
|
51 |
other.theData=0;
|
52 |
}
|
53 |
|
54 |
ProxyBase& operator=(ProxyBase&& other) {
|
55 |
if ( theData != other.theData) {
|
56 |
destroy();
|
57 |
theData = other.theData;
|
58 |
other.theData=0;
|
59 |
}
|
60 |
return *this;
|
61 |
}
|
62 |
#endif
|
63 |
|
64 |
const T& data() const { check(); return *theData;}
|
65 |
|
66 |
T& unsharedData() {
|
67 |
check();
|
68 |
if ( references() > 1) {
|
69 |
theData->removeReference();
|
70 |
theData = Cloner().clone( *theData);
|
71 |
if (theData) theData->addReference();
|
72 |
}
|
73 |
return *theData;
|
74 |
}
|
75 |
|
76 |
T& sharedData() { check(); return *theData;}
|
77 |
|
78 |
bool isValid() const { return theData != 0;}
|
79 |
|
80 |
void check() const {
|
81 |
if (theData == 0)
|
82 |
throw TrajectoryStateException("Error: uninitialized ProxyBase used");
|
83 |
}
|
84 |
|
85 |
void destroy() { if (isValid()) theData->removeReference();}
|
86 |
|
87 |
int references() const {return theData->references();}
|
88 |
|
89 |
private:
|
90 |
T* theData;
|
91 |
};
|
92 |
|
93 |
template <class T, class Cloner >
|
94 |
inline
|
95 |
void swap(ProxyBase<T,Cloner>& lh, ProxyBase<T,Cloner>& rh) {
|
96 |
lh.swap(rh);
|
97 |
}
|
98 |
|
99 |
#endif // Tracker_ProxyBase_H
|