Salome HOME
New implementation of SALOMEDSImpl package based on DF data structure instead of...
[modules/kernel.git] / src / SALOMEDSImpl / SALOMEDSImpl_AttributeSequenceOfInteger.cxx
1 // Copyright (C) 2005  OPEN CASCADE, EADS/CCR, LIP6, CEA/DEN,
2 // CEDRAT, EDF R&D, LEG, PRINCIPIA R&D, BUREAU VERITAS
3 // 
4 // This library is free software; you can redistribute it and/or
5 // modify it under the terms of the GNU Lesser General Public
6 // License as published by the Free Software Foundation; either 
7 // version 2.1 of the License.
8 // 
9 // This library is distributed in the hope that it will be useful 
10 // but WITHOUT ANY WARRANTY; without even the implied warranty of 
11 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU 
12 // Lesser General Public License for more details.
13 //
14 // You should have received a copy of the GNU Lesser General Public  
15 // License along with this library; if not, write to the Free Software 
16 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
17 //
18 // See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
19 //
20 //  File   : SALOMEDSImpl_AttributeSequenceOfInteger.cxx
21 //  Author : Sergey RUIN
22 //  Module : SALOME
23
24 #include "SALOMEDSImpl_AttributeSequenceOfInteger.hxx"
25
26 using namespace std;
27
28 //=======================================================================
29 //function : GetID
30 //purpose  : 
31 //=======================================================================
32
33 const std::string& SALOMEDSImpl_AttributeSequenceOfInteger::GetID () 
34 {
35   static std::string SALOMEDSImpl_AttributeSequenceOfIntegerID ("12837182-8F52-11d6-A8A3-0001021E8C7F");
36   return SALOMEDSImpl_AttributeSequenceOfIntegerID;
37 }
38
39
40
41 //=======================================================================
42 //function : Set
43 //purpose  : 
44 //=======================================================================
45
46 SALOMEDSImpl_AttributeSequenceOfInteger* SALOMEDSImpl_AttributeSequenceOfInteger::Set (const DF_Label& L) 
47 {
48   SALOMEDSImpl_AttributeSequenceOfInteger* A = NULL;
49   if (!(A = (SALOMEDSImpl_AttributeSequenceOfInteger*)L.FindAttribute(SALOMEDSImpl_AttributeSequenceOfInteger::GetID()))) {
50     A = new  SALOMEDSImpl_AttributeSequenceOfInteger(); 
51     L.AddAttribute(A);
52   }
53   return A;
54 }
55
56
57 //=======================================================================
58 //function : constructor
59 //purpose  : 
60 //=======================================================================
61 SALOMEDSImpl_AttributeSequenceOfInteger::SALOMEDSImpl_AttributeSequenceOfInteger()
62 :SALOMEDSImpl_GenericAttribute("AttributeSequenceOfInteger")
63 {}
64
65 //=======================================================================
66 //function : ID
67 //purpose  : 
68 //=======================================================================
69
70 const std::string& SALOMEDSImpl_AttributeSequenceOfInteger::ID () const { return GetID(); }
71
72
73 //=======================================================================
74 //function : NewEmpty
75 //purpose  : 
76 //=======================================================================
77
78 DF_Attribute* SALOMEDSImpl_AttributeSequenceOfInteger::NewEmpty () const
79 {  
80   return new SALOMEDSImpl_AttributeSequenceOfInteger(); 
81 }
82
83 //=======================================================================
84 //function : Restore
85 //purpose  : 
86 //=======================================================================
87
88 void SALOMEDSImpl_AttributeSequenceOfInteger::Restore(DF_Attribute* with) 
89 {
90   SALOMEDSImpl_AttributeSequenceOfInteger* anSeq = dynamic_cast<SALOMEDSImpl_AttributeSequenceOfInteger*>(with);
91   myValue.clear();
92   for(int i = 0, len = anSeq->Length(); i<len; i++)
93     myValue.push_back(anSeq->myValue[i]);    
94 }
95
96 //=======================================================================
97 //function : Paste
98 //purpose  : 
99 //=======================================================================
100
101 void SALOMEDSImpl_AttributeSequenceOfInteger::Paste (DF_Attribute* into)
102 {
103   dynamic_cast<SALOMEDSImpl_AttributeSequenceOfInteger*>(into)->Assign(myValue);
104 }
105
106 void SALOMEDSImpl_AttributeSequenceOfInteger::Assign(const vector<int>& other) 
107 {
108   CheckLocked();
109   Backup();
110
111   myValue = other;
112   
113   SetModifyFlag(); //SRN: Mark the study as being modified, so it could be saved 
114 }
115
116 void SALOMEDSImpl_AttributeSequenceOfInteger::ChangeValue(const int Index,const int Value) 
117 {
118   CheckLocked();  
119   Backup();
120
121   if(Index <= 0 || Index > myValue.size()) throw DFexception("Out of range");
122
123   myValue[Index-1] = Value;
124   
125   SetModifyFlag(); //SRN: Mark the study as being modified, so it could be saved 
126 }
127
128 void SALOMEDSImpl_AttributeSequenceOfInteger::Add(const int Value) 
129 {
130   CheckLocked();  
131   Backup();
132   myValue.push_back(Value);
133   
134   SetModifyFlag(); //SRN: Mark the study as being modified, so it could be saved 
135 }
136
137 void SALOMEDSImpl_AttributeSequenceOfInteger::Remove(const int Index) 
138 {
139   CheckLocked();  
140   Backup();
141
142   if(Index <= 0 || Index > myValue.size()) throw DFexception("Out of range");
143
144   typedef vector<int>::iterator VI;
145   int i = 1;    
146   for(VI p = myValue.begin(); p!=myValue.end(); p++, i++) {
147     if(i == Index) {
148       myValue.erase(p);
149       break;
150     }     
151   }
152
153   SetModifyFlag(); //SRN: Mark the study as being modified, so it could be saved 
154 }
155
156 int SALOMEDSImpl_AttributeSequenceOfInteger::Length() 
157 {
158   return myValue.size();
159 }
160 int SALOMEDSImpl_AttributeSequenceOfInteger::Value(const int Index) 
161 {
162   if(Index <= 0 || Index > myValue.size()) throw DFexception("Out of range");
163
164   return myValue[Index-1];
165 }
166
167
168
169 string SALOMEDSImpl_AttributeSequenceOfInteger::Save() 
170 {
171   int aLength = Length();
172   char* aResult = new char[aLength * 25];
173   aResult[0] = 0;
174   int aPosition = 0;
175   for (int i = 1; i <= aLength; i++) {
176     sprintf(aResult + aPosition , "%d ", Value(i));
177     aPosition += strlen(aResult + aPosition);
178   }
179   string ret(aResult);
180   delete aResult;
181   
182   return ret;
183 }
184                         
185 void SALOMEDSImpl_AttributeSequenceOfInteger::Load(const string& value) 
186 {
187   char* aCopy = (char*)value.c_str();
188   char* adr = strtok(aCopy, " ");
189   while (adr) {
190     int l =  atol(adr);
191     Add(l);
192     adr = strtok(NULL, " ");
193   }
194 }