1
0
mirror of https://github.com/ncblakely/GiantsTools synced 2024-11-25 07:35:36 +01:00
GiantsTools/Sdk/Include/IComponentContainer.h

61 lines
1.5 KiB
C
Raw Normal View History

2020-08-27 08:10:21 +02:00
#pragma once
#include <IComponent.h>
#include <wrl/client.h>
template<typename T>
using ComPtr = Microsoft::WRL::ComPtr<T>;
// {C942AA9B-C576-4D3F-A54F-B135B500E611}
2020-09-22 07:30:26 +02:00
inline const GUID IID_IComponentContainer = { 0xc942aa9b, 0xc576, 0x4d3f, 0xa5, 0x4f, 0xb1, 0x35, 0xb5, 0x0, 0xe6, 0x11 };
2020-08-27 08:10:21 +02:00
template<typename T>
struct TypedGet
{
template<typename TGet>
ComPtr<TGet> Get()
{
ComPtr<TGet> temp;
ComPtr<IComponent> pComponent = ((T*)this)->Get(__uuidof(TGet));
2020-09-22 07:30:26 +02:00
if (pComponent)
2020-08-27 08:10:21 +02:00
{
2020-09-22 07:30:26 +02:00
HRESULT hr = pComponent.As<TGet>(&temp);
if (FAILED(hr))
2020-08-27 08:10:21 +02:00
{
2020-09-22 07:30:26 +02:00
if (hr == E_NOINTERFACE)
{
throw std::invalid_argument("The interface is not supported.");
}
throw std::invalid_argument(fmt::format("Unknown exception {0:x} querying interface.", hr));
2020-08-27 08:10:21 +02:00
}
2020-09-22 07:30:26 +02:00
pComponent.Detach();
return temp;
2020-08-27 08:10:21 +02:00
}
2020-09-22 07:30:26 +02:00
return ComPtr<TGet>(); // Null
2020-08-27 08:10:21 +02:00
}
};
/// <summary>
/// Container for game components.
/// Facilitates resource acquisition across module boundaries, and interop with .NET code.
/// </summary>
struct IComponentContainer : public TypedGet<IComponentContainer>
{
virtual ~IComponentContainer() { }
virtual void Add(const IID& iid, IComponent* component) = 0;
virtual ComPtr<IComponent> Get(const IID& iid) noexcept = 0;
virtual void Remove(const IID& iid) noexcept = 0;
virtual void ReleaseAll() = 0;
template <typename T>
ComPtr<T> Get()
{
return TypedGet::Get<T>();
}
};
struct DECLSPEC_UUID("{C942AA9B-C576-4D3F-A54F-B135B500E611}") IComponentContainer;