The Gaudi Framework  master (ff829712)
Loading...
Searching...
No Matches
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
14#include <memory>
15
27template <class TYPE>
28class SmartIF {
29private:
31 TYPE* m_interface = nullptr;
32
33public:
34 inline SmartIF() = default;
36 inline SmartIF( TYPE* ptr ) : m_interface( ptr ) {
37 if ( m_interface ) m_interface->addRef();
38 }
39
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 }
94
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>
111 return SmartIF<IFace>{ *this };
112 }
113
114 // ---------- Special hacks ----------
121 inline SmartIF& operator=( IInterface* ptr ) {
122 reset( ptr );
123 return *this;
124 }
125
126 inline SmartIF& operator=( const SmartIF& rhs ) {
127 reset( rhs.get() );
128 return *this;
129 }
130
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
142template <typename IFace>
144 return SmartIF<IFace>{ iface };
145}
SmartIF< IFace > make_SmartIF(IFace *iface)
Definition SmartIF.h:143
Definition of the basic interface.
Definition IInterface.h:225
Small smart pointer class with automatic reference counting for IInterface.
Definition SmartIF.h:28
SmartIF(TYPE *ptr)
Standard constructor from pointer.
Definition SmartIF.h:36
Gaudi::Interfaces::IParticlePropertySvc * get() const
Definition SmartIF.h:82
SmartIF(SmartIF &&rhs)
Definition SmartIF.h:47
~SmartIF()
Definition SmartIF.h:61
bool isValid() const
Allow for check if smart pointer is valid.
Definition SmartIF.h:69
SmartIF()=default
SmartIF & operator=(SmartIF &&rhs)
Definition SmartIF.h:48
SmartIF & operator=(const SmartIF< T > &rhs)
Assignment operator from a different SmartIF.
Definition SmartIF.h:133
SmartIF(std::unique_ptr< T > &&rhs)
Definition SmartIF.h:64
TYPE & operator*() const
Dereference operator.
Definition SmartIF.h:80
SmartIF< IFace > as() const
return a new SmartIF instance to another interface
Definition SmartIF.h:110
TYPE * operator->() const
Dereference operator.
Definition SmartIF.h:78
void reset(TYPE *ptr=nullptr)
Set the internal pointer to the passed one disposing of the old one.
Definition SmartIF.h:88
SmartIF & operator=(IInterface *ptr)
Assignment operator from IInterface pointer.
Definition SmartIF.h:121
SmartIF & operator=(const SmartIF &rhs)
Assignment operator.
Definition SmartIF.h:126
SmartIF(const SmartIF< T > &rhs)
Constructor from another SmartIF, with a different type.
Definition SmartIF.h:58
Gaudi::Interfaces::IParticlePropertySvc * m_interface
Definition SmartIF.h:31
void reset(OTHER *ptr)
Set the internal pointer to the passed one disposing of the old one.
Definition SmartIF.h:97
SmartIF(const SmartIF &rhs)
Definition SmartIF.h:44
SmartIF(OTHER *ptr)
Standard constructor from any (IInterface-derived) pointer.
Definition SmartIF.h:41
bool operator!() const
Definition SmartIF.h:72