1 // Copyright (C) 2007-2015 CEA/DEN, EDF R&D, OPEN CASCADE
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.
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.
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
17 // See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
19 // Author : Anthony Geay (EDF R&D)
21 #ifndef __SALOMESDS_AUTOREFCOUNTPTR_HXX__
22 #define __SALOMESDS_AUTOREFCOUNTPTR_HXX__
24 #include "SALOMESDS_Exception.hxx"
26 #include "omniORB4/CORBA.h"
30 class POAHolder : public virtual PortableServer::ServantBase
33 virtual PortableServer::POA_var getPOA() const = 0;
34 CORBA::Object_var activate()
36 PortableServer::POA_var poa(getPOA());
37 PortableServer::ObjectId_var id(poa->activate_object(this));
38 CORBA::Object_var ret(poa->id_to_reference(id));
42 void enforcedRelease()
44 PortableServer::POA_var poa(getPOA());
45 PortableServer::ObjectId_var oid(poa->servant_to_id(this));
46 poa->deactivate_object(oid);
55 AutoRefCountPtr(const AutoRefCountPtr& other):_ptr(0) { referPtr(other._ptr); }
56 AutoRefCountPtr(T *ptr=0):_ptr(ptr) { }
57 ~AutoRefCountPtr() { destroyPtr(); }
58 bool operator==(const AutoRefCountPtr& other) const { return _ptr==other._ptr; }
59 bool operator==(const T *other) const { return _ptr==other; }
60 AutoRefCountPtr &operator=(const AutoRefCountPtr& other) { if(_ptr!=other._ptr) { destroyPtr(); referPtr(other._ptr); } return *this; }
61 AutoRefCountPtr &operator=(T *ptr) { if(_ptr!=ptr) { destroyPtr(); _ptr=ptr; } return *this; }
62 T *operator->() { return _ptr ; }
63 const T *operator->() const { return _ptr; }
64 T& operator*() { return *_ptr; }
65 const T& operator*() const { return *_ptr; }
66 operator T *() { return _ptr; }
67 operator const T *() const { return _ptr; }
68 T *retn() { if(_ptr) _ptr->incrRef(); return _ptr; }
70 void referPtr(T *ptr) { _ptr=ptr; if(_ptr) _ptr->incrRef(); }
71 void destroyPtr() { if(_ptr) _ptr->decrRef(); }
76 template<class T, class U>
77 typename SALOMESDS::AutoRefCountPtr<U> DynamicCast(typename SALOMESDS::AutoRefCountPtr<T>& autoSubPtr) throw()
79 T *subPtr(autoSubPtr);
80 U *ptr(dynamic_cast<U *>(subPtr));
81 typename SALOMESDS::AutoRefCountPtr<U> ret(ptr);
87 template<class T, class U>
88 typename SALOMESDS::AutoRefCountPtr<U> DynamicCastSafe(typename SALOMESDS::AutoRefCountPtr<T>& autoSubPtr)
90 T *subPtr(autoSubPtr);
91 U *ptr(dynamic_cast<U *>(subPtr));
93 throw Exception("DynamicCastSafe : U is not a subtype of T !");
94 typename SALOMESDS::AutoRefCountPtr<U> ret(ptr);
100 template<class T>// T is expected to be a POAHolder subclass
104 AutoServantPtr(T *ptr=0):_ptr(ptr) { }
105 ~AutoServantPtr() { destroyPtr(); }
106 bool operator==(const AutoServantPtr& other) const { return _ptr==other._ptr; }
107 bool operator==(const T *other) const { return _ptr==other; }
108 AutoServantPtr &operator=(T *ptr) { if(_ptr!=ptr) { destroyPtr(); _ptr=ptr; } return *this; }
109 T *operator->() { return _ptr ; }
110 const T *operator->() const { return _ptr; }
111 T& operator*() { return *_ptr; }
112 const T& operator*() const { return *_ptr; }
113 operator T *() { return _ptr; }
114 operator const T *() const { return _ptr; }
120 _ptr->enforcedRelease();