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