#pragma once #include #include namespace extra2d { /** * @brief 轻量级资源句柄 * * 只包含 32-bit 索引和世代,类似 ECS 中的 Entity ID。 * 不直接管理生命周期,由 AssetStorage 统一管理。 * * @tparam T 资源类型 */ template 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 struct hash> { size_t operator()(const extra2d::Handle& h) const { return std::hash{}( (static_cast(h.index()) << 32) | h.generation() ); } }; }