// Copyright (c) Wojciech Figat. All rights reserved.
#pragma once
#include "Engine/Core/Delegate.h"
#include "GPUResource.h"
///
/// GPU Resource container utility object.
///
class FLAXENGINE_API GPUResourcePropertyBase
{
protected:
GPUResource* _resource = nullptr;
public:
NON_COPYABLE(GPUResourcePropertyBase);
GPUResourcePropertyBase() = default;
~GPUResourcePropertyBase();
public:
///
/// Action fired when resource gets released (reference gets cleared bu async tasks should stop execution).
///
Action Released;
protected:
void OnSet(GPUResource* resource);
void OnReleased();
};
///
/// GPU Resource container utility object.
///
template
class GPUResourceProperty : public GPUResourcePropertyBase
{
public:
///
/// Initializes a new instance of the class.
///
GPUResourceProperty()
{
}
///
/// Initializes a new instance of the class.
///
/// The resource.
GPUResourceProperty(T* resource)
{
OnSet(resource);
}
///
/// Initializes a new instance of the class.
///
/// The other value.
GPUResourceProperty(const GPUResourceProperty& other)
{
OnSet(other.Get());
}
///
/// Initializes a new instance of the class.
///
/// The other value.
GPUResourceProperty(GPUResourceProperty&& other)
{
OnSet(other.Get());
other.OnSet(nullptr);
}
GPUResourceProperty& operator=(GPUResourceProperty&& other)
{
if (&other != this)
{
OnSet(other._resource);
other.OnSet(nullptr);
}
return *this;
}
///
/// Finalizes an instance of the class.
///
~GPUResourceProperty()
{
}
public:
FORCE_INLINE bool operator==(T* other) const
{
return Get() == other;
}
FORCE_INLINE bool operator==(const GPUResourceProperty& other) const
{
return Get() == other.Get();
}
FORCE_INLINE GPUResourceProperty& operator=(T& other)
{
OnSet(&other);
return *this;
}
FORCE_INLINE GPUResourceProperty& operator=(T* other)
{
OnSet(other);
return *this;
}
///
/// Implicit conversion to GPU Resource
///
FORCE_INLINE operator T*() const
{
return (T*)_resource;
}
///
/// Implicit conversion to resource
///
FORCE_INLINE operator bool() const
{
return _resource != nullptr;
}
///
/// Implicit conversion to resource
///
FORCE_INLINE T* operator->() const
{
return (T*)_resource;
}
///
/// Gets linked resource
///
FORCE_INLINE T* Get() const
{
return (T*)_resource;
}
public:
///
/// Set resource
///
/// Value to assign
void Set(T* value)
{
OnSet(value);
}
///
/// Unlink resource
///
void Unlink()
{
OnSet(nullptr);
}
};
typedef GPUResourceProperty GPUResourceReference;
typedef GPUResourceProperty GPUTextureReference;
typedef GPUResourceProperty BufferReference;