Salome HOME
update with the version in the OCC branch OCC_development_generic_2006.
[modules/med.git] / src / MEDWrapper / Base / MED_SharedPtr.hxx
1 //  
2 //
3 //  Copyright (C) 2003  OPEN CASCADE, EADS/CCR, LIP6, CEA/DEN,
4 //  CEDRAT, EDF R&D, LEG, PRINCIPIA R&D, BUREAU VERITAS 
5 // 
6 //  This library is free software; you can redistribute it and/or 
7 //  modify it under the terms of the GNU Lesser General Public 
8 //  License as published by the Free Software Foundation; either 
9 //  version 2.1 of the License. 
10 // 
11 //  This library is distributed in the hope that it will be useful, 
12 //  but WITHOUT ANY WARRANTY; without even the implied warranty of 
13 //  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU 
14 //  Lesser General Public License for more details. 
15 // 
16 //  You should have received a copy of the GNU Lesser General Public 
17 //  License along with this library; if not, write to the Free Software 
18 //  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA 
19 // 
20 //  See http://www.opencascade.org/SALOME/ or email : webmaster.salome@opencascade.org 
21 //
22 //
23 //
24 //  File   : 
25 //  Author : 
26 //  Module : 
27 //  $Header$
28
29 #ifndef MED_SharedPtr_HeaderFile
30 #define MED_SharedPtr_HeaderFile
31
32 #include <boost/shared_ptr.hpp>
33
34 namespace MED
35 {
36
37   //! To extend the boost::shared_ptr to support such features automatic dynamic cast
38   /*!
39     All entities of the MEDWrapper package are handled as pointer.
40     This class was introduced to provide correct and flexible memory management 
41     for all of the MEDWrapper objects.
42   */
43   template<class T> class SharedPtr: public boost::shared_ptr<T>
44   {
45   public:
46     //! Default constructor
47     SharedPtr() {}
48
49     //! Construct the class by any type of a pointer
50     template<class Y>
51     explicit SharedPtr(Y * p): 
52       boost::shared_ptr<T>(p) 
53     {}
54
55     //! Construct the class by any specialisation of the class
56     template<class Y>
57     SharedPtr(SharedPtr<Y> const & r):
58       boost::shared_ptr<T>(r,boost::detail::dynamic_cast_tag())
59     {}
60
61     //! Copy-constructor
62     template<class Y>
63     SharedPtr& 
64     operator=(SharedPtr<Y> const & r)
65     {
66       boost::shared_ptr<T>(r,boost::detail::dynamic_cast_tag()).swap(*this);
67       return *this;
68     }
69
70     //! Introduce a flexible way to reset the wrapped pointer
71     template<class Y> 
72     SharedPtr& 
73     operator()(Y * p) // Y must be complete
74     {
75       return operator=<Y>(SharedPtr<Y>(p));
76     }
77
78     //! Introduce a flexible way to reset the wrapped pointer
79     template<class Y> 
80     SharedPtr& 
81     operator()(SharedPtr<Y> const & r) // Y must be complete
82     {
83       return operator=<Y>(SharedPtr<Y>(r));
84     }
85
86     //! To provide a flexible way to use reference to the wrapped pointer (const version)
87     operator const T& () const 
88     { 
89       return *(this->get());
90     }
91
92     //! To provide a flexible way to use reference to the wrapped pointer
93     operator T& () 
94     { 
95       return *(this->get());
96     }
97   };
98
99 }
100
101
102 #endif