efdabe405ac4176f7b37d8a4fa015a2a7c0b219b
[occt.git] / src / Standard / Handle_Standard_Persistent.hxx
1 // Copyright (c) 1998-1999 Matra Datavision
2 // Copyright (c) 1999-2014 OPEN CASCADE SAS
3 //
4 // This file is part of Open CASCADE Technology software library.
5 //
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.
11 //
12 // Alternatively, this file may be used under the terms of Open CASCADE
13 // commercial license or contractual agreement.
14
15 //============================================================================
16 // Title : Handle_Standard_Persistent.hxx 
17 // Role  : This file just include <Standard_Persistent.hxx>
18 //============================================================================
19
20 #ifndef _Handle_Standard_Persistent_HeaderFile
21 #define _Handle_Standard_Persistent_HeaderFile
22
23 #ifndef _Standard_DefineAlloc_HeaderFile
24 #include <Standard_DefineAlloc.hxx>
25 #endif
26 #ifndef _Standard_Macro_HeaderFile
27 #include <Standard_Macro.hxx>
28 #endif
29 #ifndef _Standard_PrimitiveTypes_HeaderFile
30 #include <Standard_PrimitiveTypes.hxx>
31 #endif
32 #ifndef _Standard_Persistent_proto_HeaderFile
33 #include <Standard_Persistent_proto.hxx>
34 #endif
35
36 #ifdef _WIN32
37 // Disable the warning "conversion from 'unsigned int' to Standard_Persistent *"
38 #pragma warning (push)
39 #pragma warning (disable:4312)
40 #endif
41
42 class Standard_Persistent;
43 class Handle_Standard_Type;
44 class Handle_Standard_Persistent;
45
46 Standard_EXPORT Standard_Integer HashCode(const Handle(Standard_Persistent)& ,
47                                           const Standard_Integer);
48
49 class Handle(Standard_Persistent)
50  {
51    private:
52
53     Standard_Persistent *entity;
54
55     Standard_EXPORT void RaiseNullObject(const Standard_CString S) const;
56
57     void BeginScope() const
58       {
59        if (entity != 0) entity->count++;
60       }    
61
62     void EndScope()
63       {
64        if (entity != 0) 
65          {
66           entity->count--;
67           if (entity->count == 0) {
68             entity->Delete();
69             entity = 0 ;
70           }
71         }
72       }
73
74
75    public:
76
77     DEFINE_STANDARD_ALLOC
78
79     Handle(Standard_Persistent)()
80       {
81        entity = 0 ;
82       }
83
84     Handle(Standard_Persistent)(const Handle(Standard_Persistent)& aTid) 
85       {
86        entity = aTid.entity;
87        BeginScope();
88       } 
89
90     Handle(Standard_Persistent)(const Standard_Persistent *anItem)
91       {
92        if (!anItem)
93            entity = 0 ;
94        else {
95          entity = (Standard_Persistent *)anItem;
96          BeginScope();
97        }
98       }
99
100      Standard_EXPORT void Dump(Standard_OStream& out) const;
101
102     Standard_EXPORT ~Handle(Standard_Persistent)();
103
104      Standard_EXPORT void  ShallowDump(Standard_OStream&) const;
105      
106     bool operator==(const Handle(Standard_Persistent)& right) const
107       {
108        return entity == right.entity;
109       }
110
111     bool operator==(const Standard_Persistent *right) const
112       {
113        return entity == right;
114       }
115
116     friend bool operator==(const Standard_Persistent *left, const Handle(Standard_Persistent)& right)
117       {
118        return left == right.entity;
119       }
120
121     bool operator!=(const Handle(Standard_Persistent)& right) const
122       {
123        return entity != right.entity;
124       }
125
126     bool operator!=(const Standard_Persistent *right) const
127       {
128        return entity != right;
129       }
130
131     friend bool operator!=(const Standard_Persistent *left, const Handle(Standard_Persistent)& right)
132       {
133        return left != right.entity;
134       }
135
136     void Nullify()
137       {
138        EndScope();
139        entity =  0 ;
140       }
141
142     Standard_Boolean IsNull() const
143       {
144        return entity == 0 ;
145       } 
146
147     Standard_Persistent* Access() const
148       {
149        return entity;
150       } 
151
152    protected:
153
154     Standard_Persistent* ControlAccess() const
155       {
156        return entity;
157       } 
158
159     void Assign(const Standard_Persistent *anItem)
160       {
161        EndScope();
162        if (!anItem)
163            entity = 0 ;
164        else {
165          entity = (Standard_Persistent *)anItem;
166          BeginScope();
167        }
168       }
169
170
171   public:
172
173    operator Standard_Persistent*()
174      {
175        return Access();
176      }
177
178
179    Standard_EXPORT Handle(Standard_Persistent)& operator=(const Handle(Standard_Persistent)& aHandle);
180    Standard_EXPORT Handle(Standard_Persistent)& operator=(const Standard_Persistent* anItem);
181
182    Standard_Persistent* operator->() 
183      {
184       return ControlAccess();
185      }
186
187    Standard_Persistent* operator->() const
188      {
189       return ControlAccess();
190      }
191
192    Standard_Persistent& operator*()
193      {
194       return *(ControlAccess());
195      }
196
197    const Standard_Persistent& operator*() const
198      {
199       return *(ControlAccess());
200      }
201
202    Standard_EXPORT static const Handle(Standard_Persistent) DownCast(const Handle(Standard_Persistent)& AnObject);
203 };
204
205 class Standard_Type;
206
207 #ifdef _WIN32
208 #pragma warning (pop)
209 #endif
210
211 #endif