Salome HOME
26cf94f8b65b4c5c9398b04bc6de0b7dd575fbc6
[modules/geom.git] / src / GEOMBase / GEOM_GenericObjPtr.h
1 // Copyright (C) 2007-2023  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, or (at your option) any later version.
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 "GEOM_GEOMBase.hxx"
29
30 #include "SALOMEconfig.h"
31 #include CORBA_SERVER_HEADER(GEOM_Gen)
32
33 namespace GEOM
34 {
35   /*!
36     \class GenericObjPtr
37     \brief A smart pointer for the SALOME GenericObj interface.
38
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.
41
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.
45
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.
48
49     Usage:
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
52     Fault error.
53     - To pass the smart pointer to the function that requires CORBA _ptr type parameter,
54     use get() function.
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 corresponding servant should be deleted.
61     
62     Examples:
63     \code
64     typedef GEOM::GenericObjPtr<MyInterface> MyIPtr;
65     void MyInterface_ptr foo();
66     void MyInterface_ptr bar( MyInterface_ptr p );
67
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
74     \endcode
75    */
76   
77   template <typename TInterface> class GenericObjPtr
78   {
79     typedef typename TInterface::_var_type TInterfaceVar;
80     typedef typename TInterface::_ptr_type TInterfacePtr;
81     
82   private:
83     TInterfaceVar myObject;
84
85   private:
86     //! Increment counter for the object.
87     void Register()
88     {
89       if ( !CORBA::is_nil( this->myObject ) )
90         this->myObject->Register();
91     }
92
93     //! Decrement counter for the object.
94     void UnRegister()
95     {
96       if ( !CORBA::is_nil( this->myObject ) ) {
97         this->myObject->UnRegister();
98         this->myObject = TInterface::_nil();
99       }
100     }
101       
102   public:
103     //! Initialize pointer to nil generic object reference.
104     GenericObjPtr()
105     {}
106     
107     //! Initialize pointer to the given generic object reference.
108     GenericObjPtr( TInterfacePtr theObject )
109     {
110       this->myObject = TInterface::_duplicate( theObject );
111       this->Register();
112     }
113     
114     //! Initialize pointer with a new reference to the same object referenced by given pointer.
115     GenericObjPtr( const GenericObjPtr& thePointer )
116     {
117       this->myObject = thePointer.myObject;
118       this->Register();
119     }
120     
121     //! Destroy pointer and remove the reference to the object.
122     ~GenericObjPtr()
123     {
124       this->UnRegister();
125     }
126     
127     //! Assign object to reference and remove reference to an old object.
128     GenericObjPtr& operator=( TInterfacePtr theObject )
129     {
130       this->UnRegister();
131       this->myObject = TInterface::_duplicate( theObject );
132       this->Register();
133       return *this;
134     }
135
136     //! Assign object to reference and remove reference to an old object.
137     GenericObjPtr& operator=( const GenericObjPtr& thePointer )
138     {
139       this->UnRegister();
140       this->myObject = thePointer.myObject;
141       this->Register();
142       return *this;
143     }
144
145     static bool isSame( TInterfacePtr theLeft, TInterfacePtr theRight )
146     {
147       return theLeft->_is_equivalent( theRight );
148     }
149
150     //! Check equivalence
151     bool operator==( TInterfacePtr theObject )
152     {
153       return isSame( this->myObject, theObject );
154     }
155
156     //! Check equivalence
157     bool operator==( const GenericObjPtr& thePointer )
158     {
159       return isSame( this->myObject, thePointer.get() );;
160     }
161
162     //! Check difference
163     bool operator!=( TInterfacePtr theObject )
164     {
165       return !isSame( this->myObject, theObject );
166     }
167
168     //! Check difference
169     bool operator!=( const GenericObjPtr& thePointer )
170     {
171       return !isSame( this->myObject, thePointer.get() );;
172     }
173
174     //! Provides normal pointer target member access using operator ->.
175     TInterfacePtr operator->() const
176     {
177       return this->get();
178     }
179
180     //! Check validity of the pointer.
181     operator bool() const
182     {
183        return !this->isNull();
184     }
185
186     //! Initialize pointer to the given generic object reference and take ownership on it.
187     void take( TInterfacePtr theObject )
188     {
189       this->UnRegister();
190       this->myObject = TInterface::_duplicate( theObject );
191     }
192
193     //! Get the contained object.
194     TInterfacePtr get() const
195     {
196       return this->myObject;
197     }
198
199     //! Make the copy of the contained object and return it (caller becomes owner of the CORBA reference).
200     TInterfacePtr copy() const
201     {
202       return TInterface::_duplicate( this->myObject );
203     }
204     
205     //! Check if pointer is null.
206     bool isNull() const
207     {
208       return CORBA::is_nil( this->myObject );
209     }
210
211     //! Nullify pointer.
212     void nullify()
213     {
214       this->UnRegister();
215     }
216   };
217   
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;
232
233   template<> bool GEOMBASE_EXPORT GenericObjPtr<GEOM::GEOM_Object>::isSame( GEOM::GEOM_Object_ptr theLeft, GEOM::GEOM_Object_ptr theRight );
234 }
235
236 #endif // GEOM_GenericObjPtr_H