ec44de43a854fe6d2554932c55cff6854de81969
[occt.git] / src / StdLPersistent / StdLPersistent_Collection.cxx
1 // Copyright (c) 2015 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 <StdLPersistent_Collection.hxx>
15
16 #include <TColStd_HPackedMapOfInteger.hxx>
17 #include <TCollection_HExtendedString.hxx>
18
19
20 struct StdLPersistent_Collection::noConversion
21 {
22   noConversion (const Handle(TDF_Data)&) {}
23
24   template <class Type>
25   Type operator() (Type theValue) const { return theValue; }
26 };
27
28 struct StdLPersistent_Collection::byteConverter
29 {
30   byteConverter (const Handle(TDF_Data)&) {}
31
32   Standard_Byte operator() (Standard_Integer theValue) const
33     { return static_cast<Standard_Byte> (theValue); }
34 };
35
36 struct StdLPersistent_Collection::stringConverter
37 {
38   stringConverter (const Handle(TDF_Data)&) {}
39
40   const TCollection_ExtendedString& operator()
41     (const Handle(StdObjMgt_Persistent)& theValue) const
42   {
43     Handle(TCollection_HExtendedString) aString = theValue->ExtString();
44     if (aString)
45       return aString->String();
46     else
47     {
48       static TCollection_ExtendedString anEmptyString;
49       return anEmptyString;
50     }
51   }
52 };
53
54 struct StdLPersistent_Collection::referenceConverter
55 {
56   referenceConverter (const Handle(TDF_Data)& theDF) : myDF (theDF) {}
57
58   TDF_Label operator() (const Handle(StdObjMgt_Persistent)& theValue) const
59     { return theValue->Label (myDF); }
60
61 private:
62   Handle(TDF_Data) myDF;
63 };
64
65 //=======================================================================
66 //function : Read
67 //purpose  : Read persistent data from a file
68 //=======================================================================
69 template <class Persistent>
70 void StdLPersistent_Collection::booleanArrayBase<Persistent>::Read
71   (StdObjMgt_ReadData& theReadData)
72 {
73   StdLPersistent_Collection::booleanArrayBase<Persistent>::SingleRef::Read (theReadData);
74   theReadData >> myLower >> myUpper;
75 }
76
77 template <class Persistent>
78 template <class ArrayHandle, class Converter>
79 void StdLPersistent_Collection::booleanArrayBase<Persistent>::import
80   (const ArrayHandle& theArray, Converter theConverter) const
81 {
82   Handle(TColStd_HArray1OfByte) aByteArray =
83     new TColStd_HArray1OfByte (theArray->Lower(), theArray->Upper());
84
85   for (Standard_Integer i = theArray->Lower(); i <= theArray->Upper(); i++)
86     aByteArray->SetValue (i, theConverter (theArray->Value(i)));
87
88   this->myTransient->Init (myLower, myUpper);
89   this->myTransient->SetInternalArray (aByteArray);
90 }
91
92 template <class Persistent>
93 template <class ArrayHandle, class Converter>
94 void StdLPersistent_Collection::directArrayBase<Persistent>::import
95   (const ArrayHandle& theArray, Converter) const
96 {
97   this->myTransient->ChangeArray (theArray);
98 }
99
100 template <class Persistent>
101 template <class ArrayHandle, class Converter>
102 void StdLPersistent_Collection::arrayBase<Persistent>::import
103   (const ArrayHandle& theArray, Converter theConverter) const
104 {
105   this->myTransient->Init (theArray->Lower(), theArray->Upper());
106   for (Standard_Integer i = theArray->Lower(); i <= theArray->Upper(); i++)
107     this->myTransient->SetValue (i, theConverter (theArray->Value(i)));
108 }
109
110 template <class Persistent>
111 template <class ArrayHandle, class Converter>
112 void StdLPersistent_Collection::listBase<Persistent>::import
113   (const ArrayHandle& theArray, Converter theConverter) const
114 {
115   for (Standard_Integer i = theArray->Lower(); i <= theArray->Upper(); i++)
116     this->myTransient->Append (theConverter (theArray->Value(i)));
117 }
118
119 template <class Persistent>
120 template <class ArrayHandle, class Converter>
121 void StdLPersistent_Collection::mapBase<Persistent>::import
122   (const ArrayHandle& theArray, Converter theConverter) const
123 {
124   Handle(TColStd_HPackedMapOfInteger) anHMap = new TColStd_HPackedMapOfInteger;
125   for (Standard_Integer i = theArray->Lower(); i <= theArray->Upper(); i++)
126     anHMap->ChangeMap().Add (theConverter (theArray->Value(i)));
127   this->myTransient->ChangeMap (anHMap);
128 }
129
130 //=======================================================================
131 //function : ImportAttribute
132 //purpose  : Import transient attribuite from the persistent data
133 //=======================================================================
134 template <template<class> class BaseT,
135           class HArrayClass,
136           class AttribClass,
137           class Converter>
138 void StdLPersistent_Collection::
139        instance<BaseT, HArrayClass, AttribClass, Converter>::ImportAttribute()
140 {
141   Handle(HArrayClass) anHArray;
142   if (this->myData.Cast (anHArray))
143   {
144     typename HArrayClass::ArrayHandle anArray = anHArray->Array();
145     if (anArray)
146       this->import (anArray, Converter (this->myTransient->Label().Data()));
147     this->myData.Nullify();
148   }
149 }
150
151 //=======================================================================
152 //function : Read
153 //purpose  : Read persistent data from a file
154 //=======================================================================
155 template <class Instance>
156 void StdLPersistent_Collection::instance_1<Instance>::Read
157   (StdObjMgt_ReadData& theReadData)
158 {
159   Instance::Read (theReadData);
160   theReadData >> myDelta;
161 }
162
163 //=======================================================================
164 //function : ImportAttribute
165 //purpose  : Import transient attribuite from the persistent data
166 //=======================================================================
167 template <class Instance>
168 void StdLPersistent_Collection::instance_1<Instance>::ImportAttribute()
169 {
170   Instance::ImportAttribute();
171   this->myTransient->SetDelta (myDelta);
172 }
173
174
175 template class StdLPersistent_Collection::instance
176   <StdLPersistent_Collection::booleanArrayBase,
177    StdLPersistent_Collection::integer,
178    TDataStd_BooleanArray,
179    StdLPersistent_Collection::byteConverter>;
180
181 template class StdLPersistent_Collection::instance
182   <StdLPersistent_Collection::directArrayBase,
183    StdLPersistent_Collection::integer,
184    TDataStd_IntegerArray,
185    StdLPersistent_Collection::noConversion>;
186
187 template class StdLPersistent_Collection::instance
188   <StdLPersistent_Collection::directArrayBase,
189    StdLPersistent_Collection::real,
190    TDataStd_RealArray,
191    StdLPersistent_Collection::noConversion>;
192
193 template class StdLPersistent_Collection::instance
194   <StdLPersistent_Collection::arrayBase,
195    StdLPersistent_Collection::integer,
196    TDataStd_ByteArray,
197    StdLPersistent_Collection::byteConverter>;
198
199 template class StdLPersistent_Collection::instance
200   <StdLPersistent_Collection::arrayBase,
201    StdLPersistent_Collection::persistent,
202    TDataStd_ExtStringArray,
203    StdLPersistent_Collection::stringConverter>;
204
205 template class StdLPersistent_Collection::instance
206   <StdLPersistent_Collection::arrayBase,
207    StdLPersistent_Collection::persistent,
208    TDataStd_ReferenceArray,
209    StdLPersistent_Collection::referenceConverter>;
210
211 template class StdLPersistent_Collection::instance
212   <StdLPersistent_Collection::listBase,
213    StdLPersistent_Collection::integer,
214    TDataStd_IntegerList,
215    StdLPersistent_Collection::noConversion>;
216
217 template class StdLPersistent_Collection::instance
218   <StdLPersistent_Collection::listBase,
219    StdLPersistent_Collection::real,
220    TDataStd_RealList,
221    StdLPersistent_Collection::noConversion>;
222
223 template class StdLPersistent_Collection::instance
224   <StdLPersistent_Collection::listBase,
225    StdLPersistent_Collection::integer,
226    TDataStd_BooleanList,
227    StdLPersistent_Collection::noConversion>;
228
229 template class StdLPersistent_Collection::instance
230   <StdLPersistent_Collection::listBase,
231    StdLPersistent_Collection::persistent,
232    TDataStd_ExtStringList,
233    StdLPersistent_Collection::stringConverter>;
234
235 template class StdLPersistent_Collection::instance
236   <StdLPersistent_Collection::listBase,
237    StdLPersistent_Collection::persistent,
238    TDataStd_ReferenceList,
239    StdLPersistent_Collection::referenceConverter>;
240
241 template class StdLPersistent_Collection::instance
242   <StdLPersistent_Collection::mapBase,
243    StdLPersistent_Collection::integer,
244    TDataStd_IntPackedMap,
245    StdLPersistent_Collection::noConversion>;
246
247 template class StdLPersistent_Collection::instance_1
248   <StdLPersistent_Collection::IntegerArray>;
249
250 template class StdLPersistent_Collection::instance_1
251   <StdLPersistent_Collection::RealArray>;
252
253 template class StdLPersistent_Collection::instance_1
254   <StdLPersistent_Collection::ByteArray>;
255
256 template class StdLPersistent_Collection::instance_1
257   <StdLPersistent_Collection::ExtStringArray>;
258
259 template class StdLPersistent_Collection::instance_1
260   <StdLPersistent_Collection::IntPackedMap>;