summaryrefslogtreecommitdiffstats
path: root/ComLightLib/server/ObjectRoot.hpp
blob: 1cc8f4dfe29b22e9a21ee57c0ca356b6f94b1a1a (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
#pragma once
#include "RefCounter.hpp"
#include "../comLightCommon.h"
#include "../utils/typeTraits.hpp"

namespace ComLight
{
	// Base class of objects, implements reference counting, also a few lifetime methods.
	// The template argument is the interface you want clients to get when they ask for IID_IUnknown. By convention, that pointer defines object's identity.
	template<class I>
	class ObjectRoot : public RefCounter, public I
	{
	protected:

		inline HRESULT internalFinalConstruct()
		{
			return S_FALSE;
		}

		inline HRESULT FinalConstruct()
		{
			return S_FALSE;
		}

		inline void FinalRelease() { }

		IUnknown* getUnknown()
		{
			static_assert( details::pointersAssignable<IUnknown, I>(), "The interface doesn't derive from IUnknown" );
			return static_cast<I*>( this );
		}

		bool queryExtraInterfaces( REFIID riid, void **ppvObject ) const
		{
			return false;
		}

		// Implement query interface with 2 entries, IUnknown and I.
		bool implQueryInterface( REFIID riid, void** ppvObject )
		{
			if( riid == I::iid() || riid == IUnknown::iid() )
			{
				I* const result = this;
				result->AddRef();
				*ppvObject = result;
				return true;
			}
			return false;
		}
	};
}