Skip to content

Commit

Permalink
basic singleton
Browse files Browse the repository at this point in the history
  • Loading branch information
Ubpa committed Aug 8, 2020
1 parent 72ab62c commit 79a035c
Show file tree
Hide file tree
Showing 40 changed files with 697 additions and 385 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.10.8)
project(UECS VERSION 0.11.0)
message(STATUS "[Project] ${PROJECT_NAME}")

include(cmake/InitUCMake.cmake)
Expand Down
18 changes: 9 additions & 9 deletions include/UECS/EntityFilter.h → include/UECS/ArchetypeFilter.h
Original file line number Diff line number Diff line change
Expand Up @@ -8,21 +8,21 @@

namespace Ubpa::UECS {
// filter Archetype with All, Any and None
class EntityFilter {
class ArchetypeFilter {
public:
EntityFilter();
ArchetypeFilter();

template<typename... AllCmpts, typename... AnyCmpts, typename... NoneCmpts>
EntityFilter(TypeList<AllCmpts...> allList, TypeList<AnyCmpts...> anyList, TypeList<NoneCmpts...> noneList);
ArchetypeFilter(TypeList<AllCmpts...> allList, TypeList<AnyCmpts...> anyList, TypeList<NoneCmpts...> noneList);

template<typename... AllCmpts>
static EntityFilter CreateAll() { return { TypeList<AllCmpts...>, TypeList<>{}, TypeList<>{} }; }
static ArchetypeFilter CreateAll() { return { TypeList<AllCmpts...>, TypeList<>{}, TypeList<>{} }; }
template<typename... AnyCmpts>
static EntityFilter CreateAny() { return { TypeList<>{}, TypeList<AnyCmpts...>, TypeList<>{} }; }
static ArchetypeFilter CreateAny() { return { TypeList<>{}, TypeList<AnyCmpts...>, TypeList<>{} }; }
template<typename... NoneCmpts>
static EntityFilter CreateNone() { return { TypeList<>{}, TypeList<>{}, TypeList<NoneCmpts...> }; }
static ArchetypeFilter CreateNone() { return { TypeList<>{}, TypeList<>{}, TypeList<NoneCmpts...> }; }

EntityFilter(
ArchetypeFilter(
std::set<CmptType> allCmptTypes,
std::set<CmptType> anyCmptTypes = {},
std::set<CmptType> noneCmptTypes = {}
Expand Down Expand Up @@ -54,7 +54,7 @@ namespace Ubpa::UECS {
template<typename CmptTypeContainer> void EraseAny(const CmptTypeContainer&);
template<typename CmptTypeContainer> void EraseNone(const CmptTypeContainer&);

bool operator==(const EntityFilter& filter) const noexcept;
bool operator==(const ArchetypeFilter& filter) const noexcept;

private:
size_t GenAllHashCode() const noexcept;
Expand All @@ -74,4 +74,4 @@ namespace Ubpa::UECS {
};
}

#include "detail/EntityFilter.inl"
#include "detail/ArchetypeFilter.inl"
1 change: 1 addition & 0 deletions include/UECS/ChunkView.h
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ namespace Ubpa::UECS {
class Archetype;
struct Chunk;

// you shourld get singleton (registered in locator) by world
class ChunkView {
public:
ChunkView(Archetype* archetype, size_t chunkIdx, Chunk* chunk)
Expand Down
18 changes: 4 additions & 14 deletions include/UECS/EntityLocator.h → include/UECS/CmptLocator.h
Original file line number Diff line number Diff line change
Expand Up @@ -9,17 +9,11 @@
namespace Ubpa::UECS {
// locate components in function's argument list for Archetype
// TODO: combine with a system function's locator
class EntityLocator {
class CmptLocator {
public:
template<typename TaggedCmptList>
EntityLocator(TaggedCmptList);
CmptLocator(const CmptType* types, size_t num);

template<typename... LastFrameCmpts, typename... WriteCmpts, typename... LatestCmpts>
EntityLocator(TypeList<LastFrameCmpts...>, TypeList<WriteCmpts...>, TypeList<LatestCmpts...>);

EntityLocator(const CmptType* types, size_t num);

EntityLocator();
CmptLocator();

size_t HashCode() const noexcept { return hashCode; }

Expand All @@ -28,9 +22,7 @@ namespace Ubpa::UECS {
const std::set<CmptType>& LatestCmptTypes() const noexcept { return latestCmptTypes; }
const std::set<CmptType>& CmptTypes() const noexcept { return cmptTypes; }

AccessMode GetCmptTagMode(CmptType type) const;

bool operator==(const EntityLocator& locator) const noexcept;
bool operator==(const CmptLocator& rhs) const noexcept;
private:
size_t GenHashCode() const noexcept;

Expand All @@ -42,5 +34,3 @@ namespace Ubpa::UECS {
size_t hashCode;
};
}

#include "detail/EntityLocator.inl"
32 changes: 16 additions & 16 deletions include/UECS/CmptPtr.h
Original file line number Diff line number Diff line change
Expand Up @@ -21,22 +21,22 @@ namespace Ubpa::UECS {
template<typename Cmpt>
Cmpt* As() const noexcept { return reinterpret_cast<Cmpt*>(p); }

template<typename Cmpt>
LastFrame<Cmpt> AsLastFrame() const noexcept {
assert(type.GetAccessMode() == AccessMode::LAST_FRAME);
return p;
}

template<typename Cmpt>
Write<Cmpt> AsWrite() const noexcept {
assert(type.GetAccessMode() == AccessMode::WRITE);
return p;
}

template<typename Cmpt>
Latest<Cmpt> AsLatest() const noexcept {
assert(type.GetAccessMode() == AccessMode::LATEST);
return p;
template<typename Cmpt, AccessMode mode>
auto As() const noexcept {
if constexpr (mode == AccessMode::LAST_FRAME)
return LastFrame<Cmpt>{p};
else if constexpr (mode == AccessMode::WRITE)
return Write<Cmpt>(p);
else if constexpr (mode == AccessMode::LATEST)
return Latest<Cmpt>(p);
else if constexpr (mode == AccessMode::LAST_FRAME_SINGLETON)
return LastFrame<Singleton<Cmpt>>;
else if constexpr (mode == AccessMode::WRITE_SINGLETON)
return Write<Singleton<Cmpt>>;
else if constexpr (mode == AccessMode::LATEST_SINGLETON)
return Latest<Singleton<Cmpt>>;
else
static_assert(false);
}
private:
CmptType type;
Expand Down
161 changes: 145 additions & 16 deletions include/UECS/CmptTag.h
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,45 @@

namespace Ubpa::UECS {

// LastFrame -> Write -> Latest
// read/write tag : LastFrame -> Write -> Latest
// singleton tag : Singleton
// ====
// 1. LastFrame<Cmpt>
// 2. Write<Cmpt> == Cmpt*
// 3. Latest<Cmpt> == const Cmpt*
// 4. LastFrame<Singleton<Cmpt>>
// 5. Write<Singleton<Cmpt>> == Singleton<Cmpt>
// 6. Latest<Singleton<Cmpt>>

enum class AccessMode : size_t {
LAST_FRAME = 0, // 000, LastFrame<Cmpt>
WRITE = 1, // 001, Write<Cmpt> / Cmpt*
LATEST = 2, // 010, Latest<Cmpt> / const Cmpt*
LAST_FRAME_SINGLETON = 4, // 100, LastFrame<Singleton<Cmpt>>
WRITE_SINGLETON = 5, // 101, Write<Singleton<Cmpt>> / Singleton<Cmpt>
LATEST_SINGLETON = 6, // 110, Latest<Singleton<Cmpt>>
};

constexpr bool AccessMode_IsSingleton(AccessMode mode) noexcept {
return (static_cast<size_t>(mode) & 4) != 0;
}

template<typename Cmpt>
class Singleton {
public:
Singleton(Cmpt* cmpt) noexcept : cmpt{ cmpt } {}
Singleton(const Cmpt* cmpt) noexcept : cmpt{ const_cast<Cmpt*>(cmpt) } {}

Cmpt* Get() noexcept { return cmpt; }
const Cmpt* Get() const noexcept { return cmpt; }

operator Cmpt* () noexcept { return cmpt; }
operator const Cmpt* () const noexcept { return cmpt; }

enum class AccessMode {
LAST_FRAME,
WRITE,
LATEST
Cmpt* operator->() noexcept { return cmpt; }
const Cmpt* operator->() const noexcept { return cmpt; }
private:
Cmpt* cmpt;
};

template<typename Cmpt>
Expand All @@ -24,25 +57,86 @@ namespace Ubpa::UECS {
};

template<typename Cmpt>
using Write = Cmpt*;
class LastFrame<Singleton<Cmpt>> {
public:
LastFrame(const Cmpt* cmpt) noexcept : cmpt{ cmpt } {}
const Cmpt* Get() const noexcept { return cmpt; }
operator const Cmpt* () const noexcept { return cmpt; }
const Cmpt* operator->() const noexcept { return cmpt; }
private:
const Cmpt* cmpt;
};

template<typename Cmpt>
class Write {
public:
Write(Cmpt* cmpt) noexcept : cmpt{ cmpt } {}
Cmpt* Get() const noexcept { return cmpt; }
operator Cmpt* () const noexcept { return cmpt; }
Cmpt* operator->() const noexcept { return cmpt; }
private:
Cmpt* cmpt;
};

template<typename Cmpt>
using Latest = const Cmpt*;
class Write<Singleton<Cmpt>> {
public:
Write(Cmpt* cmpt) noexcept : cmpt{ cmpt } {}
Cmpt* Get() const noexcept { return cmpt; }
operator Cmpt* () const noexcept { return cmpt; }
Cmpt* operator->() const noexcept { return cmpt; }
private:
Cmpt* cmpt;
};

// <Cmpt>
template<typename Cmpt>
class Latest {
public:
Latest(const Cmpt* cmpt) noexcept : cmpt{ cmpt } {}
const Cmpt* Get() const noexcept { return cmpt; }
operator const Cmpt* () const noexcept { return cmpt; }
const Cmpt* operator->() const noexcept { return cmpt; }
private:
const Cmpt* cmpt;
};

template<typename Cmpt>
class Latest<Singleton<Cmpt>> {
public:
Latest(const Cmpt* cmpt) noexcept : cmpt{ cmpt } {}
const Cmpt* Get() const noexcept { return cmpt; }
operator const Cmpt* () const noexcept { return cmpt; }
const Cmpt* operator->() const noexcept { return cmpt; }
private:
const Cmpt* cmpt;
};

// <Cmpt> (without read/write and singleton tag)
template<typename TaggedCmpt>
struct RemoveTag;
template<typename TaggedCmpt>
using RemoveTag_t = typename RemoveTag<TaggedCmpt>::type;

// <Cmpt> / Singleton<Cmpt>
template<typename TaggedCmpt>
struct RemoveRWTag;
template<typename TaggedCmpt>
using RemoveRWTag_t = typename RemoveRWTag<TaggedCmpt>::type;

// LastFrame<Cmpt>
// Write<Cmpt> / Cmpt*
// Latest<Cmpt> / const Cmpt*
template<typename TaggedCmpt>
struct RemoveSingletonTag;
template<typename TaggedCmpt>
using RemoveSingletonTag_t = typename RemoveSingletonTag<TaggedCmpt>::type;

// <Cmpt>*
template<typename TaggedCmpt>
struct DecayTag;
template<typename TaggedCmpt>
using DecayTag_t = typename DecayTag<TaggedCmpt>::type;

// TODO: use IsInstance to simplify Is<some-tag>

template<typename TaggedCmpt>
struct IsLastFrame;
template<typename TaggedCmpt>
Expand All @@ -58,19 +152,54 @@ namespace Ubpa::UECS {
template<typename TaggedCmpt>
static constexpr bool IsLatest_v = IsLatest<TaggedCmpt>::value;

template<typename TaggedCmpt>
struct IsLastFrameSingleton;
template<typename TaggedCmpt>
static constexpr bool IsLastFrameSingleton_v = IsLastFrameSingleton<TaggedCmpt>::value;

template<typename TaggedCmpt>
struct IsWriteSingleton;
template<typename TaggedCmpt>
static constexpr bool IsWriteSingleton_v = IsWriteSingleton<TaggedCmpt>::value;

template<typename TaggedCmpt>
struct IsLatestSingleton;
template<typename TaggedCmpt>
static constexpr bool IsLatestSingleton_v = IsLatestSingleton<TaggedCmpt>::value;

template<typename TaggedCmpt>
struct IsSingleton : IValue<bool,
IsLastFrameSingleton_v<TaggedCmpt>
|| IsWriteSingleton_v<TaggedCmpt>
|| IsLatestSingleton_v<TaggedCmpt>
> {};
template<typename TaggedCmpt>
static constexpr bool IsSingleton_v = IsSingleton<TaggedCmpt>::value;

template<typename TaggedCmpt>
struct IsNonSingleton : IValue<bool,
IsLastFrame_v<TaggedCmpt>
|| IsWrite_v<TaggedCmpt>
|| IsLatest_v<TaggedCmpt>
> {};
template<typename TaggedCmpt>
static constexpr bool IsNonSingleton_v = IsNonSingleton<TaggedCmpt>::value;

template<typename T>
struct IsTaggedCmpt : IValue<bool, IsLastFrame_v<T> || IsWrite_v<T> || IsLatest_v<T>> {};
struct IsTaggedCmpt : IValue<bool, IsNonSingleton_v<T> || IsSingleton_v<T>> {};
template<typename T>
static constexpr bool IsTaggedCmpt_v = IsTaggedCmpt<T>::value;

template<typename T>
static constexpr AccessMode AccessModeOf =
IsLastFrame_v<T> ? AccessMode::LAST_FRAME : (
IsWrite_v<T> ? AccessMode::WRITE : (
IsLatest_v<T> ? AccessMode::LATEST
: AccessMode::WRITE // default
)
);
IsWrite_v<T> ? AccessMode::WRITE : (
IsLatest_v<T> ? AccessMode::LATEST : (
IsLastFrameSingleton_v<T> ? AccessMode::LAST_FRAME_SINGLETON : (
IsWriteSingleton_v<T> ? AccessMode::WRITE_SINGLETON : (
IsLatestSingleton_v<T> ? AccessMode::LATEST_SINGLETON :
AccessMode::WRITE // default, TODO : use static_assert
)))));
}

#include "detail/CmptTag.inl"
34 changes: 6 additions & 28 deletions include/UECS/CmptsView.h
Original file line number Diff line number Diff line change
@@ -1,41 +1,19 @@
#pragma once

#include "CmptPtr.h"
#include "CmptType.h"
#include "CmptTag.h"

#include <set>

namespace Ubpa::UECS {
class EntityLocator;

// use CmptsView::Iterator to read CmptPtr
class CmptsView {
public:
CmptsView(const EntityLocator* locator, void** cmpts)
: locator{ locator }, cmpts{ cmpts }{}
CmptsView(const CmptPtr* cmpts, size_t num)
: cmpts{ cmpts }, num{ num } {}

// check AccessMode
CmptPtr GetCmpt(CmptType) const;

template<typename Cmpt>
LastFrame<Cmpt> GetCmptAsLastFrame() const {
return GetCmpt(CmptType::Of<LastFrame<Cmpt>>).AsLastFrame<Cmpt>();
}

template<typename Cmpt>
Write<Cmpt> GetCmptAsWrite() const {
return GetCmpt(CmptType::Of<Write<Cmpt>>).AsWrite<Cmpt>();
}

template<typename Cmpt>
Latest<Cmpt> GetCmptAsLatest() const {
return GetCmpt(CmptType::Of<Latest<Cmpt>>).AsLatest<Cmpt>();
}

void* const* Components() const noexcept { return cmpts; }
const CmptPtr* Components() const noexcept { return cmpts; }
size_t NumberOfComponents() const noexcept { return num; }
private:
const EntityLocator* locator;
void* const* cmpts;
const CmptPtr* cmpts;
size_t num;
};
}
Loading

0 comments on commit 79a035c

Please sign in to comment.