Extra2D/include/assets/handle.h

54 lines
1.3 KiB
C
Raw Permalink Normal View History

#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()
);
}
};
}