54 lines
1.3 KiB
C
54 lines
1.3 KiB
C
|
|
#pragma once
|
|||
|
|
|
|||
|
|
#include <cstdint>
|
|||
|
|
#include <functional>
|
|||
|
|
|
|||
|
|
namespace extra2d {
|
|||
|
|
|
|||
|
|
/**
|
|||
|
|
* @brief 轻量级资源句柄
|
|||
|
|
*
|
|||
|
|
* 只包含 32-bit 索引和世代,类似 ECS 中的 Entity ID。
|
|||
|
|
* 不直接管理生命周期,由 AssetStorage 统一管理。
|
|||
|
|
*
|
|||
|
|
* @tparam T 资源类型
|
|||
|
|
*/
|
|||
|
|
template<typename T>
|
|||
|
|
class Handle {
|
|||
|
|
public:
|
|||
|
|
using Index = uint32_t;
|
|||
|
|
using Generation = uint32_t;
|
|||
|
|
|
|||
|
|
Handle() : index_(0), generation_(0) {}
|
|||
|
|
Handle(Index index, Generation generation)
|
|||
|
|
: index_(index), generation_(generation) {}
|
|||
|
|
|
|||
|
|
bool isValid() const { return generation_ != 0; }
|
|||
|
|
Index index() const { return index_; }
|
|||
|
|
Generation generation() const { return generation_; }
|
|||
|
|
|
|||
|
|
bool operator==(const Handle& other) const {
|
|||
|
|
return index_ == other.index_ && generation_ == other.generation_;
|
|||
|
|
}
|
|||
|
|
bool operator!=(const Handle& other) const { return !(*this == other); }
|
|||
|
|
|
|||
|
|
static Handle invalid() { return Handle(); }
|
|||
|
|
|
|||
|
|
private:
|
|||
|
|
Index index_;
|
|||
|
|
Generation generation_;
|
|||
|
|
};
|
|||
|
|
|
|||
|
|
} // namespace extra2d
|
|||
|
|
|
|||
|
|
namespace std {
|
|||
|
|
template<typename T>
|
|||
|
|
struct hash<extra2d::Handle<T>> {
|
|||
|
|
size_t operator()(const extra2d::Handle<T>& h) const {
|
|||
|
|
return std::hash<uint64_t>{}(
|
|||
|
|
(static_cast<uint64_t>(h.index()) << 32) | h.generation()
|
|||
|
|
);
|
|||
|
|
}
|
|||
|
|
};
|
|||
|
|
}
|