11#include <QtCore/QDebug>
38template <
class InterfaceType,
class PolymorphicPo
interImpl>
50 template<
typename Interface = InterfaceType>
53 if constexpr (std::is_same_v<Interface, InterfaceType>){
61 template<
typename Interface = InterfaceType>
62 const Interface*
GetPtr() const noexcept
64 if constexpr (std::is_same_v<Interface, InterfaceType>){
147 InterfaceType*
get() noexcept
155 const InterfaceType*
get() const noexcept
160 explicit operator bool() const noexcept
205 SetPtr(rootPtr, extractInterface);
210 SetPtr(rootPtr, interfacePtr);
272template <
class InterfaceType,
class RootIntefaceType = istd::IPolymorphic>
276 template<
typename U,
typename R>
283 static_assert(std::is_base_of_v<istd::IPolymorphic, RootIntefaceType>,
284 "RootIntefaceType must derive from istd::IPolymorphic");
285 static_assert(std::has_virtual_destructor_v<RootIntefaceType>,
286 "RootIntefaceType must have a virtual destructor");
287 static_assert(std::is_base_of_v<RootIntefaceType, InterfaceType>,
288 "InterfaceType must derive from RootIntefaceType");
300 template <
typename T>
303 BaseClass::m_interfacePtr = ptr.get();
304 BaseClass::m_rootPtr = std::move(ptr);
323 template <
typename DerivedType>
326 static_assert(std::is_base_of_v<InterfaceType, DerivedType>,
"DerivedType must extend InterfaceType");
327 BaseClass::m_rootPtr = std::move(ptr.m_rootPtr);
328 BaseClass::m_interfacePtr = std::exchange(ptr.m_interfacePtr,
nullptr);
337 BaseClass::m_rootPtr = std::move(ptr.m_rootPtr);
338 BaseClass::m_interfacePtr = std::exchange(ptr.m_interfacePtr,
nullptr);
346 template <
typename DerivedType>
349 static_assert(std::is_base_of_v<InterfaceType, DerivedType>,
"DerivedType must extend InterfaceType");
350 static_assert(!std::is_same_v<DerivedType, InterfaceType>,
"Non-templated move assignment operator overload must be used");
352 BaseClass::m_rootPtr = std::move(ptr.m_rootPtr);
353 BaseClass::m_interfacePtr = std::exchange(ptr.m_interfacePtr,
nullptr);
368 BaseClass::m_interfacePtr =
nullptr;
370 return BaseClass::m_rootPtr.release();
386 if (BaseClass::m_rootPtr.
get() == BaseClass::m_interfacePtr){
387 InterfaceType* retVal = BaseClass::m_interfacePtr;
389 BaseClass::m_interfacePtr =
nullptr;
391 BaseClass::m_rootPtr.release();
397 if (BaseClass::m_interfacePtr !=
nullptr && BaseClass::m_rootPtr.
get() ==
nullptr){
398 InterfaceType* retVal = BaseClass::m_interfacePtr;
400 BaseClass::m_interfacePtr =
nullptr;
406 if (BaseClass::m_rootPtr.
get() !=
nullptr && BaseClass::m_interfacePtr !=
nullptr){
408 InterfaceType* castedPtr =
dynamic_cast<InterfaceType*
>(BaseClass::m_rootPtr.get());
409 if (castedPtr !=
nullptr){
418 Q_ASSERT(
false &&
"dynamic_cast failed in PopInterfacePtr - interface pointer type mismatch");
446 *
this = std::move(from);
449 template<
class SourceInterfaceType>
452 InterfaceType* targetPtr =
dynamic_cast<InterfaceType*
>(source.GetPtr());
453 if (targetPtr !=
nullptr){
454 BaseClass::m_rootPtr = std::move(source.GetBasePtr());
455 BaseClass::m_interfacePtr = targetPtr;
462 template<
class SourceInterfaceType>
505template <
class InterfaceType,
class RootIntefaceType = istd::IPolymorphic>
512 static_assert(std::is_base_of_v<istd::IPolymorphic, RootIntefaceType>,
"RootIntefaceType must derive from istd::IPolymorphic");
513 static_assert(std::has_virtual_destructor_v<RootIntefaceType>,
"RootIntefaceType must have a virtual destructor");
514 static_assert(std::is_base_of_v<RootIntefaceType, InterfaceType>,
515 "InterfaceType must derive from RootIntefaceType");
537 BaseClass::m_rootPtr = ptr.m_rootPtr;
538 BaseClass::m_interfacePtr = ptr.m_interfacePtr;
544 template <
typename DerivedType>
547 static_assert(std::is_base_of_v<InterfaceType, DerivedType>,
"DerivedType must extend InterfaceType");
548 BaseClass::m_rootPtr = other.GetBasePtr();
549 BaseClass::m_interfacePtr =
const_cast<DerivedType*
>(other.GetPtr());
552 template<
typename DerivedType>
555 static_assert(std::is_base_of_v<RootIntefaceType, InterfaceType>,
"Here InterfaceType must extend RootIntefaceType");
556 static_assert(std::is_base_of_v<InterfaceType, DerivedType>,
"DerivedType must extend InterfaceType");
557 BaseClass::m_rootPtr = ptr;
558 BaseClass::m_interfacePtr = ptr.get();
561 template<
typename DerivedType>
564 static_assert(std::is_base_of_v<RootIntefaceType, InterfaceType>,
"Here InterfaceType must extend RootIntefaceType");
565 static_assert(std::is_base_of_v<InterfaceType, DerivedType>,
"DerivedType must extend InterfaceType");
566 BaseClass::m_interfacePtr = ptr.get();
567 BaseClass::m_rootPtr = std::move(ptr);
575 BaseClass::m_rootPtr = std::move(ptr.m_rootPtr);
576 BaseClass::m_interfacePtr = std::exchange(ptr.m_interfacePtr,
nullptr);
582 template <
typename DerivedType>
595 BaseClass::m_rootPtr.reset();
596 BaseClass::m_interfacePtr =
nullptr;
605 BaseClass::m_interfacePtr = ptr.m_interfacePtr;
612 template <
typename DerivedType>
615 static_assert(std::is_base_of_v<InterfaceType, DerivedType>,
"DerivedType must extend InterfaceType");
617 BaseClass::m_interfacePtr =
const_cast<DerivedType*
>(ptr.GetPtr());
623 BaseClass::m_rootPtr = std::move(ptr.m_rootPtr);
624 BaseClass::m_interfacePtr = std::exchange(ptr.m_interfacePtr,
nullptr);
631 template<
typename DerivedType>
634 static_assert(std::is_base_of_v<InterfaceType, DerivedType>,
"DerivedType must extend InterfaceType");
636 if (!uniquePtr.IsValid()){
641 BaseClass::m_interfacePtr = uniquePtr.GetPtr();
644 RootIntefaceType* rawRoot = uniquePtr.PopRootPtr();
645 if (rawRoot ==
nullptr){
651 BaseClass::m_rootPtr = std::shared_ptr<RootIntefaceType>(rawRoot);
664 template <
typename OtherInterface>
668 if (!uniquePtr.IsValid()){
673 InterfaceType* interfacePtr =
dynamic_cast<InterfaceType*
>(uniquePtr.GetPtr());
674 if (interfacePtr !=
nullptr){
675 RootIntefaceType* rawRoot = uniquePtr.PopRootPtr();
676 retVal.BaseClass::m_rootPtr = std::shared_ptr<RootIntefaceType>(rawRoot);
677 retVal.BaseClass::m_interfacePtr = interfacePtr;
702 template<
class SourceInterfaceType>
705 if (!source.IsValid()){
710 InterfaceType* targetPtr =
dynamic_cast<InterfaceType*
>(source.GetPtr());
711 if (targetPtr !=
nullptr){
713 RootIntefaceType* rawRoot = source.PopRootPtr();
714 BaseClass::m_rootPtr = std::shared_ptr<RootIntefaceType>(rawRoot);
715 BaseClass::m_interfacePtr = targetPtr;
722 template<
class SourceInterfaceType>
731 template<
class SourceInterfaceType>
734 if (!source.IsValid()){
739 InterfaceType* targetPtr =
dynamic_cast<InterfaceType*
>(source.GetPtr());
740 if (targetPtr !=
nullptr){
741 BaseClass::m_rootPtr = source.GetBasePtr();
742 BaseClass::m_interfacePtr = targetPtr;
Base interface for all used interfaces and implementations.
Base template for polymorphic interface pointers.
bool IsValid() const noexcept
TInterfacePtr(InterfaceType *interfacePtr) noexcept
InterfaceType * operator->() noexcept
Interface * GetPtr() noexcept
TInterfacePtr & operator=(std::nullptr_t) noexcept
PolymorphicPointerImpl RootObjectPtr
const InterfaceType & operator*() const noexcept
void SetPtr(InterfaceType *interfacePtr)
bool operator!=(std::nullptr_t) const noexcept
TInterfacePtr(const TInterfacePtr &)=delete
const Interface * GetPtr() const noexcept
TInterfacePtr & operator=(TInterfacePtr &&ptr) noexcept
const InterfaceType * operator->() const noexcept
const RootObjectPtr & GetBasePtr() const noexcept
InterfaceType & operator*() noexcept
void SetPtr(istd::IPolymorphic *rootPtr, const ExtractInterfaceFunc &extractInterface)
InterfaceType * get() noexcept
TInterfacePtr & operator=(const TInterfacePtr &)=delete
TInterfacePtr(TInterfacePtr &&ptr) noexcept
TInterfacePtr(istd::IPolymorphic *rootPtr, InterfaceType *interfacePtr) noexcept
bool operator==(std::nullptr_t) const noexcept
const InterfaceType * get() const noexcept
std::function< InterfaceType *()> ExtractInterfaceFunc
InterfaceType * m_interfacePtr
TInterfacePtr(std::nullptr_t) noexcept
RootObjectPtr & GetBasePtr() noexcept
void SetPtr(istd::IPolymorphic *rootPtr, InterfaceType *interfacePtr) noexcept
TInterfacePtr(istd::IPolymorphic *rootPtr, const ExtractInterfaceFunc &extractInterface) noexcept
Shared ownership smart pointer for interface types.
TSharedInterfacePtr & operator=(TSharedInterfacePtr &&ptr) noexcept
static TSharedInterfacePtr CreateFromUnique(TUniqueInterfacePtr< OtherInterface > &uniquePtr) noexcept
Create a shared pointer from unique pointer of other type by transferring ownership if dynamic_cast s...
TSharedInterfacePtr & operator=(const TSharedInterfacePtr &ptr) noexcept
Copy assignment operator.
TSharedInterfacePtr< const T > dynamicCast() const noexcept
TSharedInterfacePtr(const std::shared_ptr< DerivedType > &ptr) noexcept
TSharedInterfacePtr() noexcept
TSharedInterfacePtr(TSharedInterfacePtr &&ptr) noexcept
Move constructor.
TSharedInterfacePtr(RootIntefaceType *rootPtr, const ExtractInterfaceFunc &extractInterface) noexcept
TSharedInterfacePtr(std::unique_ptr< DerivedType > &&ptr) noexcept
BaseClass::ExtractInterfaceFunc ExtractInterfaceFunc
TSharedInterfacePtr< T > dynamicCast() noexcept
bool MoveCastedPtr(TUniqueInterfacePtr< SourceInterfaceType > &&source) noexcept
Move-cast from unique: transfer ownership if dynamic_cast succeeds.
bool SetCastedPtr(TSharedInterfacePtr< SourceInterfaceType > &source) noexcept
Set from another shared pointer if dynamic_cast succeeds.
TSharedInterfacePtr(InterfaceType *interfacePtr) noexcept
TSharedInterfacePtr(const TSharedInterfacePtr &ptr) noexcept
Copy constructor.
TInterfacePtr< InterfaceType, std::shared_ptr< RootIntefaceType > > BaseClass
TSharedInterfacePtr & FromUnique(TUniqueInterfacePtr< InterfaceType > &uniquePtr) noexcept
TSharedInterfacePtr(const TSharedInterfacePtr< DerivedType > &other) noexcept
Copy constructor from derived type.
static TSharedInterfacePtr CreateFromUnique(TUniqueInterfacePtr< InterfaceType > &uniquePtr) noexcept
TSharedInterfacePtr & operator=(const TSharedInterfacePtr< DerivedType > &ptr) noexcept
Copy assignment operator from derived type.
static TSharedInterfacePtr CreateFromUnique(TUniqueInterfacePtr< InterfaceType > &&uniquePtr) noexcept
Create a shared pointer from unique of the same type by transferring ownership.
TSharedInterfacePtr(TUniqueInterfacePtr< DerivedType > &&ptr) noexcept
Construct from unique by transferring ownership into shared.
bool MoveCastedPtr(TUniqueInterfacePtr< SourceInterfaceType > &source) noexcept
TSharedInterfacePtr & FromUnique(TUniqueInterfacePtr< DerivedType > &&uniquePtr) noexcept
Convert from unique to shared.
Unique ownership smart pointer for interface types.
TUniqueInterfacePtr(std::unique_ptr< T > &&ptr) noexcept
TInterfacePtr< InterfaceType, std::unique_ptr< RootIntefaceType > > BaseClass
TUniqueInterfacePtr & operator=(std::nullptr_t) noexcept
TUniqueInterfacePtr & operator=(TUniqueInterfacePtr &&ptr) noexcept
Move assignment.
void TakeOver(TUniqueInterfacePtr< InterfaceType > &from) noexcept
Transfer ownership from another unique ptr (take over raw ownership).
bool MoveCastedPtr(TUniqueInterfacePtr< SourceInterfaceType > &&source) noexcept
TUniqueInterfacePtr(const TUniqueInterfacePtr &ptr)=delete
TUniqueInterfacePtr(InterfaceType *interfacePtr) noexcept
bool MoveCastedPtr(TUniqueInterfacePtr< SourceInterfaceType > &source) noexcept
InterfaceType * PopInterfacePtr() noexcept
Intelligent pop of interface pointer.
RootIntefaceType * PopRootPtr() noexcept
Pop the root pointer.
TUniqueInterfacePtr() noexcept
TUniqueInterfacePtr(RootIntefaceType *rootPtr, const ExtractInterfaceFunc &extractInterface) noexcept
BaseClass::ExtractInterfaceFunc ExtractInterfaceFunc
InterfaceType * PopPtr() noexcept
Intelligent pop method - alias for PopInterfacePtr().
TUniqueInterfacePtr & operator=(const TUniqueInterfacePtr &ptr)=delete
TUniqueInterfacePtr(RootIntefaceType *rootPtr, InterfaceType *interfacePtr) noexcept
TUniqueInterfacePtr & operator=(TUniqueInterfacePtr< DerivedType > &&ptr) noexcept
Move assignment from derived type.
TUniqueInterfacePtr(TUniqueInterfacePtr< DerivedType > &&ptr) noexcept
Move constructor from unique pointer for the derived or same type.