242e1411cbbffedb7e9b1d7e8445f37b4bb98ef8
[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      Standard_EXPORT void  ShallowDump(Standard_OStream&) const;
96      
97     bool operator==(const Handle(Standard_Persistent)& right) const
98       {
99        return entity == right.entity;
100       }
101
102     bool operator==(const Standard_Persistent *right) const
103       {
104        return entity == right;
105       }
106
107     friend bool operator==(const Standard_Persistent *left, const Handle(Standard_Persistent)& right)
108       {
109        return left == right.entity;
110       }
111
112     bool operator!=(const Handle(Standard_Persistent)& right) const
113       {
114        return entity != right.entity;
115       }
116
117     bool operator!=(const Standard_Persistent *right) const
118       {
119        return entity != right;
120       }
121
122     friend bool operator!=(const Standard_Persistent *left, const Handle(Standard_Persistent)& right)
123       {
124        return left != right.entity;
125       }
126
127     void Nullify()
128       {
129        EndScope();
130        entity =  0 ;
131       }
132
133     Standard_Boolean IsNull() const
134       {
135        return entity == 0 ;
136       } 
137
138     Standard_Persistent* Access() const
139       {
140        return entity;
141       } 
142
143    protected:
144
145     Standard_Persistent* ControlAccess() const
146       {
147        return entity;
148       } 
149
150     void Assign(const Standard_Persistent *anItem)
151       {
152        EndScope();
153        if (!anItem)
154            entity = 0 ;
155        else {
156          entity = (Standard_Persistent *)anItem;
157          BeginScope();
158        }
159       }
160
161
162   public:
163
164    operator Standard_Persistent*()
165      {
166        return Access();
167      }
168
169
170    Standard_EXPORT Handle(Standard_Persistent)& operator=(const Handle(Standard_Persistent)& aHandle);
171    Standard_EXPORT Handle(Standard_Persistent)& operator=(const Standard_Persistent* anItem);
172
173    Standard_Persistent* operator->() 
174      {
175       return ControlAccess();
176      }
177
178    Standard_Persistent* operator->() const
179      {
180       return ControlAccess();
181      }
182
183    Standard_Persistent& operator*()
184      {
185       return *(ControlAccess());
186      }
187
188    const Standard_Persistent& operator*() const
189      {
190       return *(ControlAccess());
191      }
192
193    Standard_EXPORT static const Handle(Standard_Persistent) DownCast(const Handle(Standard_Persistent)& AnObject);
194 };
195
196 class Standard_Type;
197
198 #ifdef _WIN32
199 #pragma warning (pop)
200 #endif
201
202 #endif