Skip to content

Commit

Permalink
RTDCmptViewer
Browse files Browse the repository at this point in the history
  • Loading branch information
Ubpa committed May 24, 2020
1 parent 3ec65d8 commit cb45fda
Show file tree
Hide file tree
Showing 9 changed files with 97 additions and 21 deletions.
2 changes: 1 addition & 1 deletion doc/todo.md
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@
- [ ] batch create/instantiate (need benchmark)
- [x] lock `FilterChange`
- [ ] `EntityMngr` `Query`-driven API
- [ ] `RTDCmpts` = `const EntityLocator* locator + void** cmpts`
- [x] `RTDCmptViewer` = `const EntityLocator* locator + void** cmpts`

### maybe deprecate

Expand Down
2 changes: 2 additions & 0 deletions include/UECS/CmptPtr.h
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ namespace Ubpa {
CmptType Type() const noexcept { return type; }
void* Ptr() const noexcept { return p; }

// for static Component
template<typename Cmpt>
Cmpt* As() const noexcept {
assert(type.Is<Cmpt>());
Expand All @@ -35,6 +36,7 @@ namespace Ubpa {
CmptType Type() const noexcept { return type; }
const void* Ptr() const noexcept { return p; }

// for static Component
template<typename Cmpt>
const Cmpt* As() const noexcept {
assert(type.Is<Cmpt>());
Expand Down
2 changes: 2 additions & 0 deletions include/UECS/CmptType.h
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@ namespace Ubpa {
template<typename Cmpt>
static constexpr size_t HashCodeOf() noexcept { return TypeID<Cmpt>; }

static constexpr CmptType Invalid() noexcept { return CmptType{ static_cast<size_t>(-1) }; }

template<typename Cmpt>
bool Is() const noexcept { return hashcode == HashCodeOf<Cmpt>(); }

Expand Down
57 changes: 57 additions & 0 deletions include/UECS/RTDCmptViewer.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
#pragma once

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

#include <set>

namespace Ubpa {
class EntityLocator;
class RTDCmptViewer {
public:
class Iterator /*: public std::iterator<std::forward_iterator_tag, CmptPtr>*/ {
public:
using iterator_category = std::forward_iterator_tag;
using value_type = CmptPtr;
using difference_type = std::ptrdiff_t;
using pointer = CmptPtr*;
using reference = CmptPtr&;

Iterator(std::set<CmptType>::iterator typeIter = std::set<CmptType>::iterator{}, void** ptr_cmpt = nullptr)
: typeIter(typeIter), ptr_cmpt{ ptr_cmpt } {}
bool operator==(const Iterator& rhs) const noexcept {
return ptr_cmpt == rhs.ptr_cmpt;
}
bool operator!=(const Iterator& rhs) const noexcept {
return !this->operator==(rhs);
}
CmptPtr operator*() const noexcept { return cmptptr; }
const CmptPtr* operator->() const noexcept {
cmptptr = { *typeIter, *ptr_cmpt };
return &cmptptr;
}
Iterator& operator++() {
typeIter++;
ptr_cmpt++;
return *this;
}
private:
std::set<CmptType>::iterator typeIter;
void** ptr_cmpt;
mutable CmptPtr cmptptr{ CmptType::Invalid(), nullptr };
};

RTDCmptViewer(EntityLocator* locator, void** cmpts)
: locator{ locator }, cmpts{ cmpts }{}

Iterator begin() const noexcept;
Iterator end() const noexcept;

const std::set<CmptType>& CmptTypes() const noexcept;
void* const* Components() const noexcept { return cmpts; }

private:
EntityLocator* locator;
void** cmpts;
};
}
7 changes: 4 additions & 3 deletions include/UECS/SystemFunc.h
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

#include "EntityQuery.h"
#include "Entity.h"
#include "RTDCmptViewer.h"

#include <functional>

Expand Down Expand Up @@ -33,8 +34,8 @@ namespace Ubpa {

size_t HashCode() const noexcept { return hashCode; }

void operator()(Entity e, size_t entityIndexInQuery, const EntityLocator* locator, void** cmptArr) {
return func(e, entityIndexInQuery, locator, cmptArr);
void operator()(Entity e, size_t entityIndexInQuery, RTDCmptViewer rtdcmpts) {
return func(e, entityIndexInQuery, rtdcmpts);
}

// no arguments <Func>
Expand All @@ -45,7 +46,7 @@ namespace Ubpa {
template<typename Func, typename ArgList>
SystemFunc(Func&& func, std::string name, EntityFilter filter, ArgList);

std::function<void(Entity, size_t, const EntityLocator*, void**)> func;
std::function<void(Entity, size_t, RTDCmptViewer)> func;

std::string name;
bool isJob;
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 @@ -18,8 +18,8 @@ namespace Ubpa {
{
using ArgList = FuncTraits_ArgList<Func>;

static_assert(ContainTs_v<ArgList, const EntityLocator*, void**>,
"<Func>'s argument must contain const EntityLocator* and void**");
static_assert(ContainTs_v<ArgList, RTDCmptViewer>,
"<Func>'s argument must contain RTDCmptViewer");
}

template<typename Func>
Expand Down Expand Up @@ -49,8 +49,8 @@ namespace Ubpa::detail::System_ {
using CmptList = TypeList<Cmpts...>;
template<typename Func>
static auto run(Func&& func) noexcept {
return [func = std::forward<Func>(func)](Entity e, size_t entityIndexInQuery, const EntityLocator* locator, void** cmpt_arr) {
auto unsorted_arg_tuple = std::make_tuple(e, entityIndexInQuery, locator, cmpt_arr, reinterpret_cast<Cmpts*>(cmpt_arr[Find_v<CmptList, Cmpts>])...);
return [func = std::forward<Func>(func)](Entity e, size_t entityIndexInQuery, RTDCmptViewer rtdcmpts) {
auto unsorted_arg_tuple = std::make_tuple(e, entityIndexInQuery, rtdcmpts, reinterpret_cast<Cmpts*>(rtdcmpts.Components()[Find_v<CmptList, Cmpts>])...);
func(std::get<DecayedArgs>(unsorted_arg_tuple)...);
};
}
Expand Down
2 changes: 1 addition & 1 deletion src/core/EntityMngr.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -258,7 +258,7 @@ void EntityMngr::GenJob(Job* job, SystemFunc* sys) const {

size_t J = min(chunkCapacity, num - idxOffsetInChunk);
for (size_t j = 0; j < J; j++) {
(*sys)(entities[j], indexOffsetInQueryChunk + j, &sys->query.locator, cmpts.data());
(*sys)(entities[j], indexOffsetInQueryChunk + j, { &sys->query.locator, cmpts.data() });
for (size_t k = 0; k < cmpts.size(); k++)
reinterpret_cast<uint8_t*&>(cmpts[k]) += sizes[k];
}
Expand Down
18 changes: 18 additions & 0 deletions src/core/RTDCmptViewer.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
#include <UECS/RTDCmptViewer.h>

#include <UECS/EntityLocator.h>

using namespace Ubpa;
using namespace std;

RTDCmptViewer::Iterator RTDCmptViewer::begin() const noexcept {
return { locator->CmptTypes().begin(), cmpts };
}

RTDCmptViewer::Iterator RTDCmptViewer::end() const noexcept {
return { locator->CmptTypes().end(), cmpts + locator->CmptTypes().size() };
}

const set<CmptType>& RTDCmptViewer::CmptTypes() const noexcept {
return locator->CmptTypes();
}
20 changes: 8 additions & 12 deletions src/test/11_runtime_cmpt/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -19,25 +19,21 @@ struct RTDSystem {
);
schedule
.Register(
[](const EntityLocator* locator, void** cmpts) {
size_t i = 0;
for (auto type : locator->CmptTypes()) {
if (type == CmptType{ "LuaCmpt" }) {
double& val = *reinterpret_cast<double*>(cmpts[i]);
[](RTDCmptViewer cmpts) {
for (auto cmptptr : cmpts) {
if (cmptptr.Type() == CmptType{ "LuaCmpt" }) {
double& val = *reinterpret_cast<double*>(cmptptr.Ptr());
val = 520.;
}
i++;
}
}, "write", locator_write)
.Register(
[](const EntityLocator* locator, void** cmpts) {
size_t i = 0;
for (auto type : locator->CmptTypes()) {
if (type == CmptType{ "LuaCmpt" }) {
const double& val = *reinterpret_cast<double*>(cmpts[i]);
[](RTDCmptViewer cmpts) {
for (auto cmptptr : cmpts) {
if (cmptptr.Type() == CmptType{ "LuaCmpt" }) {
double& val = *reinterpret_cast<double*>(cmptptr.Ptr());
cout << "value : " << val << endl;
}
i++;
}
}, "read", locator_read);
}
Expand Down

0 comments on commit cb45fda

Please sign in to comment.