b311480e |
1 | // Copyright (c) 1998-1999 Matra Datavision |
973c2be1 |
2 | // Copyright (c) 1999-2014 OPEN CASCADE SAS |
b311480e |
3 | // |
973c2be1 |
4 | // This file is part of Open CASCADE Technology software library. |
b311480e |
5 | // |
973c2be1 |
6 | // This library is free software; you can redistribute it and / or modify it |
7 | // under the terms of the GNU Lesser General Public version 2.1 as published |
8 | // by the Free Software Foundation, with special exception defined in the file |
9 | // OCCT_LGPL_EXCEPTION.txt. Consult the file LICENSE_LGPL_21.txt included in OCCT |
10 | // distribution for complete text of the license and disclaimer of any warranty. |
b311480e |
11 | // |
973c2be1 |
12 | // Alternatively, this file may be used under the terms of Open CASCADE |
13 | // commercial license or contractual agreement. |
b311480e |
14 | |
7fd59977 |
15 | #ifndef _Handle_Standard_Transient_HeaderFile |
16 | #define _Handle_Standard_Transient_HeaderFile |
17 | #ifndef _Standard_HeaderFile |
18 | #include <Standard.hxx> |
19 | #endif |
1c35b92f |
20 | #ifndef _Standard_DefineAlloc_HeaderFile |
21 | #include <Standard_DefineAlloc.hxx> |
22 | #endif |
7fd59977 |
23 | #ifndef _Standard_Macro_HeaderFile |
24 | #include <Standard_Macro.hxx> |
25 | #endif |
26 | #ifndef _Standard_PrimitiveTypes_HeaderFile |
27 | #include <Standard_PrimitiveTypes.hxx> |
28 | #endif |
29 | #ifndef _Standard_Transient_proto_HeaderFile |
30 | #include <Standard_Transient_proto.hxx> |
31 | #endif |
32 | |
33 | #ifdef _WIN32 |
34 | // Disable the warning "conversion from 'unsigned int' to Standard_Transient *" |
35 | #pragma warning (push) |
36 | #pragma warning (disable:4312) |
37 | #endif |
38 | |
39 | #ifndef UndefinedHandleAddress |
40 | #ifdef _OCC64 |
41 | #define UndefinedHandleAddress ((Standard_Transient *)0xfefdfefdfefd0000) |
42 | #else |
43 | #define UndefinedHandleAddress ((Standard_Transient *)0xfefd0000) |
44 | #endif |
45 | #endif |
46 | |
47 | class Handle_Standard_Transient; |
48 | |
49 | Standard_EXPORT Standard_Integer HashCode(const Handle(Standard_Transient)& ,const Standard_Integer); |
50 | |
51 | /** |
52 | * Base class for hierarchy of smart pointers (Handles) for Transient |
53 | * objects. Uses reference counting technique to control life time |
54 | * of the referred object. |
55 | * |
56 | * Note that Handle should never be initialized by pointer to object |
57 | * created in the stack; only dinamically allocated pointers shall be used. |
58 | */ |
59 | |
60 | class Handle(Standard_Transient) |
61 | { |
62 | public: |
63 | // Public methods |
64 | |
65 | //! Empty constructor |
66 | Handle(Standard_Transient) () |
67 | : entity(UndefinedHandleAddress) |
68 | { |
69 | } |
70 | |
71 | //! Constructor from pointer to new object |
72 | Handle(Standard_Transient) (const Standard_Transient *anItem) |
73 | : entity ( anItem ? (Standard_Transient*)anItem : UndefinedHandleAddress ) |
74 | { |
75 | BeginScope(); |
76 | } |
77 | |
78 | //! Copy constructor |
79 | Handle(Standard_Transient) (const Handle(Standard_Transient)& aTid) |
80 | : entity ( aTid.entity ) |
81 | { |
82 | BeginScope(); |
83 | } |
84 | |
85 | //! Destructor |
86 | Standard_EXPORT ~Handle(Standard_Transient)() |
87 | { |
88 | EndScope(); |
89 | } |
90 | |
91 | //! Assignment operator |
92 | Handle(Standard_Transient)& operator=(const Handle(Standard_Transient)& aHandle) |
93 | { |
94 | Assign(aHandle.Access()); |
95 | return *this; |
96 | } |
97 | |
98 | //! Assignment operator |
99 | Handle(Standard_Transient)& operator=(const Standard_Transient* anItem) |
100 | { |
101 | Assign(anItem); |
102 | return *this; |
103 | } |
104 | |
105 | //! Nullify the handle |
106 | void Nullify() |
107 | { |
108 | EndScope(); |
109 | } |
110 | |
111 | //! Check for being null |
112 | Standard_Boolean IsNull() const |
113 | { |
114 | return entity == UndefinedHandleAddress; |
115 | } |
116 | |
117 | //! Returns pointer to referred object |
118 | Standard_Transient* Access() |
119 | { |
120 | return entity; |
121 | } |
122 | |
123 | //! Returns const pointer to referred object |
124 | const Standard_Transient* Access() const |
125 | { |
126 | return entity; |
127 | } |
128 | |
129 | //! Cast to pointer to referred object |
130 | operator Standard_Transient*() |
131 | { |
132 | return entity; |
133 | } |
134 | |
135 | //! Cast to const pointer to referred object |
136 | operator const Standard_Transient*() const |
137 | { |
138 | return entity; |
139 | } |
140 | |
141 | //! Member access operator (note non-const) |
142 | Standard_Transient* operator->() const |
143 | { |
144 | return entity; |
145 | } |
146 | |
147 | //! Dereferencing operator |
148 | Standard_Transient& operator*() |
149 | { |
150 | return *entity; |
151 | } |
152 | |
153 | //! Const dereferencing operator |
154 | const Standard_Transient& operator*() const |
155 | { |
156 | return *entity; |
157 | } |
158 | |
159 | //! Check for equality |
160 | int operator==(const Handle(Standard_Transient)& right) const |
161 | { |
162 | return entity == right.entity; |
163 | } |
164 | |
165 | //! Check for equality |
166 | int operator==(const Standard_Transient *right) const |
167 | { |
168 | return entity == right; |
169 | } |
170 | |
171 | //! Check for equality |
172 | friend int operator==(const Standard_Transient *left, const Handle(Standard_Transient)& right) |
173 | { |
174 | return left == right.entity; |
175 | } |
176 | |
177 | //! Check for inequality |
178 | int operator!=(const Handle(Standard_Transient)& right) const |
179 | { |
180 | return entity != right.entity; |
181 | } |
182 | |
183 | //! Check for inequality |
184 | int operator!=(const Standard_Transient *right) const |
185 | { |
186 | return entity != right; |
187 | } |
188 | |
189 | //! Check for inequality |
190 | friend int operator!=(const Standard_Transient *left, const Handle(Standard_Transient)& right) |
191 | { |
192 | return left != right.entity; |
193 | } |
194 | |
195 | //! Down casting operator; dummy provided for consistency with other classes |
196 | //! (descendants) |
197 | static const Handle(Standard_Transient)& DownCast(const Handle(Standard_Transient)& AnObject) |
198 | { |
199 | return AnObject; |
200 | } |
201 | |
202 | //! Dump pointer to a referred object to a stream |
203 | Standard_EXPORT void Dump(Standard_OStream& out) const; |
204 | |
205 | protected: |
206 | // Protected methods for descendants |
207 | |
208 | //! Returns non-const pointer to referred object |
209 | Standard_Transient* ControlAccess() const |
210 | { |
211 | return entity; |
212 | } |
213 | |
214 | //! Assignment |
215 | Standard_EXPORT void Assign (const Standard_Transient *anItem); |
216 | |
217 | private: |
218 | // Private methods |
219 | |
220 | //! Increment reference counter of referred object |
221 | Standard_EXPORT void BeginScope(); |
222 | |
223 | //! Decrement reference counter and if 0, destroy referred object |
224 | Standard_EXPORT void EndScope(); |
225 | |
226 | public: |
7fd59977 |
227 | |
1c35b92f |
228 | DEFINE_STANDARD_ALLOC |
7fd59977 |
229 | |
230 | private: |
231 | // Field |
232 | Standard_Transient *entity; |
233 | }; |
234 | |
024855ee |
235 | //! Function in global scope to check handles for equality. |
236 | //! Will be used with standard OCCT collections like NCollection_DataMap within NCollection_DefaultHasher |
237 | //! when there are no specialization defined for concrete type. |
238 | //! Notice that this implementation compares only pointers to objects! |
239 | inline Standard_Boolean IsEqual (const Handle(Standard_Transient)& theFirst, |
240 | const Handle(Standard_Transient)& theSecond) |
241 | { |
242 | return theFirst == theSecond; |
243 | } |
244 | |
7fd59977 |
245 | #ifdef _WIN32 |
246 | #pragma warning (pop) |
247 | #endif |
248 | |
249 | #endif |