]> SALOME platform Git repositories - modules/visu.git/blob - src/VISU_I/SALOME_GenericObjPointer.hh
Salome HOME
Join modifications from BR_Dev_For_4_0 tag V4_1_1.
[modules/visu.git] / src / VISU_I / SALOME_GenericObjPointer.hh
1 //  Copyright (C) 2003  OPEN CASCADE, EADS/CCR, LIP6, CEA/DEN,
2 //  CEDRAT, EDF R&D, LEG, PRINCIPIA R&D, BUREAU VERITAS 
3 // 
4 //  This library is free software; you can redistribute it and/or 
5 //  modify it under the terms of the GNU Lesser General Public 
6 //  License as published by the Free Software Foundation; either 
7 //  version 2.1 of the License. 
8 // 
9 //  This library is distributed in the hope that it will be useful, 
10 //  but WITHOUT ANY WARRANTY; without even the implied warranty of 
11 //  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU 
12 //  Lesser General Public License for more details. 
13 // 
14 //  You should have received a copy of the GNU Lesser General Public 
15 //  License along with this library; if not, write to the Free Software 
16 //  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA 
17 // 
18 // See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
19 //
20 //
21 //  File   : SALOME_GenericObjPtr.hh
22 //  Author : Oleg UVAROV
23 //  Module : SALOME
24
25 #ifndef SALOME_GenericObjPointer_HH
26 #define SALOME_GenericObjPointer_HH
27
28 #include "SALOMEconfig.h"
29 #include CORBA_SERVER_HEADER(SALOME_GenericObj)
30
31 #include <iosfwd>  // for std::basic_ostream
32
33 namespace SALOME
34 {
35   //----------------------------------------------------------------------------
36   template <class TGenericObj>
37   class GenericObjPtr
38   {
39     //! Pointer to the actual object.
40     TGenericObj* myPointer;
41
42     void
43     swap(GenericObjPtr& thePointer)
44     {
45       TGenericObj* aPointer = thePointer.myPointer;
46       thePointer.myPointer = this->myPointer;
47       this->myPointer = aPointer;
48     }
49
50     void
51     Register()
52     {
53       if(this->myPointer)
54         this->myPointer->Register();
55     }
56
57     void
58     Destroy()
59     {
60       if(this->myPointer){
61         this->myPointer->Destroy();
62         this->myPointer = NULL;
63       }
64     }
65
66   public:
67     //! Initialize smart pointer to NULL.
68     GenericObjPtr():
69       myPointer(NULL)
70     {}
71
72     //! Initialize smart pointer to given object (TSGenericObj must be complete).
73     template<class TGenObj>
74     explicit
75     GenericObjPtr(TGenObj* thePointer): 
76       myPointer(thePointer) 
77     {
78       this->Register();
79     }
80
81     /*! 
82       Initialize smart pointer with a new reference to the same object
83       referenced by given smart pointer.
84      */
85     GenericObjPtr(const GenericObjPtr& thePointer):
86       myPointer(thePointer.myPointer) 
87     {
88       this->Register();
89     }
90
91     /*! 
92       Initialize smart pointer with a new reference to the same object
93       referenced by given smart pointer.
94      */
95     template<class TGenObj>
96     GenericObjPtr(const GenericObjPtr<TGenObj>& thePointer):
97       myPointer(thePointer.get()) 
98     {
99       this->Register();
100     }
101
102     //! Destroy smart pointer and remove the reference to its object.
103     ~GenericObjPtr()
104     {
105       this->Destroy();
106     }
107
108     /*! 
109       Assign object to reference.  This removes any reference to an old
110       object.
111     */
112     template<class TGenObj>
113     GenericObjPtr&
114     operator=(TGenObj* thePointer)
115     {
116       GenericObjPtr aTmp(thePointer);
117       aTmp.swap(*this);
118       return *this;
119     }
120
121     /*! 
122       Assign object to reference.  This removes any reference to an old
123       object.
124     */
125     GenericObjPtr& 
126     operator=(const GenericObjPtr& thePointer)
127     {
128       GenericObjPtr aTmp(thePointer);
129       aTmp.swap(*this);
130       return *this;
131     }
132
133     /*! 
134       Assign object to reference.  This removes any reference to an old
135       object.
136     */
137     template<class TGenObj>
138     GenericObjPtr& 
139     operator=(const GenericObjPtr<TGenObj>& thePointer)
140     {
141       GenericObjPtr aTmp(thePointer);
142       aTmp.swap(*this);
143       return *this;
144     }
145
146     //! Get the contained pointer.
147     virtual
148     TGenericObj* 
149     get() const
150     {
151       return this->myPointer;
152     }
153
154     //! Get the contained pointer.
155     operator TGenericObj* () const
156     {
157       return this->get();
158     }
159
160     /*! 
161       Dereference the pointer and return a reference to the contained
162       object.
163     */
164     TGenericObj& 
165     operator*() const
166     {
167       return *this->get();
168     }
169
170     //! Provides normal pointer target member access using operator ->.
171     TGenericObj* operator->() const
172     {
173       return this->get();
174     }
175
176     operator bool () const
177     {
178       return this->get() != 0;
179     }
180   };
181 }
182
183 template<class T, class U> 
184 inline 
185 bool 
186 operator<(SALOME::GenericObjPtr<T> const & a, SALOME::GenericObjPtr<U> const & b)
187 {
188   return a.get() < b.get();
189 }
190
191 template<class T, class U> 
192 inline
193 bool 
194 operator==(SALOME::GenericObjPtr<T> const & a, SALOME::GenericObjPtr<U> const & b)
195 {
196   return a.get() == b.get();
197 }
198
199 template<class T, class U> 
200 inline 
201 bool 
202 operator!=(SALOME::GenericObjPtr<T> const & a, SALOME::GenericObjPtr<U> const & b)
203 {
204   return a.get() != b.get();
205 }
206
207 template<class Y> 
208 std::ostream& 
209 operator<< (std::ostream & os, SALOME::GenericObjPtr<Y> const & p)
210 {
211   os << p.get();
212   return os;
213 }
214
215
216 #endif