Salome HOME
mergefrom branch BR_V511_PR tag mergeto_trunk_03feb09
[modules/yacs.git] / src / engine / SharedPtr.hxx
1 //  Copyright (C) 2006-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 __SHAREDPTR_HXX__
20 #define __SHAREDPTR_HXX__
21
22 /*!
23  * \brief: Allow to manage memory of instances of T.
24  * The only constraint on T is to have method incrRef and DecrRef.
25  * Typically T inherits from YACS::ENGINE::RefCounter.
26  */
27 template<class T>
28 class SharedPtr
29 {
30 private:
31   T *_ptr;
32 public:
33   SharedPtr(T *ptr):_ptr(ptr) { }
34   SharedPtr(const SharedPtr<T>& other):_ptr(other._ptr) { _ptr->incrRef(); }
35   SharedPtr<T> &operator=(const SharedPtr<T>& other);
36   T &operator*() { return *_ptr; }
37   const T &operator*() const { return *_ptr; }
38   T *operator->() { return _ptr; }
39   const T *operator->() const { return _ptr; }
40   ~SharedPtr() { _ptr->decrRef(); }
41   operator T *() { return _ptr; }
42   operator const T *() const { return _ptr; }
43   operator T &() { return *_ptr; }
44   operator const T &() const { return *_ptr; }
45   SharedPtr<T> operator[](int i) const;
46   SharedPtr<T> operator[](const char *key) const;
47 };
48
49 template<class T>
50 SharedPtr<T> &SharedPtr<T>::operator=(const SharedPtr<T>& other)
51 {
52   _ptr->decrRef();
53   _ptr=other._ptr;
54   _ptr->incrRef();
55 }
56
57 template<class T>
58 SharedPtr<T> SharedPtr<T>::operator[](int i) const
59 {
60   return (*_ptr)[i];
61 }
62
63 template<class T>
64 SharedPtr<T> SharedPtr<T>::operator[](const char *key) const
65 {
66   return (*_ptr)[key];
67 }
68
69 #endif