Salome HOME
Adding support for int64 field in basic api
[tools/medcoupling.git] / src / MEDLoader / MEDFileBasis.hxx
1 // Copyright (C) 2007-2021  CEA/DEN, EDF R&D
2 //
3 // This library is free software; you can redistribute it and/or
4 // modify it under the terms of the GNU Lesser General Public
5 // License as published by the Free Software Foundation; either
6 // version 2.1 of the License, or (at your option) any later version.
7 //
8 // This library is distributed in the hope that it will be useful,
9 // but WITHOUT ANY WARRANTY; without even the implied warranty of
10 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
11 // Lesser General Public License for more details.
12 //
13 // You should have received a copy of the GNU Lesser General Public
14 // License along with this library; if not, write to the Free Software
15 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
16 //
17 // See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
18 //
19 // Author : Anthony Geay (CEA/DEN)
20
21 #ifndef __MEDFILEBASIS_HXX__
22 #define __MEDFILEBASIS_HXX__
23
24 #include "InterpKernelException.hxx"
25 #include "MEDCouplingMemArray.hxx"
26 #include "MEDCouplingMemArray.txx"
27
28 #include <string>
29 #include <vector>
30
31 #include <med.h>
32
33 namespace MEDCoupling
34 {
35   class MEDFileString
36   {
37   public:
38     MEDFileString(int maxLgth);
39     ~MEDFileString();
40     void clear();
41     void set(const char *s);
42     char *getPointer() { return _content; }
43     const char *getReprForWrite() const { return _content; }
44     std::string getRepr() const;
45   private:
46     int _max_lgth;
47     char *_content;
48   };
49
50
51   class MEDFileMultiString
52   {
53   public:
54     MEDFileMultiString(int nbOfCompo, int maxLgthPerCompo);
55     ~MEDFileMultiString();
56     void set(int compoId, const char *s);
57     const char *getReprForWrite() const;
58     std::vector<std::string> getRepr() const;
59     std::string getReprPerComp(int compId) const;
60   private:
61     int _nb_of_comp;
62     int _max_lgth_per_comp;
63     char *_content;
64   };
65 }
66
67 namespace MEDCoupling
68 {
69 #ifdef MED_INT_IS_LONG
70   using DataArrayMedInt = DataArrayInt64;
71 #else
72   using DataArrayMedInt = DataArrayInt32;
73 #endif
74
75   template<class INTARRAY>
76   DataArrayMedInt * DataArrayMedInt_Copy( const INTARRAY* intArray )
77   {
78     DataArrayMedInt* medIntArray = DataArrayMedInt::New();
79     medIntArray->alloc( intArray->getNumberOfTuples(), intArray->getNumberOfComponents() );
80     medIntArray->copyStringInfoFrom( *intArray );
81     std::copy( intArray->begin(), intArray->end(), medIntArray->getPointer() );
82     return medIntArray;
83   }
84
85   template< class T1, class T2 >
86   MCAuto<T1> StaticCast( const MCAuto< T2 >& array )
87   {
88     DataArray *src = const_cast< T2* >((const T2*) array );
89     T1*        tgt = static_cast<T1*>( src );
90     if ( tgt )
91       tgt->incrRef();
92     return tgt;
93   }
94
95   template< class INTARRAY >
96   MCAuto<DataArrayMedInt> ToMedIntArray(const MCAuto<INTARRAY>& intArray )
97   {
98     if ( sizeof( med_int ) == sizeof( typename INTARRAY::Type ))
99       return StaticCast< DataArrayMedInt >( intArray );
100     return DataArrayMedInt_Copy((const INTARRAY*) intArray );
101   }
102
103   template< class INT >
104   MCAuto<DataArrayMedInt> ToMedIntArray(const typename MEDCoupling::Traits<INT>::ArrayType* intArray )
105   {
106     if ( sizeof( med_int ) == sizeof( INT ))
107     {
108       typedef typename MEDCoupling::Traits<INT>::ArrayType INTARRAY;
109       MCAuto< INTARRAY > ia = const_cast< INTARRAY* >( intArray );
110       ia->incrRef();
111       return StaticCast< DataArrayMedInt >( ia );
112     }
113     return DataArrayMedInt_Copy( intArray );
114   }
115
116   template< class INT >
117   MCAuto< typename MEDCoupling::Traits<INT>::ArrayType> FromMedIntArray(MCAuto<DataArrayMedInt>& medIntArray )
118   {
119     typedef typename MEDCoupling::Traits<INT>::ArrayType INTARRAY;
120     if ( sizeof( med_int ) == sizeof( INT ))
121       return StaticCast< INTARRAY >( medIntArray );
122
123     INTARRAY* intArray = INTARRAY::New();
124     intArray->alloc( medIntArray->getNumberOfTuples(), medIntArray->getNumberOfComponents() );
125     intArray->copyStringInfoFrom( * medIntArray.operator->() );
126     std::copy( medIntArray->begin(), medIntArray->end(), intArray->getPointer() );
127     return intArray;
128   }
129
130   template< class INT >
131   MCAuto<DataArrayMedInt> ToMedIntArray(const std::vector<INT>& intVec )
132   {
133     DataArrayMedInt* medIntArray = DataArrayMedInt::New();
134     if ( sizeof( med_int ) == sizeof( INT ))
135       {
136         medIntArray->useArray( reinterpret_cast<const med_int*>(intVec.data()), /*owner=*/false, DeallocType::CPP_DEALLOC, intVec.size(), /*nbComp=*/1 );
137       }
138     else
139       {
140         medIntArray->alloc( intVec.size(), 1 );
141         std::copy( intVec.begin(), intVec.end(), medIntArray->getPointer() );
142       }
143     return medIntArray;
144   }
145
146   template< class INT >
147   med_int ToMedInt( INT i )
148   {
149     return static_cast< med_int >( i );
150   }
151
152   template< class INT >
153   INT FromMedInt( med_int mi )
154   {
155     return static_cast< INT >( mi );
156   }
157
158 }
159
160 #endif