b311480e |
1 | // Created on: 2009-01-30 |
2 | // Created by: Andrey BETENEV (abv) |
973c2be1 |
3 | // Copyright (c) 2009-2014 OPEN CASCADE SAS |
b311480e |
4 | // |
973c2be1 |
5 | // This file is part of Open CASCADE Technology software library. |
b311480e |
6 | // |
d5f74e42 |
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 |
973c2be1 |
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. |
b311480e |
12 | // |
973c2be1 |
13 | // Alternatively, this file may be used under the terms of Open CASCADE |
14 | // commercial license or contractual agreement. |
7fd59977 |
15 | |
16 | #ifndef NCollection_Handle_HeaderFile |
17 | #define NCollection_Handle_HeaderFile |
18 | |
25e59720 |
19 | #include <Standard_Transient.hxx> |
20 | #include <Standard_Handle.hxx> |
7fd59977 |
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. |
7fd59977 |
29 | |
30 | template <class T> |
25e59720 |
31 | class NCollection_Handle : public opencascade::handle<Standard_Transient> |
7fd59977 |
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 |
25e59720 |
43 | Ptr (T* theObj) : myPtr (theObj) {} |
7fd59977 |
44 | |
45 | //! Destructor deletes the object |
46 | ~Ptr () { if ( myPtr ) delete myPtr; myPtr = 0; } |
47 | |
96f3bacc |
48 | protected: |
49 | |
50 | //! Copy constructor |
51 | Ptr(const Ptr&); |
52 | |
53 | //! Assignment operator |
54 | Ptr& operator=(const Ptr&); |
55 | |
7fd59977 |
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) |
25e59720 |
64 | : opencascade::handle<Standard_Transient> (thePtr) {} |
7fd59977 |
65 | |
66 | public: |
dda67c1c |
67 | |
68 | typedef T element_type; |
69 | |
7fd59977 |
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) |
25e59720 |
75 | : opencascade::handle<Standard_Transient> (theObject ? new Ptr (theObject) : 0) {} |
7fd59977 |
76 | |
77 | //! Cast handle to contained type |
25e59720 |
78 | T* get () { return ((Ptr*)opencascade::handle<Standard_Transient>::get())->myPtr; } |
e7195ab4 |
79 | |
80 | //! Cast handle to contained type |
25e59720 |
81 | const T* get () const { return ((Ptr*)opencascade::handle<Standard_Transient>::get())->myPtr; } |
e7195ab4 |
82 | |
7fd59977 |
83 | //! Cast handle to contained type |
e7195ab4 |
84 | T* operator -> () { return get(); } |
7fd59977 |
85 | |
86 | //! Cast handle to contained type |
e7195ab4 |
87 | const T* operator -> () const { return get(); } |
7fd59977 |
88 | |
89 | //! Cast handle to contained type |
e7195ab4 |
90 | T& operator * () { return *get(); } |
7fd59977 |
91 | |
e7195ab4 |
92 | //! Cast handle to contained type |
93 | const T& operator * () const { return *get(); } |
94 | |
7fd59977 |
95 | //! Downcast arbitrary Handle to the argument type if contained |
96 | //! object is Handle for this type; returns null otherwise |
25e59720 |
97 | static NCollection_Handle<T> DownCast (const opencascade::handle<Standard_Transient>& theOther) |
7fd59977 |
98 | { |
69ff08ff |
99 | return NCollection_Handle<T>(dynamic_cast<Ptr*>(theOther.get()), 0); |
7fd59977 |
100 | } |
69ff08ff |
101 | |
7fd59977 |
102 | }; |
103 | |
104 | #endif |