0033040: Coding - get rid of unused headers [Storage to TopOpeBRepTool]
[occt.git] / src / TDataStd / TDataStd_DeltaOnModificationOfExtStringArray.cxx
1 // Created on: 2007-12-04
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_DeltaOnModificationOfExtStringArray.hxx>
17
18 #include <Standard_Type.hxx>
19 #include <TColStd_HArray1OfExtendedString.hxx>
20 #include <TColStd_ListOfInteger.hxx>
21 #include <TDataStd_ExtStringArray.hxx>
22 #include <TDF_DeltaOnModification.hxx>
23 #include <TDF_Label.hxx>
24
25 IMPLEMENT_STANDARD_RTTIEXT(TDataStd_DeltaOnModificationOfExtStringArray,TDF_DeltaOnModification)
26
27 #ifdef OCCT_DEBUG
28 #define MAXUP 1000
29 #endif
30 //=======================================================================
31 //function : TDataStd_DeltaOnModificationOfExtStringArray
32 //purpose  : 
33 //=======================================================================
34
35 TDataStd_DeltaOnModificationOfExtStringArray::TDataStd_DeltaOnModificationOfExtStringArray(const Handle(TDataStd_ExtStringArray)& OldAtt)
36 : TDF_DeltaOnModification(OldAtt),
37   myUp1(0),
38   myUp2(0)
39 {
40   Handle(TDataStd_ExtStringArray) CurrAtt;
41   if (Label().FindAttribute(OldAtt->ID(),CurrAtt)) {
42     {
43       Handle(TColStd_HArray1OfExtendedString) Arr1, Arr2;
44       Arr1 = OldAtt->Array();
45       Arr2 = CurrAtt->Array();
46 #ifdef OCCT_DEBUG
47       if(Arr1.IsNull())
48         std::cout <<"DeltaOnModificationOfExtStringArray:: Old IntArray is Null" <<std::endl;
49       if(Arr2.IsNull())
50         std::cout <<"DeltaOnModificationOfExtStringArray:: Current IntArray is Null" <<std::endl;
51 #endif
52
53       if(Arr1.IsNull() || Arr2.IsNull()) return;
54       if(Arr1 != Arr2) {
55         myUp1 = Arr1->Upper();
56         myUp2 = Arr2->Upper();
57         Standard_Integer i, N =0, aCase=0; 
58         if(myUp1 == myUp2) 
59           {aCase = 1; N = myUp1;}
60         else if(myUp1 < myUp2) 
61           {aCase = 2; N = myUp1;}
62         else 
63           {aCase = 3; N = myUp2;}//Up1 > Up2
64
65         TColStd_ListOfInteger aList;
66         for(i=Arr1->Lower();i <= N; i++)
67           if(Arr1->Value(i) != Arr2->Value(i)) 
68             aList.Append(i);
69         if(aCase == 3) {
70           for(i = N+1;i <= myUp1; i++)
71             aList.Append(i);
72         }
73
74         if(aList.Extent()) {
75           myIndxes = new TColStd_HArray1OfInteger(1,aList.Extent());
76           myValues = new TColStd_HArray1OfExtendedString(1,aList.Extent());
77           TColStd_ListIteratorOfListOfInteger anIt(aList);
78           for(i =1;anIt.More();anIt.Next(),i++) {
79             myIndxes->SetValue(i, anIt.Value());
80             myValues->SetValue(i, Arr1->Value(anIt.Value()));
81           }
82         }
83       }
84     }
85     OldAtt->RemoveArray();
86 #ifdef OCCT_DEBUG
87     if(OldAtt->Array().IsNull())
88       std::cout << "BackUp Arr is Nullified" << std::endl;
89 #endif
90   }
91 }
92
93
94 //=======================================================================
95 //function : Apply
96 //purpose  : 
97 //=======================================================================
98
99 void TDataStd_DeltaOnModificationOfExtStringArray::Apply()
100 {
101
102   Handle(TDF_Attribute) TDFAttribute = Attribute();
103   Handle(TDataStd_ExtStringArray) BackAtt = Handle(TDataStd_ExtStringArray)::DownCast (TDFAttribute);
104   if(BackAtt.IsNull()) {
105 #ifdef OCCT_DEBUG
106     std::cout << "DeltaOnModificationOfExtStringArray::Apply: OldAtt is Null" <<std::endl;
107 #endif
108     return;
109   }
110   
111   Handle(TDataStd_ExtStringArray) aCurAtt;
112   if (!Label().FindAttribute(BackAtt->ID(),aCurAtt)) {
113
114     Label().AddAttribute(BackAtt);
115   }
116
117   if(aCurAtt.IsNull()) {
118 #ifdef OCCT_DEBUG
119     std::cout << "DeltaOnModificationOfExtStringArray::Apply: CurAtt is Null" <<std::endl;
120 #endif
121     return;
122   }
123   else 
124     aCurAtt->Backup();
125
126   Standard_Integer aCase;
127   if(myUp1 == myUp2) 
128     aCase = 1;
129   else if(myUp1 < myUp2) 
130     aCase = 2;
131   else 
132     aCase = 3;//Up1 > Up2
133
134   if (aCase == 1 && (myIndxes.IsNull() || myValues.IsNull()))
135     return;
136   
137   Standard_Integer i;
138   Handle(TColStd_HArray1OfExtendedString) aStrArr = aCurAtt->Array();
139   if(aStrArr.IsNull()) return;
140
141   if(aCase == 1)   
142     for(i = 1; i <= myIndxes->Upper();i++) 
143       aStrArr->ChangeArray1().SetValue(myIndxes->Value(i), myValues->Value(i));
144   else if(aCase == 2) {    
145     Handle(TColStd_HArray1OfExtendedString) strArr = 
146       new TColStd_HArray1OfExtendedString(aStrArr->Lower(), myUp1);
147     for(i = aStrArr->Lower(); i <= myUp1 && i <= aStrArr->Upper(); i++) 
148       strArr->SetValue(i, aStrArr->Value(i));
149     if(!myIndxes.IsNull() && !myValues.IsNull())
150       for(i = 1; i <= myIndxes->Upper();i++) 
151         strArr->ChangeArray1().SetValue(myIndxes->Value(i), myValues->Value(i));
152     aCurAtt->myValue = strArr;
153   }
154   else { // == 3
155     Standard_Integer low = aStrArr->Lower();
156     Handle(TColStd_HArray1OfExtendedString) strArr = 
157       new TColStd_HArray1OfExtendedString(low, myUp1);
158     for(i = aStrArr->Lower(); i <= myUp2 && i <= aStrArr->Upper(); i++) 
159       strArr->SetValue(i, aStrArr->Value(i));
160     if(!myIndxes.IsNull() && !myValues.IsNull())
161       for(i = 1; i <= myIndxes->Upper();i++) {
162 #ifdef OCCT_DEBUG
163         std::cout << "i = " << i << "  myIndxes->Upper = " << myIndxes->Upper() << std::endl;
164         std::cout << "myIndxes->Value(i) = " << myIndxes->Value(i) << std::endl;
165         std::cout << "myValues->Value(i) = " << myValues->Value(i) << std::endl;
166 #endif
167         strArr->ChangeArray1().SetValue(myIndxes->Value(i), myValues->Value(i));      
168       }
169     aCurAtt->myValue = strArr;
170   }
171
172   
173 #ifdef OCCT_DEBUG
174   std::cout << " << Array Dump after Delta Apply >>" <<std::endl;
175   Handle(TColStd_HArray1OfExtendedString) aStrArr2 = aCurAtt->Array();
176   for(i=aStrArr2->Lower(); i<= aStrArr2->Upper() && i<= MAXUP;i++)
177     std::cout << aStrArr2->Value(i) << "  ";
178   std::cout <<std::endl;
179 #endif
180 }
181
182