Estella 0.113.0
A fast 2D and 3D game engine powered by WebAssembly and ECS
Loading...
Searching...
No Matches
ActiveView.hpp
Go to the documentation of this file.
1// SPDX-License-Identifier: Apache-2.0
2// SPDX-FileCopyrightText: Copyright (c) 2024-present ESEngine Team
3#pragma once
4#include "Entity.hpp"
5#include <tuple>
6#include <type_traits>
7#include <utility>
8
9namespace esengine::ecs {
10
11// Storage views stay unfiltered for inspection, serialization and hierarchy
12// maintenance. Simulation/render consumers explicitly ask for an active view.
13template<class V, class R>
15public:
16 class Iterator {
17 public:
18 Iterator(typename V::Iterator at, typename V::Iterator end, R* registry)
19 : at_(at), end_(end), registry_(registry) { skip(); }
20 Entity operator*() const { return *at_; }
21 Iterator& operator++() { ++at_; skip(); return *this; }
22 bool operator!=(const Iterator& other) const { return at_ != other.at_; }
23 bool operator==(const Iterator& other) const { return at_ == other.at_; }
24 private:
25 void skip() { while (at_ != end_ && !registry_->isEntityActive(*at_)) ++at_; }
26 typename V::Iterator at_, end_;
27 R* registry_;
28 };
29 ActiveView(V view, R& registry) : view_(std::move(view)), registry_(&registry) {}
30 Iterator begin() const { return {view_.begin(), view_.end(), registry_}; }
31 Iterator end() const { return {view_.end(), view_.end(), registry_}; }
32 template<class T> T& get(Entity e) { return view_.template get<T>(e); }
33 decltype(auto) get(Entity e) { return view_.get(e); }
34 bool empty() const { return begin() == end(); }
35 usize sizeHint() const { return view_.sizeHint(); }
36 template<class F> void each(F&& f) {
37 view_.each([&](Entity e, auto&... data) {
38 if (!registry_->isEntityActive(e)) return;
39 invoke(f, e, data...);
40 });
41 }
42 template<class F> void eachLive(F&& f) {
43 view_.eachLive([&](Entity e, auto&... data) {
44 if (!registry_->isEntityActive(e)) return;
45 invoke(f, e, data...);
46 });
47 }
48private:
49 template<class F, class... Data>
50 static void invoke(F& f, Entity e, Data&... data) {
51 if constexpr (std::is_invocable_v<F&, Entity, Data&...>) f(e, data...);
52 else if constexpr (std::is_invocable_v<F&, Data&...>) f(data...);
53 else f(e);
54 }
55 V view_;
56 R* registry_;
57};
58} // namespace esengine::ecs
Entity utilities for the ECS system.
bool operator==(const Iterator &other) const
Iterator(typename V::Iterator at, typename V::Iterator end, R *registry)
bool operator!=(const Iterator &other) const
Iterator begin() const
ActiveView(V view, R &registry)
decltype(auto) get(Entity e)
std::size_t usize
Size type for indexing and sizes.
Definition Types.hpp:64
Entity identifier with packed index + generation.
Definition Types.hpp:135