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