0eed97c9da3ac9e4c5c42a7df1aa479e4c973a6e
[occt.git] / src / Standard / Standard_Handle.hxx
1 // Copyright (c) 2014 OPEN CASCADE SAS
2 //
3 // This file is part of Open CASCADE Technology software library.
4 //
5 // This library is free software; you can redistribute it and/or modify it under
6 // the terms of the GNU Lesser General Public License version 2.1 as published
7 // by the Free Software Foundation, with special exception defined in the file
8 // OCCT_LGPL_EXCEPTION.txt. Consult the file LICENSE_LGPL_21.txt included in OCCT
9 // distribution for complete text of the license and disclaimer of any warranty.
10 //
11 // Alternatively, this file may be used under the terms of Open CASCADE
12 // commercial license or contractual agreement.
13
14 #ifndef _Standard_Handle_HeaderFile
15 #define _Standard_Handle_HeaderFile
16
17 #include <Standard_Address.hxx>
18 #include <Standard_Stream.hxx>
19 #include <Standard_Transient.hxx>
20
21 #include <type_traits>
22
23 class Standard_Transient;
24
25 namespace opencascade {
26
27   //! Trait yielding true if class T1 is base of T2 but not the same
28   template <class T1, class T2, class Dummy = void>
29   struct is_base_but_not_same : std::is_base_of <T1, T2> {};
30
31   //! Explicit specialization of is_base_of trait to workaround the
32   //! requirement of type to be complete when T1 and T2 are the same.
33   template <class T1, class T2>
34   struct is_base_but_not_same <T1, T2, typename std::enable_if <std::is_same <T1, T2>::value>::type> : std::false_type {};
35
36   //! Intrusive smart pointer for use with Standard_Transient class and its descendants.
37   //!
38   //! This class is similar to boost::intrusive_ptr<>, with additional
39   //! feature historically supported by Handles in OCCT:
40   //! it has type conversion to const reference to handle to the base types,
41   //! which allows it to be passed by reference
42   //! in functions accepting reference to handle to base class.
43   //!
44   //! These casts (potentially unsafe) can be disabled by defining macro
45   //! OCCT_HANDLE_NOCAST; if it is defined, generalized copy constructor
46   //! and assignment operators are defined allowing to initialize handle
47   //! of base type from handle to derived type.
48   template <class T>
49   class handle
50   {
51   public:
52     //! STL-compliant typedef of contained type
53     typedef T element_type;
54
55   public:
56   
57     //! Empty constructor
58     handle () : entity(0) {}
59
60     //! Constructor from pointer to new object
61     handle (const T *thePtr) : entity(const_cast<T*>(thePtr))
62     {
63       BeginScope();
64     }
65
66     //! Copy constructor
67     handle (const handle& theHandle) : entity(theHandle.entity)
68     {
69       BeginScope();
70     }
71
72     //! Move constructor
73     handle (handle&& theHandle) : entity(theHandle.entity)
74     {
75       theHandle.entity = 0;
76     }
77
78     //! Destructor
79     ~handle ()
80     {
81       EndScope();
82     }
83
84     //! Nullify the handle
85     void Nullify()
86     {
87       EndScope();
88     }
89
90     //! Check for being null
91     bool IsNull() const { return entity == 0; } 
92
93     //! Reset by new pointer
94     void reset (T* thePtr)
95     {
96       Assign (thePtr);
97     }
98
99     //! Assignment operator
100     handle& operator= (const handle& theHandle)
101     {
102       Assign (theHandle.entity);
103       return *this;
104     }
105
106     //! Assignment to pointer
107     handle& operator= (const T* thePtr)
108     {
109       Assign (const_cast<T*>(thePtr));
110       return *this;
111     }
112
113     //! Move operator
114     handle& operator= (handle&& theHandle)
115     {
116       std::swap (this->entity, theHandle.entity);
117       return *this;
118     }
119
120     //! STL-like cast to pointer to referred object
121     const T* get () const { return static_cast<const T*>(this->entity); }
122
123     //! STL-like cast to pointer to referred object
124     T* get () { return static_cast<T*>(this->entity); }
125
126     //! Member access operator (note non-const)
127     T* operator-> () const { return static_cast<T*>(this->entity); }
128
129     //! Dereferencing operator
130     T& operator* () { return *get(); }
131
132     //! Const dereferencing operator
133     const T& operator*() const { return *get(); }
134
135     //! Check for equality
136     template <class T2>
137     bool operator== (const handle<T2>& theHandle) const
138     { 
139       return get() == theHandle.get();
140     }
141
142     //! Check for equality
143     template <class T2>
144     bool operator== (const T2 *thePtr) const
145     { 
146       return get() == thePtr;
147     }
148
149     //! Check for equality
150     template <class T2>
151     friend bool operator== (const T2 *left, const handle& right)
152     {
153       return left == right.get();
154     }
155
156     //! Check for inequality
157     template <class T2>
158     bool operator!= (const handle<T2>& theHandle) const
159     {
160       return get() != theHandle.get();
161     }
162
163     //! Check for inequality
164     template <class T2>
165     bool operator!= (const T2 *thePtr) const
166     {
167       return get() != thePtr;
168     }
169
170     //! Check for inequality
171     template <class T2>
172     friend bool operator!= (const T2 *left, const handle& right)
173     {
174       return left != right.get();
175     }
176
177     //! Compare operator for possible use in std::map<> etc. 
178     template <class T2>
179     bool operator< (const handle<T2>& theHandle) const
180     { 
181       return get() < theHandle.get();
182     }
183
184     //! Down casting operator from handle to base type
185     template <class T2>
186     static typename std::enable_if<is_base_but_not_same<T2, T>::value, handle>::type
187       DownCast (const handle<T2>& theObject)
188     {
189       return handle (dynamic_cast<T*>(const_cast<T2*>(theObject.get())));
190     }
191
192     //! Down casting operator from pointer to base type
193     template <class T2>
194     static typename std::enable_if<is_base_but_not_same<T2, T>::value, handle>::type 
195       DownCast (const T2* thePtr)
196     {
197       return handle (dynamic_cast<T*>(const_cast<T2*>(thePtr)));
198     }
199
200     //! For compatibility, define down casting operator from non-base type, as deprecated
201     template <class T2>
202     Standard_DEPRECATED("down-casting from object of the same or unrelated type is meaningless")
203     static handle DownCast (const handle<T2>& theObject, typename std::enable_if<!is_base_but_not_same<T2, T>::value, void*>::type = 0)
204     {
205       return handle (dynamic_cast<T*>(const_cast<T2*>(theObject.get())));
206     }
207
208     //! For compatibility, define down casting operator from non-base type, as deprecated
209     template <class T2>
210     Standard_DEPRECATED("down-casting from object of the same or unrelated type is meaningless")
211     static handle DownCast (const T2* thePtr, typename std::enable_if<!is_base_but_not_same<T2, T>::value, void*>::type = 0)
212     {
213       return handle (dynamic_cast<T*>(const_cast<T2*>(thePtr)));
214     }
215
216 #if (defined(__clang__)) || (defined(__INTEL_COMPILER) && __INTEL_COMPILER >= 1300) || \
217     (defined(_MSC_VER) && _MSC_VER >= 1800) || \
218     (defined(__GNUC__) && (__GNUC__ > 4 || (__GNUC__ == 4 && __GNUC_MINOR__ >= 6)))
219
220     //! Conversion to bool for use in conditional expressions
221     explicit operator bool () const
222     { 
223       return entity != nullptr;
224     }
225
226 #else /* fallback version for compilers not supporting explicit conversion operators (VC10, VC11, GCC below 4.5) */
227
228     //! Conversion to bool-compatible type for use in conditional expressions
229     operator Standard_Transient* handle::* () const
230     { 
231       return entity ? &handle::entity : 0;
232     }
233
234 #endif
235
236     // Support of conversions to handle of base type:
237     // - copy and move constructors and assignment operators if OCCT_HANDLE_NOCAST is defined
238     // - operators of upcast to const reference to base type otherwise
239 #if (defined(__clang__)) || (defined(__INTEL_COMPILER) && __INTEL_COMPILER >= 1206) || \
240     (defined(_MSC_VER) && _MSC_VER >= 1800) || \
241     (defined(__GNUC__) && (__GNUC__ > 4 || (__GNUC__ == 4 && __GNUC_MINOR__ >= 3)))
242
243 #ifdef OCCT_HANDLE_NOCAST
244
245     //! Generalized copy constructor.
246     //! Constructs handle holding entity of base type (T) from the one which holds entity of derived type (T2).
247     template <class T2, typename = typename std::enable_if <is_base_but_not_same <T, T2>::value>::type>
248     handle (const handle<T2>& theHandle) :
249       entity(theHandle.entity)
250     {
251       BeginScope();
252     }
253
254     //! Generalized move constructor
255     template <class T2, typename = typename std::enable_if <is_base_but_not_same <T, T2>::value>::type>
256     handle (handle<T2>&& theHandle)
257       : entity(theHandle.entity)
258     {
259       theHandle.entity = 0;
260     }
261
262     //! Generalized assignment operator
263     template <class T2, typename = typename std::enable_if <is_base_but_not_same <T, T2>::value>::type>
264     handle operator = (const handle<T2>& theHandle)
265     {
266       Assign (theHandle.entity);
267       return *this;
268     }
269
270     //! Generalized move operator
271     template <class T2, typename = typename std::enable_if <is_base_but_not_same <T, T2>::value>::type>
272     handle& operator= (handle<T2>&& theHandle)
273     {
274       std::swap (this->entity, theHandle.entity);
275       return *this;
276     }
277
278 #else
279
280     //! Upcast to const reference to base type.
281     template <class T2, typename = typename std::enable_if<is_base_but_not_same<T2, T>::value>::type>
282     operator const handle<T2>& () const
283     {
284       return reinterpret_cast<const handle<T2>&>(*this);
285     }
286
287     //! Upcast to non-const reference to base type.
288     //! NB: this cast can be dangerous, but required for legacy code; see #26377
289     template <class T2, typename = typename std::enable_if<is_base_but_not_same<T2, T>::value>::type>
290     operator handle<T2>& ()
291     {
292       return reinterpret_cast<handle<T2>&>(*this);
293     }
294
295 #endif /* OCCT_HANDLE_NOCAST */
296
297 #else /* fallback version for compilers not supporting default arguments of function templates (VC10, VC11, GCC below 4.3) */
298
299 #ifdef OCCT_HANDLE_NOCAST
300
301     //! Generalized copy constructor.
302     //! Constructs handle holding entity of base type (T) from the one which holds entity of derived type (T2).
303     template <class T2>
304     handle (const handle<T2>& theHandle, typename std::enable_if <is_base_but_not_same <T, T2>::value>::type* = nullptr) :
305       entity(theHandle.entity)
306     {
307       BeginScope();
308     }
309
310     //! Generalized move constructor
311     template <class T2>
312     handle (handle<T2>&& theHandle, typename std::enable_if <is_base_but_not_same <T, T2>::value>::type* = nullptr)
313       : entity(theHandle.entity)
314     {
315       theHandle.entity = 0;
316     }
317
318     //! Generalized assignment operator.
319     template <class T2>
320     handle operator = (const handle<T2>& theHandle)
321     {
322       std::enable_if <is_base_but_not_same <T, T2>::value, void*>::type aTypeCheckHelperVar;
323       (void)aTypeCheckHelperVar;
324       Assign (theHandle.entity);
325       return *this;
326     }
327
328     //! Generalized move operator
329     template <class T2>
330     handle& operator= (handle<T2>&& theHandle)
331     {
332       std::enable_if <is_base_but_not_same <T, T2>::value, void*>::type aTypeCheckHelperVar;
333       (void)aTypeCheckHelperVar;
334       std::swap (this->entity, theHandle.entity);
335       return *this;
336     }
337
338 #else
339
340     //! Upcast to const reference to base type.
341     //! NB: this implementation will cause ambiguity errors on calls to overloaded
342     //! functions accepting handles to different types, since compatibility is 
343     //! checked in the cast code rather than ensured by SFINAE (possible with C++11)
344     template <class T2>
345     operator const handle<T2>& () const
346     {
347       // error "type is not a member of enable_if" will be generated if T2 is not sub-type of T
348       // (handle is being cast to const& to handle of non-base type)
349       return reinterpret_cast<typename std::enable_if<is_base_but_not_same<T2, T>::value, const handle<T2>&>::type>(*this);
350     }
351
352     //! Upcast to non-const reference to base type.
353     //! NB: this cast can be dangerous, but required for legacy code; see #26377
354     template <class T2>
355     Standard_DEPRECATED("Passing non-const reference to handle of base type in function is unsafe; use variable of exact type")
356     operator handle<T2>& ()
357     {
358       // error "type is not a member of enable_if" will be generated if T2 is not sub-type of T
359       // (handle is being cast to const& to handle of non-base type)
360       return reinterpret_cast<typename std::enable_if<is_base_but_not_same<T2, T>::value, handle<T2>&>::type>(*this);
361     }
362
363 #endif /* OCCT_HANDLE_NOCAST */
364
365 #endif /* compiler switch */
366
367   private:
368
369     //! Assignment
370     void Assign (Standard_Transient *thePtr)
371     {
372       if (thePtr == entity)
373         return;
374       EndScope();
375       entity = thePtr;
376       BeginScope();
377     }
378   
379     //! Increment reference counter of referred object 
380     void BeginScope()
381     {
382       if (entity != 0)
383         entity->IncrementRefCounter();
384     }
385
386     //! Decrement reference counter and if 0, destroy referred object
387     void EndScope()
388     {
389       if (entity != 0 && entity->DecrementRefCounter() == 0)
390         entity->Delete();
391       entity = 0;
392     }
393
394     template <class T2> friend class handle;
395
396   private:
397     Standard_Transient* entity;
398   };
399
400 } // namespace opencascade
401
402 //! Define Handle() macro
403 #define Handle(Class) opencascade::handle<Class>
404
405 //! Global method HashCode(), for use in hash maps
406 template <class T>
407 inline Standard_Integer HashCode (const Handle(T)& theHandle, const Standard_Integer theUpper)
408 {
409   return ::HashCode (const_cast<Standard_Address>(static_cast<const void*>(theHandle.get())), theUpper);
410 }
411
412 //! For compatibility with previous versions of OCCT, defines typedef opencascade::handle<Class> Handle(Class)
413 #define DEFINE_STANDARD_HANDLECLASS(C1,C2,BC) class C1; typedef Handle(C1) Handle_##C1;
414 #define DEFINE_STANDARD_HANDLE(C1,C2) DEFINE_STANDARD_HANDLECLASS(C1,C2,Standard_Transient)
415 #define DEFINE_STANDARD_PHANDLE(C1,C2) DEFINE_STANDARD_HANDLECLASS(C1,C2,Standard_Persistent)
416
417 #endif