Skip to content

Commit

Permalink
defaut constrcut
Browse files Browse the repository at this point in the history
- Entity
- Cmpt(Access)Type
- Cmpt(Access)Ptr
  • Loading branch information
Ubpa committed Aug 26, 2020
1 parent e6cefe7 commit 1a9c196
Show file tree
Hide file tree
Showing 6 changed files with 20 additions and 9 deletions.
2 changes: 1 addition & 1 deletion CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
cmake_minimum_required(VERSION 3.14 FATAL_ERROR)

project(UECS VERSION 0.12.0)
project(UECS VERSION 0.12.1)
message(STATUS "[Project] ${PROJECT_NAME}")

include(cmake/InitUCMake.cmake)
Expand Down
1 change: 1 addition & 0 deletions doc/todo.md
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
- [x] serialize (`IListner`)
- [x] system base -> `System`
- [x] singleton
- [ ] doxygen

### maybe support in future

Expand Down
6 changes: 4 additions & 2 deletions include/UECS/CmptPtr.h
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,10 @@ namespace Ubpa::UECS {
// CmptType + void*
class CmptPtr {
public:
constexpr CmptPtr(CmptType type, void* p) noexcept : type{ type }, p{ p }{}
constexpr CmptPtr(CmptType type, void* p) noexcept : type{ type }, p{ p } {}
template<typename Cmpt>
constexpr CmptPtr(Cmpt* p) noexcept : type{ CmptType::Of<Cmpt> }, p{ p }{}
constexpr CmptPtr(Cmpt* p) noexcept : type{ CmptType::Of<Cmpt> }, p{ p } {}
constexpr CmptPtr() noexcept : CmptPtr{ Invalid() } {}

constexpr void* Ptr() const noexcept { return p; }

Expand All @@ -35,6 +36,7 @@ namespace Ubpa::UECS {
template<typename TaggedCmpt>
constexpr CmptAccessPtr(TaggedCmpt p) noexcept : accessType{ CmptAccessType::Of<TaggedCmpt> }, p{ CastToVoidPointer(p) } {}
explicit constexpr CmptAccessPtr(CmptPtr p) noexcept : CmptAccessPtr{ p, AccessMode::LATEST } {}
explicit constexpr CmptAccessPtr() noexcept : CmptAccessPtr{ Invalid() } {}

explicit constexpr operator CmptPtr() const noexcept { return { CmptType{accessType}, p }; }

Expand Down
2 changes: 2 additions & 0 deletions include/UECS/CmptType.h
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ namespace Ubpa::UECS {
public:
explicit constexpr CmptType(size_t id) noexcept : hashcode{ id } {}
explicit constexpr CmptType(std::string_view type_name) noexcept : hashcode{ RuntimeTypeID(type_name) } {}
explicit constexpr CmptType() noexcept : CmptType{ Invalid() } {}

template<typename Cmpt, std::enable_if_t<!IsTaggedCmpt_v<Cmpt>, int> = 0>
static constexpr CmptType Of = CmptType{ TypeID<Cmpt> };
Expand Down Expand Up @@ -45,6 +46,7 @@ namespace Ubpa::UECS {
constexpr CmptAccessType(CmptType type, AccessMode mode) noexcept
: type{ type }, mode{ mode } {}
explicit constexpr CmptAccessType(CmptType type) noexcept : CmptAccessType{ type, AccessMode::LATEST } {}
explicit constexpr CmptAccessType() noexcept : CmptAccessType{ Invalid() } {}

template<typename Cmpt, AccessMode mode = AccessMode::LATEST>
static constexpr CmptAccessType Of = CmptAccessType{ CmptType::Of<RemoveTag_t<Cmpt>>, AccessModeOf_default<Cmpt, mode> };
Expand Down
12 changes: 6 additions & 6 deletions include/UECS/Entity.h
Original file line number Diff line number Diff line change
Expand Up @@ -6,20 +6,20 @@ namespace Ubpa::UECS {
// index + version
class Entity {
public:
size_t Idx() const noexcept { return idx; }
size_t Version() const noexcept { return version; }
constexpr Entity(size_t idx, size_t version) noexcept : idx(idx), version(version) {}
constexpr Entity() noexcept : Entity{ Invalid() } {}
constexpr size_t Idx() const noexcept { return idx; }
constexpr size_t Version() const noexcept { return version; }
static constexpr Entity Invalid() noexcept { return { size_t_invalid,size_t_invalid }; }
constexpr bool IsValid() const noexcept { return idx == size_t_invalid; }
bool operator==(const Entity& rhs) const noexcept {
constexpr bool operator==(const Entity& rhs) const noexcept {
return idx == rhs.idx && version == rhs.version;
}
bool operator<(const Entity& rhs) const noexcept {
constexpr bool operator<(const Entity& rhs) const noexcept {
return idx < rhs.idx || (idx == rhs.idx && version < rhs.version);
}
private:
friend class EntityMngr;
friend class Archetype;
constexpr Entity(size_t idx, size_t version) noexcept : idx(idx), version(version) {}
size_t idx;
size_t version;
};
Expand Down
6 changes: 6 additions & 0 deletions include/UECS/EntityMngr.h
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,8 @@ namespace Ubpa::UECS {
// [important]
// - some API with CmptType need RTDCmptTraits to get {size|alignment|lifecycle function} (throw std::logic_error)
// - API with Entity require Entity exist (throw std::invalid_argument)
// [details]
// - when free entries is empty, use new entity entry (version is 0)
class EntityMngr {
public:
RTDCmptTraits cmptTraits;
Expand Down Expand Up @@ -64,7 +66,11 @@ namespace Ubpa::UECS {

void Destroy(Entity);

size_t TotalEntityNum() const noexcept { return entityTable.size() - entityTableFreeEntry.size(); }
size_t EntityNum(const EntityQuery&) const;
// use entry in reverse
const std::vector<size_t>& GetEntityFreeEntries() const noexcept { return entityTableFreeEntry; }
size_t GetEntityVersion(size_t idx) const noexcept { return entityTable.at(idx).version; }

std::tuple<bool, std::vector<CmptAccessPtr>> LocateSingletons(const SingletonLocator&) const;

Expand Down

0 comments on commit 1a9c196

Please sign in to comment.