Salome HOME
PR: merge from branch BR_UnitTests tag mergeto_trunk_17oct05
[modules/yacs.git] / src / SALOMEDS / SALOMEDS_AttributeTableOfReal_i.cxx
1 //  File   : SALOMEDS_AttributeTableOfReal_i.cxx
2 //  Author : Sergey RUIN
3 //  Module : SALOME
4
5
6 #include "SALOMEDS_AttributeTableOfReal_i.hxx"
7 #include "SALOMEDS.hxx"
8
9 #include <TColStd_HSequenceOfReal.hxx>
10 #include <Standard_Failure.hxx>
11 #include <Standard_ErrorHandler.hxx>
12
13 #include <strstream>
14 #include <string>
15
16 using namespace std;
17
18 #include "Utils_ExceptHandlers.hxx"
19 UNEXPECT_CATCH(ATR_IncorrectIndex, SALOMEDS::AttributeTableOfReal::IncorrectIndex);
20 UNEXPECT_CATCH(ATR_IncorrectArgumentLength, SALOMEDS::AttributeTableOfReal::IncorrectArgumentLength);
21
22 void SALOMEDS_AttributeTableOfReal_i::SetTitle(const char* theTitle) 
23 {
24   SALOMEDS::Locker lock;     
25   CheckLocked();
26   Handle(SALOMEDSImpl_AttributeTableOfReal) aTable = Handle(SALOMEDSImpl_AttributeTableOfReal)::DownCast(_impl);
27   CORBA::String_var aStr = CORBA::string_dup(theTitle);
28   aTable->SetTitle(TCollection_ExtendedString(aStr));
29 }
30
31 char* SALOMEDS_AttributeTableOfReal_i::GetTitle() 
32 {
33   SALOMEDS::Locker lock; 
34   Handle(SALOMEDSImpl_AttributeTableOfReal) aTable = Handle(SALOMEDSImpl_AttributeTableOfReal)::DownCast(_impl);
35   CORBA::String_var c_s = CORBA::string_dup(TCollection_AsciiString(aTable->GetTitle()).ToCString());
36   return c_s._retn();
37 }
38
39 void SALOMEDS_AttributeTableOfReal_i::SetRowTitle(CORBA::Long theIndex, const char* theTitle)
40      throw (SALOMEDS::AttributeTableOfReal::IncorrectIndex)
41 {
42   SALOMEDS::Locker lock; 
43   Unexpect aCatch (ATR_IncorrectIndex);
44   CheckLocked();
45   Handle(SALOMEDSImpl_AttributeTableOfReal) aTable = Handle(SALOMEDSImpl_AttributeTableOfReal)::DownCast(_impl);
46   if (theIndex <= 0 || theIndex > aTable->GetNbRows()) throw SALOMEDS::AttributeTableOfReal::IncorrectIndex();
47   aTable->SetRowTitle(theIndex, TCollection_ExtendedString((char*)theTitle));
48 }
49
50 void SALOMEDS_AttributeTableOfReal_i::SetRowTitles(const SALOMEDS::StringSeq& theTitles)
51      throw (SALOMEDS::AttributeTableOfReal::IncorrectArgumentLength)
52 {
53   SALOMEDS::Locker lock; 
54   Unexpect aCatch (ATR_IncorrectArgumentLength);
55   CheckLocked();
56   Handle(SALOMEDSImpl_AttributeTableOfReal) aTable = Handle(SALOMEDSImpl_AttributeTableOfReal)::DownCast(_impl);
57   if (theTitles.length() != aTable->GetNbRows()) throw SALOMEDS::AttributeTableOfReal::IncorrectArgumentLength();
58   for (int i = 0; i < theTitles.length(); i++) {
59     aTable->SetRowTitle(i + 1, (char*)theTitles[i].in());
60   }
61 }
62
63 SALOMEDS::StringSeq* SALOMEDS_AttributeTableOfReal_i::GetRowTitles() 
64 {
65   SALOMEDS::Locker lock; 
66   Handle(SALOMEDSImpl_AttributeTableOfReal) aTable = Handle(SALOMEDSImpl_AttributeTableOfReal)::DownCast(_impl);
67   SALOMEDS::StringSeq_var aTitles = new SALOMEDS::StringSeq;
68   aTitles->length(aTable->GetNbRows());
69   for(int i = 0; i < aTitles->length(); i++)
70     aTitles[i] = CORBA::string_dup(TCollection_AsciiString(aTable->GetRowTitle(i + 1)).ToCString());
71   return aTitles._retn();
72 }
73
74 void SALOMEDS_AttributeTableOfReal_i::SetColumnTitle(CORBA::Long theIndex, const char* theTitle)
75      throw (SALOMEDS::AttributeTableOfReal::IncorrectIndex)
76 {
77   SALOMEDS::Locker lock; 
78   Unexpect aCatch (ATR_IncorrectIndex);
79   CheckLocked();
80   Handle(SALOMEDSImpl_AttributeTableOfReal) aTable = Handle(SALOMEDSImpl_AttributeTableOfReal)::DownCast(_impl);
81   if (theIndex <= 0 || theIndex > aTable->GetNbColumns()) throw SALOMEDS::AttributeTableOfReal::IncorrectIndex();
82   CORBA::String_var aStr = CORBA::string_dup(theTitle);
83   aTable->SetColumnTitle(theIndex, TCollection_ExtendedString(aStr));
84 }
85
86 void SALOMEDS_AttributeTableOfReal_i::SetColumnTitles(const SALOMEDS::StringSeq& theTitles)
87      throw (SALOMEDS::AttributeTableOfReal::IncorrectArgumentLength)
88 {
89   SALOMEDS::Locker lock; 
90   Unexpect aCatch(ATR_IncorrectArgumentLength);
91   CheckLocked();
92   Handle(SALOMEDSImpl_AttributeTableOfReal) aTable = Handle(SALOMEDSImpl_AttributeTableOfReal)::DownCast(_impl);
93   if (theTitles.length() != aTable->GetNbColumns()) throw SALOMEDS::AttributeTableOfReal::IncorrectArgumentLength();
94   for (int i = 0; i < theTitles.length(); i++) {
95     aTable->SetColumnTitle(i + 1, (char*)theTitles[i].in());
96   }
97 }
98
99 SALOMEDS::StringSeq* SALOMEDS_AttributeTableOfReal_i::GetColumnTitles() 
100 {
101   SALOMEDS::Locker lock; 
102   Handle(SALOMEDSImpl_AttributeTableOfReal) aTable = Handle(SALOMEDSImpl_AttributeTableOfReal)::DownCast(_impl);
103   SALOMEDS::StringSeq_var aTitles = new SALOMEDS::StringSeq;
104   aTitles->length(aTable->GetNbColumns());
105   for(int i = 0; i < aTitles->length(); i++)
106     aTitles[i] = CORBA::string_dup(TCollection_AsciiString(aTable->GetColumnTitle(i + 1)).ToCString());
107   return aTitles._retn();
108 }
109
110 //Units support
111 void SALOMEDS_AttributeTableOfReal_i::SetRowUnit(CORBA::Long theIndex, const char* theUnit)
112      throw (SALOMEDS::AttributeTableOfReal::IncorrectIndex)
113 {
114   SALOMEDS::Locker lock; 
115   Unexpect aCatch (ATR_IncorrectIndex);
116   CheckLocked();
117   Handle(SALOMEDSImpl_AttributeTableOfReal) aTable = Handle(SALOMEDSImpl_AttributeTableOfReal)::DownCast(_impl);
118   if (theIndex <= 0 || theIndex > aTable->GetNbRows()) throw SALOMEDS::AttributeTableOfReal::IncorrectIndex();
119   aTable->SetRowUnit(theIndex, TCollection_ExtendedString((char*)theUnit));
120 }
121
122 void SALOMEDS_AttributeTableOfReal_i::SetRowUnits(const SALOMEDS::StringSeq& theUnits)
123      throw (SALOMEDS::AttributeTableOfReal::IncorrectArgumentLength)
124 {
125   SALOMEDS::Locker lock; 
126   Unexpect aCatch (ATR_IncorrectArgumentLength);
127   CheckLocked();
128   Handle(SALOMEDSImpl_AttributeTableOfReal) aTable = Handle(SALOMEDSImpl_AttributeTableOfReal)::DownCast(_impl);
129   if (theUnits.length() != aTable->GetNbRows()) throw SALOMEDS::AttributeTableOfReal::IncorrectArgumentLength();
130   for (int i = 0; i < theUnits.length(); i++) {
131     aTable->SetRowUnit(i + 1, (char*)theUnits[i].in());
132   }
133 }
134
135 SALOMEDS::StringSeq* SALOMEDS_AttributeTableOfReal_i::GetRowUnits() 
136 {
137   SALOMEDS::Locker lock; 
138   Handle(SALOMEDSImpl_AttributeTableOfReal) aTable = Handle(SALOMEDSImpl_AttributeTableOfReal)::DownCast(_impl);
139   SALOMEDS::StringSeq_var aUnits = new SALOMEDS::StringSeq;
140   aUnits->length(aTable->GetNbRows());
141   for(int i = 0; i < aUnits->length(); i++)
142     aUnits[i] = CORBA::string_dup(TCollection_AsciiString(aTable->GetRowUnit(i + 1)).ToCString());
143   return aUnits._retn();
144 }
145
146
147 CORBA::Long SALOMEDS_AttributeTableOfReal_i::GetNbRows() 
148 {
149   SALOMEDS::Locker lock; 
150   return Handle(SALOMEDSImpl_AttributeTableOfReal)::DownCast(_impl)->GetNbRows();
151 }
152
153 CORBA::Long SALOMEDS_AttributeTableOfReal_i::GetNbColumns() 
154 {
155   SALOMEDS::Locker lock; 
156   return Handle(SALOMEDSImpl_AttributeTableOfReal)::DownCast(_impl)->GetNbColumns();
157 }
158
159 void SALOMEDS_AttributeTableOfReal_i::AddRow(const SALOMEDS::DoubleSeq& theData)
160      throw (SALOMEDS::AttributeTableOfReal::IncorrectArgumentLength)
161 {
162   SALOMEDS::Locker lock; 
163   Unexpect aCatch(ATR_IncorrectArgumentLength);
164   CheckLocked();
165   Handle(SALOMEDSImpl_AttributeTableOfReal) aTable = Handle(SALOMEDSImpl_AttributeTableOfReal)::DownCast(_impl);
166   
167   Handle(TColStd_HSequenceOfReal) aRow = new TColStd_HSequenceOfReal;
168   for (int i = 0; i < theData.length(); i++) aRow->Append(theData[i]);
169   aTable->SetRowData(aTable->GetNbRows() + 1, aRow);
170 }
171
172 void SALOMEDS_AttributeTableOfReal_i::SetRow(CORBA::Long theRow, const SALOMEDS::DoubleSeq& theData)
173      throw (SALOMEDS::AttributeTableOfReal::IncorrectArgumentLength, SALOMEDS::AttributeTableOfReal::IncorrectIndex)
174 {
175   SALOMEDS::Locker lock; 
176   Unexpect aCatch(ATR_IncorrectArgumentLength);
177   CheckLocked();
178   Handle(SALOMEDSImpl_AttributeTableOfReal) aTable = Handle(SALOMEDSImpl_AttributeTableOfReal)::DownCast(_impl);
179   
180   Handle(TColStd_HSequenceOfReal) aRow = new TColStd_HSequenceOfReal;
181   for (int i = 0; i < theData.length(); i++) aRow->Append(theData[i]);
182   aTable->SetRowData(theRow, aRow);
183 }
184
185 SALOMEDS::DoubleSeq* SALOMEDS_AttributeTableOfReal_i::GetRow(CORBA::Long theRow)
186      throw (SALOMEDS::AttributeTableOfReal::IncorrectIndex)
187 {
188   SALOMEDS::Locker lock; 
189   Unexpect aCatch(ATR_IncorrectIndex);
190   Handle(SALOMEDSImpl_AttributeTableOfReal) aTable = Handle(SALOMEDSImpl_AttributeTableOfReal)::DownCast(_impl);
191   if (theRow <= 0 || theRow > aTable->GetNbRows()) throw SALOMEDS::AttributeTableOfReal::IncorrectIndex();
192
193   SALOMEDS::DoubleSeq_var CorbaSeq = new SALOMEDS::DoubleSeq;
194   Handle(TColStd_HSequenceOfReal) aRow = aTable->GetRowData(theRow);
195   CorbaSeq->length(aRow->Length());
196   for (int i = 0; i < aRow->Length(); i++) {
197     CorbaSeq[i] = aRow->Value(i + 1);
198   }
199   return CorbaSeq._retn();
200 }
201
202 void SALOMEDS_AttributeTableOfReal_i::AddColumn(const SALOMEDS::DoubleSeq& theData)
203      throw (SALOMEDS::AttributeTableOfReal::IncorrectArgumentLength)
204 {
205   SALOMEDS::Locker lock; 
206   Unexpect aCatch(ATR_IncorrectArgumentLength);
207   CheckLocked();
208   Handle(SALOMEDSImpl_AttributeTableOfReal) aTable = Handle(SALOMEDSImpl_AttributeTableOfReal)::DownCast(_impl);
209   
210   Handle(TColStd_HSequenceOfReal) aColumn = new TColStd_HSequenceOfReal;
211   for (int i = 0; i < theData.length(); i++) aColumn->Append(theData[i]);
212   aTable->SetColumnData(aTable->GetNbColumns() + 1, aColumn);
213 }
214
215 void SALOMEDS_AttributeTableOfReal_i::SetColumn(CORBA::Long theColumn, const SALOMEDS::DoubleSeq& theData)
216      throw (SALOMEDS::AttributeTableOfReal::IncorrectArgumentLength, SALOMEDS::AttributeTableOfReal::IncorrectIndex)
217 {
218   SALOMEDS::Locker lock; 
219   Unexpect aCatch(ATR_IncorrectArgumentLength);
220   CheckLocked();
221   Handle(SALOMEDSImpl_AttributeTableOfReal) aTable = Handle(SALOMEDSImpl_AttributeTableOfReal)::DownCast(_impl);
222   
223   Handle(TColStd_HSequenceOfReal) aColumn = new TColStd_HSequenceOfReal;
224   for (int i = 0; i < theData.length(); i++) aColumn->Append(theData[i]);
225   aTable->SetColumnData(theColumn, aColumn);
226 }
227
228 SALOMEDS::DoubleSeq* SALOMEDS_AttributeTableOfReal_i::GetColumn(CORBA::Long theColumn)
229      throw (SALOMEDS::AttributeTableOfReal::IncorrectIndex)
230 {
231   SALOMEDS::Locker lock; 
232   Unexpect aCatch(ATR_IncorrectIndex);
233   Handle(SALOMEDSImpl_AttributeTableOfReal) aTable = Handle(SALOMEDSImpl_AttributeTableOfReal)::DownCast(_impl);
234   if (theColumn <= 0 || theColumn > aTable->GetNbColumns()) throw SALOMEDS::AttributeTableOfReal::IncorrectIndex();
235
236   SALOMEDS::DoubleSeq_var CorbaSeq = new SALOMEDS::DoubleSeq;
237   Handle(TColStd_HSequenceOfReal) aColumn = aTable->GetColumnData(theColumn);
238   CorbaSeq->length(aColumn->Length());
239   for (int i = 0; i < aColumn->Length(); i++) {
240     CorbaSeq[i] = aColumn->Value(i + 1);
241   }
242   return CorbaSeq._retn();
243 }
244
245 void SALOMEDS_AttributeTableOfReal_i::PutValue(CORBA::Double theValue, CORBA::Long theRow, CORBA::Long theColumn)
246      throw (SALOMEDS::AttributeTableOfReal::IncorrectIndex)
247 {
248   SALOMEDS::Locker lock; 
249   Unexpect aCatch(ATR_IncorrectIndex);
250   CheckLocked();
251   Handle(SALOMEDSImpl_AttributeTableOfReal) aTable = Handle(SALOMEDSImpl_AttributeTableOfReal)::DownCast(_impl);
252
253   aTable->PutValue(theValue, theRow, theColumn);
254 }
255
256 CORBA::Boolean SALOMEDS_AttributeTableOfReal_i::HasValue(CORBA::Long theRow, CORBA::Long theColumn) 
257 {
258   SALOMEDS::Locker lock; 
259   return Handle(SALOMEDSImpl_AttributeTableOfReal)::DownCast(_impl)->HasValue(theRow, theColumn);
260 }
261
262 CORBA::Double SALOMEDS_AttributeTableOfReal_i::GetValue(CORBA::Long theRow, CORBA::Long theColumn)
263      throw (SALOMEDS::AttributeTableOfReal::IncorrectIndex)
264 {
265   SALOMEDS::Locker lock; 
266   Unexpect aCatch(ATR_IncorrectIndex);
267   Handle(SALOMEDSImpl_AttributeTableOfReal) aTable = Handle(SALOMEDSImpl_AttributeTableOfReal)::DownCast(_impl);
268   if (theRow > aTable->GetNbRows()) throw SALOMEDS::AttributeTableOfReal::IncorrectIndex();
269
270   CORBA::Double aValue;
271
272   try {
273     aValue = aTable->GetValue(theRow, theColumn);
274   }
275   catch(Standard_Failure) {
276     throw SALOMEDS::AttributeTableOfReal::IncorrectIndex();
277   }
278
279   return aValue;
280 }
281
282
283 SALOMEDS::LongSeq* SALOMEDS_AttributeTableOfReal_i::GetRowSetIndices(CORBA::Long theRow) 
284 {
285   SALOMEDS::Locker lock; 
286   Handle(SALOMEDSImpl_AttributeTableOfReal) aTable = Handle(SALOMEDSImpl_AttributeTableOfReal)::DownCast(_impl);
287
288   if(theRow <= 0 || theRow > aTable->GetNbRows()) throw SALOMEDS::AttributeTableOfReal::IncorrectIndex();
289
290   SALOMEDS::LongSeq_var CorbaSeq = new SALOMEDS::LongSeq;
291   Handle(TColStd_HSequenceOfInteger) aSeq = aTable->GetSetRowIndices(theRow);
292   CorbaSeq->length(aSeq->Length());
293   for (int i = 0; i < aSeq->Length(); i++) {
294     CorbaSeq[i] = aSeq->Value(i + 1);
295   }
296   return CorbaSeq._retn(); 
297 }
298
299
300 void SALOMEDS_AttributeTableOfReal_i::SetNbColumns(CORBA::Long theNbColumns)
301 {
302   SALOMEDS::Locker lock; 
303   Handle(SALOMEDSImpl_AttributeTableOfReal) aTable = Handle(SALOMEDSImpl_AttributeTableOfReal)::DownCast(_impl);
304   aTable->SetNbColumns(theNbColumns);
305 }
306
307 bool SALOMEDS_AttributeTableOfReal_i::ReadFromFile(const SALOMEDS::TMPFile& theStream) 
308 {
309   SALOMEDS::Locker lock; 
310   Handle(SALOMEDSImpl_AttributeTableOfReal) aTable = Handle(SALOMEDSImpl_AttributeTableOfReal)::DownCast(_impl);
311
312   istrstream aStream((char*)&theStream[0], theStream.length());
313   return aTable->RestoreFromString(aStream);
314 }
315
316 SALOMEDS::TMPFile*  SALOMEDS_AttributeTableOfReal_i::SaveToFile()
317 {
318   SALOMEDS::Locker lock; 
319   Handle(SALOMEDSImpl_AttributeTableOfReal) aTable = Handle(SALOMEDSImpl_AttributeTableOfReal)::DownCast(_impl);
320
321   ostrstream ostr;
322   string aString;
323   aTable->ConvertToString(ostr);
324
325   aString = ostr.rdbuf()->str();
326
327   char* aBuffer = (char*)CORBA::string_dup(aString.c_str());
328   int aBufferSize = strlen((char*)aBuffer);
329
330   CORBA::Octet* anOctetBuf =  (CORBA::Octet*)aBuffer;
331
332   SALOMEDS::TMPFile_var aStreamFile = new SALOMEDS::TMPFile(aBufferSize, aBufferSize, anOctetBuf, 1);
333
334   return aStreamFile._retn();
335 }