Salome HOME
Merge from BR_V5_DEV 16Feb09
[modules/med.git] / src / MEDCoupling / MemArray.hxx
1 //  Copyright (C) 2007-2008  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.
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 #ifndef __PARAMEDMEM_MEMARRAY_HXX__
20 #define __PARAMEDMEM_MEMARRAY_HXX__
21
22 #include "RefCountObject.hxx"
23
24 #include <string>
25 #include <vector>
26
27 namespace ParaMEDMEM
28 {
29   template<class T>
30   class MemArray
31   {
32   public:
33     MemArray():_nb_of_elem(-1),_ownership(false),_pointer(0),_dealloc(CPP_DEALLOC) { }
34     T *getPointer() const { return _pointer; }
35     MemArray<T> &operator=(const MemArray<T>& other);
36     T operator[](int id) const { return _pointer[id]; }
37     T& operator[](int id) { return _pointer[id]; }
38     void alloc(int nbOfElements);
39     void reAlloc(int newNbOfElements);
40     void useArray(void *array, bool ownership, DeallocType type, int nbOfElem);
41     void writeOnPlace(int id, T element0, const T *others, int sizeOfOthers);
42     ~MemArray() { destroy(); }
43   private:
44     void destroy();
45     static void destroyPointer(T *pt, DeallocType type);
46   private:
47     int _nb_of_elem;
48     bool _ownership;
49     T *_pointer;
50     DeallocType _dealloc;
51   };
52
53   class DataArray : public RefCountObject
54   {
55   public:
56     void setName(const char *name);
57     std::string getName() const { return _name; }
58     std::string getInfoOnComponent(int i) const { return _info_on_compo[i]; }
59     void setInfoOnComponent(int i, const char *info) { _info_on_compo[i]=info; }
60     int getNumberOfComponents() const { return _info_on_compo.size(); }
61     int getNumberOfTuples() const { return _nb_of_tuples; }
62     int getNbOfElems() const { return _info_on_compo.size()*_nb_of_tuples; }
63   protected:
64     DataArray():_nb_of_tuples(-1) { }
65   protected:
66     int _nb_of_tuples;
67     std::string _name;
68     std::vector<std::string> _info_on_compo;
69   };
70 }
71
72 #include "MemArray.txx"
73
74 namespace ParaMEDMEM
75 {
76   class DataArrayDouble : public DataArray
77   {
78   public:
79     static DataArrayDouble *New();
80     void alloc(int nbOfTuple, int nbOfCompo);
81     //!alloc or useArray should have been called before.
82     void reAlloc(int nbOfTuples);
83     double getIJ(int tupleId, int compoId) const { return _mem[tupleId*_info_on_compo.size()+compoId]; }
84     void setIJ(int tupleId, int compoId, double newVal) { _mem[tupleId*_info_on_compo.size()+compoId]=newVal; }
85     double *getPointer() const { return _mem.getPointer(); }
86     void useArray(double *array, bool ownership, DeallocType type, int nbOfTuple, int nbOfCompo);
87     void writeOnPlace(int id, double element0, const double *others, int sizeOfOthers) { _mem.writeOnPlace(id,element0,others,sizeOfOthers); }
88     //! nothing to do here because this class does not aggregate any TimeLabel instance.
89     void updateTime() { }
90   private:
91     DataArrayDouble() { }
92   private:
93     MemArray<double> _mem;
94   };
95
96   class DataArrayInt : public DataArray
97   {
98   public:
99     static DataArrayInt *New();
100     void alloc(int nbOfTuple, int nbOfCompo);
101     //!alloc or useArray should have been called before.
102     void reAlloc(int nbOfTuples);
103     int getIJ(int tupleId, int compoId) const { return _mem[tupleId*_info_on_compo.size()+compoId]; }
104     int *getPointer() const { return _mem.getPointer(); }
105     void useArray(int *array, bool ownership, DeallocType type, int nbOfTuple, int nbOfCompo);
106     void writeOnPlace(int id, int element0, const int *others, int sizeOfOthers) { _mem.writeOnPlace(id,element0,others,sizeOfOthers); }
107     //! nothing to do here because this class does not aggregate any TimeLabel instance.
108     void updateTime() { }
109   private:
110     DataArrayInt() { }
111   private:
112     MemArray<int> _mem;
113   };
114 }
115
116 #endif