0030895: Coding Rules - specify std namespace explicitly for std::cout and streams
[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
17 #include <Standard_GUID.hxx>
18 #include <Standard_Type.hxx>
19 #include <TColStd_HPackedMapOfInteger.hxx>
20 #include <TColStd_PackedMapOfInteger.hxx>
21 #include <TDataStd_DeltaOnModificationOfIntPackedMap.hxx>
22 #include <TDataStd_IntPackedMap.hxx>
23 #include <TDF_Attribute.hxx>
24 #include <TDF_DefaultDeltaOnModification.hxx>
25 #include <TDF_DeltaOnModification.hxx>
26 #include <TDF_Label.hxx>
27 #include <TDF_RelocationTable.hxx>
28
29 IMPLEMENT_STANDARD_RTTIEXT(TDataStd_IntPackedMap,TDF_Attribute)
30
31 //=======================================================================
32 //function : GetID
33 //purpose  : 
34 //=======================================================================
35 const Standard_GUID& TDataStd_IntPackedMap::GetID()
36 {
37   static Standard_GUID theGUID ("7031faff-161e-44df-8239-7c264a81f5a1");
38   return theGUID;
39 }
40
41 //=======================================================================
42 //function : Set
43 //purpose  : 
44 //=======================================================================
45
46 Handle(TDataStd_IntPackedMap) TDataStd_IntPackedMap::Set (const TDF_Label& theLabel, 
47                                                           const Standard_Boolean isDelta)
48 {
49   Handle(TDataStd_IntPackedMap) anAtt;
50   if (!theLabel.FindAttribute(TDataStd_IntPackedMap::GetID(), anAtt))
51   {
52     anAtt = new TDataStd_IntPackedMap;
53     anAtt->Clear();
54     anAtt->SetDelta(isDelta);
55     theLabel.AddAttribute(anAtt);
56   }
57   return anAtt;
58 }
59 //=======================================================================
60 //function : Constructor
61 //purpose  : 
62 //=======================================================================
63 TDataStd_IntPackedMap::TDataStd_IntPackedMap ()
64      :myIsDelta(Standard_False)
65 {
66   myMap = new TColStd_HPackedMapOfInteger ();
67 }
68 //=======================================================================
69 //function : ChangeMap
70 //purpose  : 
71 //=======================================================================
72
73 Standard_Boolean TDataStd_IntPackedMap::ChangeMap (const Handle(TColStd_HPackedMapOfInteger)& theMap)
74 {
75   if(theMap.IsNull()) return Standard_False;  
76   if (myMap != theMap)
77   {
78     if (!myMap->Map().IsEqual(theMap->Map()))
79     {
80       Backup();      
81       myMap->ChangeMap().Assign(theMap->Map());
82       return Standard_True;
83     }
84   }
85   return Standard_False;
86 }
87
88 //=======================================================================
89 //function : ChangeMap
90 //purpose  :
91 //=======================================================================
92 Standard_Boolean TDataStd_IntPackedMap::ChangeMap (const TColStd_PackedMapOfInteger& theMap)
93 {
94   if (!myMap->Map().IsEqual(theMap))
95   {
96     Backup();
97     myMap->ChangeMap().Assign(theMap);
98     return Standard_True;
99   }
100   return Standard_False;
101 }
102 //=======================================================================
103 //function : Clear
104 //purpose  : 
105 //=======================================================================
106
107 Standard_Boolean TDataStd_IntPackedMap::Clear ()
108 {
109   if (!myMap->Map().IsEmpty())
110   {
111     Backup();
112     myMap = new TColStd_HPackedMapOfInteger;
113     return Standard_True;
114   }
115   return Standard_False;
116 }
117
118 //=======================================================================
119 //function : Contains
120 //purpose  : 
121 //=======================================================================
122 Standard_Boolean TDataStd_IntPackedMap::Contains(const Standard_Integer theKey) const
123 {
124   return myMap->Map().Contains(theKey);
125 }
126
127 //=======================================================================
128 //function : Add
129 //purpose  : 
130 //=======================================================================
131
132 Standard_Boolean TDataStd_IntPackedMap::Add(const Standard_Integer theKey)
133 {
134   Standard_Boolean aResult = !myMap->Map().Contains(theKey);
135   if (aResult)
136   {
137     Backup();
138     aResult = myMap->ChangeMap().Add(theKey);
139   }
140   return aResult;
141 }
142 //=======================================================================
143 //function : Remove
144 //purpose  : 
145 //=======================================================================
146
147 Standard_Boolean TDataStd_IntPackedMap::Remove(const Standard_Integer theKey)
148 {
149   Standard_Boolean aResult = myMap->Map().Contains(theKey);
150   if (aResult)
151   {
152     Backup();
153     aResult = myMap->ChangeMap().Remove(theKey);
154   }
155   return aResult;
156 }
157
158 //=======================================================================
159 //function : NewEmpty
160 //purpose  : 
161 //=======================================================================
162
163 Handle(TDF_Attribute) TDataStd_IntPackedMap::NewEmpty () const
164 {
165   return new TDataStd_IntPackedMap;
166 }
167
168 //=======================================================================
169 //function : Restore
170 //purpose  : 
171 //=======================================================================
172
173 void TDataStd_IntPackedMap::Restore (const Handle(TDF_Attribute)& theWith)
174 {
175   Handle(TDataStd_IntPackedMap) aWith = 
176     Handle(TDataStd_IntPackedMap)::DownCast(theWith);
177   if (aWith->myMap.IsNull())
178     myMap.Nullify();
179   else
180   {
181     myMap = new TColStd_HPackedMapOfInteger;
182     myMap->ChangeMap().Assign(aWith->myMap->Map());
183     myIsDelta = aWith->myIsDelta;
184   }
185 }
186
187 //=======================================================================
188 //function : Paste
189 //purpose  : 
190 //=======================================================================
191
192 void TDataStd_IntPackedMap::Paste (const Handle(TDF_Attribute)& theInto,
193                               const Handle(TDF_RelocationTable)&) const
194
195   Handle(TDataStd_IntPackedMap) anInto = 
196     Handle(TDataStd_IntPackedMap)::DownCast(theInto);
197   if(!anInto.IsNull()) {
198     anInto->ChangeMap(myMap);
199     anInto->SetDelta(myIsDelta);
200   }
201 }
202
203 //=======================================================================
204 //function : ID
205 //purpose  : 
206 //=======================================================================
207 const Standard_GUID& TDataStd_IntPackedMap::ID() const 
208 { return GetID(); }
209
210 //=======================================================================
211 //function : Dump
212 //purpose  : 
213 //=======================================================================
214
215 Standard_OStream& TDataStd_IntPackedMap::Dump(Standard_OStream& theOS) const
216 {
217   Standard_OStream& anOS = TDF_Attribute::Dump( theOS );
218   anOS << "IntPackedMap size = " << Extent();
219   anOS << " Delta is " << (myIsDelta ? "ON":"OFF");
220   anOS << std::endl;
221   return anOS;
222 }
223
224 //=======================================================================
225 //function : DeltaOnModification
226 //purpose  : 
227 //=======================================================================
228
229 Handle(TDF_DeltaOnModification) TDataStd_IntPackedMap::DeltaOnModification
230 (const Handle(TDF_Attribute)& OldAttribute) const
231 {
232   if(myIsDelta)
233     return new TDataStd_DeltaOnModificationOfIntPackedMap(Handle(TDataStd_IntPackedMap)::DownCast (OldAttribute));
234   else return new TDF_DefaultDeltaOnModification(OldAttribute);
235 }
236
237