1 // Copyright (c) 2017 OPEN CASCADE SAS
3 // This file is part of Open CASCADE Technology software library.
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.
11 // Alternatively, this file may be used under the terms of Open CASCADE
12 // commercial license or contractual agreement.
14 #include <Standard_ErrorHandler.hxx>
15 #include <Standard_NoSuchObject.hxx>
16 #include <StdDrivers.hxx>
17 #include <StdStorage_TypeData.hxx>
18 #include <Storage_BaseDriver.hxx>
19 #include <Storage_StreamTypeMismatchError.hxx>
20 #include <TCollection_AsciiString.hxx>
22 IMPLEMENT_STANDARD_RTTIEXT(StdStorage_TypeData, Standard_Transient)
24 StdStorage_TypeData::StdStorage_TypeData()
26 myErrorStatus(Storage_VSOk)
28 StdDrivers::BindTypes(myMapOfPInst);
31 Standard_Boolean StdStorage_TypeData::Read(Storage_BaseDriver& theDriver)
33 // Check driver open mode
34 if (theDriver.OpenMode() != Storage_VSRead
35 && theDriver.OpenMode() != Storage_VSReadWrite)
37 myErrorStatus = Storage_VSModeError;
38 myErrorStatusExt = "OpenMode";
39 return Standard_False;
43 myErrorStatus = theDriver.BeginReadTypeSection();
44 if (myErrorStatus != Storage_VSOk)
46 myErrorStatusExt = "BeginReadTypeSection";
47 return Standard_False;
50 Standard_Integer aTypeNum;
51 TCollection_AsciiString aTypeName;
53 Standard_Integer len = theDriver.TypeSectionSize();
54 for (Standard_Integer i = 1; i <= len; i++)
59 theDriver.ReadTypeInformations (aTypeNum, aTypeName);
61 catch (Storage_StreamTypeMismatchError const&)
63 myErrorStatus = Storage_VSTypeMismatch;
64 myErrorStatusExt = "ReadTypeInformations";
65 return Standard_False;
68 myPt.Add (aTypeName, aTypeNum);
71 myErrorStatus = theDriver.EndReadTypeSection();
72 if (myErrorStatus != Storage_VSOk)
74 myErrorStatusExt = "EndReadTypeSection";
75 return Standard_False;
81 Standard_Boolean StdStorage_TypeData::Write(Storage_BaseDriver& theDriver)
83 // Check driver open mode
84 if (theDriver.OpenMode() != Storage_VSWrite
85 && theDriver.OpenMode() != Storage_VSReadWrite)
87 myErrorStatus = Storage_VSModeError;
88 myErrorStatusExt = "OpenMode";
89 return Standard_False;
93 myErrorStatus = theDriver.BeginWriteTypeSection();
94 if (myErrorStatus != Storage_VSOk)
96 myErrorStatusExt = "BeginWriteTypeSection";
97 return Standard_False;
100 Standard_Integer len = NumberOfTypes();
101 theDriver.SetTypeSectionSize(len);
102 for (Standard_Integer i = 1; i <= len; i++)
107 theDriver.WriteTypeInformations(i, Type(i));
109 catch (Storage_StreamTypeMismatchError const&)
111 myErrorStatus = Storage_VSTypeMismatch;
112 myErrorStatusExt = "WriteTypeInformations";
113 return Standard_False;
117 myErrorStatus = theDriver.EndWriteTypeSection();
118 if (myErrorStatus != Storage_VSOk)
120 myErrorStatusExt = "EndWriteTypeSection";
121 return Standard_False;
124 return Standard_True;
127 Standard_Integer StdStorage_TypeData::NumberOfTypes() const
129 return myPt.Extent();
132 Standard_Boolean StdStorage_TypeData::IsType(const TCollection_AsciiString& aName) const
134 return myPt.Contains(aName);
137 Handle(TColStd_HSequenceOfAsciiString) StdStorage_TypeData::Types() const
139 Handle(TColStd_HSequenceOfAsciiString) r = new TColStd_HSequenceOfAsciiString;
142 for (i = 1; i <= myPt.Extent(); i++) {
143 r->Append(myPt.FindKey(i));
149 void StdStorage_TypeData::AddType(const TCollection_AsciiString& aTypeName, const Standard_Integer aTypeNum)
151 myPt.Add(aTypeName, aTypeNum);
152 myTypeId = Max(aTypeNum, myTypeId);
155 Standard_Integer StdStorage_TypeData::AddType(const Handle(StdObjMgt_Persistent)& aPObj)
157 TCollection_AsciiString aTypeName = aPObj->PName();
158 if (IsType(aTypeName))
159 return Type(aTypeName);
161 if (!myMapOfPInst.IsBound(aTypeName)) {
162 Standard_SStream aSS;
163 aSS << "StdStorage_TypeData::Type " << aTypeName << " isn't registered";
164 throw Standard_NoSuchObject(aSS.str().c_str());
167 Standard_Integer aTypeId = ++myTypeId;
168 AddType(aTypeName, aTypeId);
173 TCollection_AsciiString StdStorage_TypeData::Type(const Standard_Integer aTypeNum) const
175 TCollection_AsciiString r;
177 if (aTypeNum <= myPt.Extent() && aTypeNum > 0)
178 r = myPt.FindKey(aTypeNum);
180 Standard_SStream aSS;
181 aSS << "StdStorage_TypeData::Type " << aTypeNum << " not in range";
182 throw Standard_NoSuchObject(aSS.str().c_str());
188 Standard_Integer StdStorage_TypeData::Type(const TCollection_AsciiString& aTypeName) const
190 Standard_Integer r = 0;
192 if (myPt.Contains(aTypeName))
193 r = myPt.FindFromKey(aTypeName);
195 Standard_SStream aSS;
196 aSS << "StdStorage_TypeData::Type " << aTypeName << " not found";
197 throw Standard_NoSuchObject(aSS.str().c_str());
203 StdObjMgt_Persistent::Instantiator
204 StdStorage_TypeData::Instantiator(const Standard_Integer aTypeNum) const
206 TCollection_AsciiString aTypeName = Type(aTypeNum);
207 StdObjMgt_Persistent::Instantiator anInstantiator = 0;
208 if (!myMapOfPInst.Find(aTypeName, anInstantiator))
210 return anInstantiator;
213 void StdStorage_TypeData::Clear()
218 Storage_Error StdStorage_TypeData::ErrorStatus() const
220 return myErrorStatus;
223 void StdStorage_TypeData::SetErrorStatus(const Storage_Error anError)
225 myErrorStatus = anError;
228 void StdStorage_TypeData::ClearErrorStatus()
230 myErrorStatus = Storage_VSOk;
231 myErrorStatusExt.Clear();
234 TCollection_AsciiString StdStorage_TypeData::ErrorStatusExtension() const
236 return myErrorStatusExt;
239 void StdStorage_TypeData::SetErrorStatusExtension(const TCollection_AsciiString& anErrorExt)
241 myErrorStatusExt = anErrorExt;