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