GeoModel - GeoModelCore
ConstLink.h
1 /*
2  Copyright (C) 2002-2017 CERN for the benefit of the ATLAS collaboration
3 */
4 
5 #ifndef GEOMODELKERNEL_CONSTLINK_H
6 #define GEOMODELKERNEL_CONSTLINK_H
7 
18 template <class T>
19 class ConstLink
20 {
21  public:
22 
23 
27  ConstLink();
28 
32  ConstLink(const ConstLink< T > &right);
33 
37  ConstLink (const T *target);
38 
42  virtual ~ConstLink();
43 
47  ConstLink< T > & operator=(const ConstLink< T > &right);
48 
52  int operator==(const ConstLink< T > &right) const;
53 
57  int operator!=(const ConstLink< T > &right) const;
58 
59 
63  int operator<(const ConstLink< T > &right) const;
64 
68  int operator>(const ConstLink< T > &right) const;
69 
73  int operator<=(const ConstLink< T > &right) const;
74 
78  int operator>=(const ConstLink< T > &right) const;
79 
80 
81 
85  virtual const T & operator * () const;
86 
90  virtual const T * operator -> () const;
91 
95  operator bool () const;
96 
97 
98 
99  private:
103  mutable T *m_ptr;
104 };
105 
106 
107 
108 template <class T>
110  :m_ptr(nullptr)
111 {
112 }
113 
114 template <class T>
116  :m_ptr(right.m_ptr)
117 {
118  if (m_ptr) m_ptr->ref();
119 }
120 
121 template <class T>
122 inline ConstLink<T>::ConstLink (const T *target)
123  :m_ptr(const_cast<T *> (target))
124 {
125  if (target) target->ref();
126 }
127 
128 
129 template <class T>
131 {
132  if (m_ptr) m_ptr->unref();
133 }
134 
135 
136 template <class T>
138 {
139  if (this!=&right) {
140  if (m_ptr) m_ptr->unref();
141  m_ptr = right.m_ptr;
142  if (m_ptr) m_ptr->ref();
143  }
144  return *this;
145 }
146 
147 
148 template <class T>
149 inline int ConstLink<T>::operator==(const ConstLink<T> &right) const
150 {
151  return m_ptr==right.m_ptr;
152 }
153 
154 template <class T>
155 inline int ConstLink<T>::operator!=(const ConstLink<T> &right) const
156 {
157  return m_ptr!=right.m_ptr;
158 }
159 
160 
161 template <class T>
162 inline int ConstLink<T>::operator<(const ConstLink<T> &right) const
163 {
164  return m_ptr<right.m_ptr;
165 }
166 
167 template <class T>
168 inline int ConstLink<T>::operator>(const ConstLink<T> &right) const
169 {
170  return m_ptr>right.m_ptr;
171 }
172 
173 template <class T>
174 inline int ConstLink<T>::operator<=(const ConstLink<T> &right) const
175 {
176  return m_ptr<=right.m_ptr;
177 }
178 
179 template <class T>
180 inline int ConstLink<T>::operator>=(const ConstLink<T> &right) const
181 {
182  return m_ptr>=right.m_ptr;
183 }
184 
185 
186 
187 template <class T>
188 inline const T & ConstLink<T>::operator * () const
189 {
190  return *m_ptr;
191 }
192 
193 template <class T>
194 inline const T * ConstLink<T>::operator -> () const
195 {
196  return m_ptr;
197 }
198 
199 template <class T>
200 inline ConstLink<T>::operator bool () const
201 {
202  return m_ptr;
203 }
204 
205 #endif