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