All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Groups Pages
SmartIF.h
Go to the documentation of this file.
1 // $Id: SmartIF.h,v 1.10 2008/10/27 19:22:20 marcocle Exp $
2 #ifndef GAUDI_SMARTIF_H
3 #define GAUDI_SMARTIF_H 1
4 
5 // Framework include files
7 
19 template <class TYPE> class SmartIF {
20 private:
22  TYPE* m_interface;
23 public:
24  // ---------- Construction and destruction ----------
26  inline SmartIF(): m_interface(0) {}
28  inline SmartIF(TYPE* ptr): m_interface(ptr) {
29  if (m_interface) m_interface->addRef();
30  }
32  template <class OTHER>
33  inline SmartIF(OTHER* ptr): m_interface(0) {
34  if (ptr) reset(ptr);
35  }
37  inline SmartIF(const SmartIF& rhs): m_interface(rhs.get()) {
38  if (m_interface) m_interface->addRef();
39  }
42  template <class T>
43  inline explicit SmartIF(const SmartIF<T>& rhs): m_interface(0) {
44  reset(rhs.get());
45  }
47  inline ~SmartIF() { reset(); }
48 
49  // ---------- Boolean and comparison methods ----------
51  inline bool isValid() const { return m_interface != 0; }
52 
53  // ---------- Pointer access methods ----------
56  inline operator TYPE* () const { return m_interface; }
58  inline TYPE* operator->() const { return m_interface; }
60  inline TYPE& operator *() const { return *m_interface; }
62  inline TYPE* get() const { return m_interface; }
63 #if !defined(GAUDI_V22_API) && !defined(NEW_SMARTIF)
64  inline TYPE*& pRef() {
66  return m_interface;
67  }
68 #endif
69 
70  // ---------- Cast methods ----------
74  inline void reset(TYPE* ptr = 0) {
75  if (ptr == m_interface) return;
76  if (m_interface) m_interface->release();
77  if (ptr) {
78  m_interface = ptr;
79  m_interface->addRef();
80  } else {
81  m_interface = 0;
82  }
83  }
86  template <class OTHER>
87  inline void reset(OTHER* ptr) {
88  if (static_cast<IInterface*>(ptr) == static_cast<IInterface*>(m_interface)) return;
89  if (m_interface) m_interface->release();
90  if (ptr) {
91  ptr->queryInterface(TYPE::interfaceID(), pp_cast<void>(&m_interface)).ignore();
92  } else {
93  m_interface = 0;
94  }
95  }
96 
97  // ---------- Special hacks ----------
105  reset(ptr);
106  return *this;
107  }
109  inline SmartIF& operator = (const SmartIF& rhs) {
110  reset(rhs.get());
111  return *this;
112  }
115  template <class T>
116  inline SmartIF& operator = (const SmartIF<T>& rhs) {
117  reset(rhs.get());
118  return *this;
119  }
120 };
121 
122 #if defined(_MSC_VER) && (_MSC_VER < 1500)
123 template <class TYPE>
131 inline bool operator == (long lhs, const SmartIF<TYPE> &rhs) {
132  return rhs == reinterpret_cast<TYPE*>(lhs);
133 }
141 template <class TYPE>
142 inline bool operator != (long lhs, const SmartIF<TYPE> &rhs) {
143  return rhs != reinterpret_cast<TYPE*>(lhs);
144 }
145 #endif
146 #endif // GAUDI_SMARTIF_H
TYPE * m_interface
Pointer to the instance.
Definition: SmartIF.h:22
SmartIF()
Default constructor.
Definition: SmartIF.h:26
bool operator!=(const GaudiUtils::Allocator< T1 > &, const GaudiUtils::Allocator< T2 > &)
Definition: Allocator.h:261
Small smart pointer class with automatic reference counting for IInterface.
Definition: IConverter.h:14
SmartIF & operator=(IInterface *ptr)
Assignment operator from IInterface pointer.
Definition: SmartIF.h:104
~SmartIF()
Standard Destructor.
Definition: SmartIF.h:47
SmartIF(const SmartIF &rhs)
Copy constructor.
Definition: SmartIF.h:37
SmartIF(const SmartIF< T > &rhs)
Constructor from another SmartIF, with a different type.
Definition: SmartIF.h:43
SmartIF(OTHER *ptr)
Standard constructor from any (IInterface-derived) pointer.
Definition: SmartIF.h:33
void reset(OTHER *ptr)
Set the internal pointer to the passed one disposing of the old one.
Definition: SmartIF.h:87
TYPE *& pRef()
Get reference to the pointer.
Definition: SmartIF.h:65
bool isValid() const
Allow for check if smart pointer is valid.
Definition: SmartIF.h:51
bool operator==(const GaudiUtils::Allocator< T1 > &, const GaudiUtils::Allocator< T2 > &)
Definition: Allocator.h:249
Definition of the basic interface.
Definition: IInterface.h:160
TYPE * get() const
Get interface pointer.
Definition: SmartIF.h:62
TYPE & operator*() const
Dereference operator.
Definition: SmartIF.h:60
SmartIF(TYPE *ptr)
Standard constructor from pointer.
Definition: SmartIF.h:28
TYPE * operator->() const
Dereference operator.
Definition: SmartIF.h:58
void reset(TYPE *ptr=0)
Set the internal pointer to the passed one disposing of the old one.
Definition: SmartIF.h:74