Skip to content

Commit

Permalink
clean up
Browse files Browse the repository at this point in the history
  • Loading branch information
Ubpa committed Mar 15, 2021
1 parent c27334e commit 4f6e328
Show file tree
Hide file tree
Showing 6 changed files with 27 additions and 8 deletions.
4 changes: 4 additions & 0 deletions doc/todo.md
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,10 @@
- [x] random access
- [x] system traits
- [ ] change filter
- [ ] resource
- [ ] world
- [ ] frame
- [ ] view + iterator

### maybe support in future

Expand Down
8 changes: 2 additions & 6 deletions include/UECS/Schedule.h
Original file line number Diff line number Diff line change
Expand Up @@ -25,10 +25,6 @@ namespace Ubpa::UECS {
// schedule will be clear at the beginning of the next World::Update()
class Schedule {
public:
Schedule() :
rsrc{std::make_unique<std::pmr::unsynchronized_pool_resource>()},
sysFuncAllocator{ rsrc.get() } {}

// Func's argument list:
// [const] World*
// {LastFrame|Latest}<Singleton<Cmpt>>
Expand Down Expand Up @@ -115,8 +111,8 @@ namespace Ubpa::UECS {

std::map<int, std::vector<std::function<void(World*)>>> commandBuffer;

std::unique_ptr<std::pmr::unsynchronized_pool_resource> rsrc;
std::pmr::polymorphic_allocator<SystemFunc> sysFuncAllocator;
std::pmr::unsynchronized_pool_resource sysfuncRsrc;
std::pmr::polymorphic_allocator<SystemFunc> GetSysFuncAllocator();
friend class World;
};
}
Expand Down
1 change: 1 addition & 0 deletions include/UECS/SystemTraits.h
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@ namespace Ubpa::UECS {
// register system's name and get an ID
// if it is already registered, return it's ID directly
Name Register(std::string_view name);
void Unregister(NameID);

// ID must exist
void RegisterOnCreate (NameID, std::function<OnCreate> );
Expand Down
2 changes: 1 addition & 1 deletion include/UECS/details/Schedule.inl
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ namespace Ubpa::UECS {

template<typename... Args>
const SystemFunc* Schedule::Request(Args&&... args) {
SystemFunc* sysFunc = sysFuncAllocator.allocate(1);
SystemFunc* sysFunc = GetSysFuncAllocator().allocate(1);
new(sysFunc)SystemFunc(std::forward<Args>(args)...);
sysFuncs.emplace(sysFunc->GetValue(), sysFunc);
return sysFunc;
Expand Down
7 changes: 6 additions & 1 deletion src/core/Schedule.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,10 @@ using namespace Ubpa;
using namespace Ubpa::UECS;
using namespace std;

std::pmr::polymorphic_allocator<SystemFunc> Schedule::GetSysFuncAllocator() {
return &sysfuncRsrc;
}

Schedule& Schedule::Order(string_view x, string_view y) {
sysFuncOrder.emplace(SystemFunc::GetValue(x), SystemFunc::GetValue(y));
return *this;
Expand All @@ -28,9 +32,10 @@ Schedule& Schedule::EraseNone(string_view sys, TypeID type) {
}

void Schedule::Clear() {
auto alloc = GetSysFuncAllocator();
for (const auto& [hash, sysFunc] : sysFuncs) {
sysFunc->~SystemFunc();
sysFuncAllocator.deallocate(sysFunc, 1);
alloc.deallocate(sysFunc, 1);
}
sysFuncs.clear();
sysFuncOrder.clear();
Expand Down
13 changes: 13 additions & 0 deletions src/core/SystemTraits.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,19 @@ Name SystemTraits::Register(std::string_view name) {
return { newname, id };
}

void SystemTraits::Unregister(NameID ID) {
auto target = names.find(ID);
if (target == names.end())
return;

names.erase(target);
createMap.erase(ID);
activateMap.erase(ID);
updateMap.erase(ID);
deactivateMap.erase(ID);
destroyMap.erase(ID);
}

bool SystemTraits::IsRegistered(NameID ID) const noexcept {
return names.contains(ID);
}
Expand Down

0 comments on commit 4f6e328

Please sign in to comment.