Salome HOME
Merge from V6_main_20120808 08Aug12
[modules/med.git] / src / MEDMEM_I / MEDMEM_FieldTemplate_i.hxx
1 // Copyright (C) 2007-2012  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 #ifndef __MED_FIELDTEMPLATE_I_HXX__
24 #define __MED_FIELDTEMPLATE_I_HXX__
25
26 #include <SALOMEconfig.h> 
27 #include  CORBA_SERVER_HEADER(MED)
28 #include "MEDMEM_Field_i.hxx"
29 #include "MEDMEM_Field.hxx"
30 #include "SenderFactory.hxx"
31 #include "MultiCommException.hxx"
32 #include "MEDMEM_ArrayConvert.hxx"
33 #include "MEDMEM_TraitsForFields.hxx"
34 #include "utilities.h"
35
36 namespace MEDMEM
37 {
38   template < class T, class INTERLACING_TAG=FullInterlace >
39   class FIELDTEMPLATE_I : public FIELDI_TRAITS<T,INTERLACING_TAG>::InterfaceForServant,
40                           public FIELD_i,
41                           public SALOMEMultiComm
42   {
43   public:
44     FIELDTEMPLATE_I();
45     ~FIELDTEMPLATE_I();
46     FIELDTEMPLATE_I(MEDMEM::FIELD<T, INTERLACING_TAG> *f, bool ownCppPtr=false);
47     FIELDTEMPLATE_I(FIELDTEMPLATE_I & f);
48     typename FIELDI_TRAITS<T,INTERLACING_TAG>::SeqType* getValue (SALOME_MED::medModeSwitch mode ) 
49       throw (SALOME::SALOME_Exception);
50     typename FIELDI_TRAITS<T,INTERLACING_TAG>::SenderPtrType getSenderForValue(SALOME_MED::medModeSwitch mode)
51       throw (SALOME::SALOME_Exception);
52   };
53
54   //=============================================================================
55   /*!
56    * Default constructor
57    */
58   //=============================================================================
59   template < class T, class INTERLACING_TAG >
60   FIELDTEMPLATE_I<T,INTERLACING_TAG>::FIELDTEMPLATE_I(): FIELD_i()
61   {
62   }
63   //=============================================================================
64   /*!
65    * Destructor
66    */
67   //=============================================================================
68   template < class T, class INTERLACING_TAG >
69   FIELDTEMPLATE_I<T,INTERLACING_TAG>::~FIELDTEMPLATE_I()
70   {
71   }
72   //=============================================================================
73   /*!
74    * Constructor par recopie
75    */
76   //=============================================================================
77   template < class T, class INTERLACING_TAG >
78   FIELDTEMPLATE_I<T,INTERLACING_TAG>::FIELDTEMPLATE_I(FIELDTEMPLATE_I<T, INTERLACING_TAG>& fd):
79     FIELD_i(fd)
80   {
81   }
82   //=============================================================================
83   /*!
84    * Default constructor
85    */
86   //=============================================================================
87   template < class T, class INTERLACING_TAG >
88   FIELDTEMPLATE_I<T,INTERLACING_TAG>::FIELDTEMPLATE_I(MEDMEM::FIELD<T, INTERLACING_TAG> *f, bool ownCppPtr):FIELD_i(f,ownCppPtr)
89   {
90   }
91
92   //=============================================================================
93   /*!
94    * CORBA: Accessor for Field's values
95    */
96   //=============================================================================
97   template < class T, class INTERLACING_TAG >
98   typename FIELDI_TRAITS<T,INTERLACING_TAG>::SeqType *FIELDTEMPLATE_I<T,INTERLACING_TAG>::getValue( SALOME_MED::medModeSwitch mode ) 
99     throw (SALOME::SALOME_Exception)
100   {
101     if (_fieldTptr==NULL)
102       THROW_SALOME_CORBA_EXCEPTION("No associated Field", \
103                                    SALOME::INTERNAL_ERROR);
104     typename FIELDI_TRAITS<T,INTERLACING_TAG>::SeqVarType myseq = new typename FIELDI_TRAITS<T,INTERLACING_TAG>::SeqType;
105     try
106       {
107         medModeSwitch modemed=convertIdlModeToMedMode(mode);
108         //              ::FIELD<T> *ptrD=dynamic_cast< ::FIELD<T>* >(_fieldTptr);
109         // the alternative is not safe but the dynamic_cast fails using the python API
110         MEDMEM::FIELD<T, INTERLACING_TAG> *ptrD = static_cast<MEDMEM::FIELD<T, INTERLACING_TAG>* >(_fieldTptr);
111         int nbval=ptrD->getValueLength();
112         if(ptrD->getInterlacingType()==modemed)
113           {
114             const T* values =ptrD->getValue();
115             myseq->length(nbval);
116             for (int i=0; i<nbval; i++)
117               {
118                 myseq[i]=values[i];
119               }
120           }
121         else
122           {
123             T *values;
124             if  ( ptrD->getGaussPresence() ) 
125               {
126                 typename MEDMEM_ArrayInterface<T,INTERLACING_TAG,Gauss>::Array * myArray = ptrD->getArrayGauss();
127                 int size=myArray->getArraySize();
128                 values=new T[size];
129                 delete ArrayConvert(*myArray,values);
130               }
131             else 
132               {
133                 typename MEDMEM_ArrayInterface<T,INTERLACING_TAG,NoGauss>::Array * myArray = ptrD->getArrayNoGauss();
134                 int size=myArray->getArraySize();
135                 values=new T[size];
136                 delete ArrayConvert(*myArray,values);
137               }
138             for (int i=0; i<nbval; i++)
139               {
140                 myseq[i]=values[i];
141               }
142           }
143       }
144     catch (MEDEXCEPTION &ex)
145       {
146         MESSAGE("Unable to acces Field ");
147         THROW_SALOME_CORBA_EXCEPTION(ex.what(), SALOME::INTERNAL_ERROR);
148       }
149     return myseq._retn();
150   }
151   //=============================================================================
152   /*!
153    * CORBA: Accessor for Field's values
154    */
155   //=============================================================================
156   template < class T, class INTERLACING_TAG >
157   typename FIELDI_TRAITS<T,INTERLACING_TAG>::SenderPtrType FIELDTEMPLATE_I<T,INTERLACING_TAG>::getSenderForValue( SALOME_MED::medModeSwitch mode ) 
158     throw (SALOME::SALOME_Exception)
159   {
160     if (_fieldTptr==NULL)
161       THROW_SALOME_CORBA_EXCEPTION("No associated Field", \
162                                    SALOME::INTERNAL_ERROR);
163     typename FIELDI_TRAITS<T,INTERLACING_TAG>::SenderPtrType ret;
164     try
165       {
166         medModeSwitch modemed=convertIdlModeToMedMode(mode);
167         // ::FIELD<T> *ptrD=dynamic_cast< ::FIELD<T>* >(_fieldTptr);
168         // the alternative is not safe but the dynamic_cast fails using the python API
169         MEDMEM::FIELD<T, INTERLACING_TAG> *ptrD=static_cast< MEDMEM::FIELD<T, INTERLACING_TAG>* >(_fieldTptr);
170         int nbval=ptrD->getValueLength();
171         if(ptrD->getInterlacingType()==modemed)
172           {
173             const T* values =ptrD->getValue();
174             ret=SenderFactory::buildSender(*this,values,nbval);
175           }
176         else
177           {
178             T *values;
179             if  ( ptrD->getGaussPresence() ) 
180               {
181                 typename MEDMEM_ArrayInterface<T,INTERLACING_TAG,Gauss>::Array * myArray = ptrD->getArrayGauss();
182                 int size=myArray->getArraySize();
183                 values=new T[size];
184                 delete ArrayConvert(*myArray,values);
185                 
186               }
187             else 
188               {
189                 typename MEDMEM_ArrayInterface<T,INTERLACING_TAG,NoGauss>::Array * myArray = ptrD->getArrayNoGauss();
190                 int size=myArray->getArraySize();
191                 values=new T[size];
192                 delete ArrayConvert(*myArray,values);
193               }
194             ret=SenderFactory::buildSender(*this,values,nbval);
195           }
196       }
197     catch (MEDEXCEPTION &ex)
198       {
199         MESSAGE("Unable to acces Field ");
200         THROW_SALOME_CORBA_EXCEPTION(ex.what(), SALOME::INTERNAL_ERROR);
201       }
202     return ret;
203   }
204
205 }
206
207 #endif