Version: 8.3.0
SALOMESDS_AutoRefCountPtr.hxx
Go to the documentation of this file.
1 // Copyright (C) 2007-2016 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(poa->activate_object(this));
38  CORBA::Object_var ret(poa->id_to_reference(id));
39  return ret;
40  }
41 
43  {
44  PortableServer::POA_var poa(getPOA());
45  PortableServer::ObjectId_var oid(poa->servant_to_id(this));
46  poa->deactivate_object(oid);
47  _remove_ref();
48  }
49  };
50 
51  template<class T>
53  {
54  public:
55  AutoRefCountPtr(const AutoRefCountPtr& other):_ptr(0) { referPtr(other._ptr); }
56  AutoRefCountPtr(T *ptr=0):_ptr(ptr) { }
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; }
69  private:
70  void referPtr(T *ptr) { _ptr=ptr; if(_ptr) _ptr->incrRef(); }
71  void destroyPtr() { if(_ptr) _ptr->decrRef(); }
72  private:
73  T *_ptr;
74  };
75 
76  template<class T, class U>
78  {
79  T *subPtr(autoSubPtr);
80  U *ptr(dynamic_cast<U *>(subPtr));
81  typename SALOMESDS::AutoRefCountPtr<U> ret(ptr);
82  if(ptr)
83  ptr->incrRef();
84  return ret;
85  }
86 
87  template<class T, class U>
89  {
90  T *subPtr(autoSubPtr);
91  U *ptr(dynamic_cast<U *>(subPtr));
92  if(subPtr && !ptr)
93  throw Exception("DynamicCastSafe : U is not a subtype of T !");
94  typename SALOMESDS::AutoRefCountPtr<U> ret(ptr);
95  if(ptr)
96  ptr->incrRef();
97  return ret;
98  }
99 
100  template<class T>// T is expected to be a POAHolder subclass
102  {
103  public:
104  AutoServantPtr(T *ptr=0):_ptr(ptr) { }
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; }
115  private:
116  void destroyPtr()
117  {
118  if(!_ptr)
119  return;
120  _ptr->enforcedRelease();
121  }
122  private:
123  T *_ptr;
124  };
125 }
126 
127 #endif