| The Gaudi Framework v27r1

- Main Page
- Related Pages
- Modules
- Namespaces
- Classes
- Files

- File List
- File Members

SmartIF.h
Go to the documentation of this file.
1 #ifndef GAUDI_SMARTIF_H
2 #define GAUDI_SMARTIF_H 1
3
4 // Framework include files
5 #include "GaudiKernel/IInterface.h"
6
18 template <class TYPE> class SmartIF {
19 private:
21 TYPE* m_interface = nullptr;
22 public:
23 // ---------- Construction and destruction ----------
25 inline SmartIF() = default;
27 inline SmartIF(TYPE* ptr): m_interface(ptr) {
28 if (m_interface) m_interface->addRef();
29 }
31 template <class OTHER>
32 inline SmartIF(OTHER* ptr) {
33 if (ptr) reset(ptr);
34 }
36 inline SmartIF(const SmartIF& rhs): m_interface(rhs.get()) {
37 if (m_interface) m_interface->addRef();
38 }
40 inline SmartIF(SmartIF&& rhs) : m_interface( rhs.m_interface ){
41 rhs.m_interface = nullptr;
42 }
44 inline SmartIF& operator=(SmartIF&& rhs) {
45 if (m_interface) m_interface->release();
46 m_interface = rhs.m_interface;
47 rhs.m_interface = nullptr;
48 return *this;
49 }
50
53 template <class T>
54 inline explicit SmartIF(const SmartIF<T>& rhs) {
55 reset(rhs.get());
56 }
58 inline ~SmartIF() { reset(); }
59
60 // ---------- Boolean and comparison methods ----------
62 inline bool isValid() const { return m_interface != nullptr; }
63
64 inline explicit operator bool() const { return isValid(); }
65 inline bool operator!() const { return !isValid(); }
66
67 // ---------- Pointer access methods ----------
70 inline operator TYPE* () const { return m_interface; }
72 inline TYPE* operator->() const { return m_interface; }
74 inline TYPE& operator *() const { return *m_interface; }
76 inline TYPE* get() const { return m_interface; }
77 #if !defined(GAUDI_V22_API) && !defined(NEW_SMARTIF)
78 inline TYPE*& pRef() {
80 return m_interface;
81 }
82 #endif
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<IInterface*>(ptr) == static_cast<IInterface*>(m_interface)) return;
99 if (m_interface) m_interface->release();
100 if (ptr) {
101 ptr->queryInterface(TYPE::interfaceID(), pp_cast<void>(&m_interface)).ignore();
102 } else {
103 m_interface = nullptr;
104 }
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
146 #endif // GAUDI_SMARTIF_H
SmartIF::SmartIF
SmartIF(SmartIF &&rhs)
Move constructor.
Definition: SmartIF.h:40
SmartIF
Small smart pointer class with automatic reference counting for IInterface.
Definition: IConverter.h:14
SmartIF::reset
void reset(OTHER *ptr)
Set the internal pointer to the passed one disposing of the old one.
Definition: SmartIF.h:97
SmartIF::operator*
TYPE & operator*() const
Dereference operator.
Definition: SmartIF.h:74
SmartIF::~SmartIF
~SmartIF()
Standard Destructor.
Definition: SmartIF.h:58
SmartIF::operator->
TYPE * operator->() const
Dereference operator.
Definition: SmartIF.h:72
SmartIF::get
TYPE * get() const
Get interface pointer.
Definition: SmartIF.h:76
SmartIF::SmartIF
SmartIF()=default
Default constructor.
SmartIF::as
SmartIF< IFace > as() const
return a new SmartIF instance to another interface
Definition: SmartIF.h:110
SmartIF::m_interface
TYPE * m_interface
Pointer to the instance.
Definition: SmartIF.h:21
IInterface
Definition of the basic interface.
Definition: IInterface.h:234
SmartIF::SmartIF
SmartIF(const SmartIF &rhs)
Copy constructor.
Definition: SmartIF.h:36
SmartIF::SmartIF
SmartIF(TYPE *ptr)
Standard constructor from pointer.
Definition: SmartIF.h:27
SmartIF::isValid
bool isValid() const
Allow for check if smart pointer is valid.
Definition: SmartIF.h:62
SmartIF::operator=
SmartIF & operator=(SmartIF &&rhs)
Move assignement.
Definition: SmartIF.h:44
SmartIF::SmartIF
SmartIF(OTHER *ptr)
Standard constructor from any (IInterface-derived) pointer.
Definition: SmartIF.h:32
SmartIF::SmartIF
SmartIF(const SmartIF< T > &rhs)
Constructor from another SmartIF, with a different type.
Definition: SmartIF.h:54
make_SmartIF
SmartIF< IFace > make_SmartIF(IFace *iface)
Definition: SmartIF.h:143
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::pRef
TYPE *& pRef()
Get reference to the pointer.
Definition: SmartIF.h:79
IInterface.h
SmartIF::operator!
bool operator!() const
Definition: SmartIF.h:65