The Gaudi Framework  master (37c0b60a)
SmartIF.h
Go to the documentation of this file.
1 /***********************************************************************************\
2 * (c) Copyright 1998-2024 CERN for the benefit of the LHCb and ATLAS collaborations *
3 * *
4 * This software is distributed under the terms of the Apache version 2 licence, *
5 * copied verbatim in the file "LICENSE". *
6 * *
7 * In applying this licence, CERN does not waive the privileges and immunities *
8 * granted to it by virtue of its status as an Intergovernmental Organization *
9 * or submit itself to any jurisdiction. *
10 \***********************************************************************************/
11 #ifndef GAUDI_SMARTIF_H
12 #define GAUDI_SMARTIF_H 1
13 
14 // Framework include files
15 #include <GaudiKernel/IInterface.h>
16 
28 template <class TYPE>
29 class SmartIF {
30 private:
32  TYPE* m_interface = nullptr;
33 
34 public:
35  // ---------- Construction and destruction ----------
37  inline SmartIF() = default;
39  inline SmartIF( TYPE* ptr ) : m_interface( ptr ) {
40  if ( m_interface ) m_interface->addRef();
41  }
43  template <class OTHER>
44  inline SmartIF( OTHER* ptr ) {
45  if ( ptr ) reset( ptr );
46  }
48  inline SmartIF( const SmartIF& rhs ) : m_interface( rhs.get() ) {
49  if ( m_interface ) m_interface->addRef();
50  }
52  inline SmartIF( SmartIF&& rhs ) : m_interface( rhs.m_interface ) { rhs.m_interface = nullptr; }
54  inline SmartIF& operator=( SmartIF&& rhs ) {
55  if ( m_interface ) m_interface->release();
56  m_interface = rhs.m_interface;
57  rhs.m_interface = nullptr;
58  return *this;
59  }
60 
63  template <class T>
64  inline explicit SmartIF( const SmartIF<T>& rhs ) {
65  reset( rhs.get() );
66  }
68  inline ~SmartIF() { reset(); }
69 
70  // ---------- Boolean and comparison methods ----------
72  inline bool isValid() const { return m_interface != nullptr; }
73 
74  inline explicit operator bool() const { return isValid(); }
75  inline bool operator!() const { return !isValid(); }
76 
77  // ---------- Pointer access methods ----------
80  inline operator TYPE*() const { return m_interface; }
82  inline TYPE* operator->() const { return m_interface; }
84  inline TYPE& operator*() const { return *m_interface; }
86  inline TYPE* get() const { return m_interface; }
87 #if !defined( GAUDI_V22_API ) && !defined( NEW_SMARTIF )
88  [[deprecated]] inline TYPE*& pRef() { return m_interface; }
90 #endif
91 
92  // ---------- Cast methods ----------
96  inline void reset( TYPE* ptr = nullptr ) {
97  if ( ptr == m_interface ) return;
98  if ( m_interface ) m_interface->release();
99  m_interface = ptr;
100  if ( m_interface ) m_interface->addRef();
101  }
104  template <class OTHER>
105  inline void reset( OTHER* ptr ) {
106  if ( static_cast<IInterface*>( ptr ) == static_cast<IInterface*>( m_interface ) ) return;
107  if ( m_interface ) m_interface->release();
108  if ( ptr ) {
109  ptr->queryInterface( TYPE::interfaceID(), pp_cast<void>( &m_interface ) ).ignore();
110  } else {
111  m_interface = nullptr;
112  }
113  }
114 
116  template <typename IFace>
117  SmartIF<IFace> as() const {
118  return SmartIF<IFace>{ *this };
119  }
120 
121  // ---------- Special hacks ----------
128  inline SmartIF& operator=( IInterface* ptr ) {
129  reset( ptr );
130  return *this;
131  }
133  inline SmartIF& operator=( const SmartIF& rhs ) {
134  reset( rhs.get() );
135  return *this;
136  }
139  template <class T>
140  inline SmartIF& operator=( const SmartIF<T>& rhs ) {
141  reset( rhs.get() );
142  return *this;
143  }
144 };
145 
146 // helper function to turn a pointer to an interface into
147 // the corresponding SmartIF -- this avoids having to type
148 // the typename twice, and thus insures consistency
149 template <typename IFace>
150 SmartIF<IFace> make_SmartIF( IFace* iface ) {
151  return SmartIF<IFace>{ iface };
152 }
153 
154 #endif // GAUDI_SMARTIF_H
SmartIF::operator*
TYPE & operator*() const
Dereference operator.
Definition: SmartIF.h:84
SmartIF::reset
void reset(OTHER *ptr)
Set the internal pointer to the passed one disposing of the old one.
Definition: SmartIF.h:105
SmartIF::operator=
SmartIF & operator=(IInterface *ptr)
Assignment operator from IInterface pointer.
Definition: SmartIF.h:128
SmartIF::~SmartIF
~SmartIF()
Standard Destructor.
Definition: SmartIF.h:68
SmartIF::reset
void reset(TYPE *ptr=nullptr)
Set the internal pointer to the passed one disposing of the old one.
Definition: SmartIF.h:96
SmartIF::operator=
SmartIF & operator=(SmartIF &&rhs)
Move assignement.
Definition: SmartIF.h:54
SmartIF::operator=
SmartIF & operator=(const SmartIF< T > &rhs)
Assignment operator from a different SmartIF.
Definition: SmartIF.h:140
SmartIF::SmartIF
SmartIF(SmartIF &&rhs)
Move constructor.
Definition: SmartIF.h:52
SmartIF::SmartIF
SmartIF(OTHER *ptr)
Standard constructor from any (IInterface-derived) pointer.
Definition: SmartIF.h:44
SmartIF::pRef
TYPE *& pRef()
Get reference to the pointer.
Definition: SmartIF.h:89
SmartIF::isValid
bool isValid() const
Allow for check if smart pointer is valid.
Definition: SmartIF.h:72
SmartIF::SmartIF
SmartIF(const SmartIF &rhs)
Copy constructor.
Definition: SmartIF.h:48
IInterface.h
SmartIF::m_interface
TYPE * m_interface
Pointer to the instance.
Definition: SmartIF.h:32
SmartIF
Definition: IConverter.h:25
SmartIF::SmartIF
SmartIF(TYPE *ptr)
Standard constructor from pointer.
Definition: SmartIF.h:39
SmartIF::as
SmartIF< IFace > as() const
return a new SmartIF instance to another interface
Definition: SmartIF.h:117
make_SmartIF
SmartIF< IFace > make_SmartIF(IFace *iface)
Definition: SmartIF.h:150
SmartIF::get
TYPE * get() const
Get interface pointer.
Definition: SmartIF.h:86
SmartIF::operator->
TYPE * operator->() const
Dereference operator.
Definition: SmartIF.h:82
SmartIF::operator!
bool operator!() const
Definition: SmartIF.h:75
IInterface
Definition: IInterface.h:239
SmartIF::SmartIF
SmartIF()=default
Default constructor.
SmartIF::SmartIF
SmartIF(const SmartIF< T > &rhs)
Constructor from another SmartIF, with a different type.
Definition: SmartIF.h:64
SmartIF::operator=
SmartIF & operator=(const SmartIF &rhs)
Assignment operator.
Definition: SmartIF.h:133