Skip to content

Commit

Permalink
move function definitions to cpp files
Browse files Browse the repository at this point in the history
  • Loading branch information
Ubpa committed Dec 30, 2021
1 parent 071a9f1 commit cb09d27
Show file tree
Hide file tree
Showing 30 changed files with 241 additions and 130 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.16.1)
project(UECS VERSION 0.16.2)
message(STATUS "[Project] ${PROJECT_NAME}")

include(cmake/InitUCMake.cmake)
Expand Down
33 changes: 17 additions & 16 deletions include/UECS/Chunk.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,10 @@ namespace Ubpa::UECS {
bool DidOrderChange(std::size_t version) const noexcept;

std::uint64_t GetComponentVersion(TypeID cmptType) const noexcept;
std::uint64_t GetOrderVersion() const noexcept { return GetHead()->order_version; }
std::uint64_t GetOrderVersion() const noexcept;

std::size_t EntityNum() const noexcept { return GetHead()->num_entity; }
std::size_t ComponentNum() const noexcept { return GetHead()->num_component; }
std::size_t EntityNum() const noexcept;
std::size_t ComponentNum() const noexcept;

// if not contains the component, return nullptr
void* GetCmptArray(TypeID cmptType) const noexcept;
Expand All @@ -34,10 +34,10 @@ namespace Ubpa::UECS {
else return {};
}

std::span<Entity> GetEntityArray() const noexcept { return GetCmptArray<Entity>(); }
std::span<Entity> GetEntityArray() const noexcept;

bool Full() const noexcept { return GetHead()->capacity == GetHead()->num_entity; }
bool Empty() const noexcept { return GetHead()->num_entity == 0; }
bool Full() const noexcept;
bool Empty() const noexcept;

bool HasAnyChange(std::span<const TypeID> types, std::uint64_t version) const noexcept;

Expand All @@ -46,10 +46,11 @@ namespace Ubpa::UECS {
// ApplyChanges
std::tuple<Entity*, small_vector<CmptAccessPtr>, small_vector<std::size_t>> Locate(std::span<const AccessTypeID> types);

std::pmr::unsynchronized_pool_resource* GetChunkUnsyncResource() noexcept { return &GetHead()->chunk_unsync_rsrc; }
std::pmr::monotonic_buffer_resource* GetChunkUnsyncFrameResource() noexcept { return (std::pmr::monotonic_buffer_resource*)&GetHead()->chunk_unsync_frame_rsrc; }
std::pmr::unsynchronized_pool_resource* GetChunkUnsyncResource() noexcept;
std::pmr::monotonic_buffer_resource* GetChunkUnsyncFrameResource() noexcept;

template<typename T, typename... Args> T* ChunkUnsyncNewFrameObject(Args&&... args) {
template<typename T, typename... Args>
T* ChunkUnsyncNewFrameObject(Args&&... args) {
auto rsrc = GetChunkUnsyncFrameResource();
auto obj = (T*)rsrc->allocate(sizeof(T), alignof(T));
std::pmr::polymorphic_allocator{ rsrc }.construct(obj, std::forward<Args>(args)...);
Expand Down Expand Up @@ -79,20 +80,20 @@ namespace Ubpa::UECS {
static_assert(sizeof(CmptInfo) == 24);

// sorted by ID
std::span<CmptInfo> GetCmptInfos() noexcept { return { (CmptInfo*)(this + 1), num_component }; }
std::span<const CmptInfo> GetCmptInfos() const noexcept { return const_cast<Head*>(this)->GetCmptInfos(); }

void ForceUpdateVersion(std::uint64_t version);
std::span<CmptInfo> GetCmptInfos() noexcept;
std::span<const CmptInfo> GetCmptInfos() const noexcept;
};

Chunk() noexcept = default;
~Chunk() { GetHead()->~Head(); }
~Chunk();

Head* GetHead() noexcept { return reinterpret_cast<Head*>(data); }
const Head* GetHead() const noexcept { return reinterpret_cast<const Head*>(data); }
Head* GetHead() noexcept;
const Head* GetHead() const noexcept;

std::size_t Erase(std::size_t idx);

void ForceUpdateVersion(std::uint64_t version);

static_assert(ChunkSize > sizeof(Head));
std::uint8_t data[ChunkSize];
};
Expand Down
4 changes: 2 additions & 2 deletions include/UECS/CmptLocator.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -21,9 +21,9 @@ namespace Ubpa::UECS {
template<typename Func>
CmptLocator& Combine();

std::size_t GetValue() const noexcept { return hashCode; }
std::size_t GetValue() const noexcept;

const AccessTypeIDSet& AccessTypeIDs() const noexcept { return cmptTypes; }
const AccessTypeIDSet& AccessTypeIDs() const noexcept;

bool operator==(const CmptLocator& rhs) const noexcept;

Expand Down
6 changes: 3 additions & 3 deletions include/UECS/CmptsView.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,11 @@
namespace Ubpa::UECS {
class CmptsView {
public:
CmptsView() noexcept = default;
CmptsView(std::span<const CmptAccessPtr> cmpts) noexcept : cmpts{ cmpts } {}
CmptsView() noexcept;
CmptsView(std::span<const CmptAccessPtr> cmpts) noexcept;

CmptAccessPtr GetCmpt(AccessTypeID) const noexcept;
std::span<const CmptAccessPtr> Components() const noexcept { return cmpts; }
std::span<const CmptAccessPtr> AccessComponents() const noexcept;
private:
std::span<const CmptAccessPtr> cmpts;
};
Expand Down
30 changes: 11 additions & 19 deletions include/UECS/CommandBuffer.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,32 +6,24 @@
namespace Ubpa::UECS {
class CommandBuffer {
public:
void AddCommand(std::function<void()> command) { commands.push_back(std::move(command)); }
void AddCommandBuffer(CommandBuffer cb) {
commands.reserve(commands.size() + cb.commands.size());
for (auto& cmd : cb.commands)
commands.push_back(std::move(cmd));
}
bool Empty() const noexcept { return commands.empty(); }
void Clear() { commands.clear(); }
void AddCommand(std::function<void()> command);
void AddCommandBuffer(CommandBuffer cb);
bool Empty() const noexcept;
void Clear() noexcept;

auto& GetCommands() noexcept { return commands; }
const auto& GetCommands() const noexcept { return commands; }
std::span<std::function<void()>> GetCommands() noexcept;
std::span<const std::function<void()>> GetCommands() const noexcept;

void Run() {
for (const auto& cmd : commands)
cmd();
commands.clear();
}
void Run();
private:
std::vector<std::function<void()>> commands;
};

class CommandBufferView {
class CommandBufferPtr {
public:
CommandBufferView(CommandBuffer* cb = nullptr) : commandBuffer{ cb } {}
CommandBuffer* GetCommandBuffer() const noexcept { return commandBuffer; }
CommandBuffer* operator->()const noexcept { return commandBuffer; }
CommandBufferPtr(CommandBuffer* cb = nullptr);
CommandBuffer* Get() const noexcept;
CommandBuffer* operator->()const noexcept;
private:
CommandBuffer* commandBuffer;
};
Expand Down
4 changes: 3 additions & 1 deletion include/UECS/Entity.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,9 @@ namespace Ubpa::UECS {
std::uint64_t index;
std::uint64_t version;

static constexpr Entity Invalid() noexcept { return { .index = static_cast<std::uint64_t>(-1), .version = static_cast<std::uint64_t>(-1) }; }
static constexpr Entity Invalid() noexcept
{ return { .index = static_cast<std::uint64_t>(-1), .version = static_cast<std::uint64_t>(-1) }; }

constexpr bool Valid() const noexcept { return index != static_cast<std::uint64_t>(-1); }
constexpr auto operator<=>(const Entity&) const = default;
};
Expand Down
28 changes: 14 additions & 14 deletions include/UECS/EntityMngr.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,8 @@ namespace Ubpa::UECS {
// auto maintain Component's lifecycle ({default|copy|move} constructor, destructor)
// [API]
// - Entity: Create, Instantiate, Destroy, Exist
// - Component: Attach, Emplace, Detach, Have, Get, Components
// - Singleton: IsSingleton, GetSingletonEntity, GetSingleton
// - Component: Attach, Emplace, Detach, Have, Get, AccessComponents
// - Singleton: IsSingleton, GetSingletonEntity, AccessSingleton
// - other: EntityNum, AddCommand
// [important]
// - some API with TypeID need CmptTraits to get {size|alignment|lifecycle function} (throw std::logic_error)
Expand All @@ -44,17 +44,17 @@ namespace Ubpa::UECS {
bool Have(Entity, TypeID) const;

// nullptr if not containts TypeID
CmptAccessPtr GetComponent(Entity, AccessTypeID) const;
CmptAccessPtr WriteComponent(Entity e, TypeID t) const { return GetComponent(e, { t, AccessMode::WRITE }); }
CmptAccessPtr ReadComponent(Entity e, TypeID t) const { return GetComponent(e, { t, AccessMode::LATEST }); }
CmptAccessPtr AccessComponent(Entity, AccessTypeID) const;
CmptAccessPtr WriteComponent(Entity e, TypeID t) const;
CmptAccessPtr ReadComponent(Entity e, TypeID t) const;
template<typename Cmpt>
Cmpt* WriteComponent(Entity e) const { return WriteComponent(e, TypeID_of<Cmpt>).template As<Cmpt, AccessMode::WRITE>(); }
template<typename Cmpt>
const Cmpt* ReadComponent(Entity e) const { return ReadComponent(e, TypeID_of<Cmpt>).template As<Cmpt, AccessMode::LATEST>(); }

std::vector<CmptAccessPtr> Components(Entity, AccessMode) const;
std::vector<CmptAccessPtr> WriteComponents(Entity e) const { return Components(e, AccessMode::WRITE); }
std::vector<CmptAccessPtr> ReadComponents(Entity e) const { return Components(e, AccessMode::LATEST); }
std::vector<CmptAccessPtr> AccessComponents(Entity, AccessMode) const;
std::vector<CmptAccessPtr> WriteComponents(Entity e) const;
std::vector<CmptAccessPtr> ReadComponents(Entity e) const;

// chunk + index in chunk
std::tuple<Chunk*, std::size_t> GetChunk(Entity e) const;
Expand All @@ -63,17 +63,17 @@ namespace Ubpa::UECS {

void Destroy(Entity);

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

bool IsSingleton(TypeID) const;
Entity GetSingletonEntity(TypeID) const;
CmptAccessPtr GetSingleton(AccessTypeID) const;
CmptAccessPtr WriteSingleton(TypeID type) const { return GetSingleton({ type, AccessMode::WRITE }); }
CmptAccessPtr ReadSingleton(TypeID type) const { return GetSingleton({ type, AccessMode::LATEST }); }
CmptAccessPtr AccessSingleton(AccessTypeID) const;
CmptAccessPtr WriteSingleton(TypeID type) const;
CmptAccessPtr ReadSingleton(TypeID type) const;
template<typename Cmpt>
Cmpt* WriteSingleton() const { return WriteSingleton(TypeID_of<Cmpt>).template As<Cmpt, AccessMode::WRITE>(); }
template<typename Cmpt>
Expand Down
20 changes: 4 additions & 16 deletions include/UECS/EntityQuery.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -10,24 +10,12 @@ namespace Ubpa::UECS {
ArchetypeFilter filter;
CmptLocator locator;

std::size_t GetValue() const noexcept { return hash_combine(filter.GetValue(), locator.GetValue()); }
std::size_t GetValue() const noexcept;

bool IsMatch(const small_flat_set<TypeID>& types) const noexcept{
return std::find_if_not(filter.all.begin(), filter.all.end(),
[&](const auto& type) { return types.contains(type); }) == filter.all.end()
&& (filter.any.empty()
|| std::find_if(filter.any.begin(), filter.any.end(),
[&](const auto& type) { return types.contains(type); }) != filter.any.end())
&& std::find_if(filter.none.begin(), filter.none.end(),
[&](const auto& type) { return types.contains(type); }) == filter.none.end()
&& std::find_if_not(locator.AccessTypeIDs().begin(), locator.AccessTypeIDs().end(),
[&](const auto& type) { return types.contains(type); }) == locator.AccessTypeIDs().end();
}

friend bool operator==(const EntityQuery& lhs, const EntityQuery& rhs) noexcept {
return lhs.filter == rhs.filter && lhs.locator == rhs.locator;
}
bool IsMatch(const small_flat_set<TypeID>& types) const noexcept;
};

bool operator==(const EntityQuery& lhs, const EntityQuery& rhs) noexcept;
}

template<>
Expand Down
2 changes: 1 addition & 1 deletion include/UECS/Schedule.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -118,7 +118,7 @@ namespace Ubpa::UECS {
Schedule& AddNone(std::string_view sys, TypeID, int layer = 0);
Schedule& Disable(std::string_view sys, int layer = 0);

World* GetWorld() const noexcept { return world; }
World* GetWorld() const noexcept;

std::string_view RegisterFrameString(std::string_view str);

Expand Down
6 changes: 3 additions & 3 deletions include/UECS/SingletonLocator.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -8,17 +8,17 @@
namespace Ubpa::UECS {
class SingletonLocator {
public:
SingletonLocator(std::set<AccessTypeID> types) : singletonTypes{ std::move(types) } {}
SingletonLocator(std::set<AccessTypeID> types);
SingletonLocator(std::span<const AccessTypeID> types);
SingletonLocator() = default;
SingletonLocator();

template<typename Func>
static SingletonLocator Generate();

template<typename Func>
SingletonLocator& Combine();

const std::set<AccessTypeID>& SingletonTypes() const noexcept { return singletonTypes; }
const std::set<AccessTypeID>& SingletonTypes() const noexcept;

bool HasWriteSingletonType() const noexcept;

Expand Down
2 changes: 1 addition & 1 deletion include/UECS/SingletonsView.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ namespace Ubpa::UECS {
SingletonsView(std::span<const CmptAccessPtr> singletons) noexcept
: singletons{ singletons } {}

CmptAccessPtr GetSingleton(AccessTypeID) const noexcept;
CmptAccessPtr AccessSingleton(AccessTypeID) const noexcept;
std::span<const CmptAccessPtr> Singletons() const noexcept { return singletons; }
private:
std::span<const CmptAccessPtr> singletons;
Expand Down
20 changes: 10 additions & 10 deletions include/UECS/SystemFunc.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -28,11 +28,11 @@ namespace Ubpa::UECS {
// * <tagged-components>: {LastFrame|Write|Latest}<Cmpt>...
// * CmptsView
// * ChunkView
// * CommandBufferView
// * CommandBufferPtr
// 2. Mode::Chunk
// * std::size_t entityBeginIndexInQuery
// * ChunkView (necessary)
// * CommandBufferView
// * CommandBufferPtr
// 3. Mode::Job
// * Write<Singleton<Cmpt>> (only job can write singletons)
class SystemFunc {
Expand Down Expand Up @@ -62,27 +62,27 @@ namespace Ubpa::UECS {
template<typename Func>
SystemFunc(Func&&, std::string_view name, SingletonLocator, RandomAccessor);

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

static constexpr std::size_t GetValue(std::string_view name) noexcept { return string_hash(name); }

std::size_t GetValue() const noexcept { return hashCode; }
std::size_t GetValue() const noexcept;

void operator()(World*, SingletonsView, Entity, std::size_t entityIndexInQuery, CmptsView, CommandBufferView) const;
void operator()(World*, SingletonsView, std::size_t entityBeginIndexInQuery, ChunkView, CommandBufferView) const;
void operator()(World*, SingletonsView, Entity, std::size_t entityIndexInQuery, CmptsView, CommandBufferPtr) const;
void operator()(World*, SingletonsView, std::size_t entityBeginIndexInQuery, ChunkView, CommandBufferPtr) const;
void operator()(World*, SingletonsView) const;

Mode GetMode() const noexcept { return mode; }
bool IsParallel() const noexcept { return isParallel; }
Mode GetMode() const noexcept;
bool IsParallel() const noexcept;

bool operator==(const SystemFunc& sysFunc) const noexcept { return name == sysFunc.name; }
bool operator==(const SystemFunc& sysFunc) const noexcept;
private:
friend class Schedule;
Mode mode;
std::string_view name;
std::size_t hashCode; // after name
bool isParallel;
std::function<void(World*, SingletonsView, Entity, std::size_t, CmptsView, ChunkView, CommandBufferView)> func;
std::function<void(World*, SingletonsView, Entity, std::size_t, CmptsView, ChunkView, CommandBufferPtr)> func;
};
}

Expand Down
10 changes: 5 additions & 5 deletions include/UECS/World.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -147,15 +147,15 @@ namespace Ubpa::UECS {
SingletonLocator = {}
) const;

synchronized_monotonic_buffer_resource* GetSyncFrameResource() { return sync_frame_rsrc.get(); }
std::pmr::monotonic_buffer_resource* GetUnsyncFrameResource() { return unsync_frame_rsrc.get(); }
std::pmr::synchronized_pool_resource* GetSyncResource() { return sync_rsrc.get(); }
std::pmr::unsynchronized_pool_resource* GetUnsyncResource() { return unsync_rsrc.get(); }
synchronized_monotonic_buffer_resource* GetSyncFrameResource();
std::pmr::monotonic_buffer_resource* GetUnsyncFrameResource();
std::pmr::synchronized_pool_resource* GetSyncResource();
std::pmr::unsynchronized_pool_resource* GetUnsyncResource();

template<typename T, typename... Args> T* SyncNewFrameObject(Args&&... args);
template<typename T, typename... Args> T* UnsyncNewFrameObject(Args&&... args);

std::uint64_t Version() const noexcept { return version; }
std::uint64_t Version() const noexcept;
private:
bool inRunningJobGraph{ false };

Expand Down
10 changes: 5 additions & 5 deletions include/UECS/details/SystemFunc.inl
Original file line number Diff line number Diff line change
Expand Up @@ -104,8 +104,8 @@ namespace Ubpa::UECS {
&& !Contain_v<ArgList, std::size_t>
&& !Contain_v<ArgList, CmptsView>
&& !Contain_v<ArgList, ChunkView>
&& !Contain_v<ArgList, CommandBufferView>,
"(Mode::Job) SystemFunc's argument list cann't have Entity, indexInQuery CmptsView, ChunkView or CommandBufferView"
&& !Contain_v<ArgList, CommandBufferPtr>,
"(Mode::Job) SystemFunc's argument list cann't have Entity, indexInQuery CmptsView, ChunkView or CommandBufferPtr"
);
}
}
Expand All @@ -127,7 +127,7 @@ namespace Ubpa::UECS::details {
std::size_t entityIndexInQuery,
CmptsView cmpts,
ChunkView chunkView,
CommandBufferView cbv)
CommandBufferPtr cb)
{
auto args = std::tuple{
w,
Expand All @@ -136,9 +136,9 @@ namespace Ubpa::UECS::details {
e,
entityIndexInQuery,
cmpts,
reinterpret_cast<NonSingletons*>(cmpts.Components()[Find_v<NonSingletonList, NonSingletons>].Ptr())...,
reinterpret_cast<NonSingletons*>(cmpts.AccessComponents()[Find_v<NonSingletonList, NonSingletons>].Ptr())...,
chunkView,
cbv
cb
};
func(std::get<DecayedArgs>(args)...);
};
Expand Down
Loading

0 comments on commit cb09d27

Please sign in to comment.