1 // Copyright (C) 2007-2010 CEA/DEN, EDF R&D, OPEN CASCADE
3 // Copyright (C) 2003-2007 OPEN CASCADE, EADS/CCR, LIP6, CEA/DEN,
4 // CEDRAT, EDF R&D, LEG, PRINCIPIA R&D, BUREAU VERITAS
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.
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.
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
20 // See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
22 // File : GEOM_GenericObjPtr.h
23 // Author : Vadim SANDLER, Open CASCADE S.A.S. (vadim.sandler@opencascade.com)
25 #ifndef GEOM_GenericObjPtr_H
26 #define GEOM_GenericObjPtr_H
28 #include "SALOMEconfig.h"
29 #include CORBA_SERVER_HEADER(GEOM_Gen)
35 \brief A smart pointer for the SALOME GenericObj interface.
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.
40 The smart pointer class automatically invokes Register() / Destroy() functions of th
41 interface in order to prevent memory leaks and other such problems caused by improper
42 usage of the CORBA references.
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.
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
51 - To pass the smart pointer to the function that requires CORBA _ptr type parameter,
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.
62 typedef GEOM::GenericObjPtr<MyInterface> MyIPtr;
63 void MyInterface_ptr foo();
64 void MyInterface_ptr bar( MyInterface_ptr p );
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
75 template <typename TInterface> class GenericObjPtr
77 typedef typename TInterface::_var_type TInterfaceVar;
78 typedef typename TInterface::_ptr_type TInterfacePtr;
81 TInterfaceVar myObject;
84 //! Increment counter for the object.
87 if ( !CORBA::is_nil( this->myObject ) )
88 this->myObject->Register();
91 //! Decrement counter for the object.
94 if ( !CORBA::is_nil( this->myObject ) ) {
95 this->myObject->Destroy();
96 this->myObject = TInterface::_nil();
101 //! Initialize pointer to nil generic object reference.
105 //! Initialize pointer to the given generic object reference.
106 GenericObjPtr( TInterfacePtr theObject )
108 this->myObject = TInterface::_duplicate( theObject );
112 //! Initialize pointer with a new reference to the same object referenced by given pointer.
113 GenericObjPtr( const GenericObjPtr& thePointer )
115 this->myObject = thePointer.myObject;
119 //! Destroy pointer and remove the reference to the object.
125 //! Assign object to reference and remove reference to an old object.
126 GenericObjPtr& operator=( TInterfacePtr theObject )
129 this->myObject = TInterface::_duplicate( theObject );
134 //! Assign object to reference and remove reference to an old object.
135 GenericObjPtr& operator=( const GenericObjPtr& thePointer )
138 this->myObject = thePointer.myObject;
143 static bool isSame( TInterfacePtr theLeft, TInterfacePtr theRight )
145 return theLeft->_is_equivalent( theRight );
148 //! Check equivalence
149 bool operator==( TInterfacePtr theObject )
151 return isSame( this->myObject, theObject );
154 //! Check equivalence
155 bool operator==( const GenericObjPtr& thePointer )
157 return isSame( this->myObject, thePointer.get() );;
161 bool operator!=( TInterfacePtr theObject )
163 return !isSame( this->myObject, theObject );
167 bool operator!=( const GenericObjPtr& thePointer )
169 return !isSame( this->myObject, thePointer.get() );;
172 //! Provides normal pointer target member access using operator ->.
173 TInterfacePtr operator->() const
178 //! Check validity of the pointer.
179 operator bool() const
181 return !this->isNull();
184 //! Initialize pointer to the given generic object reference and take ownership on it.
185 void take( TInterfacePtr theObject )
188 this->myObject = TInterface::_duplicate( theObject );
191 //! Get the contained object.
192 TInterfacePtr get() const
194 return this->myObject;
197 //! Make the copy of the contained object and return it (caller becomes owner of the CORBA reference).
198 TInterfacePtr copy() const
200 return TInterface::_duplicate( this->myObject );
203 //! Check if pointer is null.
206 return CORBA::is_nil( this->myObject );
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;
231 template<> bool GenericObjPtr<GEOM::GEOM_Object>::isSame( GEOM::GEOM_Object_ptr theLeft, GEOM::GEOM_Object_ptr theRight );
234 #endif // GEOM_GenericObjPtr_H