Skip to content

Commit

Permalink
del namespace Ubpa::UECS::CmptTag
Browse files Browse the repository at this point in the history
  • Loading branch information
Ubpa committed Jul 30, 2020
1 parent 3d379c4 commit 1c51806
Show file tree
Hide file tree
Showing 11 changed files with 79 additions and 80 deletions.
8 changes: 4 additions & 4 deletions doc/API.md
Original file line number Diff line number Diff line change
Expand Up @@ -181,9 +181,9 @@ if `e` is invalid, throw `std::invalid_argument`.
use tag to dstinguish write, read before write and read after write
- `CmptTag::LastFrame<Component>`: read before write
- `CmptTag::Write<Component>`: write, equal to `<Component> *`
- `CmptTag::Latest<Component>`: read after write, equal to `const <Component> *`
- `LastFrame<Component>`: read before write
- `Write<Component>`: write, equal to `<Component> *`
- `Latest<Component>`: read after write, equal to `const <Component> *`
## `EntityLocator`
Expand All @@ -210,7 +210,7 @@ query.filter can be change dynamically by other `<System>` with Schedule
[system function kind] (distinguish by argument list)
- per entity function: `[[const] Entity e, ] [size_t indexInQuery, ] <Tagged_Component>...`
- tagged component: `CmptTag::{LastFrame|Write|Latest}<Component>`
- tagged component: `{LastFrame|Write|Latest}<Component>`
- job: empty argument list
- runtime dynamic function: `const EntityLocator* locator, void** cmpts`
Expand Down
95 changes: 47 additions & 48 deletions include/UECS/CmptTag.h
Original file line number Diff line number Diff line change
Expand Up @@ -3,64 +3,63 @@
#include <UTemplate/Typelist.h>

namespace Ubpa::UECS {
namespace CmptTag {
// LastFrame -> Write -> Latest

enum class Mode {
LAST_FRAME,
WRITE,
LATEST
};
// LastFrame -> Write -> Latest

template<typename Cmpt>
class LastFrame {
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;
};
enum class Mode {
LAST_FRAME,
WRITE,
LATEST
};

template<typename Cmpt>
using Write = Cmpt*;
template<typename Cmpt>
class LastFrame {
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>
using Latest = const Cmpt*;
template<typename Cmpt>
using Write = Cmpt*;

// <Cmpt>
template<typename TaggedCmpt>
struct RemoveTag;
template<typename TaggedCmpt>
using RemoveTag_t = typename RemoveTag<TaggedCmpt>::type;
template<typename Cmpt>
using Latest = const Cmpt*;

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

template<typename TaggedCmpt>
struct IsLastFrame;
template<typename TaggedCmpt>
static constexpr bool IsLastFrame_v = IsLastFrame<TaggedCmpt>::value;
// <Cmpt>*
template<typename TaggedCmpt>
struct DecayTag;
template<typename TaggedCmpt>
using DecayTag_t = typename DecayTag<TaggedCmpt>::type;

template<typename TaggedCmpt>
struct IsWrite;
template<typename TaggedCmpt>
static constexpr bool IsWrite_v = IsWrite<TaggedCmpt>::value;
template<typename TaggedCmpt>
struct IsLastFrame;
template<typename TaggedCmpt>
static constexpr bool IsLastFrame_v = IsLastFrame<TaggedCmpt>::value;

template<typename TaggedCmpt>
struct IsLatest;
template<typename TaggedCmpt>
static constexpr bool IsLatest_v = IsLatest<TaggedCmpt>::value;
template<typename TaggedCmpt>
struct IsWrite;
template<typename TaggedCmpt>
static constexpr bool IsWrite_v = IsWrite<TaggedCmpt>::value;

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

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

#include "detail/CmptTag.inl"
2 changes: 1 addition & 1 deletion include/UECS/EntityLocator.h
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ namespace Ubpa::UECS {
const std::set<CmptType>& LatestCmptTypes() const noexcept { return latestCmptTypes; }
const std::set<CmptType>& CmptTypes() const noexcept { return cmptTypes; }

CmptTag::Mode GetCmptTagMode(CmptType type) const;
Mode GetCmptTagMode(CmptType type) const;

bool operator==(const EntityLocator& locator) const noexcept;
private:
Expand Down
14 changes: 7 additions & 7 deletions include/UECS/RTDCmptsView.h
Original file line number Diff line number Diff line change
Expand Up @@ -17,28 +17,28 @@ namespace Ubpa::UECS {
// for read/write control
class CmptHandle {
public:
CmptHandle(CmptType type, void* cmpt, CmptTag::Mode mode)
CmptHandle(CmptType type, void* cmpt, Mode mode)
: type{ type }, cmpt{ cmpt }, mode{ mode }{}

CmptType GetCmptType() const noexcept { return type; }
CmptTag::Mode GetMode() const noexcept { return mode; }
Mode GetMode() const noexcept { return mode; }

CmptCPtr AsLastFrame() const noexcept {
assert(mode == CmptTag::Mode::LAST_FRAME);
assert(mode == Mode::LAST_FRAME);
return { type, cmpt };
}
CmptPtr AsWrite() const noexcept {
assert(mode == CmptTag::Mode::WRITE);
assert(mode == Mode::WRITE);
return { type, cmpt };
}
CmptCPtr AsLatest() const noexcept {
assert(mode == CmptTag::Mode::LATEST);
assert(mode == Mode::LATEST);
return { type, cmpt };
}
private:
CmptType type;
void* cmpt;
CmptTag::Mode mode;
Mode mode;
};

// forward
Expand Down Expand Up @@ -69,7 +69,7 @@ namespace Ubpa::UECS {
EntityLocator* locator;
std::set<CmptType>::iterator typeIter;
void* const* ptr_cmpt;
mutable CmptHandle handle{ CmptType::Invalid(), nullptr, CmptTag::Mode{} };
mutable CmptHandle handle{ CmptType::Invalid(), nullptr, Mode{} };
};

RTDCmptsView(EntityLocator* locator, void** cmpts)
Expand Down
2 changes: 1 addition & 1 deletion include/UECS/SystemFunc.h
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ namespace Ubpa::UECS {
// query.filter can be change dynamically by other <System> with Schedule
// [system function kind] (distinguish by argument list)
// 1. per entity function: [[const] Entity e, ] [size_t indexInQuery, ] <Tagged_Component>...
// - - tagged component: CmptTag::{LastFrame|Write|Latest}<Component>
// - - tagged component: {LastFrame|Write|Latest}<Component>
// 2. job: empty argument list
// 3. runtime dynamic function: const EntityLocator* locator, void** cmpts
class SystemFunc {
Expand Down
2 changes: 1 addition & 1 deletion include/UECS/detail/CmptTag.inl
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ namespace Ubpa::UECS {
class EntityLocator;
}

namespace Ubpa::UECS::CmptTag {
namespace Ubpa::UECS {
template<typename Cmpt> struct RemoveTag<const Cmpt*> : IType<Cmpt> {};
template<typename Cmpt> struct RemoveTag<Cmpt*> : IType<Cmpt> {};
template<typename Cmpt> struct RemoveTag<LastFrame<Cmpt>> : IType<Cmpt> {};
Expand Down
14 changes: 7 additions & 7 deletions include/UECS/detail/EntityLocator.inl
Original file line number Diff line number Diff line change
Expand Up @@ -9,18 +9,18 @@
namespace Ubpa::UECS {
template<typename TaggedCmptList>
EntityLocator::EntityLocator(TaggedCmptList)
: EntityLocator{ Filter_t<TaggedCmptList, CmptTag::IsLastFrame>{},
Filter_t<TaggedCmptList, CmptTag::IsWrite>{},
Filter_t<TaggedCmptList, CmptTag::IsLatest>{} }
: EntityLocator{ Filter_t<TaggedCmptList, IsLastFrame>{},
Filter_t<TaggedCmptList, IsWrite>{},
Filter_t<TaggedCmptList, IsLatest>{} }
{
}

template<typename... LastFrameCmpts, typename... WriteCmpts, typename... LatestCmpts>
EntityLocator::EntityLocator(TypeList<LastFrameCmpts...>, TypeList<WriteCmpts...>, TypeList<LatestCmpts...>)
: lastFrameCmptTypes{ CmptType::Of<CmptTag::RemoveTag_t<LastFrameCmpts>>... },
writeCmptTypes{ CmptType::Of<CmptTag::RemoveTag_t<WriteCmpts>>... },
latestCmptTypes{ CmptType::Of<CmptTag::RemoveTag_t<LatestCmpts>>... },
cmptTypes{ CmptType::Of<CmptTag::RemoveTag_t<LastFrameCmpts>>..., CmptType::Of<CmptTag::RemoveTag_t<WriteCmpts>>...,CmptType::Of<CmptTag::RemoveTag_t<LatestCmpts>>... },
: lastFrameCmptTypes{ CmptType::Of<RemoveTag_t<LastFrameCmpts>>... },
writeCmptTypes{ CmptType::Of<RemoveTag_t<WriteCmpts>>... },
latestCmptTypes{ CmptType::Of<RemoveTag_t<LatestCmpts>>... },
cmptTypes{ CmptType::Of<RemoveTag_t<LastFrameCmpts>>..., CmptType::Of<RemoveTag_t<WriteCmpts>>...,CmptType::Of<RemoveTag_t<LatestCmpts>>... },
hashCode{ GenHashCode() }
{
}
Expand Down
8 changes: 4 additions & 4 deletions include/UECS/detail/SystemFunc.inl
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ namespace Ubpa::UECS {
func{ detail::System_::Pack(std::forward<Func>(func)) },
name{ std::move(name) },
hashCode{ HashCode(this->name) },
query{ std::move(filter), EntityLocator{Filter_t<ArgList, CmptTag::IsTaggedCmpt>{}} }
query{ std::move(filter), EntityLocator{Filter_t<ArgList, IsTaggedCmpt>{}} }
{
}
}
Expand All @@ -60,12 +60,12 @@ namespace Ubpa::UECS::detail::System_ {
auto Pack(Func&& func) noexcept {
using ArgList = FuncTraits_ArgList<Func>;

using DecayedArgList = Transform_t<ArgList, CmptTag::DecayTag>;
using DecayedArgList = Transform_t<ArgList, DecayTag>;
static_assert(IsSet_v<DecayedArgList>, "detail::System_::Pack: <Func>'s argument types must be a set");

using TaggedCmptList = Filter_t<ArgList, CmptTag::IsTaggedCmpt>;
using TaggedCmptList = Filter_t<ArgList, IsTaggedCmpt>;

using CmptList = Transform_t<TaggedCmptList, CmptTag::RemoveTag>;
using CmptList = Transform_t<TaggedCmptList, RemoveTag>;
using SortedCmptList = QuickSort_t<CmptList, TypeID_Less>;

return Packer<DecayedArgList, SortedCmptList>::run(std::forward<Func>(func));
Expand Down
8 changes: 4 additions & 4 deletions src/core/EntityLocator.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -30,12 +30,12 @@ bool EntityLocator::operator==(const EntityLocator& locator) const noexcept {
&& latestCmptTypes == latestCmptTypes;
}

CmptTag::Mode EntityLocator::GetCmptTagMode(CmptType type) const {
Mode EntityLocator::GetCmptTagMode(CmptType type) const {
assert(cmptTypes.find(type) != cmptTypes.end());
if (lastFrameCmptTypes.find(type) != lastFrameCmptTypes.end())
return CmptTag::Mode::LAST_FRAME;
return Mode::LAST_FRAME;
else if (writeCmptTypes.find(type) != writeCmptTypes.end())
return CmptTag::Mode::WRITE;
return Mode::WRITE;
else // lastestCmptTypes.find(type) != lastestCmptTypes.end())
return CmptTag::Mode::LATEST;
return Mode::LATEST;
}
4 changes: 2 additions & 2 deletions src/test/01_tag/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,8 @@ struct Data {};
struct DataSystem {
static void OnUpdate(Schedule& schedule) {
schedule
.Register([](CmptTag::LastFrame<Data> d) { cout << "lastFrame_sys0" << endl; }, "lastFrame_sys0")
.Register([](CmptTag::LastFrame<Data> d) { cout << "lastFrame_sys1" << endl; }, "lastFrame_sys1")
.Register([](LastFrame<Data> d) { cout << "lastFrame_sys0" << endl; }, "lastFrame_sys0")
.Register([](LastFrame<Data> d) { cout << "lastFrame_sys1" << endl; }, "lastFrame_sys1")
.Register([](Data* d) { cout << "writer_sys0" << endl; }, "writer_sys0")
.Register([](Data* d) { cout << "writer_sys1" << endl; }, "writer_sys1")
.Register([](Data* d) { cout << "writer_sys2" << endl; }, "writer_sys2")
Expand Down
2 changes: 1 addition & 1 deletion src/test/12_framegraph/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ struct MySystem {
);
schedule
.Register(
[](CmptTag::LastFrame<A> a, CmptTag::Write<B> b, CmptTag::Latest<C> c) {},
[](LastFrame<A> a, Write<B> b, Latest<C> c) {},
"System Func",
filter
);
Expand Down

0 comments on commit 1c51806

Please sign in to comment.