Salome HOME
Updated copyright comment
[modules/kernel.git] / src / SALOMEDSImpl / SALOMEDSImpl_AttributeSequenceOfInteger.cxx
1 // Copyright (C) 2007-2024  CEA, EDF, 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, or (at your option) any later version.
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 #include <stdlib.h>
30
31 //=======================================================================
32 //function : GetID
33 //purpose  : 
34 //=======================================================================
35
36 const std::string& SALOMEDSImpl_AttributeSequenceOfInteger::GetID () 
37 {
38   static std::string SALOMEDSImpl_AttributeSequenceOfIntegerID ("12837182-8F52-11d6-A8A3-0001021E8C7F");
39   return SALOMEDSImpl_AttributeSequenceOfIntegerID;
40 }
41
42
43
44 //=======================================================================
45 //function : Set
46 //purpose  : 
47 //=======================================================================
48
49 SALOMEDSImpl_AttributeSequenceOfInteger* SALOMEDSImpl_AttributeSequenceOfInteger::Set (const DF_Label& L) 
50 {
51   SALOMEDSImpl_AttributeSequenceOfInteger* A = NULL;
52   if (!(A = (SALOMEDSImpl_AttributeSequenceOfInteger*)L.FindAttribute(SALOMEDSImpl_AttributeSequenceOfInteger::GetID()))) {
53     A = new  SALOMEDSImpl_AttributeSequenceOfInteger(); 
54     L.AddAttribute(A);
55   }
56   return A;
57 }
58
59
60 //=======================================================================
61 //function : constructor
62 //purpose  : 
63 //=======================================================================
64 SALOMEDSImpl_AttributeSequenceOfInteger::SALOMEDSImpl_AttributeSequenceOfInteger()
65 :SALOMEDSImpl_GenericAttribute("AttributeSequenceOfInteger")
66 {}
67
68 //=======================================================================
69 //function : ID
70 //purpose  : 
71 //=======================================================================
72
73 const std::string& SALOMEDSImpl_AttributeSequenceOfInteger::ID () const { return GetID(); }
74
75
76 //=======================================================================
77 //function : NewEmpty
78 //purpose  : 
79 //=======================================================================
80
81 DF_Attribute* SALOMEDSImpl_AttributeSequenceOfInteger::NewEmpty () const
82 {  
83   return new SALOMEDSImpl_AttributeSequenceOfInteger(); 
84 }
85
86 //=======================================================================
87 //function : Restore
88 //purpose  : 
89 //=======================================================================
90
91 void SALOMEDSImpl_AttributeSequenceOfInteger::Restore(DF_Attribute* with) 
92 {
93   SALOMEDSImpl_AttributeSequenceOfInteger* anSeq = dynamic_cast<SALOMEDSImpl_AttributeSequenceOfInteger*>(with);
94   myValue.clear();
95   for(int i = 0, len = anSeq->Length(); i<len; i++)
96     myValue.push_back(anSeq->myValue[i]);    
97 }
98
99 //=======================================================================
100 //function : Paste
101 //purpose  : 
102 //=======================================================================
103
104 void SALOMEDSImpl_AttributeSequenceOfInteger::Paste (DF_Attribute* into)
105 {
106   dynamic_cast<SALOMEDSImpl_AttributeSequenceOfInteger*>(into)->Assign(myValue);
107 }
108
109 void SALOMEDSImpl_AttributeSequenceOfInteger::Assign(const std::vector<int>& other) 
110 {
111   CheckLocked();
112   Backup();
113
114   myValue = other;
115   
116   SetModifyFlag(); //SRN: Mark the study as being modified, so it could be saved 
117 }
118
119 void SALOMEDSImpl_AttributeSequenceOfInteger::ChangeValue(const int Index,const int Value) 
120 {
121   CheckLocked();  
122   Backup();
123
124   if(Index <= 0 || Index > (int)myValue.size()) throw DFexception("Out of range"); // TODO: mismatch signed/unsigned
125
126   myValue[Index-1] = Value;
127   
128   SetModifyFlag(); //SRN: Mark the study as being modified, so it could be saved 
129 }
130
131 void SALOMEDSImpl_AttributeSequenceOfInteger::Add(const int Value) 
132 {
133   CheckLocked();  
134   Backup();
135   myValue.push_back(Value);
136   
137   SetModifyFlag(); //SRN: Mark the study as being modified, so it could be saved 
138 }
139
140 void SALOMEDSImpl_AttributeSequenceOfInteger::Remove(const int Index) 
141 {
142   CheckLocked();  
143   Backup();
144
145   if(Index <= 0 || Index > (int)myValue.size()) throw DFexception("Out of range"); // TODO: mismatch signed/unsigned
146
147   typedef std::vector<int>::iterator VI;
148   int i = 1;    
149   for(VI p = myValue.begin(); p!=myValue.end(); p++, i++) {
150     if(i == Index) {
151       myValue.erase(p);
152       break;
153     }     
154   }
155
156   SetModifyFlag(); //SRN: Mark the study as being modified, so it could be saved 
157 }
158
159 int SALOMEDSImpl_AttributeSequenceOfInteger::Length() 
160 {
161   return (int)myValue.size(); //!< TODO: conversion from size_t to int, possible loss of data
162 }
163 int SALOMEDSImpl_AttributeSequenceOfInteger::Value(const int Index) 
164 {
165   if(Index <= 0 || Index > (int)myValue.size()) throw DFexception("Out of range"); // TODO: mismatch signed/unsigned
166
167   return myValue[Index-1];
168 }
169
170
171
172 std::string SALOMEDSImpl_AttributeSequenceOfInteger::Save() 
173 {
174   int aLength = Length();
175   char* aResult = new char[aLength * 25];
176   aResult[0] = 0;
177   size_t aPosition = 0;
178   for (int i = 1; i <= aLength; i++) {
179     sprintf(aResult + aPosition , "%d ", Value(i));
180     aPosition += strlen(aResult + aPosition);
181   }
182   std::string ret(aResult);
183   delete aResult;
184   
185   return ret;
186 }
187                         
188 void SALOMEDSImpl_AttributeSequenceOfInteger::Load(const std::string& value) 
189 {
190   char* aCopy = (char*)value.c_str();
191   char* adr = strtok(aCopy, " ");
192   while (adr) {
193     int l =  atol(adr);
194     Add(l);
195     adr = strtok(NULL, " ");
196   }
197 }