Salome HOME
#18963 Minimize compiler warnings
[modules/kernel.git] / src / SALOMESDS / SALOMESDS_AutoRefCountPtr.hxx
1 // Copyright (C) 2007-2020  CEA/DEN, EDF R&D, OPEN CASCADE
2 //
3 // This library is free software; you can redistribute it and/or
4 // modify it under the terms of the GNU Lesser General Public
5 // License as published by the Free Software Foundation; either
6 // version 2.1 of the License, or (at your option) any later version.
7 //
8 // This library is distributed in the hope that it will be useful,
9 // but WITHOUT ANY WARRANTY; without even the implied warranty of
10 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
11 // Lesser General Public License for more details.
12 //
13 // You should have received a copy of the GNU Lesser General Public
14 // License along with this library; if not, write to the Free Software
15 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
16 //
17 // See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
18 //
19 // Author : Anthony Geay (EDF R&D)
20
21 #ifndef __SALOMESDS_AUTOREFCOUNTPTR_HXX__
22 #define __SALOMESDS_AUTOREFCOUNTPTR_HXX__
23
24 #include "SALOMESDS_Exception.hxx"
25
26 #include "omniORB4/CORBA.h"
27
28 namespace SALOMESDS
29 {
30   class POAHolder : public virtual PortableServer::ServantBase
31   {
32   public:
33     virtual PortableServer::POA_var getPOA() const = 0;
34     CORBA::Object_var activate()
35     {
36       PortableServer::POA_var poa(getPOA());
37       PortableServer::ObjectId_var id;
38       try
39       {
40           id=poa->activate_object(this);
41       }
42       catch(PortableServer::POA::ServantAlreadyActive& /*e*/) //!< TODO: unused variables
43       {
44           id=poa->servant_to_id(this);
45       }
46       CORBA::Object_var ret(poa->id_to_reference(id));
47       return ret;
48     }
49
50     void enforcedRelease()
51     {
52       PortableServer::POA_var poa(getPOA());
53       PortableServer::ObjectId_var oid(poa->servant_to_id(this));
54       poa->deactivate_object(oid);
55       _remove_ref();
56     }
57   };
58   
59   template<class T>
60   class AutoRefCountPtr
61   {
62   public:
63     AutoRefCountPtr(const AutoRefCountPtr& other):_ptr(0) { referPtr(other._ptr); }
64     AutoRefCountPtr(T *ptr=0):_ptr(ptr) { }
65     ~AutoRefCountPtr() { destroyPtr(); }
66     bool operator==(const AutoRefCountPtr& other) const { return _ptr==other._ptr; }
67     bool operator==(const T *other) const { return _ptr==other; }
68     AutoRefCountPtr &operator=(const AutoRefCountPtr& other) { if(_ptr!=other._ptr) { destroyPtr(); referPtr(other._ptr); } return *this; }
69     AutoRefCountPtr &operator=(T *ptr) { if(_ptr!=ptr) { destroyPtr(); _ptr=ptr; } return *this; }
70     T *operator->() { return _ptr ; }
71     const T *operator->() const { return _ptr; }
72     T& operator*() { return *_ptr; }
73     const T& operator*() const { return *_ptr; }
74     operator T *() { return _ptr; }
75     operator const T *() const { return _ptr; }
76     T *retn() { if(_ptr) _ptr->incrRef(); return _ptr; }
77   private:
78     void referPtr(T *ptr) { _ptr=ptr; if(_ptr) _ptr->incrRef(); }
79     void destroyPtr() { if(_ptr) _ptr->decrRef(); }
80   private:
81     T *_ptr;
82   };
83
84   template<class T, class U>
85   typename SALOMESDS::AutoRefCountPtr<U> DynamicCast(typename SALOMESDS::AutoRefCountPtr<T>& autoSubPtr)
86   {
87     T *subPtr(autoSubPtr);
88     U *ptr(dynamic_cast<U *>(subPtr));
89     typename SALOMESDS::AutoRefCountPtr<U> ret(ptr);
90     if(ptr)
91       ptr->incrRef();
92     return ret;
93   }
94
95   template<class T, class U>
96   typename SALOMESDS::AutoRefCountPtr<U> DynamicCastSafe(typename SALOMESDS::AutoRefCountPtr<T>& autoSubPtr)
97   {
98     T *subPtr(autoSubPtr);
99     U *ptr(dynamic_cast<U *>(subPtr));
100     if(subPtr && !ptr)
101       throw Exception("DynamicCastSafe : U is not a subtype of T !");
102     typename SALOMESDS::AutoRefCountPtr<U> ret(ptr);
103     if(ptr)
104       ptr->incrRef();
105     return ret;
106   }
107
108   template<class T>// T is expected to be a POAHolder subclass
109   class AutoServantPtr
110   {
111   public:
112     AutoServantPtr(T *ptr=0):_ptr(ptr) { }
113     ~AutoServantPtr() { destroyPtr(); }
114     bool operator==(const AutoServantPtr& other) const { return _ptr==other._ptr; }
115     bool operator==(const T *other) const { return _ptr==other; }
116     AutoServantPtr &operator=(T *ptr) { if(_ptr!=ptr) { destroyPtr(); _ptr=ptr; } return *this; }
117     T *operator->() { return _ptr ; }
118     const T *operator->() const { return _ptr; }
119     T& operator*() { return *_ptr; }
120     const T& operator*() const { return *_ptr; }
121     operator T *() { return _ptr; }
122     operator const T *() const { return _ptr; }
123     bool isNull() const { return _ptr==NULL; }
124   private:
125     void destroyPtr()
126     {
127       if(!_ptr)
128         return;
129       _ptr->enforcedRelease();
130     }
131   private:
132     T *_ptr;
133   };
134 }
135
136 #endif