Salome HOME
ea984a760b850b371262754b7321ae22b3a388df
[tools/medcoupling.git] / src / INTERP_KERNEL / Bases / InterpKernelAutoPtr.hxx
1 // Copyright (C) 2007-2016  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 __INTERPKERNELAUTOPTR_HXX__
22 #define __INTERPKERNELAUTOPTR_HXX__
23
24 namespace INTERP_KERNEL
25 {
26   template<class T>
27   class AutoPtr
28   {
29   public:
30     AutoPtr(T *ptr=0):_ptr(ptr) {  }
31     ~AutoPtr() { destroyPtr(); }
32     bool isNull() const { return _ptr==0; }
33     bool isNotNull() const { return !isNull(); }
34     AutoPtr &operator=(T *ptr) { if(_ptr!=ptr) { destroyPtr(); _ptr=ptr; } return *this; }
35     T *operator->() { return _ptr ; }
36     const T *operator->() const { return _ptr; }
37     T& operator*() { return *_ptr; }
38     const T& operator*() const { return *_ptr; }
39     operator T *() { return _ptr; }
40     operator const T *() const { return _ptr; }
41   private:
42     void destroyPtr() { delete [] _ptr; }
43   private:
44     T *_ptr;
45   };
46
47   template<class T>
48   class AutoCppPtr
49   {
50   public:
51     AutoCppPtr(T *ptr=0):_ptr(ptr) {  }
52     ~AutoCppPtr() { destroyPtr(); }
53     bool isNull() const { return _ptr==0; }
54     bool isNotNull() const { return !isNull(); }
55     AutoCppPtr &operator=(T *ptr) { if(_ptr!=ptr) { destroyPtr(); _ptr=ptr; } return *this; }
56     T *operator->() { return _ptr ; }
57     const T *operator->() const { return _ptr; }
58     T& operator*() { return *_ptr; }
59     const T& operator*() const { return *_ptr; }
60     operator T *() { return _ptr; }
61     operator const T *() const { return _ptr; }
62   private:
63     void destroyPtr() { delete _ptr; }
64   private:
65     T *_ptr;
66   };
67
68   template<class T>
69   class AutoCPtr
70   {
71   public:
72     AutoCPtr(T *ptr=0):_ptr(ptr) {  }
73     ~AutoCPtr() { destroyPtr(); }
74     bool isNull() const { return _ptr==0; }
75     bool isNotNull() const { return !isNull(); }
76     AutoCPtr &operator=(T *ptr) { if(_ptr!=ptr) { destroyPtr(); _ptr=ptr; } return *this; }
77     T *operator->() { return _ptr ; }
78     const T *operator->() const { return _ptr; }
79     T& operator*() { return *_ptr; }
80     const T& operator*() const { return *_ptr; }
81     operator T *() { return _ptr; }
82     operator const T *() const { return _ptr; }
83   private:
84     void destroyPtr() { free(_ptr); }
85   private:
86     T *_ptr;
87   };
88 }
89
90 #endif