0031313: Foundation Classes - Dump improvement for classes
[occt.git] / src / TDataStd / TDataStd_IntPackedMap.cxx
1 // Created on: 2007-07-31
2 // Created by: Sergey ZARITCHNY
3 // Copyright (c) 2007-2014 OPEN CASCADE SAS
4 //
5 // This file is part of Open CASCADE Technology software library.
6 //
7 // This library is free software; you can redistribute it and/or modify it under
8 // the terms of the GNU Lesser General Public License version 2.1 as published
9 // by the Free Software Foundation, with special exception defined in the file
10 // OCCT_LGPL_EXCEPTION.txt. Consult the file LICENSE_LGPL_21.txt included in OCCT
11 // distribution for complete text of the license and disclaimer of any warranty.
12 //
13 // Alternatively, this file may be used under the terms of Open CASCADE
14 // commercial license or contractual agreement.
15
16 #include <TDataStd_IntPackedMap.hxx>
17
18 #include <Standard_Dump.hxx>
19 #include <Standard_GUID.hxx>
20 #include <Standard_Type.hxx>
21 #include <TColStd_HPackedMapOfInteger.hxx>
22 #include <TColStd_PackedMapOfInteger.hxx>
23 #include <TDataStd_DeltaOnModificationOfIntPackedMap.hxx>
24 #include <TDF_Attribute.hxx>
25 #include <TDF_DefaultDeltaOnModification.hxx>
26 #include <TDF_DeltaOnModification.hxx>
27 #include <TDF_Label.hxx>
28 #include <TDF_RelocationTable.hxx>
29
30 IMPLEMENT_STANDARD_RTTIEXT(TDataStd_IntPackedMap,TDF_Attribute)
31
32 //=======================================================================
33 //function : GetID
34 //purpose  : 
35 //=======================================================================
36 const Standard_GUID& TDataStd_IntPackedMap::GetID()
37 {
38   static Standard_GUID theGUID ("7031faff-161e-44df-8239-7c264a81f5a1");
39   return theGUID;
40 }
41
42 //=======================================================================
43 //function : Set
44 //purpose  : 
45 //=======================================================================
46
47 Handle(TDataStd_IntPackedMap) TDataStd_IntPackedMap::Set (const TDF_Label& theLabel, 
48                                                           const Standard_Boolean isDelta)
49 {
50   Handle(TDataStd_IntPackedMap) anAtt;
51   if (!theLabel.FindAttribute(TDataStd_IntPackedMap::GetID(), anAtt))
52   {
53     anAtt = new TDataStd_IntPackedMap;
54     anAtt->Clear();
55     anAtt->SetDelta(isDelta);
56     theLabel.AddAttribute(anAtt);
57   }
58   return anAtt;
59 }
60 //=======================================================================
61 //function : Constructor
62 //purpose  : 
63 //=======================================================================
64 TDataStd_IntPackedMap::TDataStd_IntPackedMap ()
65      :myIsDelta(Standard_False)
66 {
67   myMap = new TColStd_HPackedMapOfInteger ();
68 }
69 //=======================================================================
70 //function : ChangeMap
71 //purpose  : 
72 //=======================================================================
73
74 Standard_Boolean TDataStd_IntPackedMap::ChangeMap (const Handle(TColStd_HPackedMapOfInteger)& theMap)
75 {
76   if(theMap.IsNull()) return Standard_False;  
77   if (myMap != theMap)
78   {
79     if (!myMap->Map().IsEqual(theMap->Map()))
80     {
81       Backup();      
82       myMap->ChangeMap().Assign(theMap->Map());
83       return Standard_True;
84     }
85   }
86   return Standard_False;
87 }
88
89 //=======================================================================
90 //function : ChangeMap
91 //purpose  :
92 //=======================================================================
93 Standard_Boolean TDataStd_IntPackedMap::ChangeMap (const TColStd_PackedMapOfInteger& theMap)
94 {
95   if (!myMap->Map().IsEqual(theMap))
96   {
97     Backup();
98     myMap->ChangeMap().Assign(theMap);
99     return Standard_True;
100   }
101   return Standard_False;
102 }
103 //=======================================================================
104 //function : Clear
105 //purpose  : 
106 //=======================================================================
107
108 Standard_Boolean TDataStd_IntPackedMap::Clear ()
109 {
110   if (!myMap->Map().IsEmpty())
111   {
112     Backup();
113     myMap = new TColStd_HPackedMapOfInteger;
114     return Standard_True;
115   }
116   return Standard_False;
117 }
118
119 //=======================================================================
120 //function : Contains
121 //purpose  : 
122 //=======================================================================
123 Standard_Boolean TDataStd_IntPackedMap::Contains(const Standard_Integer theKey) const
124 {
125   return myMap->Map().Contains(theKey);
126 }
127
128 //=======================================================================
129 //function : Add
130 //purpose  : 
131 //=======================================================================
132
133 Standard_Boolean TDataStd_IntPackedMap::Add(const Standard_Integer theKey)
134 {
135   Standard_Boolean aResult = !myMap->Map().Contains(theKey);
136   if (aResult)
137   {
138     Backup();
139     aResult = myMap->ChangeMap().Add(theKey);
140   }
141   return aResult;
142 }
143 //=======================================================================
144 //function : Remove
145 //purpose  : 
146 //=======================================================================
147
148 Standard_Boolean TDataStd_IntPackedMap::Remove(const Standard_Integer theKey)
149 {
150   Standard_Boolean aResult = myMap->Map().Contains(theKey);
151   if (aResult)
152   {
153     Backup();
154     aResult = myMap->ChangeMap().Remove(theKey);
155   }
156   return aResult;
157 }
158
159 //=======================================================================
160 //function : NewEmpty
161 //purpose  : 
162 //=======================================================================
163
164 Handle(TDF_Attribute) TDataStd_IntPackedMap::NewEmpty () const
165 {
166   return new TDataStd_IntPackedMap;
167 }
168
169 //=======================================================================
170 //function : Restore
171 //purpose  : 
172 //=======================================================================
173
174 void TDataStd_IntPackedMap::Restore (const Handle(TDF_Attribute)& theWith)
175 {
176   Handle(TDataStd_IntPackedMap) aWith = 
177     Handle(TDataStd_IntPackedMap)::DownCast(theWith);
178   if (aWith->myMap.IsNull())
179     myMap.Nullify();
180   else
181   {
182     myMap = new TColStd_HPackedMapOfInteger;
183     myMap->ChangeMap().Assign(aWith->myMap->Map());
184     myIsDelta = aWith->myIsDelta;
185   }
186 }
187
188 //=======================================================================
189 //function : Paste
190 //purpose  : 
191 //=======================================================================
192
193 void TDataStd_IntPackedMap::Paste (const Handle(TDF_Attribute)& theInto,
194                               const Handle(TDF_RelocationTable)&) const
195
196   Handle(TDataStd_IntPackedMap) anInto = 
197     Handle(TDataStd_IntPackedMap)::DownCast(theInto);
198   if(!anInto.IsNull()) {
199     anInto->ChangeMap(myMap);
200     anInto->SetDelta(myIsDelta);
201   }
202 }
203
204 //=======================================================================
205 //function : ID
206 //purpose  : 
207 //=======================================================================
208 const Standard_GUID& TDataStd_IntPackedMap::ID() const 
209 { return GetID(); }
210
211 //=======================================================================
212 //function : Dump
213 //purpose  : 
214 //=======================================================================
215
216 Standard_OStream& TDataStd_IntPackedMap::Dump(Standard_OStream& theOS) const
217 {
218   Standard_OStream& anOS = TDF_Attribute::Dump( theOS );
219   anOS << "IntPackedMap size = " << Extent();
220   anOS << " Delta is " << (myIsDelta ? "ON":"OFF");
221   anOS << std::endl;
222   return anOS;
223 }
224
225 //=======================================================================
226 //function : DeltaOnModification
227 //purpose  : 
228 //=======================================================================
229
230 Handle(TDF_DeltaOnModification) TDataStd_IntPackedMap::DeltaOnModification
231 (const Handle(TDF_Attribute)& OldAttribute) const
232 {
233   if(myIsDelta)
234     return new TDataStd_DeltaOnModificationOfIntPackedMap(Handle(TDataStd_IntPackedMap)::DownCast (OldAttribute));
235   else return new TDF_DefaultDeltaOnModification(OldAttribute);
236 }
237
238 //=======================================================================
239 //function : DumpJson
240 //purpose  : 
241 //=======================================================================
242 void TDataStd_IntPackedMap::DumpJson (Standard_OStream& theOStream, Standard_Integer theDepth) const
243 {
244   OCCT_DUMP_TRANSIENT_CLASS_BEGIN (theOStream)
245
246   OCCT_DUMP_BASE_CLASS (theOStream, theDepth, TDF_Attribute)
247
248   for (TColStd_PackedMapOfInteger::Iterator aMapIt (myMap->Map()); aMapIt.More(); aMapIt.Next())
249   {
250     Standard_Integer aKey = aMapIt.Key();
251     OCCT_DUMP_FIELD_VALUE_NUMERICAL (theOStream, aKey)
252   }
253
254   OCCT_DUMP_FIELD_VALUE_NUMERICAL (theOStream, myIsDelta)
255 }