// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. // SPDX-License-Identifier: Apache-2.0 #pragma once #include /** * This class will mix in a reference count into a class heirarchy rooted with BaseInterface. * @tparam Parent The class to inherit from. Must be a child class of BaseInterface. */ template class RefCountedInterface : public Parent { public: RefCountedInterface(); virtual ~RefCountedInterface(); // From InterfaceServer virtual BaseInterface* GetInterface( Interface_ID id ); // From BaseInterface virtual BaseInterface::LifetimeType LifetimeControl(); virtual BaseInterface* AcquireInterface(); virtual void ReleaseInterface(); virtual void DeleteInterface(); private: volatile long m_refCount; }; template inline RefCountedInterface::RefCountedInterface() : m_refCount( 0 ) {} template inline RefCountedInterface::~RefCountedInterface() {} template inline BaseInterface* RefCountedInterface::GetInterface( Interface_ID id ) { if( Parent::GetID() == id ) return static_cast( this ); return Parent::GetInterface( id ); } template inline BaseInterface::LifetimeType RefCountedInterface::LifetimeControl() { return BaseInterface::wantsRelease; } template inline BaseInterface* RefCountedInterface::AcquireInterface() { InterlockedIncrement( &m_refCount ); return this; } template inline void RefCountedInterface::ReleaseInterface() { if( InterlockedDecrement( &m_refCount ) == 0 ) delete this; } template inline void RefCountedInterface::DeleteInterface() { // Do nothing }