Salome HOME
5a5c7d88800a1a3dd3effe85ee6b62240051c2c5
[modules/kernel.git] / src / GenericObj / SALOME_GenericObj_i.cc
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_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 #include <iostream>
30 #include <typeinfo>
31
32
33 //#define IS_OBJ_IN_QUESTION(where) is_obj_in_question(this, myRefCounter, where)
34 #define IS_OBJ_IN_QUESTION(where)
35
36 namespace SALOME
37 {
38   void is_obj_in_question( const GenericObj_i* o, int myRefCounter,const char* where)
39   {
40     if ( std::string( typeid(*o).name() ).find("SALOMEDS") != std::string::npos )
41       return;
42     // if ( std::string( typeid(*o).name() ).find("SMESH_") != std::string::npos ||
43     //      std::string( typeid(*o).name() ).find("StdMesher") != std::string::npos )
44     {
45       std::cout<< typeid(*o).name() << " " << o << " " << where << "  myRefCounter ==> " << myRefCounter;
46       if ( myRefCounter == 0 ) std::cout << " DELETE !";
47       std::cout << std::endl;
48     }
49   }
50
51   /*!
52     \class SALOME::GenericObj_i
53     \brief Implementation of the base servant for SALOME objects with reference counter.
54
55     This class can be used to implement data entities with life-cycle management based on
56     the reference counting. 
57
58     The object is initially created with the reference counter equal to 1.
59     The function Register() can be used to incrfement the reference counter.
60     Function UnRegister() should be used to decrement reference counter.
61     As soon as reference counter goes to zero, the object is automatically deactivated in POA
62     (and, eventually its destructor is automatically called).
63   */
64   
65   /*!
66     \brief Constructor.
67     Creates an object with the reference counter initially set to 1.
68   
69     The default POA for the servant can be passed as a parameter \a thePOA.
70     By default, root POA is used.
71
72     \param thePOA optional default POA for the servant
73   */
74   GenericObj_i::GenericObj_i(PortableServer::POA_ptr thePOA): myRefCounter(1)
75   {
76
77     if(CORBA::is_nil(thePOA)) {
78 #ifndef WIN32
79       myPOA = PortableServer::ServantBase::_default_POA();
80 #else
81       myPOA = ServantBase::_default_POA();
82 #endif
83     }
84     else {
85       myPOA = PortableServer::POA::_duplicate(thePOA);
86     }
87   }
88
89   /*!
90     \brief Get default POA for the servant object.
91
92     This function is implicitly called from "_this()" function.
93     Default POA can be set via the constructor.
94
95     \return reference to the default POA for the servant
96   */
97   PortableServer::POA_ptr GenericObj_i::_default_POA()
98   {
99     return PortableServer::POA::_duplicate(myPOA);
100   }
101
102   /*!
103     \brief Increment reference counter.
104   */
105   void GenericObj_i::Register()
106   {
107     MESSAGE("GenericObj_i::Register " << this << "; myRefCounter = " << myRefCounter);
108     ++myRefCounter;
109     IS_OBJ_IN_QUESTION( "Register" );
110   }
111
112   /*!
113     \brief Decrement reference counter.
114
115     As soon as reference counter goes to zero, the object is automatically
116     deactivated.
117   */
118   void GenericObj_i::UnRegister()
119   {
120     MESSAGE("GenericObj_i::UnRegister " << this << "; myRefCounter = " << myRefCounter);
121     --myRefCounter;
122     IS_OBJ_IN_QUESTION( "UnRegister" );
123     if(myRefCounter <= 0){
124       PortableServer::ObjectId_var anObjectId = myPOA->servant_to_id(this);
125       myPOA->deactivate_object(anObjectId.in());
126       _remove_ref();
127     }
128   }
129
130   /*!
131     \brief Decrement reference counter.
132     \deprecated Use UnRegister() instead.
133   */
134   void GenericObj_i::Destroy()
135   {
136     MESSAGE("WARNING SALOME::GenericObj::Destroy() function is obsolete! Use UnRegister() instead.");
137     UnRegister();
138   }
139
140 } // end of namespace SALOME