9ad299fd9b99c061721e9605885c1e66cad65b7e
[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 under
7 // the terms of the GNU Lesser General Public License 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 #include <Standard_DefineAlloc.hxx>
24 #include <Standard_TypeDef.hxx>
25 #include <Standard_Persistent_proto.hxx>
26
27 #ifdef _WIN32
28 // Disable the warning "conversion from 'unsigned int' to Standard_Persistent *"
29 #pragma warning (push)
30 #pragma warning (disable:4312)
31 #endif
32
33 class Standard_Persistent;
34 class Handle_Standard_Type;
35 class Handle_Standard_Persistent;
36
37 Standard_EXPORT Standard_Integer HashCode(const Handle(Standard_Persistent)& ,
38                                           const Standard_Integer);
39
40 class Handle(Standard_Persistent)
41  {
42    private:
43
44     Standard_Persistent *entity;
45
46     Standard_EXPORT void RaiseNullObject(const Standard_CString S) const;
47
48     void BeginScope() const
49       {
50        if (entity != 0) entity->count++;
51       }    
52
53     void EndScope()
54       {
55        if (entity != 0) 
56          {
57           entity->count--;
58           if (entity->count == 0) {
59             entity->Delete();
60             entity = 0 ;
61           }
62         }
63       }
64
65
66    public:
67
68     DEFINE_STANDARD_ALLOC
69
70     Handle(Standard_Persistent)()
71       {
72        entity = 0 ;
73       }
74
75     Handle(Standard_Persistent)(const Handle(Standard_Persistent)& aTid) 
76       {
77        entity = aTid.entity;
78        BeginScope();
79       } 
80
81     Handle(Standard_Persistent)(const Standard_Persistent *anItem)
82       {
83        if (!anItem)
84            entity = 0 ;
85        else {
86          entity = (Standard_Persistent *)anItem;
87          BeginScope();
88        }
89       }
90
91      Standard_EXPORT void Dump(Standard_OStream& out) const;
92
93     Standard_EXPORT ~Handle(Standard_Persistent)();
94
95     bool operator==(const Handle(Standard_Persistent)& right) const
96       {
97        return entity == right.entity;
98       }
99
100     bool operator==(const Standard_Persistent *right) const
101       {
102        return entity == right;
103       }
104
105     friend bool operator==(const Standard_Persistent *left, const Handle(Standard_Persistent)& right)
106       {
107        return left == right.entity;
108       }
109
110     bool operator!=(const Handle(Standard_Persistent)& right) const
111       {
112        return entity != right.entity;
113       }
114
115     bool operator!=(const Standard_Persistent *right) const
116       {
117        return entity != right;
118       }
119
120     friend bool operator!=(const Standard_Persistent *left, const Handle(Standard_Persistent)& right)
121       {
122        return left != right.entity;
123       }
124
125     void Nullify()
126       {
127        EndScope();
128        entity =  0 ;
129       }
130
131     Standard_Boolean IsNull() const
132       {
133        return entity == 0 ;
134       } 
135
136     Standard_Persistent* Access() const
137       {
138        return entity;
139       } 
140
141    protected:
142
143     Standard_Persistent* ControlAccess() const
144       {
145        return entity;
146       } 
147
148     void Assign(const Standard_Persistent *anItem)
149       {
150        EndScope();
151        if (!anItem)
152            entity = 0 ;
153        else {
154          entity = (Standard_Persistent *)anItem;
155          BeginScope();
156        }
157       }
158
159
160   public:
161
162    operator Standard_Persistent*()
163      {
164        return Access();
165      }
166
167
168    Standard_EXPORT Handle(Standard_Persistent)& operator=(const Handle(Standard_Persistent)& aHandle);
169    Standard_EXPORT Handle(Standard_Persistent)& operator=(const Standard_Persistent* anItem);
170
171    Standard_Persistent* operator->() 
172      {
173       return ControlAccess();
174      }
175
176    Standard_Persistent* operator->() const
177      {
178       return ControlAccess();
179      }
180
181    Standard_Persistent& operator*()
182      {
183       return *(ControlAccess());
184      }
185
186    const Standard_Persistent& operator*() const
187      {
188       return *(ControlAccess());
189      }
190
191    Standard_EXPORT static const Handle(Standard_Persistent) DownCast(const Handle(Standard_Persistent)& AnObject);
192 };
193
194 class Standard_Type;
195
196 #ifdef _WIN32
197 #pragma warning (pop)
198 #endif
199
200 #endif