- Split core resource architecture into Core/Asset/ (IResource, ResourceManager, ResourceCache, etc.)
- Moved IO layer into Core/IO/ (IResourceLoader, ResourceFileSystem, etc.)
- Reorganized concrete resource types into Resources/{Texture,Mesh,Material,Shader,AudioClip}/
- Updated all include paths from relative to <XCEngine/...> format
- Fixed circular dependency in Material.h (removed unnecessary ResourceManager.h include)
- Fixed malformed include syntax in ResourceManager.h and AsyncLoader.h
- Fixed glad.h path issues in CMakeLists.txt
105 lines
2.6 KiB
C++
105 lines
2.6 KiB
C++
#pragma once
|
|
|
|
#include "IResource.h"
|
|
#include <type_traits>
|
|
#include <functional>
|
|
|
|
namespace XCEngine {
|
|
namespace Resources {
|
|
|
|
// Forward declaration
|
|
class ResourceManager;
|
|
|
|
template<typename T>
|
|
class ResourceHandle {
|
|
static_assert(std::is_base_of_v<IResource, T>, "T must derive from IResource");
|
|
|
|
public:
|
|
ResourceHandle() = default;
|
|
|
|
explicit ResourceHandle(T* resource)
|
|
: m_resource(resource) {
|
|
if (m_resource) {
|
|
ResourceManager::Get().AddRef(m_resource->GetGUID());
|
|
}
|
|
}
|
|
|
|
ResourceHandle(const ResourceHandle& other)
|
|
: m_resource(other.m_resource) {
|
|
if (m_resource) {
|
|
ResourceManager::Get().AddRef(m_resource->GetGUID());
|
|
}
|
|
}
|
|
|
|
ResourceHandle(ResourceHandle&& other) noexcept
|
|
: m_resource(other.m_resource) {
|
|
other.m_resource = nullptr;
|
|
}
|
|
|
|
~ResourceHandle() {
|
|
Reset();
|
|
}
|
|
|
|
ResourceHandle& operator=(const ResourceHandle& other) {
|
|
if (this != &other) {
|
|
Reset();
|
|
m_resource = other.m_resource;
|
|
if (m_resource) {
|
|
ResourceManager::Get().AddRef(m_resource->GetGUID());
|
|
}
|
|
}
|
|
return *this;
|
|
}
|
|
|
|
ResourceHandle& operator=(ResourceHandle&& other) noexcept {
|
|
if (this != &other) {
|
|
Reset();
|
|
m_resource = other.m_resource;
|
|
other.m_resource = nullptr;
|
|
}
|
|
return *this;
|
|
}
|
|
|
|
T* Get() const { return m_resource; }
|
|
T* operator->() const { return m_resource; }
|
|
T& operator*() const { return *m_resource; }
|
|
|
|
bool IsValid() const { return m_resource != nullptr && m_resource->IsValid(); }
|
|
explicit operator bool() const { return IsValid(); }
|
|
|
|
ResourceGUID GetGUID() const {
|
|
return m_resource ? m_resource->GetGUID() : ResourceGUID(0);
|
|
}
|
|
|
|
ResourceType GetResourceType() const {
|
|
return m_resource ? m_resource->GetType() : ResourceType::Unknown;
|
|
}
|
|
|
|
void Reset() {
|
|
if (m_resource) {
|
|
ResourceManager::Get().Release(m_resource->GetGUID());
|
|
m_resource = nullptr;
|
|
}
|
|
}
|
|
|
|
void Swap(ResourceHandle& other) {
|
|
std::swap(m_resource, other.m_resource);
|
|
}
|
|
|
|
private:
|
|
T* m_resource = nullptr;
|
|
};
|
|
|
|
template<typename T>
|
|
bool operator==(const ResourceHandle<T>& lhs, const ResourceHandle<T>& rhs) {
|
|
return lhs.GetGUID() == rhs.GetGUID();
|
|
}
|
|
|
|
template<typename T>
|
|
bool operator!=(const ResourceHandle<T>& lhs, const ResourceHandle<T>& rhs) {
|
|
return !(lhs == rhs);
|
|
}
|
|
|
|
} // namespace Resources
|
|
} // namespace XCEngine
|