Salome HOME
updated copyright message
[modules/yacs.git] / src / evalyfx / YACSEvalAutoPtr.hxx
1 // Copyright (C) 2012-2023  CEA, EDF
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 (EDF R&D)
20
21 #ifndef __YACSEVALYFXAUTOPTR_HXX__
22 #define __YACSEVALYFXAUTOPTR_HXX__
23
24 namespace YACS
25 {
26   template<class T>
27   class AutoPtr
28   {
29   public:
30     AutoPtr(T *ptr=0):_ptr(ptr) {  }
31     ~AutoPtr() { destroyPtr(); }
32     AutoPtr &operator=(T *ptr) { if(_ptr!=ptr) { destroyPtr(); _ptr=ptr; } return *this; }
33     T *operator->() { return _ptr ; }
34     const T *operator->() const { return _ptr; }
35     T& operator*() { return *_ptr; }
36     const T& operator*() const { return *_ptr; }
37     operator T *() { return _ptr; }
38     operator const T *() const { return _ptr; }
39   private:
40     void destroyPtr() { delete [] _ptr; }
41   private:
42     T *_ptr;
43   };
44
45   template<class T>
46   class AutoCppPtr
47   {
48   public:
49     AutoCppPtr(T *ptr=0):_ptr(ptr) {  }
50     ~AutoCppPtr() { destroyPtr(); }
51     AutoCppPtr &operator=(T *ptr) { if(_ptr!=ptr) { destroyPtr(); _ptr=ptr; } return *this; }
52     T *operator->() { return _ptr ; }
53     const T *operator->() const { return _ptr; }
54     T& operator*() { return *_ptr; }
55     const T& operator*() const { return *_ptr; }
56     operator T *() { return _ptr; }
57     operator const T *() const { return _ptr; }
58     T *dettach() { T *ret(_ptr); _ptr=0; return ret; }
59   private:
60     void destroyPtr() { delete _ptr; }
61   private:
62     T *_ptr;
63   };
64
65   template<class T>
66   class AutoCPtr
67   {
68   public:
69     AutoCPtr(T *ptr=0):_ptr(ptr) {  }
70     ~AutoCPtr() { destroyPtr(); }
71     AutoCPtr &operator=(T *ptr) { if(_ptr!=ptr) { destroyPtr(); _ptr=ptr; } return *this; }
72     T *operator->() { return _ptr ; }
73     const T *operator->() const { return _ptr; }
74     T& operator*() { return *_ptr; }
75     const T& operator*() const { return *_ptr; }
76     operator T *() { return _ptr; }
77     operator const T *() const { return _ptr; }
78   private:
79     void destroyPtr() { free(_ptr); }
80   private:
81     T *_ptr;
82   };
83 }
84
85 #endif