Salome HOME
updated copyright message
[modules/kernel.git] / src / GenericObj / SALOME_GenericObj_wrap.hxx
1 // Copyright (C) 2007-2023  CEA, EDF, 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
23 // File      : SALOME_GenericObj_wrap.hxx
24 // Created   : Fri Dec  7 11:32:45 2012
25 // Author    : Edward AGAPOV (eap)
26
27
28 #ifndef __SALOME_GenericObj_wrap_HXX__
29 #define __SALOME_GenericObj_wrap_HXX__
30
31 namespace SALOME
32 {
33   // call UnRegister on each element in a sequence of SALOME::GenericObj's
34   template< class SEQ_OF_GENOBJ >
35   void UnRegister( SEQ_OF_GENOBJ& seq )
36   {
37     for ( size_t i = 0; i < seq->length(); ++i )
38       if ( !CORBA::is_nil( seq[i] ))
39         seq[i]->UnRegister();
40   }
41
42   // This class is intended to free the user of objects, iherited from SALOME::GenericObj,
43   // from a need to call UnRegister() after having finished using the object.
44   // The behavior of this class is similar to that of var CORBA class and thus it
45   // can replace var types of wrapped CORBA classes in some code like follows:
46   //
47   // namespace SALOMEDS
48   // {
49   //   typedef SALOME::GenericObj_wrap< SObject >       SObject_wrap;
50   //   typedef SALOME::GenericObj_wrap< ChildIterator > ChildIterator_wrap;
51   // }
52   // ...
53   // SALOMEDS::ChildIterator_wrap anIter = aStudy->NewChildIterator( mainSO );
54   // for ( ; anIter->More(); anIter->Next() ) {
55   //   SALOMEDS::SObject_wrap so = anIter->Value();
56   // ...
57   //
58   // Note that the need to call UnRegister(), and thus to use GenericObj_wrap, depends on
59   // implementation of a method returning a GenericObj servant. If the method creates a
60   // servant, than UnRegister() must be called. If the method retrieves a servant from
61   // some storage, then it's wrong to call UnRegister().
62   //
63   // A pleasant additional feature privided by this class is that one can assign
64   // GenericObj_wrap< BASE_CLASS > to GenericObj_wrap< CHILD_CLASS > without
65   // calling CHILD_CLASS::_narrow().
66   //
67   template< class GENOBJ >
68   class GenericObj_wrap
69   {
70   public:
71     typedef typename GENOBJ::_ptr_type TPtr;
72     typedef typename GENOBJ::_var_type TVar;
73
74     // CONSTRUCTION
75     GenericObj_wrap()
76     {
77       _isOwn = false;
78     }
79     GenericObj_wrap( const TPtr& obj )
80       : _obj( obj ), _isOwn( true )
81     {}
82     GenericObj_wrap( const TVar& obj )
83       : _obj( obj ), _isOwn( true )
84     {}
85     GenericObj_wrap( const GenericObj_wrap& wrap )
86       : _obj( wrap._obj ), _isOwn( wrap._getIsOwn() )
87     {
88       _register();
89     }
90     template< class BASE_GENOBJ >
91     GenericObj_wrap( const GenericObj_wrap<BASE_GENOBJ>& wrap )
92       : _obj( GENOBJ::_narrow( wrap.in() )), _isOwn( wrap._getIsOwn() )
93     {
94       _register();
95     }
96     // DESTRUCTION
97     ~GenericObj_wrap()
98     {
99       _release();
100     }
101     // COPYING
102     GenericObj_wrap& operator=( const TPtr& obj )
103     {
104       _release();
105       _obj   = obj;
106       _isOwn = true;
107       return *this;
108     }
109     GenericObj_wrap& operator=( const TVar& obj )
110     {
111       _release();
112       _obj   = obj;
113       _isOwn = true;
114       return *this;
115     }
116     GenericObj_wrap& operator=( const GenericObj_wrap& wrap )
117     {
118       _release();
119       _obj   = wrap._obj;//wrap.in();
120       _isOwn = wrap._getIsOwn();
121       _register();
122       return *this;
123     }
124     template< class BASE_GENOBJ >
125     GenericObj_wrap& operator=( const GenericObj_wrap<BASE_GENOBJ>& wrap )
126     {
127       _release();
128       _obj   = GENOBJ::_narrow( wrap.in() );
129       _isOwn = wrap._getIsOwn();
130       _register();
131       return *this;
132     }
133     // ACCESS
134     operator TPtr() const
135     {
136       return _obj.in();
137     }
138     operator TVar() const
139     {
140       return _obj;
141     }
142     TVar operator->() const
143     {
144       return _obj;
145     }
146     TPtr in() const
147     {
148       return _obj;
149     }
150     TPtr& inout()
151     {
152       _release(); // returned reference to _obj will be initialized with another object
153       _isOwn = true;
154       return _obj.inout();
155     }
156     TPtr _retn()
157     {
158       _isOwn = false;
159       return _obj._retn();
160     }
161
162     bool _getIsOwn() const { return  _isOwn; }
163
164   private:
165     void _register()
166     {
167       if ( _isOwn && !CORBA::is_nil( _obj ))
168         _obj->Register();
169     }
170     void _release()
171     {
172       if ( _isOwn && !CORBA::is_nil( _obj ))
173         _obj->UnRegister();
174       _isOwn = false;
175     }
176     TVar _obj;
177     bool _isOwn;
178   };
179 } // namespace SALOME
180
181 #endif