1 // Copyright (C) 2007-2015 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, or (at your option) any later version.
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 "GEOM_GEOMBase.hxx"
30 #include "SALOMEconfig.h"
31 #include CORBA_SERVER_HEADER(GEOM_Gen)
37 \brief A smart pointer for the SALOME GenericObj interface.
39 This class can be used in conjunction with the references to the CORBA objects which
40 interfaces are inherited from the SALOME::GenericObj CORBA interface.
42 The smart pointer class automatically invokes Register() / UnRegister() functions of th
43 interface in order to prevent memory leaks and other such problems caused by improper
44 usage of the CORBA references.
46 Smart pointers can be easily copied, stored within class as data members, passed to the
47 functions requiring native CORBA reference as parameters, etc.
50 - If you want to assign the smart pointer to the CORBA _var type variable, use copy()
51 function to make a copy of the stored CORBA object. Otherwise you might cause Segmentation
53 - To pass the smart pointer to the function that requires CORBA _ptr type parameter,
55 - If you want to take an ownership on the CORBA object, use take() function.
56 In case of SALOME Generic object this is useful when some function returns newly created
57 object that should be removed by the caller as soon as the object is no more required.
58 For example, function GetSubShape() of the GEOM_IShapesOperation interface always creates
59 new servant object and returns new object reference to it. If the object is not published
60 in the study, it has to be destroyed and the coresponding servant should be deleted.
64 typedef GEOM::GenericObjPtr<MyInterface> MyIPtr;
65 void MyInterface_ptr foo();
66 void MyInterface_ptr bar( MyInterface_ptr p );
68 MyIPtr v1; // create empty (nil) pointer
69 MyIPtr v2 = foo(); // get some CORBA reference and store it within the smart pointer
70 v1 = v2; // copy smart pointer (reference counter is incremented)
71 v2 = bar( v1.get() ); // pass smart pointer to the function
72 MyInterface_var var = v2.copy(); // initialize _var variable with the smart pointer contents
73 v1.take( foo() ); // take ownership on the newly created object
77 template <typename TInterface> class GenericObjPtr
79 typedef typename TInterface::_var_type TInterfaceVar;
80 typedef typename TInterface::_ptr_type TInterfacePtr;
83 TInterfaceVar myObject;
86 //! Increment counter for the object.
89 if ( !CORBA::is_nil( this->myObject ) )
90 this->myObject->Register();
93 //! Decrement counter for the object.
96 if ( !CORBA::is_nil( this->myObject ) ) {
97 this->myObject->UnRegister();
98 this->myObject = TInterface::_nil();
103 //! Initialize pointer to nil generic object reference.
107 //! Initialize pointer to the given generic object reference.
108 GenericObjPtr( TInterfacePtr theObject )
110 this->myObject = TInterface::_duplicate( theObject );
114 //! Initialize pointer with a new reference to the same object referenced by given pointer.
115 GenericObjPtr( const GenericObjPtr& thePointer )
117 this->myObject = thePointer.myObject;
121 //! Destroy pointer and remove the reference to the object.
127 //! Assign object to reference and remove reference to an old object.
128 GenericObjPtr& operator=( TInterfacePtr theObject )
131 this->myObject = TInterface::_duplicate( theObject );
136 //! Assign object to reference and remove reference to an old object.
137 GenericObjPtr& operator=( const GenericObjPtr& thePointer )
140 this->myObject = thePointer.myObject;
145 static bool isSame( TInterfacePtr theLeft, TInterfacePtr theRight )
147 return theLeft->_is_equivalent( theRight );
150 //! Check equivalence
151 bool operator==( TInterfacePtr theObject )
153 return isSame( this->myObject, theObject );
156 //! Check equivalence
157 bool operator==( const GenericObjPtr& thePointer )
159 return isSame( this->myObject, thePointer.get() );;
163 bool operator!=( TInterfacePtr theObject )
165 return !isSame( this->myObject, theObject );
169 bool operator!=( const GenericObjPtr& thePointer )
171 return !isSame( this->myObject, thePointer.get() );;
174 //! Provides normal pointer target member access using operator ->.
175 TInterfacePtr operator->() const
180 //! Check validity of the pointer.
181 operator bool() const
183 return !this->isNull();
186 //! Initialize pointer to the given generic object reference and take ownership on it.
187 void take( TInterfacePtr theObject )
190 this->myObject = TInterface::_duplicate( theObject );
193 //! Get the contained object.
194 TInterfacePtr get() const
196 return this->myObject;
199 //! Make the copy of the contained object and return it (caller becomes owner of the CORBA reference).
200 TInterfacePtr copy() const
202 return TInterface::_duplicate( this->myObject );
205 //! Check if pointer is null.
208 return CORBA::is_nil( this->myObject );
218 typedef GenericObjPtr<GEOM::GEOM_Object> GeomObjPtr;
219 typedef GenericObjPtr<GEOM::GEOM_Field> GeomFieldPtr;
220 typedef GenericObjPtr<GEOM::GEOM_IBasicOperations> BasicOpPtr;
221 typedef GenericObjPtr<GEOM::GEOM_ITransformOperations> TransformOpPtr;
222 typedef GenericObjPtr<GEOM::GEOM_I3DPrimOperations> I3DPrimOpPtr;
223 typedef GenericObjPtr<GEOM::GEOM_IShapesOperations> ShapesOpPtr;
224 typedef GenericObjPtr<GEOM::GEOM_IBlocksOperations> BlocksOpPtr;
225 typedef GenericObjPtr<GEOM::GEOM_IBooleanOperations> BooleanOpPtr;
226 typedef GenericObjPtr<GEOM::GEOM_ICurvesOperations> CurvesOpPtr;
227 typedef GenericObjPtr<GEOM::GEOM_ILocalOperations> LocalOpPtr;
228 typedef GenericObjPtr<GEOM::GEOM_IHealingOperations> HealingOpPtr;
229 typedef GenericObjPtr<GEOM::GEOM_IInsertOperations> InsertOpPtr;
230 typedef GenericObjPtr<GEOM::GEOM_IMeasureOperations> MeasureOpPtr;
231 typedef GenericObjPtr<GEOM::GEOM_IGroupOperations> GroupOpPtr;
233 template<> bool GEOMBASE_EXPORT GenericObjPtr<GEOM::GEOM_Object>::isSame( GEOM::GEOM_Object_ptr theLeft, GEOM::GEOM_Object_ptr theRight );
236 #endif // GEOM_GenericObjPtr_H