Salome HOME
32888e774adc3c88d62228c83788bbf27b2938a7
[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
70   class DataArrayMedInt : public DataArrayDiscreteSigned< med_int >
71   {
72     friend class DataArrayDiscrete<med_int>;
73   public:
74     template<class INTARRAY>
75     static DataArrayMedInt *Copy( const INTARRAY* array );
76     static DataArrayMedInt *New() { return new DataArrayMedInt(); }
77     std::string getClassName() const override { return std::string("DataArrayMedInt"); }
78     DataArrayMedInt *deepCopy() const { return new DataArrayMedInt(*this); }
79     //DataArrayMedInt *buildNewEmptyInstance() const { return new DataArrayMedInt(); }//ko
80     DataArray *buildNewEmptyInstance() const { if ( sizeof(med_int)==sizeof(int)) return DataArrayInt32::New(); return DataArrayInt64::New(); }
81   public:
82     DataArrayMedInt *copySorted(bool asc=true) const override { MCAuto<DataArrayMedInt> ret(this->deepCopy()); ret->sort(); return ret.retn(); }
83     DataArray *selectByTupleId(const mcIdType *new2OldBg, const mcIdType *new2OldEnd) const { return this->mySelectByTupleId(new2OldBg,new2OldEnd); }
84     DataArray *selectByTupleId(const DataArrayIdType& di) const { return this->mySelectByTupleId(di); }
85     DataArray *selectByTupleIdSafe(const mcIdType *new2OldBg, const mcIdType *new2OldEnd) const { return this->mySelectByTupleIdSafe(new2OldBg,new2OldEnd); }
86     DataArray *keepSelectedComponents(const std::vector<std::size_t>& compoIds) const { return this->myKeepSelectedComponents(compoIds); }
87     DataArray *selectByTupleIdSafeSlice(mcIdType bg, mcIdType end2, mcIdType step) const { return this->mySelectByTupleIdSafeSlice(bg,end2,step); }
88     DataArray *selectByTupleRanges(const std::vector<std::pair<mcIdType,mcIdType> >& ranges) const { return this->mySelectByTupleRanges(ranges); }
89   private:
90     ~DataArrayMedInt() { }
91     DataArrayMedInt() { }
92   };
93
94   template< class T1, class T2 >
95   MCAuto<T1> StaticCast( const MCAuto< T2 >& array )
96   {
97     DataArray *src = const_cast< T2* >((const T2*) array );
98     T1*        tgt = static_cast<T1*>( src );
99     if ( tgt )
100       tgt->incrRef();
101     return tgt;
102   }
103
104   template< class INTARRAY >
105   MCAuto<DataArrayMedInt> ToMedIntArray(const MCAuto<INTARRAY>& intArray )
106   {
107     if ( sizeof( med_int ) == sizeof( typename INTARRAY::Type ))
108       return StaticCast< DataArrayMedInt >( intArray );
109     return DataArrayMedInt::Copy((const INTARRAY*) intArray );
110   }
111
112   template< class INT >
113   MCAuto<DataArrayMedInt> ToMedIntArray(const typename MEDCoupling::Traits<INT>::ArrayType* intArray )
114   {
115     if ( sizeof( med_int ) == sizeof( INT ))
116     {
117       typedef typename MEDCoupling::Traits<INT>::ArrayType INTARRAY;
118       MCAuto< INTARRAY > ia = const_cast< INTARRAY* >( intArray );
119       ia->incrRef();
120       return StaticCast< DataArrayMedInt >( ia );
121     }
122     return DataArrayMedInt::Copy( intArray );
123   }
124
125   template< class INT >
126   MCAuto< typename MEDCoupling::Traits<INT>::ArrayType> FromMedIntArray(MCAuto<DataArrayMedInt>& medIntArray )
127   {
128     typedef typename MEDCoupling::Traits<INT>::ArrayType INTARRAY;
129     if ( sizeof( med_int ) == sizeof( INT ))
130       return StaticCast< INTARRAY >( medIntArray );
131
132     INTARRAY* intArray = INTARRAY::New();
133     intArray->alloc( medIntArray->getNumberOfTuples(), medIntArray->getNumberOfComponents() );
134     intArray->copyStringInfoFrom( * medIntArray.operator->() );
135     std::copy( medIntArray->begin(), medIntArray->end(), intArray->getPointer() );
136     return intArray;
137   }
138
139   template< class INT >
140   MCAuto<DataArrayMedInt> ToMedIntArray(const std::vector<INT>& intVec )
141   {
142     DataArrayMedInt* medIntArray = DataArrayMedInt::New();
143     if ( sizeof( med_int ) == sizeof( INT ))
144       {
145         medIntArray->useArray( reinterpret_cast<const med_int*>(intVec.data()), /*owner=*/false, DeallocType::CPP_DEALLOC, intVec.size(), /*nbComp=*/1 );
146       }
147     else
148       {
149         medIntArray->alloc( intVec.size(), 1 );
150         std::copy( intVec.begin(), intVec.end(), medIntArray->getPointer() );
151       }
152     return medIntArray;
153   }
154
155   template< class INT >
156   med_int ToMedInt( INT i )
157   {
158     return static_cast< med_int >( i );
159   }
160
161   template< class INT >
162   INT FromMedInt( med_int mi )
163   {
164     return static_cast< INT >( mi );
165   }
166
167   template<class INTARRAY>
168   DataArrayMedInt * DataArrayMedInt::Copy( const INTARRAY* intArray )
169   {
170     DataArrayMedInt* medIntArray = DataArrayMedInt::New();
171     medIntArray->alloc( intArray->getNumberOfTuples(), intArray->getNumberOfComponents() );
172     medIntArray->copyStringInfoFrom( *intArray );
173     std::copy( intArray->begin(), intArray->end(), medIntArray->getPointer() );
174     return medIntArray;
175   }
176 }
177
178 #endif