ec964372 |
1 | // Copyright (c) 2017 OPEN CASCADE SAS |
2 | // |
3 | // This file is part of Open CASCADE Technology software library. |
4 | // |
5 | // This library is free software; you can redistribute it and/or modify it under |
6 | // the terms of the GNU Lesser General Public License version 2.1 as published |
7 | // by the Free Software Foundation, with special exception defined in the file |
8 | // OCCT_LGPL_EXCEPTION.txt. Consult the file LICENSE_LGPL_21.txt included in OCCT |
9 | // distribution for complete text of the license and disclaimer of any warranty. |
10 | // |
11 | // Alternatively, this file may be used under the terms of Open CASCADE |
12 | // commercial license or contractual agreement. |
13 | |
14 | #include <StdObjMgt_Persistent.hxx> |
15 | #include <Standard_ErrorHandler.hxx> |
16 | #include <Standard_NoSuchObject.hxx> |
17 | #include <StdStorage_RootData.hxx> |
18 | #include <StdStorage_Root.hxx> |
19 | #include <Storage_BaseDriver.hxx> |
20 | #include <Storage_StreamTypeMismatchError.hxx> |
21 | #include <Storage_DataMapIteratorOfMapOfPers.hxx> |
22 | #include <TCollection_AsciiString.hxx> |
23 | |
25e59720 |
24 | IMPLEMENT_STANDARD_RTTIEXT(StdStorage_RootData, Standard_Transient) |
ec964372 |
25 | |
26 | StdStorage_RootData::StdStorage_RootData() |
27 | : myErrorStatus(Storage_VSOk) |
28 | { |
29 | } |
30 | |
31 | Standard_Boolean StdStorage_RootData::Read(Storage_BaseDriver& theDriver) |
32 | { |
33 | // Check driver open mode |
34 | if (theDriver.OpenMode() != Storage_VSRead |
35 | && theDriver.OpenMode() != Storage_VSReadWrite) |
36 | { |
37 | myErrorStatus = Storage_VSModeError; |
38 | myErrorStatusExt = "OpenMode"; |
39 | return Standard_False; |
40 | } |
41 | |
42 | // Read root section |
43 | myErrorStatus = theDriver.BeginReadRootSection(); |
44 | if (myErrorStatus != Storage_VSOk) |
45 | { |
46 | myErrorStatusExt = "BeginReadRootSection"; |
47 | return Standard_False; |
48 | } |
49 | |
50 | TCollection_AsciiString aRootName, aTypeName; |
51 | Standard_Integer aRef; |
52 | |
53 | Standard_Integer len = theDriver.RootSectionSize(); |
54 | for (Standard_Integer i = 1; i <= len; i++) |
55 | { |
56 | try |
57 | { |
58 | OCC_CATCH_SIGNALS |
59 | theDriver.ReadRoot(aRootName, aRef, aTypeName); |
60 | } |
61 | catch (Storage_StreamTypeMismatchError) |
62 | { |
63 | myErrorStatus = Storage_VSTypeMismatch; |
64 | myErrorStatusExt = "ReadRoot"; |
65 | return Standard_False; |
66 | } |
67 | |
68 | Handle(StdStorage_Root) aRoot = new StdStorage_Root(aRootName, aRef, aTypeName); |
69 | myObjects.Bind(aRootName, aRoot); |
70 | } |
71 | |
72 | myErrorStatus = theDriver.EndReadRootSection(); |
73 | if (myErrorStatus != Storage_VSOk) |
74 | { |
75 | myErrorStatusExt = "EndReadRootSection"; |
76 | return Standard_False; |
77 | } |
78 | |
79 | return Standard_True; |
80 | } |
81 | |
82 | Standard_Boolean StdStorage_RootData::Write(Storage_BaseDriver& theDriver) |
83 | { |
84 | // Check driver open mode |
85 | if (theDriver.OpenMode() != Storage_VSWrite |
86 | && theDriver.OpenMode() != Storage_VSReadWrite) |
87 | { |
88 | myErrorStatus = Storage_VSModeError; |
89 | myErrorStatusExt = "OpenMode"; |
90 | return Standard_False; |
91 | } |
92 | |
93 | // Write root section |
94 | myErrorStatus = theDriver.BeginWriteRootSection(); |
95 | if (myErrorStatus != Storage_VSOk) |
96 | { |
97 | myErrorStatusExt = "BeginWriteRootSection"; |
98 | return Standard_False; |
99 | } |
100 | |
101 | theDriver.SetRootSectionSize(NumberOfRoots()); |
102 | for (StdStorage_MapOfRoots::Iterator anIt(myObjects); anIt.More(); anIt.Next()) |
103 | { |
104 | const Handle(StdStorage_Root)& aRoot = anIt.Value(); |
105 | try |
106 | { |
107 | OCC_CATCH_SIGNALS |
108 | theDriver.WriteRoot(aRoot->Name(), aRoot->Reference(), aRoot->Type()); |
109 | } |
110 | catch (Storage_StreamTypeMismatchError) |
111 | { |
112 | myErrorStatus = Storage_VSTypeMismatch; |
113 | myErrorStatusExt = "ReadRoot"; |
114 | return Standard_False; |
115 | } |
116 | } |
117 | |
118 | myErrorStatus = theDriver.EndWriteRootSection(); |
119 | if (myErrorStatus != Storage_VSOk) |
120 | { |
121 | myErrorStatusExt = "EndWriteRootSection"; |
122 | return Standard_False; |
123 | } |
124 | |
125 | return Standard_True; |
126 | } |
127 | |
128 | Standard_Integer StdStorage_RootData::NumberOfRoots() const |
129 | { |
130 | return myObjects.Extent(); |
131 | } |
132 | |
133 | void StdStorage_RootData::AddRoot(const Handle(StdStorage_Root)& aRoot) |
134 | { |
135 | myObjects.Bind(aRoot->Name(), aRoot); |
136 | aRoot->myRef = myObjects.Size(); |
137 | } |
138 | |
139 | Handle(StdStorage_HSequenceOfRoots) StdStorage_RootData::Roots() const |
140 | { |
141 | Handle(StdStorage_HSequenceOfRoots) anObjectsSeq = new StdStorage_HSequenceOfRoots; |
142 | StdStorage_DataMapIteratorOfMapOfRoots it(myObjects); |
143 | |
144 | for (; it.More(); it.Next()) { |
145 | anObjectsSeq->Append(it.Value()); |
146 | } |
147 | |
148 | return anObjectsSeq; |
149 | } |
150 | |
151 | Handle(StdStorage_Root) StdStorage_RootData::Find(const TCollection_AsciiString& aName) const |
152 | { |
153 | Handle(StdStorage_Root) p; |
154 | |
155 | if (myObjects.IsBound(aName)) { |
156 | p = myObjects.Find(aName); |
157 | } |
158 | |
159 | return p; |
160 | } |
161 | |
162 | Standard_Boolean StdStorage_RootData::IsRoot(const TCollection_AsciiString& aName) const |
163 | { |
164 | return myObjects.IsBound(aName); |
165 | } |
166 | |
167 | void StdStorage_RootData::RemoveRoot(const TCollection_AsciiString& aName) |
168 | { |
169 | if (myObjects.IsBound(aName)) { |
170 | myObjects.ChangeFind(aName)->myRef = 0; |
171 | myObjects.UnBind(aName); |
172 | Standard_Integer aRef = 1; |
173 | for (StdStorage_MapOfRoots::Iterator anIt(myObjects); anIt.More(); anIt.Next(), ++aRef) |
174 | anIt.ChangeValue()->myRef = aRef; |
175 | } |
176 | } |
177 | |
178 | void StdStorage_RootData::Clear() |
179 | { |
180 | for (StdStorage_MapOfRoots::Iterator anIt(myObjects); anIt.More(); anIt.Next()) |
181 | anIt.ChangeValue()->myRef = 0; |
182 | |
183 | myObjects.Clear(); |
184 | } |
185 | |
186 | Storage_Error StdStorage_RootData::ErrorStatus() const |
187 | { |
188 | return myErrorStatus; |
189 | } |
190 | |
191 | void StdStorage_RootData::SetErrorStatus(const Storage_Error anError) |
192 | { |
193 | myErrorStatus = anError; |
194 | } |
195 | |
196 | void StdStorage_RootData::ClearErrorStatus() |
197 | { |
198 | myErrorStatus = Storage_VSOk; |
199 | myErrorStatusExt.Clear(); |
200 | } |
201 | |
202 | TCollection_AsciiString StdStorage_RootData::ErrorStatusExtension() const |
203 | { |
204 | return myErrorStatusExt; |
205 | } |
206 | |
207 | void StdStorage_RootData::SetErrorStatusExtension(const TCollection_AsciiString& anErrorExt) |
208 | { |
209 | myErrorStatusExt = anErrorExt; |
210 | } |