Salome HOME
Porting SALOME KERNEL to CMake
[modules/kernel.git] / src / GenericObj / SALOME_GenericObj_i.cc
1 // Copyright (C) 2007-2012  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
23 //  File   : SALOME_GenericObj_i.cc
24 //  Author : Alexey PETROV, Open CASCADE S.A.S. (alexey.petrov@opencascade.com)
25
26 #include "SALOME_GenericObj_i.hh"
27 #include "utilities.h"
28
29 #ifdef _DEBUG_
30 static int MYDEBUG = 0;
31 #else
32 static int MYDEBUG = 0;
33 #endif
34
35 namespace SALOME
36 {
37
38   /*!
39     \class SALOME::GenericObj_i
40     \brief Implementation of the base servant for SALOME objects with reference counter.
41
42     This class can be used to implement data entities with life-cycle management based on
43     the reference counting. 
44
45     The object is initially created with the reference counter equal to 1.
46     The function Register() can be used to incrfement the reference counter.
47     Function UnRegister() should be used to decrement reference counter.
48     As soon as reference counter goes to zero, the object is automatically deactivated in POA
49     (and, eventually its destructor is automatically called).
50   */
51   
52   /*!
53     \brief Constructor.
54     Creates an object with the reference counter initially set to 1.
55   
56     The default POA for the servant can be passed as a parameter \a thePOA.
57     By default, root POA is used.
58
59     \param thePOA optional default POA for the servant
60   */
61   GenericObj_i::GenericObj_i(PortableServer::POA_ptr thePOA): myRefCounter(1)
62   {
63     if(MYDEBUG) 
64       MESSAGE("GenericObj_i::GenericObj_i() - this = "<<this<<
65               "; CORBA::is_nil(thePOA) = "<<CORBA::is_nil(thePOA));
66     if(CORBA::is_nil(thePOA)) {
67 #ifndef WIN32
68       myPOA = PortableServer::ServantBase::_default_POA();
69 #else
70       myPOA = ServantBase::_default_POA();
71 #endif
72     }
73     else {
74       myPOA = PortableServer::POA::_duplicate(thePOA);
75     }
76   }
77
78   /*!
79     \brief Get default POA for the servant object.
80
81     This function is implicitly called from "_this()" function.
82     Default POA can be set via the constructor.
83
84     \return reference to the default POA for the servant
85   */
86   PortableServer::POA_ptr GenericObj_i::_default_POA()
87   {
88     return PortableServer::POA::_duplicate(myPOA);
89   }
90
91   /*!
92     \brief Increment reference counter.
93   */
94   void GenericObj_i::Register()
95   {
96     if(MYDEBUG)
97       MESSAGE("GenericObj_i::Register "<<this<<"; myRefCounter = "<<myRefCounter);
98     ++myRefCounter;
99   }
100
101   /*!
102     \brief Decrement reference counter.
103
104     As soon as reference counter goes to zero, the object is automatically
105     deactivated.
106   */
107   void GenericObj_i::UnRegister()
108   {
109     if(MYDEBUG)
110       MESSAGE("GenericObj_i::UnRegister "<<this<<"; myRefCounter = "<<myRefCounter);
111     if(--myRefCounter <= 0){
112       PortableServer::ObjectId_var anObjectId = myPOA->servant_to_id(this);
113       myPOA->deactivate_object(anObjectId.in());
114       _remove_ref();
115     }
116   }
117
118   /*!
119     \brief Decrement reference counter.
120     \deprecated Use UnRegister() instead.
121   */
122   void GenericObj_i::Destroy()
123   {
124     MESSAGE("WARNING SALOME::GenericObj::Destroy() function is obsolete! Use UnRegister() instead.");
125     UnRegister();
126   }
127
128 }; // end of namespace SALOME