Salome HOME
adding a new test for makeMesh method.
[modules/med.git] / src / MEDCoupling / MemArray.txx
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_TXX__
20 #define __PARAMEDMEM_MEMARRAY_TXX__
21
22 #include "MemArray.hxx"
23 #include "NormalizedUnstructuredMesh.hxx"
24 #include "InterpKernelException.hxx"
25
26 #include <sstream>
27 #include <algorithm>
28
29 namespace ParaMEDMEM
30 {
31   template<class T>
32   void MemArray<T>::useArray(void *array, bool ownership, DeallocType type, int nbOfElem)
33   {
34     _nb_of_elem=nbOfElem;
35     destroy();
36     _pointer=(T *)array;
37     _ownership=ownership;
38     _dealloc=type;
39   }
40
41   template<class T>
42   void MemArray<T>::writeOnPlace(int id, T element0, const T *others, int sizeOfOthers)
43   {
44     if(id+sizeOfOthers>=_nb_of_elem)
45       reAlloc(2*_nb_of_elem+sizeOfOthers+1);
46     _pointer[id]=element0;
47     memcpy(_pointer+id+1,others,sizeOfOthers*sizeof(T));
48   }
49
50   template<class T>
51   void MemArray<T>::alloc(int nbOfElements)
52   {
53     destroy();
54     _nb_of_elem=nbOfElements;
55     _pointer=new T[_nb_of_elem];
56     _ownership=true;
57     _dealloc=CPP_DEALLOC;
58   }
59   
60   template<class T>
61   void MemArray<T>::reAlloc(int newNbOfElements)
62   {
63     T *pointer=new T[newNbOfElements];
64     memcpy(pointer,_pointer,std::min<int>(_nb_of_elem,newNbOfElements)*sizeof(int));
65     destroyPointer(_pointer,_dealloc);
66     _pointer=pointer;
67     _nb_of_elem=newNbOfElements;
68     _ownership=true;
69     _dealloc=CPP_DEALLOC;
70   }
71
72   template<class T>
73   void MemArray<T>::destroyPointer(T *pt, DeallocType type)
74   {
75     switch(type)
76       {
77       case CPP_DEALLOC:
78         {
79           delete [] pt;
80           return ;
81         }
82       case C_DEALLOC:
83         {
84           free(pt);
85           return ;
86         }
87       default:
88         std::stringstream stream;
89         stream << "Invalid deallocation requested for pointer " << pt;
90         throw INTERP_KERNEL::Exception(stream.str().c_str());
91       }
92   }
93
94   template<class T>
95   void MemArray<T>::destroy()
96   {
97     if(_ownership)
98       destroyPointer(_pointer,_dealloc);
99     _pointer=0;
100     _ownership=false;
101   }
102   
103   template<class T>
104   MemArray<T> &MemArray<T>::operator=(const MemArray<T>& other)
105   {
106     alloc(other._nb_of_elem);
107     memcpy(_pointer,other._pointer,_nb_of_elem*sizeof(T));
108     return *this;
109   }
110 }
111
112 #endif