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