Skip to content

Commit

Permalink
RTDCmptViewer -> RTDCmptsView, add CmptHandle for r/w
Browse files Browse the repository at this point in the history
  • Loading branch information
Ubpa committed May 29, 2020
1 parent f954f5d commit 36dadba
Show file tree
Hide file tree
Showing 9 changed files with 147 additions and 95 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.9.3)
project(UECS VERSION 0.9.4)
message(STATUS "[Project] ${PROJECT_NAME}")

include(FetchContent)
Expand Down
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
- [x] `RTDCmptViewer` = `const EntityLocator* locator + void** cmpts`
- [x] `RTDCmptsView` = `const EntityLocator* locator + void** cmpts`

### maybe deprecate

Expand Down
61 changes: 0 additions & 61 deletions include/UECS/RTDCmptViewer.h

This file was deleted.

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

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

#include <set>

namespace Ubpa {
class EntityLocator;

// use RTDCmptsView::Iterator to read CmptPtr
// use CmptHandle for read/write control
// use begin() and end() to iterate
class RTDCmptsView {
public:
// for read/write control
class CmptHandle {
public:
enum class Mode {
INVALID,
LAST_FRAME,
WRITE,
LATEST
};

CmptHandle(CmptType type, void* cmpt, Mode mode)
: type{ type }, cmpt{ cmpt }, mode{ mode }{}

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

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

// forward
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(EntityLocator* locator, std::set<CmptType>::iterator typeIter = std::set<CmptType>::iterator{}, void* const* ptr_cmpt = nullptr)
: locator{ locator }, 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 ptr_cmpt != rhs.ptr_cmpt;
}
CmptHandle operator*() const {
return { *typeIter, *ptr_cmpt, GetMode() };
}
const CmptHandle* operator->() const noexcept {
handle = { *typeIter, *ptr_cmpt, GetMode() };
return &handle;
}
Iterator& operator++() {
typeIter++;
ptr_cmpt++;
return *this;
}
private:
CmptHandle::Mode GetMode() const;

EntityLocator* locator;
std::set<CmptType>::iterator typeIter;
void* const* ptr_cmpt;
mutable CmptHandle handle{ CmptType::Invalid(), nullptr, CmptHandle::Mode::INVALID };
};

RTDCmptsView(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* const* cmpts;
};
}
6 changes: 3 additions & 3 deletions include/UECS/SystemFunc.h
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

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

#include <functional>

Expand Down Expand Up @@ -34,7 +34,7 @@ namespace Ubpa {

size_t HashCode() const noexcept { return hashCode; }

void operator()(Entity e, size_t entityIndexInQuery, RTDCmptViewer rtdcmpts) {
void operator()(Entity e, size_t entityIndexInQuery, RTDCmptsView rtdcmpts) {
return func(e, entityIndexInQuery, rtdcmpts);
}

Expand All @@ -46,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, RTDCmptViewer)> func;
std::function<void(Entity, size_t, RTDCmptsView)> func;

std::string name;
bool isJob;
Expand Down
6 changes: 3 additions & 3 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, RTDCmptViewer>,
"<Func>'s argument must contain RTDCmptViewer");
static_assert(ContainTs_v<ArgList, RTDCmptsView>,
"<Func>'s argument must contain RTDCmptsView");
}

template<typename Func>
Expand Down Expand Up @@ -49,7 +49,7 @@ 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, RTDCmptViewer rtdcmpts) {
return [func = std::forward<Func>(func)](Entity e, size_t entityIndexInQuery, RTDCmptsView 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
18 changes: 0 additions & 18 deletions src/core/RTDCmptViewer.cpp

This file was deleted.

30 changes: 30 additions & 0 deletions src/core/RTDCmptsView.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
#include <UECS/RTDCmptsView.h>

#include <UECS/EntityLocator.h>

using namespace Ubpa;
using namespace std;

RTDCmptsView::CmptHandle::Mode RTDCmptsView::Iterator::GetMode() const {
auto type = *typeIter;
if (locator->LastFrameCmptTypes().find(type) != locator->LastFrameCmptTypes().end())
return CmptHandle::Mode::LAST_FRAME;
else if (locator->WriteCmptTypes().find(type) != locator->WriteCmptTypes().end())
return CmptHandle::Mode::WRITE;
else if (locator->LatestCmptTypes().find(type) != locator->LatestCmptTypes().end())
return CmptHandle::Mode::LATEST;
else
return CmptHandle::Mode::INVALID;
}

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

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

const set<CmptType>& RTDCmptsView::CmptTypes() const noexcept {
return locator->CmptTypes();
}
16 changes: 8 additions & 8 deletions src/test/11_runtime_cmpt/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -19,19 +19,19 @@ struct RTDSystem {
);
schedule
.Register(
[](RTDCmptViewer cmpts) {
for (auto cmptptr : cmpts) {
if (cmptptr.Type() == CmptType{ "LuaCmpt" }) {
double& val = *reinterpret_cast<double*>(cmptptr.Ptr());
[](RTDCmptsView cmpts) {
for (auto handle : cmpts) {
if (handle.GetCmptType() == CmptType{ "LuaCmpt" }) {
double& val = *reinterpret_cast<double*>(handle.AsWrite().Ptr());
val = 520.;
}
}
}, "write", locator_write)
.Register(
[](RTDCmptViewer cmpts) {
for (auto cmptptr : cmpts) {
if (cmptptr.Type() == CmptType{ "LuaCmpt" }) {
double& val = *reinterpret_cast<double*>(cmptptr.Ptr());
[](RTDCmptsView cmpts) {
for (auto handle : cmpts) {
if (handle.GetCmptType() == CmptType{ "LuaCmpt" }) {
const double& val = *reinterpret_cast<const double*>(handle.AsLatest().Ptr());
cout << "value : " << val << endl;
}
}
Expand Down

0 comments on commit 36dadba

Please sign in to comment.