0030611: Coding Rules - eliminate GCC compiler warnings -Wcatch-value
[occt.git] / src / StdStorage / StdStorage_TypeData.cxx
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 <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>
21
22 IMPLEMENT_STANDARD_RTTIEXT(StdStorage_TypeData, Standard_Transient)
23
24 StdStorage_TypeData::StdStorage_TypeData() 
25 : myTypeId(0),
26   myErrorStatus(Storage_VSOk)
27 {
28   StdDrivers::BindTypes(myMapOfPInst);
29 }
30
31 Standard_Boolean StdStorage_TypeData::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 type section
43   myErrorStatus = theDriver.BeginReadTypeSection();
44   if (myErrorStatus != Storage_VSOk)
45   {
46     myErrorStatusExt = "BeginReadTypeSection";
47     return Standard_False;
48   }
49
50   Standard_Integer        aTypeNum;
51   TCollection_AsciiString aTypeName;
52
53   Standard_Integer len = theDriver.TypeSectionSize();
54   for (Standard_Integer i = 1; i <= len; i++)
55   {
56     try
57     {
58       OCC_CATCH_SIGNALS
59       theDriver.ReadTypeInformations (aTypeNum, aTypeName);
60     }
61     catch (Storage_StreamTypeMismatchError const&)
62     {
63       myErrorStatus = Storage_VSTypeMismatch;
64       myErrorStatusExt = "ReadTypeInformations";
65       return Standard_False;
66     }
67
68     myPt.Add (aTypeName, aTypeNum);
69   }
70
71   myErrorStatus = theDriver.EndReadTypeSection();
72   if (myErrorStatus != Storage_VSOk)
73   {
74     myErrorStatusExt = "EndReadTypeSection";
75     return Standard_False;
76   }
77
78   return Standard_True;
79 }
80
81 Standard_Boolean StdStorage_TypeData::Write(Storage_BaseDriver& theDriver)
82 {
83   // Check driver open mode
84   if (theDriver.OpenMode() != Storage_VSWrite
85     && theDriver.OpenMode() != Storage_VSReadWrite)
86   {
87     myErrorStatus = Storage_VSModeError;
88     myErrorStatusExt = "OpenMode";
89     return Standard_False;
90   }
91
92   // Write type section
93   myErrorStatus = theDriver.BeginWriteTypeSection();
94   if (myErrorStatus != Storage_VSOk)
95   {
96     myErrorStatusExt = "BeginWriteTypeSection";
97     return Standard_False;
98   }
99
100   Standard_Integer len = NumberOfTypes();
101   theDriver.SetTypeSectionSize(len);
102   for (Standard_Integer i = 1; i <= len; i++)
103   {
104     try
105     {
106       OCC_CATCH_SIGNALS
107       theDriver.WriteTypeInformations(i, Type(i));
108     }
109     catch (Storage_StreamTypeMismatchError const&)
110     {
111       myErrorStatus = Storage_VSTypeMismatch;
112       myErrorStatusExt = "WriteTypeInformations";
113       return Standard_False;
114     }
115   }
116
117   myErrorStatus = theDriver.EndWriteTypeSection();
118   if (myErrorStatus != Storage_VSOk)
119   {
120     myErrorStatusExt = "EndWriteTypeSection";
121     return Standard_False;
122   }
123
124   return Standard_True;
125 }
126
127 Standard_Integer StdStorage_TypeData::NumberOfTypes() const
128 {
129   return myPt.Extent();
130 }
131
132 Standard_Boolean StdStorage_TypeData::IsType(const TCollection_AsciiString& aName) const
133 {
134   return myPt.Contains(aName);
135 }
136
137 Handle(TColStd_HSequenceOfAsciiString) StdStorage_TypeData::Types() const
138 {
139   Handle(TColStd_HSequenceOfAsciiString) r = new TColStd_HSequenceOfAsciiString;
140   Standard_Integer                       i;
141
142   for (i = 1; i <= myPt.Extent(); i++) {
143     r->Append(myPt.FindKey(i));
144   }
145
146   return r;
147 }
148
149 void StdStorage_TypeData::AddType(const TCollection_AsciiString& aTypeName, const Standard_Integer aTypeNum)
150 {
151   myPt.Add(aTypeName, aTypeNum);
152   myTypeId = Max(aTypeNum, myTypeId);
153 }
154
155 Standard_Integer StdStorage_TypeData::AddType(const Handle(StdObjMgt_Persistent)& aPObj)
156 {
157   TCollection_AsciiString aTypeName = aPObj->PName();
158   if (IsType(aTypeName))
159     return Type(aTypeName);
160
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());
165   }
166
167   Standard_Integer aTypeId = ++myTypeId;
168   AddType(aTypeName, aTypeId);
169
170   return aTypeId;
171 }
172
173 TCollection_AsciiString StdStorage_TypeData::Type(const Standard_Integer aTypeNum) const
174 {
175   TCollection_AsciiString r;
176
177   if (aTypeNum <= myPt.Extent() && aTypeNum > 0)
178     r = myPt.FindKey(aTypeNum);
179   else {
180     Standard_SStream aSS;
181     aSS << "StdStorage_TypeData::Type " << aTypeNum << " not in range";
182     throw Standard_NoSuchObject(aSS.str().c_str());
183   }
184
185   return r;
186 }
187
188 Standard_Integer StdStorage_TypeData::Type(const TCollection_AsciiString& aTypeName) const
189 {
190   Standard_Integer r = 0;
191
192   if (myPt.Contains(aTypeName))
193     r = myPt.FindFromKey(aTypeName);
194   else {
195     Standard_SStream aSS;
196     aSS << "StdStorage_TypeData::Type " << aTypeName << " not found";
197     throw Standard_NoSuchObject(aSS.str().c_str());
198   }
199
200   return r;
201 }
202
203 StdObjMgt_Persistent::Instantiator 
204 StdStorage_TypeData::Instantiator(const Standard_Integer aTypeNum) const
205 {
206   TCollection_AsciiString aTypeName = Type(aTypeNum);
207   StdObjMgt_Persistent::Instantiator anInstantiator = 0;
208   if (!myMapOfPInst.Find(aTypeName, anInstantiator))
209     return 0;
210   return anInstantiator;
211 }
212
213 void StdStorage_TypeData::Clear()
214 {
215   myPt.Clear();
216 }
217
218 Storage_Error StdStorage_TypeData::ErrorStatus() const
219 {
220   return myErrorStatus;
221 }
222
223 void StdStorage_TypeData::SetErrorStatus(const Storage_Error anError)
224 {
225   myErrorStatus = anError;
226 }
227
228 void StdStorage_TypeData::ClearErrorStatus()
229 {
230   myErrorStatus = Storage_VSOk;
231   myErrorStatusExt.Clear();
232 }
233
234 TCollection_AsciiString StdStorage_TypeData::ErrorStatusExtension() const
235 {
236   return myErrorStatusExt;
237 }
238
239 void StdStorage_TypeData::SetErrorStatusExtension(const TCollection_AsciiString& anErrorExt)
240 {
241   myErrorStatusExt = anErrorExt;
242 }