0023912: TDataStd_ExtStringArray::Value() returns a copy of TCollection_ExtendedStrin...
[occt.git] / src / TDataStd / TDataStd_DeltaOnModificationOfExtStringArray.cxx
1 // Created on: 2007-12-04
2 // Created by: Sergey ZARITCHNY
3 // Copyright (c) 2007-2012 OPEN CASCADE SAS
4 //
5 // The content of this file is subject to the Open CASCADE Technology Public
6 // License Version 6.5 (the "License"). You may not use the content of this file
7 // except in compliance with the License. Please obtain a copy of the License
8 // at http://www.opencascade.org and read it completely before using this file.
9 //
10 // The Initial Developer of the Original Code is Open CASCADE S.A.S., having its
11 // main offices at: 1, place des Freres Montgolfier, 78280 Guyancourt, France.
12 //
13 // The Original Code and all software distributed under the License is
14 // distributed on an "AS IS" basis, without warranty of any kind, and the
15 // Initial Developer hereby disclaims all such warranties, including without
16 // limitation, any warranties of merchantability, fitness for a particular
17 // purpose or non-infringement. Please see the License for the specific terms
18 // and conditions governing the rights and limitations under the License.
19
20
21 #include <TDataStd_DeltaOnModificationOfExtStringArray.ixx>
22 #include <TDataStd_ExtStringArray.hxx>
23 #include <TDF_DeltaOnModification.hxx>
24 #include <TDF_Label.hxx>
25 #include <TColStd_HArray1OfExtendedString.hxx>
26 #include <TColStd_ListOfInteger.hxx>
27 #include <TColStd_ListIteratorOfListOfInteger.hxx>
28 #include <TDF_AttributeIterator.hxx>
29
30 #ifdef DEB
31 #define MAXUP 1000
32 #endif
33 //=======================================================================
34 //function : TDataStd_DeltaOnModificationOfExtStringArray
35 //purpose  : 
36 //=======================================================================
37
38 TDataStd_DeltaOnModificationOfExtStringArray::TDataStd_DeltaOnModificationOfExtStringArray(const Handle(TDataStd_ExtStringArray)& OldAtt)
39 : TDF_DeltaOnModification(OldAtt)
40 {
41   Handle(TDataStd_ExtStringArray) CurrAtt;
42   if (Label().FindAttribute(OldAtt->ID(),CurrAtt)) {
43     {
44       Handle(TColStd_HArray1OfExtendedString) Arr1, Arr2;
45       Arr1 = OldAtt->Array();
46       Arr2 = CurrAtt->Array();
47 #ifdef DEB
48       if(Arr1.IsNull())
49         cout <<"DeltaOnModificationOfExtStringArray:: Old IntArray is Null" <<endl;
50       if(Arr2.IsNull())
51         cout <<"DeltaOnModificationOfExtStringArray:: Current IntArray is Null" <<endl;
52 #endif
53
54       if(Arr1.IsNull() || Arr2.IsNull()) return;
55       if(Arr1 != Arr2) {
56         myUp1 = Arr1->Upper();
57         myUp2 = Arr2->Upper();
58         Standard_Integer i, N =0, aCase=0; 
59         if(myUp1 == myUp2) 
60           {aCase = 1; N = myUp1;}
61         else if(myUp1 < myUp2) 
62           {aCase = 2; N = myUp1;}
63         else 
64           {aCase = 3; N = myUp2;}//Up1 > Up2
65
66         TColStd_ListOfInteger aList;
67         for(i=Arr1->Lower();i <= N; i++)
68           if(Arr1->Value(i) != Arr2->Value(i)) 
69             aList.Append(i);
70         if(aCase == 3) {
71           for(i = N+1;i <= myUp1; i++)
72             aList.Append(i);
73         }
74
75         if(aList.Extent()) {
76           myIndxes = new TColStd_HArray1OfInteger(1,aList.Extent());
77           myValues = new TColStd_HArray1OfExtendedString(1,aList.Extent());
78           TColStd_ListIteratorOfListOfInteger anIt(aList);
79           for(i =1;anIt.More();anIt.Next(),i++) {
80             myIndxes->SetValue(i, anIt.Value());
81             myValues->SetValue(i, Arr1->Value(anIt.Value()));
82           }
83         }
84       }
85     }
86     OldAtt->RemoveArray();
87 #ifdef DEB
88     if(OldAtt->Array().IsNull())
89       cout << "BackUp Arr is Nullified" << endl;
90 #endif
91   }
92 }
93
94
95 //=======================================================================
96 //function : Apply
97 //purpose  : 
98 //=======================================================================
99
100 void TDataStd_DeltaOnModificationOfExtStringArray::Apply()
101 {
102
103   Handle(TDF_Attribute) TDFAttribute = Attribute();
104   Handle(TDataStd_ExtStringArray) BackAtt = (*((Handle(TDataStd_ExtStringArray)*)&TDFAttribute));
105   if(BackAtt.IsNull()) {
106 #ifdef DEB
107     cout << "DeltaOnModificationOfExtStringArray::Apply: OldAtt is Null" <<endl;
108 #endif
109     return;
110   }
111   
112   Handle(TDataStd_ExtStringArray) aCurAtt;
113   if (!Label().FindAttribute(BackAtt->ID(),aCurAtt)) {
114
115     Label().AddAttribute(BackAtt);
116   }
117
118   if(aCurAtt.IsNull()) {
119 #ifdef DEB
120     cout << "DeltaOnModificationOfExtStringArray::Apply: CurAtt is Null" <<endl;
121 #endif
122     return;
123   }
124   else 
125     aCurAtt->Backup();
126
127   Standard_Integer aCase;
128   if(myUp1 == myUp2) 
129     aCase = 1;
130   else if(myUp1 < myUp2) 
131     aCase = 2;
132   else 
133     aCase = 3;//Up1 > Up2
134
135   if (aCase == 1 && (myIndxes.IsNull() || myValues.IsNull()))
136     return;
137   
138   Standard_Integer i;
139   Handle(TColStd_HArray1OfExtendedString) aStrArr = aCurAtt->Array();
140   if(aStrArr.IsNull()) return;
141
142   if(aCase == 1)   
143     for(i = 1; i <= myIndxes->Upper();i++) 
144       aStrArr->ChangeArray1().SetValue(myIndxes->Value(i), myValues->Value(i));
145   else if(aCase == 2) {    
146     Handle(TColStd_HArray1OfExtendedString) strArr = 
147       new TColStd_HArray1OfExtendedString(aStrArr->Lower(), myUp1);
148     for(i = aStrArr->Lower(); i <= myUp1 && i <= aStrArr->Upper(); i++) 
149       strArr->SetValue(i, aStrArr->Value(i));
150     if(!myIndxes.IsNull() && !myValues.IsNull())
151       for(i = 1; i <= myIndxes->Upper();i++) 
152         strArr->ChangeArray1().SetValue(myIndxes->Value(i), myValues->Value(i));
153     aCurAtt->myValue = strArr;
154   }
155   else { // == 3
156     Standard_Integer low = aStrArr->Lower();
157     Handle(TColStd_HArray1OfExtendedString) strArr = 
158       new TColStd_HArray1OfExtendedString(low, myUp1);
159     for(i = aStrArr->Lower(); i <= myUp2 && i <= aStrArr->Upper(); i++) 
160       strArr->SetValue(i, aStrArr->Value(i));
161     if(!myIndxes.IsNull() && !myValues.IsNull())
162       for(i = 1; i <= myIndxes->Upper();i++) {
163 #ifdef DEB  
164         cout << "i = " << i << "  myIndxes->Upper = " << myIndxes->Upper() << endl;
165         cout << "myIndxes->Value(i) = " << myIndxes->Value(i) << endl;
166         cout << "myValues->Value(i) = " << myValues->Value(i) << endl;
167 #endif
168         strArr->ChangeArray1().SetValue(myIndxes->Value(i), myValues->Value(i));      
169       }
170     aCurAtt->myValue = strArr;
171   }
172
173   
174 #ifdef DEB
175   cout << " << Array Dump after Delta Apply >>" <<endl;
176   Handle(TColStd_HArray1OfExtendedString) aStrArr2 = aCurAtt->Array();
177   for(i=aStrArr2->Lower(); i<= aStrArr2->Upper() && i<= MAXUP;i++)
178     cout << aStrArr2->Value(i) << "  ";
179   cout <<endl;
180 #endif
181 }
182
183