ImagingTools Core SDK
TCompPtr.h
1// SPDX-License-Identifier: LGPL-2.1-or-later OR GPL-2.0-or-later OR GPL-3.0-or-later OR LicenseRef-ImtCore-Commercial
2#pragma once
3
4
5// STD includes
6#include <memory>
7
8// ACF includes
9#include <icomp/CComponentBase.h>
10
11
12namespace imtbase
13{
14
15
16template <class InterfaceType>
17class TCompPtr: protected CInterfaceManipBase
18{
19public:
20 typedef CInterfaceManipBase BaseClass;
21
22 TCompPtr();
23
24 explicit TCompPtr(icomp::IComponent* pointer);
25
29 bool IsValid() const;
30
34 const InterfaceType* GetPtr() const;
35
39 InterfaceType* GetPtr();
40
44 void Reset();
45
46 template <class CastedInterfaceType>
47 CastedInterfaceType Cast(const QByteArray& subComponentId = QByteArray()) const
48 {
49 CastedInterfaceType* castedPtr = BaseClass::ExtractInterface<CastedInterfaceType>(m_ptr.data(), subComponentId);
50
51 return castedPtr;
52 }
53
54 InterfaceType& operator*() const;
55 InterfaceType* operator->() const;
56 bool operator==(const TCompPtr& other) const;
57
58 void SetPtr(icomp::IComponent* pointer);
59
60private:
61 QSharedPointer<icomp::IComponent> m_ptr;
62 InterfaceType* m_interfacePtr;
63};
64
65
66template <class InterfaceType>
67inline TCompPtr<InterfaceType>::TCompPtr()
68 :m_interfacePtr(nullptr)
69{
70}
71
72
73template <class InterfaceType>
74inline TCompPtr<InterfaceType>::TCompPtr(icomp::IComponent* pointer)
75{
76 SetPtr(pointer);
77}
78
79
80
81// public methods
82
83template<class InterfaceType>
84inline bool TCompPtr<InterfaceType>::IsValid() const
85{
86 return m_impl.data() != NULL;
87}
88
89
90template<class InterfaceType>
91inline const InterfaceType* TCompPtr<InterfaceType>::GetPtr() const
92{
93 return m_interfacePtr;
94}
95
96
97template<class InterfaceType>
98inline InterfaceType* TCompPtr<InterfaceType>::GetPtr()
99{
100 return m_interfacePtr;
101}
102
103
104template<class InterfaceType>
105inline void TCompPtr<InterfaceType>::Reset()
106{
107 m_impl.reset();
108
109 m_interfacePtr = nullptr;
110}
111
112
113
114template<class InterfaceType>
115inline InterfaceType & TCompPtr<InterfaceType>::operator*() const
116{
117 Q_ASSERT(m_interfacePtr != nullptr);
118
119 return *m_interfacePtr;
120}
121
122
123template<class InterfaceType>
124inline InterfaceType * TCompPtr<InterfaceType>::operator->() const
125{
126 return m_interfacePtr;
127}
128
129
130template <class InterfaceType>
131inline bool TCompPtr<InterfaceType>::operator==(const TCompPtr<InterfaceType>& other) const
132{
133 return m_ptr == other.m_ptr;
134}
135
136
137template <class InterfaceType>
138inline void TCompPtr<InterfaceType>::SetPtr(icomp::IComponent* pointer)
139{
140 InterfaceType* castedPtr = BaseClass::ExtractInterface<InterfaceType>(pointer);
141 if (castedPtr != nullptr){
142 m_ptr.reset(pointer);
143
144 m_interfacePtr = castedPtr;
145 }
146}
147
148
149} // namespace imtbase
150
151