0023024: Update headers of OCCT files
[occt.git] / src / Standard / Handle_Standard_Transient.hxx
CommitLineData
b311480e 1// Copyright (c) 1998-1999 Matra Datavision
2// Copyright (c) 1999-2012 OPEN CASCADE SAS
3//
4// The content of this file is subject to the Open CASCADE Technology Public
5// License Version 6.5 (the "License"). You may not use the content of this file
6// except in compliance with the License. Please obtain a copy of the License
7// at http://www.opencascade.org and read it completely before using this file.
8//
9// The Initial Developer of the Original Code is Open CASCADE S.A.S., having its
10// main offices at: 1, place des Freres Montgolfier, 78280 Guyancourt, France.
11//
12// The Original Code and all software distributed under the License is
13// distributed on an "AS IS" basis, without warranty of any kind, and the
14// Initial Developer hereby disclaims all such warranties, including without
15// limitation, any warranties of merchantability, fitness for a particular
16// purpose or non-infringement. Please see the License for the specific terms
17// and conditions governing the rights and limitations under the License.
18
7fd59977 19#ifndef _Handle_Standard_Transient_HeaderFile
20#define _Handle_Standard_Transient_HeaderFile
21#ifndef _Standard_HeaderFile
22#include <Standard.hxx>
23#endif
1c35b92f 24#ifndef _Standard_DefineAlloc_HeaderFile
25#include <Standard_DefineAlloc.hxx>
26#endif
7fd59977 27#ifndef _Standard_Macro_HeaderFile
28#include <Standard_Macro.hxx>
29#endif
30#ifndef _Standard_PrimitiveTypes_HeaderFile
31#include <Standard_PrimitiveTypes.hxx>
32#endif
33#ifndef _Standard_Transient_proto_HeaderFile
34#include <Standard_Transient_proto.hxx>
35#endif
36
37#ifdef _WIN32
38// Disable the warning "conversion from 'unsigned int' to Standard_Transient *"
39#pragma warning (push)
40#pragma warning (disable:4312)
41#endif
42
43#ifndef UndefinedHandleAddress
44#ifdef _OCC64
45#define UndefinedHandleAddress ((Standard_Transient *)0xfefdfefdfefd0000)
46#else
47#define UndefinedHandleAddress ((Standard_Transient *)0xfefd0000)
48#endif
49#endif
50
51class Handle_Standard_Transient;
52
53Standard_EXPORT Standard_Integer HashCode(const Handle(Standard_Transient)& ,const Standard_Integer);
54
55/**
56 * Base class for hierarchy of smart pointers (Handles) for Transient
57 * objects. Uses reference counting technique to control life time
58 * of the referred object.
59 *
60 * Note that Handle should never be initialized by pointer to object
61 * created in the stack; only dinamically allocated pointers shall be used.
62 */
63
64class Handle(Standard_Transient)
65{
66public:
67 // Public methods
68
69 //! Empty constructor
70 Handle(Standard_Transient) ()
71 : entity(UndefinedHandleAddress)
72 {
73 }
74
75 //! Constructor from pointer to new object
76 Handle(Standard_Transient) (const Standard_Transient *anItem)
77 : entity ( anItem ? (Standard_Transient*)anItem : UndefinedHandleAddress )
78 {
79 BeginScope();
80 }
81
82 //! Copy constructor
83 Handle(Standard_Transient) (const Handle(Standard_Transient)& aTid)
84 : entity ( aTid.entity )
85 {
86 BeginScope();
87 }
88
89 //! Destructor
90 Standard_EXPORT ~Handle(Standard_Transient)()
91 {
92 EndScope();
93 }
94
95 //! Assignment operator
96 Handle(Standard_Transient)& operator=(const Handle(Standard_Transient)& aHandle)
97 {
98 Assign(aHandle.Access());
99 return *this;
100 }
101
102 //! Assignment operator
103 Handle(Standard_Transient)& operator=(const Standard_Transient* anItem)
104 {
105 Assign(anItem);
106 return *this;
107 }
108
109 //! Nullify the handle
110 void Nullify()
111 {
112 EndScope();
113 }
114
115 //! Check for being null
116 Standard_Boolean IsNull() const
117 {
118 return entity == UndefinedHandleAddress;
119 }
120
121 //! Returns pointer to referred object
122 Standard_Transient* Access()
123 {
124 return entity;
125 }
126
127 //! Returns const pointer to referred object
128 const Standard_Transient* Access() const
129 {
130 return entity;
131 }
132
133 //! Cast to pointer to referred object
134 operator Standard_Transient*()
135 {
136 return entity;
137 }
138
139 //! Cast to const pointer to referred object
140 operator const Standard_Transient*() const
141 {
142 return entity;
143 }
144
145 //! Member access operator (note non-const)
146 Standard_Transient* operator->() const
147 {
148 return entity;
149 }
150
151 //! Dereferencing operator
152 Standard_Transient& operator*()
153 {
154 return *entity;
155 }
156
157 //! Const dereferencing operator
158 const Standard_Transient& operator*() const
159 {
160 return *entity;
161 }
162
163 //! Check for equality
164 int operator==(const Handle(Standard_Transient)& right) const
165 {
166 return entity == right.entity;
167 }
168
169 //! Check for equality
170 int operator==(const Standard_Transient *right) const
171 {
172 return entity == right;
173 }
174
175 //! Check for equality
176 friend int operator==(const Standard_Transient *left, const Handle(Standard_Transient)& right)
177 {
178 return left == right.entity;
179 }
180
181 //! Check for inequality
182 int operator!=(const Handle(Standard_Transient)& right) const
183 {
184 return entity != right.entity;
185 }
186
187 //! Check for inequality
188 int operator!=(const Standard_Transient *right) const
189 {
190 return entity != right;
191 }
192
193 //! Check for inequality
194 friend int operator!=(const Standard_Transient *left, const Handle(Standard_Transient)& right)
195 {
196 return left != right.entity;
197 }
198
199 //! Down casting operator; dummy provided for consistency with other classes
200 //! (descendants)
201 static const Handle(Standard_Transient)& DownCast(const Handle(Standard_Transient)& AnObject)
202 {
203 return AnObject;
204 }
205
206 //! Dump pointer to a referred object to a stream
207 Standard_EXPORT void Dump(Standard_OStream& out) const;
208
209protected:
210 // Protected methods for descendants
211
212 //! Returns non-const pointer to referred object
213 Standard_Transient* ControlAccess() const
214 {
215 return entity;
216 }
217
218 //! Assignment
219 Standard_EXPORT void Assign (const Standard_Transient *anItem);
220
221private:
222 // Private methods
223
224 //! Increment reference counter of referred object
225 Standard_EXPORT void BeginScope();
226
227 //! Decrement reference counter and if 0, destroy referred object
228 Standard_EXPORT void EndScope();
229
230public:
7fd59977 231
1c35b92f 232 DEFINE_STANDARD_ALLOC
7fd59977 233
234private:
235 // Field
236 Standard_Transient *entity;
237};
238
239#ifdef _WIN32
240#pragma warning (pop)
241#endif
242
243#endif