Archived
1
Fork 0
This repository has been archived on 2025-04-12. You can view files and clone it, but cannot push or open issues or pull requests.
prism/engine/asset/include/assetptr.hpp
2021-10-14 08:51:58 -04:00

76 lines
1.3 KiB
C++

#pragma once
#include <memory>
#include <string>
struct ReferenceBlock {
uint64_t references = 0;
};
class Asset {
public:
std::string path;
};
template<class T>
struct AssetPtr {
AssetPtr() = default;
AssetPtr(T* ptr, ReferenceBlock* block) : handle(ptr), block(block) {
block->references++;
}
AssetPtr(const AssetPtr &rhs) {
handle = rhs.handle;
block = rhs.block;
if(block != nullptr)
block->references++;
}
AssetPtr& operator=(const AssetPtr& rhs) {
handle = rhs.handle;
block = rhs.block;
if(block != nullptr)
block->references++;
return *this;
}
~AssetPtr() {
if(block != nullptr)
block->references--;
}
void clear() {
if(block != nullptr)
block->references--;
block = nullptr;
handle = nullptr;
}
T* handle = nullptr;
ReferenceBlock* block = nullptr;
explicit operator bool() const{
return handle != nullptr;
}
T* operator->() {
return handle;
}
T* operator->() const {
return handle;
}
T* operator*() {
return handle;
}
T* operator*() const {
return handle;
}
};