]> SALOME platform Git repositories - modules/geom.git/blob - src/GEOMBase/GEOM_GenericObjPtr.h
Salome HOME
SALOME::GenericObj : Destroy() -> UnRegister()
[modules/geom.git] / src / GEOMBase / GEOM_GenericObjPtr.h
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 //  File   : GEOM_GenericObjPtr.h
23 //  Author : Vadim SANDLER, Open CASCADE S.A.S. (vadim.sandler@opencascade.com)
24
25 #ifndef GEOM_GenericObjPtr_H
26 #define GEOM_GenericObjPtr_H
27
28 #include "SALOMEconfig.h"
29 #include CORBA_SERVER_HEADER(GEOM_Gen)
30
31 namespace GEOM
32 {
33   /*!
34     \class GenericObjPtr
35     \brief A smart pointer for the SALOME GenericObj interface.
36
37     This class can be used in conjunction with the references to the CORBA objects which
38     interfaces are inherited from the SALOME::GenericObj CORBA interface.
39
40     The smart pointer class automatically invokes Register() / UnRegister() functions of th
41     interface in order to prevent memory leaks and other such problems caused by improper
42     usage of the CORBA references.
43
44     Smart pointers can be easily copied, stored within class as data members, passed to the
45     functions requiring native CORBA reference as parameters, etc.
46
47     Usage:
48     - If you want to assign the smart pointer to the CORBA _var type variable, use copy()
49     function to make a copy of the stored CORBA object. Otherwise you might cause Segmentation
50     Fault error.
51     - To pass the smart pointer to the function that requires CORBA _ptr type parameter,
52     use get() function.
53     - If you want to take an ownership on the CORBA object, use take() function.
54     In case of SALOME Generic object this is useful when some function returns newly created
55     object that should be removed by the caller as soon as the object is no more required.
56     For example, function GetSubShape() of the GEOM_IShapesOperation interface always creates
57     new servant object and returns new object reference to it. If the object is not published
58     in the study, it has to be destroyed and the coresponding servant should be deleted.
59     
60     Examples:
61     \code
62     typedef GEOM::GenericObjPtr<MyInterface> MyIPtr;
63     void MyInterface_ptr foo();
64     void MyInterface_ptr bar( MyInterface_ptr p );
65
66     MyIPtr v1;                       // create empty (nil) pointer
67     MyIPtr v2 = foo();               // get some CORBA reference and store it within the smart pointer
68     v1 = v2;                         // copy smart pointer (reference counter is incremented)
69     v2 = bar( v1.get() );            // pass smart pointer to the function
70     MyInterface_var var = v2.copy(); // initialize _var variable with the smart pointer contents
71     v1.take( foo() );                // take ownership on the newly created object
72     \endcode
73    */
74   
75   template <typename TInterface> class GenericObjPtr
76   {
77     typedef typename TInterface::_var_type TInterfaceVar;
78     typedef typename TInterface::_ptr_type TInterfacePtr;
79     
80   private:
81     TInterfaceVar myObject;
82
83   private:
84     //! Increment counter for the object.
85     void Register()
86     {
87       if ( !CORBA::is_nil( this->myObject ) )
88         this->myObject->Register();
89     }
90
91     //! Decrement counter for the object.
92     void UnRegister()
93     {
94       if ( !CORBA::is_nil( this->myObject ) ) {
95         this->myObject->UnRegister();
96         this->myObject = TInterface::_nil();
97       }
98     }
99       
100   public:
101     //! Initialize pointer to nil generic object reference.
102     GenericObjPtr()
103     {}
104     
105     //! Initialize pointer to the given generic object reference.
106     GenericObjPtr( TInterfacePtr theObject )
107     {
108       this->myObject = TInterface::_duplicate( theObject );
109       this->Register();
110     }
111     
112     //! Initialize pointer with a new reference to the same object referenced by given pointer.
113     GenericObjPtr( const GenericObjPtr& thePointer )
114     {
115       this->myObject = thePointer.myObject;
116       this->Register();
117     }
118     
119     //! Destroy pointer and remove the reference to the object.
120     ~GenericObjPtr()
121     {
122       this->UnRegister();
123     }
124     
125     //! Assign object to reference and remove reference to an old object.
126     GenericObjPtr& operator=( TInterfacePtr theObject )
127     {
128       this->UnRegister();
129       this->myObject = TInterface::_duplicate( theObject );
130       this->Register();
131       return *this;
132     }
133
134     //! Assign object to reference and remove reference to an old object.
135     GenericObjPtr& operator=( const GenericObjPtr& thePointer )
136     {
137       this->UnRegister();
138       this->myObject = thePointer.myObject;
139       this->Register();
140       return *this;
141     }
142
143     static bool isSame( TInterfacePtr theLeft, TInterfacePtr theRight )
144     {
145       return theLeft->_is_equivalent( theRight );
146     }
147
148     //! Check equivalence
149     bool operator==( TInterfacePtr theObject )
150     {
151       return isSame( this->myObject, theObject );
152     }
153
154     //! Check equivalence
155     bool operator==( const GenericObjPtr& thePointer )
156     {
157       return isSame( this->myObject, thePointer.get() );;
158     }
159
160     //! Check difference
161     bool operator!=( TInterfacePtr theObject )
162     {
163       return !isSame( this->myObject, theObject );
164     }
165
166     //! Check difference
167     bool operator!=( const GenericObjPtr& thePointer )
168     {
169       return !isSame( this->myObject, thePointer.get() );;
170     }
171
172     //! Provides normal pointer target member access using operator ->.
173     TInterfacePtr operator->() const
174     {
175       return this->get();
176     }
177
178     //! Check validity of the pointer.
179     operator bool() const
180     {
181        return !this->isNull();
182     }
183
184     //! Initialize pointer to the given generic object reference and take ownership on it.
185     void take( TInterfacePtr theObject )
186     {
187       this->UnRegister();
188       this->myObject = TInterface::_duplicate( theObject );
189     }
190
191     //! Get the contained object.
192     TInterfacePtr get() const
193     {
194       return this->myObject;
195     }
196
197     //! Make the copy of the contained object and return it (caller becomes owner of the CORBA reference).
198     TInterfacePtr copy() const
199     {
200       return TInterface::_duplicate( this->myObject );
201     }
202     
203     //! Check if pointer is null.
204     bool isNull() const
205     {
206       return CORBA::is_nil( this->myObject );
207     }
208
209     //! Nullify pointer.
210     void nullify()
211     {
212       this->UnRegister();
213     }
214   };
215   
216   typedef GenericObjPtr<GEOM::GEOM_Object>               GeomObjPtr;
217   typedef GenericObjPtr<GEOM::GEOM_IBasicOperations>     BasicOpPtr;
218   typedef GenericObjPtr<GEOM::GEOM_ITransformOperations> TransformOpPtr;
219   typedef GenericObjPtr<GEOM::GEOM_I3DPrimOperations>    I3DPrimOpPtr;
220   typedef GenericObjPtr<GEOM::GEOM_IShapesOperations>    ShapesOpPtr;
221   typedef GenericObjPtr<GEOM::GEOM_IBlocksOperations>    BlocksOpPtr;
222   typedef GenericObjPtr<GEOM::GEOM_IBooleanOperations>   BooleanOpPtr;
223   typedef GenericObjPtr<GEOM::GEOM_ICurvesOperations>    CurvesOpPtr;
224   typedef GenericObjPtr<GEOM::GEOM_ILocalOperations>     LocalOpPtr;
225   typedef GenericObjPtr<GEOM::GEOM_IHealingOperations>   HealingOpPtr;
226   typedef GenericObjPtr<GEOM::GEOM_IInsertOperations>    InsertOpPtr;
227   typedef GenericObjPtr<GEOM::GEOM_IMeasureOperations>   MeasureOpPtr;
228   typedef GenericObjPtr<GEOM::GEOM_IGroupOperations>     GroupOpPtr;
229   typedef GenericObjPtr<GEOM::GEOM_IAdvancedOperations>  AdvancedOpPtr;
230
231   template<> bool GenericObjPtr<GEOM::GEOM_Object>::isSame( GEOM::GEOM_Object_ptr theLeft, GEOM::GEOM_Object_ptr theRight );
232 }
233
234 #endif // GEOM_GenericObjPtr_H