]> SALOME platform Git repositories - modules/visu.git/blob - src/VISU_I/SALOME_GenericObjPointer.hh
Salome HOME
c54c762a017939847f6da50efcfb6bf833ec2f41
[modules/visu.git] / src / VISU_I / SALOME_GenericObjPointer.hh
1 //  Copyright (C) 2007-2008  CEA/DEN, EDF R&D, OPEN CASCADE
2 //
3 //  Copyright (C) 2003-2007  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.salome-platform.org/ or email : webmaster.salome@opencascade.com
21 //
22 //  File   : SALOME_GenericObjPtr.hh
23 //  Author : Oleg UVAROV
24 //  Module : SALOME
25 //
26 #ifndef SALOME_GenericObjPointer_HH
27 #define SALOME_GenericObjPointer_HH
28
29 #include "SALOMEconfig.h"
30 #include CORBA_SERVER_HEADER(SALOME_GenericObj)
31
32 #include <iosfwd>  // for std::basic_ostream
33
34 namespace SALOME
35 {
36   //----------------------------------------------------------------------------
37   template <class TGenericObj>
38   class GenericObjPtr
39   {
40     //! Pointer to the actual object.
41     TGenericObj* myPointer;
42
43     void
44     swap(GenericObjPtr& thePointer)
45     {
46       TGenericObj* aPointer = thePointer.myPointer;
47       thePointer.myPointer = this->myPointer;
48       this->myPointer = aPointer;
49     }
50
51     void
52     Register()
53     {
54       if(this->myPointer)
55         this->myPointer->Register();
56     }
57
58     void
59     Destroy()
60     {
61       if(this->myPointer){
62         this->myPointer->Destroy();
63         this->myPointer = NULL;
64       }
65     }
66
67   public:
68     //! Initialize smart pointer to NULL.
69     GenericObjPtr():
70       myPointer(NULL)
71     {}
72
73     //! Initialize smart pointer to given object (TSGenericObj must be complete).
74     template<class TGenObj>
75     explicit
76     GenericObjPtr(TGenObj* thePointer): 
77       myPointer(thePointer) 
78     {
79       this->Register();
80     }
81
82     /*! 
83       Initialize smart pointer with a new reference to the same object
84       referenced by given smart pointer.
85      */
86     GenericObjPtr(const GenericObjPtr& thePointer):
87       myPointer(thePointer.myPointer) 
88     {
89       this->Register();
90     }
91
92     /*! 
93       Initialize smart pointer with a new reference to the same object
94       referenced by given smart pointer.
95      */
96     template<class TGenObj>
97     GenericObjPtr(const GenericObjPtr<TGenObj>& thePointer):
98       myPointer(thePointer.get()) 
99     {
100       this->Register();
101     }
102
103     //! Destroy smart pointer and remove the reference to its object.
104     ~GenericObjPtr()
105     {
106       this->Destroy();
107     }
108
109     /*! 
110       Assign object to reference.  This removes any reference to an old
111       object.
112     */
113     template<class TGenObj>
114     GenericObjPtr&
115     operator=(TGenObj* thePointer)
116     {
117       GenericObjPtr aTmp(thePointer);
118       aTmp.swap(*this);
119       return *this;
120     }
121
122     /*! 
123       Assign object to reference.  This removes any reference to an old
124       object.
125     */
126     GenericObjPtr& 
127     operator=(const GenericObjPtr& thePointer)
128     {
129       GenericObjPtr aTmp(thePointer);
130       aTmp.swap(*this);
131       return *this;
132     }
133
134     /*! 
135       Assign object to reference.  This removes any reference to an old
136       object.
137     */
138     template<class TGenObj>
139     GenericObjPtr& 
140     operator=(const GenericObjPtr<TGenObj>& thePointer)
141     {
142       GenericObjPtr aTmp(thePointer);
143       aTmp.swap(*this);
144       return *this;
145     }
146
147     //! Get the contained pointer.
148     virtual
149     TGenericObj* 
150     get() const
151     {
152       return this->myPointer;
153     }
154
155     //! Get the contained pointer.
156     operator TGenericObj* () const
157     {
158       return this->get();
159     }
160
161     /*! 
162       Dereference the pointer and return a reference to the contained
163       object.
164     */
165     TGenericObj& 
166     operator*() const
167     {
168       return *this->get();
169     }
170
171     //! Provides normal pointer target member access using operator ->.
172     TGenericObj* operator->() const
173     {
174       return this->get();
175     }
176
177     operator bool () const
178     {
179       return this->get() != 0;
180     }
181   };
182 }
183
184 template<class T, class U> 
185 inline 
186 bool 
187 operator<(SALOME::GenericObjPtr<T> const & a, SALOME::GenericObjPtr<U> const & b)
188 {
189   return a.get() < b.get();
190 }
191
192 template<class T, class U> 
193 inline
194 bool 
195 operator==(SALOME::GenericObjPtr<T> const & a, SALOME::GenericObjPtr<U> const & b)
196 {
197   return a.get() == b.get();
198 }
199
200 template<class T, class U> 
201 inline 
202 bool 
203 operator!=(SALOME::GenericObjPtr<T> const & a, SALOME::GenericObjPtr<U> const & b)
204 {
205   return a.get() != b.get();
206 }
207
208 template<class Y> 
209 std::ostream& 
210 operator<< (std::ostream & os, SALOME::GenericObjPtr<Y> const & p)
211 {
212   os << p.get();
213   return os;
214 }
215
216
217 #endif