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

77 lines
1.2 KiB
C++
Raw Normal View History

2020-08-11 12:07:21 -04:00
#pragma once
#include <memory>
2020-09-20 23:31:03 -04:00
#include <string>
2020-08-11 12:07:21 -04:00
struct ReferenceBlock {
uint64_t references = 0;
};
class Asset {
public:
std::string path;
};
template<class T>
struct AssetPtr {
AssetPtr() {}
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;
operator bool() const{
return handle != nullptr;
}
T* operator->() {
return handle;
}
T* operator->() const {
return handle;
}
T* operator*() {
return handle;
}
T* operator*() const {
return handle;
}
};