Skip to content

Commit

Permalink
* SingletonLocator, CmptLocator
Browse files Browse the repository at this point in the history
  • Loading branch information
Ubpa committed Aug 8, 2020
1 parent 79a035c commit ff1ca9a
Show file tree
Hide file tree
Showing 10 changed files with 166 additions and 63 deletions.
8 changes: 8 additions & 0 deletions include/UECS/CmptLocator.h
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,12 @@ namespace Ubpa::UECS {

CmptLocator();

template<typename Func>
static CmptLocator Generate();

template<typename Func>
CmptLocator& Combine();

size_t HashCode() const noexcept { return hashCode; }

const std::set<CmptType>& LastFrameCmptTypes() const noexcept { return lastFrameCmptTypes; }
Expand All @@ -34,3 +40,5 @@ namespace Ubpa::UECS {
size_t hashCode;
};
}

#include "detail/CmptsLocator.inl"
14 changes: 12 additions & 2 deletions include/UECS/Schedule.h
Original file line number Diff line number Diff line change
Expand Up @@ -26,11 +26,21 @@ namespace Ubpa::UECS {
class Schedule {
public:
template<typename Func>
const SystemFunc* Register(Func&& func, std::string name, ArchetypeFilter filter = ArchetypeFilter{});
const SystemFunc* Register(
Func&& func,
std::string name,
ArchetypeFilter filter = ArchetypeFilter{},
SingletonLocator singletonLocator = SingletonLocator{}
);

// run-time dynamic function
template<typename Func>
const SystemFunc* Register(Func&& func, std::string name, CmptLocator locator, ArchetypeFilter filter = ArchetypeFilter{});
const SystemFunc* Register(
Func&& func,
std::string name,
CmptLocator locator,
ArchetypeFilter filter = ArchetypeFilter{},
SingletonLocator singletonLocator = SingletonLocator{});

Schedule& LockFilter(std::string_view sys);

Expand Down
8 changes: 8 additions & 0 deletions include/UECS/SingletonLocator.h
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,12 @@ namespace Ubpa::UECS {

SingletonLocator();

template<typename Func>
static SingletonLocator Generate();

template<typename Func>
SingletonLocator& Combine();

const std::set<CmptType>& LastFrameSingletonTypes() const noexcept { return lastFrameSingletonTypes; }
const std::set<CmptType>& WriteSingletonTypes() const noexcept { return writeSingletonTypes; }
const std::set<CmptType>& LatestSingletonTypes() const noexcept { return latestSingletonTypes; }
Expand All @@ -25,3 +31,5 @@ namespace Ubpa::UECS {
std::set<CmptType> singletonTypes;
};
}

#include "detail/SingletonLocator.inl"
5 changes: 4 additions & 1 deletion include/UECS/SystemFunc.h
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,10 @@ namespace Ubpa::UECS {
SingletonLocator singletonLocator;

template<typename Func>
SystemFunc(Func&& func, std::string name, ArchetypeFilter archetypeFilter);
SystemFunc(Func&& func, std::string name, ArchetypeFilter archetypeFilter, SingletonLocator singletonLocator);

template<typename Func>
SystemFunc(Func&& func, std::string name, CmptLocator cmptLocator, ArchetypeFilter archetypeFilter, SingletonLocator singletonLocator);

const std::string& Name() const noexcept { return name; }

Expand Down
35 changes: 35 additions & 0 deletions include/UECS/detail/CmptsLocator.inl
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
#pragma once

#include <UTemplate/Func.h>
#include <UContainer/Algorithm.h>

namespace Ubpa::UECS::detail {
template<typename... Cmpts>
CmptLocator GenerateCmptLocator(TypeList<Cmpts...>) {
if constexpr (sizeof...(Cmpts) > 0) {
constexpr std::array<CmptType, sizeof...(Cmpts)> types{ CmptType::Of<Cmpts>... };
return CmptLocator{ types.data(), types.size() };
}
else
return CmptLocator{};
}
}

namespace Ubpa::UECS {
template<typename Func>
CmptLocator CmptLocator::Generate() {
using ArgList = FuncTraits_ArgList<std::decay_t<Func>>;
using CmptList = Filter_t<ArgList, IsNonSingleton>;
return detail::GenerateCmptLocator(CmptList{});
}

template<typename Func>
CmptLocator& CmptLocator::Combine() {
CmptLocator funcLocator = Generate<Func>();
lastFrameCmptTypes = SetUnion(lastFrameCmptTypes, funcLocator.lastFrameCmptTypes);
writeCmptTypes = SetUnion(writeCmptTypes, funcLocator.writeCmptTypes);
latestCmptTypes = SetUnion(latestCmptTypes, funcLocator.latestCmptTypes);
cmptTypes = SetUnion(cmptTypes, funcLocator.cmptTypes);
return *this;
}
}
30 changes: 26 additions & 4 deletions include/UECS/detail/Schedule.inl
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,35 @@

namespace Ubpa::UECS {
template<typename Func>
const SystemFunc* Schedule::Register(Func&& func, std::string name, ArchetypeFilter filter) {
return Request(std::forward<Func>(func), std::move(name), std::move(filter));
const SystemFunc* Schedule::Register(
Func&& func,
std::string name,
ArchetypeFilter filter,
SingletonLocator singletonLocator)
{
return Request(
std::forward<Func>(func),
std::move(name),
std::move(filter),
std::move(singletonLocator)
);
}

template<typename Func>
const SystemFunc* Schedule::Register(Func&& func, std::string name, CmptLocator locator, ArchetypeFilter filter) {
return Request(std::forward<Func>(func), std::move(name), std::move(locator), std::move(filter));
const SystemFunc* Schedule::Register(
Func&& func,
std::string name,
CmptLocator locator,
ArchetypeFilter filter,
SingletonLocator singletonLocator
) {
return Request(
std::forward<Func>(func),
std::move(name),
std::move(locator),
std::move(filter),
std::move(singletonLocator)
);
}

template<typename... Args>
Expand Down
35 changes: 35 additions & 0 deletions include/UECS/detail/SingletonLocator.inl
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
#pragma once

#include <UTemplate/Func.h>
#include <UContainer/Algorithm.h>

namespace Ubpa::UECS::detail {
template<typename... Singletons>
SingletonLocator GenerateSingletonLocator(TypeList<Singletons...>) {
if constexpr (sizeof...(Singletons) > 0) {
constexpr std::array<CmptType, sizeof...(Singletons)> types{ CmptType::Of<Singletons>... };
return SingletonLocator{ types.data(), types.size() };
}
else
return SingletonLocator{};
}
}

namespace Ubpa::UECS {
template<typename Func>
SingletonLocator SingletonLocator::Generate() {
using ArgList = FuncTraits_ArgList<std::decay_t<Func>>;
using SingletonList = Filter_t<ArgList, IsSingleton>;
return detail::GenerateSingletonLocator(SingletonList{});
}

template<typename Func>
SingletonLocator& SingletonLocator::Combine() {
SingletonLocator funcLocator = Generate<Func>();
lastFrameSingletonTypes = SetUnion(lastFrameSingletonTypes, funcLocator.lastFrameSingletonTypes);
writeSingletonTypes = SetUnion(writeSingletonTypes, funcLocator.writeSingletonTypes);
latestSingletonTypes = SetUnion(latestSingletonTypes, funcLocator.latestSingletonTypes);
singletonTypes = SetUnion(singletonTypes, funcLocator.singletonTypes);
return *this;
}
}
90 changes: 36 additions & 54 deletions include/UECS/detail/SystemFunc.inl
Original file line number Diff line number Diff line change
Expand Up @@ -7,24 +7,24 @@
namespace Ubpa::UECS::detail {
template<typename Func>
auto Pack(Func&& func) noexcept;
template<typename Func>
constexpr CmptLocator GenCmptLocator() noexcept;
template<typename Func>
constexpr SingletonLocator GenSingletonLocator() noexcept;
}

namespace Ubpa::UECS {
template<typename Func>
SystemFunc::SystemFunc(Func&& func, std::string name, ArchetypeFilter archetypeFilter)
:
SystemFunc::SystemFunc(
Func&& func,
std::string name,
ArchetypeFilter archetypeFilter,
SingletonLocator singletonLocator
) :
func{ detail::Pack(std::forward<Func>(func)) },
entityQuery{ std::move(archetypeFilter), detail::GenCmptLocator<decltype(func)>() },
singletonLocator{ detail::GenSingletonLocator<decltype(func)>() },
entityQuery{ std::move(archetypeFilter), CmptLocator::Generate<decltype(func)>() },
singletonLocator{ std::move(singletonLocator.Combine<decltype(func)>()) },
name{ std::move(name) },
hashCode{ HashCode(this->name) }
{
using ArgList = FuncTraits_ArgList<std::decay_t<Func>>;
static_assert(!Contain_v<ArgList, CmptsView> && !Contain_v<ArgList, SingletonsView>);
static_assert(!Contain_v<ArgList, CmptsView>);

if constexpr (Length_v<Filter_t<ArgList, IsNonSingleton>> > 0) {
static_assert(!Contain_v<ArgList, ChunkView>);
Expand All @@ -38,6 +38,26 @@ namespace Ubpa::UECS {
mode = Mode::Job;
}
}

template<typename Func>
SystemFunc::SystemFunc(
Func&& func,
std::string name,
CmptLocator cmptLocator,
ArchetypeFilter archetypeFilter,
SingletonLocator singletonLocator
) :
mode{ Mode::Entity },
func{ detail::Pack(std::forward<Func>(func)) },
entityQuery{ std::move(archetypeFilter), std::move(cmptLocator.Combine<decltype(func)>()) },
singletonLocator{ std::move(singletonLocator.Combine<decltype(func)>()) },
name{ std::move(name) },
hashCode{ HashCode(this->name) }
{
using ArgList = FuncTraits_ArgList<std::decay_t<Func>>;
static_assert(!Contain_v<ArgList, ChunkView>);
assert(!entityQuery.locator.CmptTypes().empty());
}
}

namespace Ubpa::UECS::detail {
Expand All @@ -46,26 +66,26 @@ namespace Ubpa::UECS::detail {

template<typename... DecayedArgs, typename... Singletons, typename... NonSingletons>
struct Packer<TypeList<DecayedArgs...>, TypeList<Singletons...>, TypeList<NonSingletons...>> {
using SortedSingletonList = TypeList<Singletons...>; // sorted
using SortedNonSingletonList = TypeList<NonSingletons...>; // sorted
using SingletonList = TypeList<Singletons...>; // sorted
using NonSingletonList = TypeList<NonSingletons...>; // sorted
template<typename Func>
static auto run(Func&& func) noexcept {
return [func = std::forward<Func>(func)](
World* w,
SingletonsView singletonsView,
SingletonsView singletons,
Entity e,
size_t entityIndexInQuery,
CmptsView cmptsView,
CmptsView cmpts,
ChunkView chunkView)
{
auto args = std::tuple{
w,
reinterpret_cast<Singletons*>(singletonsView.Singletons()[Find_v<SortedSingletonList, Singletons>].Ptr())...,
reinterpret_cast<Singletons*>(singletons.Singletons()[Find_v<SingletonList, Singletons>].Ptr())...,
e,
entityIndexInQuery,
cmptsView,
cmpts,
chunkView,
reinterpret_cast<NonSingletons*>(cmptsView.Components()[Find_v<SortedNonSingletonList, NonSingletons>].Ptr())...
reinterpret_cast<NonSingletons*>(cmpts.Components()[Find_v<NonSingletonList, NonSingletons>].Ptr())...
};
func(std::get<DecayedArgs>(args)...);
};
Expand All @@ -92,42 +112,4 @@ namespace Ubpa::UECS::detail {

return Packer<DecayedArgList, SortedSingletonList, SortedNonSingletonList>::run(std::forward<Func>(func));
}

// ====================

template<typename... Cmpts>
constexpr CmptLocator GenCmptLocator(TypeList<Cmpts...>) noexcept {
if constexpr (sizeof...(Cmpts) > 0) {
constexpr std::array<CmptType, sizeof...(Cmpts)> types{ CmptType::Of<Cmpts>... };
return CmptLocator{ types.data(), types.size() };
}
else
return CmptLocator{};
}

template<typename Func>
constexpr CmptLocator GenCmptLocator() noexcept {
using ArgList = FuncTraits_ArgList<std::decay_t<Func>>;
using CmptList = Filter_t<ArgList, IsNonSingleton>;
return GenCmptLocator(CmptList{});
}

// ====================

template<typename... Singletons>
constexpr SingletonLocator GenSingletonLocator(TypeList<Singletons...>) noexcept {
if constexpr (sizeof...(Singletons) > 0) {
constexpr std::array<CmptType, sizeof...(Singletons)> types{ CmptType::Of<Singletons>... };
return SingletonLocator{ types.data(), types.size() };
}
else
return SingletonLocator{};
}

template<typename Func>
constexpr SingletonLocator GenSingletonLocator() noexcept {
using ArgList = FuncTraits_ArgList<std::decay_t<Func>>;
using SingletonList = Filter_t<ArgList, IsSingleton>;
return GenSingletonLocator(SingletonList{});
}
}
2 changes: 1 addition & 1 deletion src/test/08_job/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ class MySystem : public System {
);
schedule.Order("system function", "job");

size_t num = GetWorld()->entityMngr.EntityNum(f->query);
size_t num = GetWorld()->entityMngr.EntityNum(f->entityQuery);
buffer->resize(num);
}
};
Expand Down
2 changes: 1 addition & 1 deletion src/test/09_idx_in_query/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ class MySystem : public System {
}, "print flag"
);
schedule.Order("set flag", "print flag");
size_t num = GetWorld()->entityMngr.EntityNum(f->query);
size_t num = GetWorld()->entityMngr.EntityNum(f->entityQuery);
flags->insert(flags->begin(), num, false);
}
};
Expand Down

0 comments on commit ff1ca9a

Please sign in to comment.