The Gaudi Framework  master (d98a2936)
All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Modules Pages
SmartIF.h
Go to the documentation of this file.
1 /***********************************************************************************\
2 * (c) Copyright 1998-2025 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 #pragma once
12 
13 #include <GaudiKernel/IInterface.h>
14 #include <memory>
15 
27 template <class TYPE>
28 class SmartIF {
29 private:
31  TYPE* m_interface = nullptr;
32 
33 public:
34  inline SmartIF() = default;
36  inline SmartIF( TYPE* ptr ) : m_interface( ptr ) {
37  if ( m_interface ) m_interface->addRef();
38  }
40  template <class OTHER>
41  inline SmartIF( OTHER* ptr ) {
42  if ( ptr ) reset( ptr );
43  }
44  inline SmartIF( const SmartIF& rhs ) : m_interface( rhs.get() ) {
45  if ( m_interface ) m_interface->addRef();
46  }
47  inline SmartIF( SmartIF&& rhs ) : m_interface( rhs.m_interface ) { rhs.m_interface = nullptr; }
48  inline SmartIF& operator=( SmartIF&& rhs ) {
49  if ( m_interface ) m_interface->release();
50  m_interface = rhs.m_interface;
51  rhs.m_interface = nullptr;
52  return *this;
53  }
54 
57  template <class T>
58  inline explicit SmartIF( const SmartIF<T>& rhs ) {
59  reset( rhs.get() );
60  }
61  inline ~SmartIF() { reset(); }
62 
63  template <class T>
64  inline SmartIF( std::unique_ptr<T>&& rhs ) {
65  reset( rhs.release() );
66  }
67 
69  inline bool isValid() const { return m_interface != nullptr; }
70 
71  inline explicit operator bool() const { return isValid(); }
72  inline bool operator!() const { return !isValid(); }
73 
76  inline operator TYPE*() const { return m_interface; }
78  inline TYPE* operator->() const { return m_interface; }
80  inline TYPE& operator*() const { return *m_interface; }
82  inline TYPE* get() const { return m_interface; }
83 
84  // ---------- Cast methods ----------
88  inline void reset( TYPE* ptr = nullptr ) {
89  if ( ptr == m_interface ) return;
90  if ( m_interface ) m_interface->release();
91  m_interface = ptr;
92  if ( m_interface ) m_interface->addRef();
93  }
96  template <class OTHER>
97  inline void reset( OTHER* ptr ) {
98  if ( static_cast<const IInterface*>( ptr ) == static_cast<const IInterface*>( m_interface ) ) return;
99  if ( m_interface ) m_interface->release();
100  if ( ptr ) {
101  m_interface = ptr->template cast<TYPE>();
102  if ( m_interface ) m_interface->addRef();
103  } else {
104  m_interface = nullptr;
105  }
106  }
107 
109  template <typename IFace>
110  SmartIF<IFace> as() const {
111  return SmartIF<IFace>{ *this };
112  }
113 
114  // ---------- Special hacks ----------
121  inline SmartIF& operator=( IInterface* ptr ) {
122  reset( ptr );
123  return *this;
124  }
126  inline SmartIF& operator=( const SmartIF& rhs ) {
127  reset( rhs.get() );
128  return *this;
129  }
132  template <class T>
133  inline SmartIF& operator=( const SmartIF<T>& rhs ) {
134  reset( rhs.get() );
135  return *this;
136  }
137 };
138 
139 // helper function to turn a pointer to an interface into
140 // the corresponding SmartIF -- this avoids having to type
141 // the typename twice, and thus insures consistency
142 template <typename IFace>
143 SmartIF<IFace> make_SmartIF( IFace* iface ) {
144  return SmartIF<IFace>{ iface };
145 }
SmartIF::operator*
TYPE & operator*() const
Dereference operator.
Definition: SmartIF.h:80
SmartIF::reset
void reset(OTHER *ptr)
Set the internal pointer to the passed one disposing of the old one.
Definition: SmartIF.h:97
SmartIF::SmartIF
SmartIF(std::unique_ptr< T > &&rhs)
Definition: SmartIF.h:64
SmartIF::operator=
SmartIF & operator=(IInterface *ptr)
Assignment operator from IInterface pointer.
Definition: SmartIF.h:121
SmartIF::~SmartIF
~SmartIF()
Definition: SmartIF.h:61
SmartIF::reset
void reset(TYPE *ptr=nullptr)
Set the internal pointer to the passed one disposing of the old one.
Definition: SmartIF.h:88
SmartIF::operator=
SmartIF & operator=(SmartIF &&rhs)
Definition: SmartIF.h:48
SmartIF::operator=
SmartIF & operator=(const SmartIF< T > &rhs)
Assignment operator from a different SmartIF.
Definition: SmartIF.h:133
SmartIF::SmartIF
SmartIF(SmartIF &&rhs)
Definition: SmartIF.h:47
SmartIF::SmartIF
SmartIF(OTHER *ptr)
Standard constructor from any (IInterface-derived) pointer.
Definition: SmartIF.h:41
SmartIF::isValid
bool isValid() const
Allow for check if smart pointer is valid.
Definition: SmartIF.h:69
SmartIF::SmartIF
SmartIF(const SmartIF &rhs)
Definition: SmartIF.h:44
IInterface.h
SmartIF::m_interface
TYPE * m_interface
Pointer to the instance.
Definition: SmartIF.h:31
SmartIF
Definition: IConverter.h:22
SmartIF::SmartIF
SmartIF(TYPE *ptr)
Standard constructor from pointer.
Definition: SmartIF.h:36
SmartIF::as
SmartIF< IFace > as() const
return a new SmartIF instance to another interface
Definition: SmartIF.h:110
make_SmartIF
SmartIF< IFace > make_SmartIF(IFace *iface)
Definition: SmartIF.h:143
SmartIF::get
TYPE * get() const
Get interface pointer.
Definition: SmartIF.h:82
SmartIF::operator->
TYPE * operator->() const
Dereference operator.
Definition: SmartIF.h:78
SmartIF::operator!
bool operator!() const
Definition: SmartIF.h:72
IInterface
Definition: IInterface.h:225
SmartIF::SmartIF
SmartIF()=default
SmartIF::SmartIF
SmartIF(const SmartIF< T > &rhs)
Constructor from another SmartIF, with a different type.
Definition: SmartIF.h:58
SmartIF::operator=
SmartIF & operator=(const SmartIF &rhs)
Assignment operator.
Definition: SmartIF.h:126