BOSS 8.0.0
BESIII Offline Software System
Loading...
Searching...
No Matches
Event/GeneratorObject/include/DataModel/DataPtr.h
Go to the documentation of this file.
1#ifndef DATAMODEL_DATAPTR_H
2#define DATAMODEL_DATAPTR_H
3
4#ifndef _CPP_ALGORITHM
5# include <algorithm> // for std::swap
6#endif
7#include <cassert>
8#ifndef __TYPEINFO__
9# include <typeinfo>
10#endif
11
12template <typename T> class DataPtr {
13 /// A wrapper around boost shared_ptr. Adds automatic conversion to/from T*
14public:
15 typedef T DataPtr_type;
16 typedef unsigned short counter_type;
17
18 /// default constructor
19 DataPtr() : m_pointer( 0 ), p_count( new counter_type( 0 ) ) {
20#ifdef DATAPTR_DEBUG
21 cout << typeid( *this ).name() << " DEBUG: "
22 << "default constructor called on " << this << "->" << ptr() << " use_count "
23 << use_count() << endl;
24#endif
25 }
26
27 /// constructor from ptr: stores a copy of pointer
28 /// Y must be convertible to a T*
29 template <typename Y>
30 DataPtr( Y* pointer ) : m_pointer( pointer ), p_count( new counter_type( 1 ) ) {
31#ifdef DATAPTR_DEBUG
32 cout << typeid( *this ).name() << " DEBUG: "
33 << "pointer constructor called on " << this << "->" << ptr() << " input " << pointer
34 << " use_count " << use_count() << endl;
35#endif
36 }
37
38 /// copy contructor: makes a copy of the pointer in rhs and of its count ptr
39 DataPtr( const DataPtr& rhs ) : m_pointer( rhs.m_pointer ), p_count( rhs.p_count ) {
40 ++( *p_count );
41#ifdef DATAPTR_DEBUG
42 cout << typeid( *this ).name() << " DEBUG: "
43 << "copy constructor called on " << this << "->" << ptr() << " input " << rhs.ptr()
44 << " use_count " << use_count() << endl;
45#endif
46 }
47
48 /// the destructor will delete the object pointed to
50#ifdef DATAPTR_DEBUG
51 cout << typeid( *this ).name() << " DEBUG: "
52 << "destructor called on " << this << "->" << ptr() << " use_count " << use_count()
53 << endl;
54#endif
55 if ( *p_count == 0 )
56 {
57 assert( m_pointer == 0 );
58 delete p_count;
59 }
60 else if ( *p_count == 1 )
61 {
62 delete p_count;
63 delete m_pointer;
64 }
65 else --( *p_count );
66 }
67
68 /// dereference operators:
69 T& operator*() const { return *ptr(); }
70 T* operator->() const { return ptr(); }
71
72 // converter to pointer
73 operator T*() const { return ptr(); }
74
75 /// explicit pointer accessors (sometimes necessary to help the compiler)
76 T* ptr() const { return m_pointer; }
77
78 /// assignment operator, protect against self-assignment and exceptions
79 DataPtr& operator=( const DataPtr& rhs ) {
80 DataPtr<T> temp( rhs );
81 swap( temp );
82#ifdef DATAPTR_DEBUG
83 cout << typeid( *this ).name() << " DEBUG: "
84 << "equal operator called on " << this << "->" << ptr() << " input " << rhs.ptr()
85 << " use_count " << use_count() << endl;
86#endif
87 return *this;
88 }
89
90 /// assignment from a Convertible DataPtr
91 template <typename U> DataPtr& operator=( const DataPtr<U>& rhs ) {
92 DataPtr<T> temp( rhs );
93 swap( temp );
94#ifdef DATAPTR_DEBUG
95 cout << typeid( *this ).name() << " DEBUG: "
96 << "convert equal operator called on " << this << "->" << ptr() << " input "
97 << rhs.ptr() << " use_count " << use_count() << endl;
98#endif
99 return *this;
100 }
101
102 /// swap contents
103 void swap( DataPtr& rhs ) {
104 std::swap( m_pointer, rhs.m_pointer );
105 std::swap( p_count, rhs.p_count );
106#ifdef DATAPTR_DEBUG
107 cout << typeid( *this ).name() << " DEBUG: "
108 << "swap called on " << this << "->" << ptr() << " input " << rhs.ptr()
109 << " use_count " << use_count() << endl;
110#endif
111 }
112
113 /// reset contents
114 void reset() {
115 swap( DataPtr<T>(), this );
116#ifdef DATAPTR_DEBUG
117 cout << typeid( *this ).name() << " DEBUG: "
118 << "reset called on " << this << "->" << ptr() << " use_count " << use_count()
119 << endl;
120#endif
121 }
122
123 /// returns number of objects using the shared_ptr. Slow!
124 long use_count() const {
125 assert( p_count );
126 return *p_count;
127 }
128
129private:
130 T* m_pointer;
131 counter_type* p_count;
132};
133
134// comparison operator at the object level
135// this of course requires the pointed-to type T to define operator ==
136template <typename T> inline bool operator==( const DataPtr<T>& lhs, const DataPtr<T>& rhs ) {
137 return ( lhs.ptr() == rhs.ptr() );
138 // FIXME this requires T to be concrete
139 // boost::function_requires< boost::EqualityComparableConcept<T> >();
140 // FIXME this requires T to have an == oper
141 // return ((lhs.operator->() == 0 && rhs.operator->() == 0) ||
142 // (lhs.operator->() != 0 && rhs.operator->() != 0 && *lhs == *rhs) );
143}
144template <typename T> inline bool operator!=( const DataPtr<T>& lhs, const DataPtr<T>& rhs ) {
145 return !( lhs == rhs );
146}
147
148#endif
bool operator==(const DataPtr< T > &lhs, const DataPtr< T > &rhs)
bool operator!=(const DataPtr< T > &lhs, const DataPtr< T > &rhs)
T DataPtr_type
A wrapper around boost shared_ptr. Adds automatic conversion to/from T*.
long use_count() const
returns number of objects using the shared_ptr. Slow!
DataPtr & operator=(const DataPtr &rhs)
assignment operator, protect against self-assignment and exceptions
DataPtr(const DataPtr &rhs)
copy contructor: makes a copy of the pointer in rhs and of its count ptr
T * ptr() const
explicit pointer accessors (sometimes necessary to help the compiler)
DataPtr & operator=(const DataPtr< U > &rhs)
assignment from a Convertible DataPtr
~DataPtr()
the destructor will delete the object pointed to
void swap(DataPtr &rhs)
swap contents
T & operator*() const
dereference operators: