0022048: Visualization, AIS_InteractiveContext - single object selection should alway...
[occt.git] / src / NCollection / NCollection_Handle.hxx
1 // Created on: 2009-01-30
2 // Created by: Andrey BETENEV (abv)
3 // Copyright (c) 2009-2014 OPEN CASCADE SAS
4 //
5 // This file is part of Open CASCADE Technology software library.
6 //
7 // This library is free software; you can redistribute it and/or modify it under
8 // the terms of the GNU Lesser General Public License version 2.1 as published
9 // by the Free Software Foundation, with special exception defined in the file
10 // OCCT_LGPL_EXCEPTION.txt. Consult the file LICENSE_LGPL_21.txt included in OCCT
11 // distribution for complete text of the license and disclaimer of any warranty.
12 //
13 // Alternatively, this file may be used under the terms of Open CASCADE
14 // commercial license or contractual agreement.
15
16 #ifndef NCollection_Handle_HeaderFile
17 #define NCollection_Handle_HeaderFile
18
19 #include <Standard_Transient.hxx>
20 #include <Standard_Handle.hxx>
21   
22 //! Purpose: This template class is used to define Handle adaptor
23 //! for allocated dynamically objects of arbitrary type.
24 //!
25 //! The advantage is that this handle will automatically destroy 
26 //! the object when last referred Handle is destroyed (i.e. it is a 
27 //! typical smart pointer), and that it can be handled as 
28 //! Handle(Standard_Transient) in OCCT components.
29
30 template <class T>
31 class NCollection_Handle : public opencascade::handle<Standard_Transient>
32 {
33  private:
34
35   //! Internal adaptor class wrapping actual type
36   //! and enhancing it by reference counter inherited from
37   //! Standard_Transient
38   class Ptr : public Standard_Transient
39   {
40   public:
41
42     //! Constructor: stores pointer to the object
43     Ptr (T* theObj) : myPtr (theObj) {}
44
45     //! Destructor deletes the object
46     ~Ptr () { if ( myPtr ) delete myPtr; myPtr = 0; }
47
48   protected:
49
50     //! Copy constructor
51     Ptr(const Ptr&);
52
53     //! Assignment operator
54     Ptr& operator=(const Ptr&);
55
56   public:
57     T* myPtr; //!< Pointer to the object
58   };
59   
60   //! Constructor of handle from pointer on newly allocated object.
61   //! Note that additional argument is used to avoid ambiguity with
62   //! public constructor from pointer when Handle is intilialized by 0.
63   NCollection_Handle (Ptr* thePtr, int) 
64   : opencascade::handle<Standard_Transient> (thePtr) {}
65   
66  public:
67
68   typedef T element_type;
69
70   //! Default constructor; creates null handle
71   NCollection_Handle () {}
72   
73   //! Constructor of handle from pointer on newly allocated object
74   NCollection_Handle (T* theObject) 
75   : opencascade::handle<Standard_Transient> (theObject ? new Ptr (theObject) : 0) {}
76   
77   //! Cast handle to contained type
78   T* get () { return ((Ptr*)opencascade::handle<Standard_Transient>::get())->myPtr; }
79
80   //! Cast handle to contained type
81   const T* get () const { return ((Ptr*)opencascade::handle<Standard_Transient>::get())->myPtr; }
82
83   //! Cast handle to contained type
84   T* operator -> () { return get(); }
85   
86   //! Cast handle to contained type
87   const T* operator -> () const { return get(); }
88   
89   //! Cast handle to contained type
90   T& operator * () { return *get(); }
91   
92   //! Cast handle to contained type
93   const T& operator * () const { return *get(); }
94
95   //! Downcast arbitrary Handle to the argument type if contained
96   //! object is Handle for this type; returns null otherwise
97   static NCollection_Handle<T> DownCast (const opencascade::handle<Standard_Transient>& theOther)
98   {
99     return NCollection_Handle<T>(dynamic_cast<Ptr*>(theOther.get()), 0);
100   }
101
102 };
103
104 #endif