mirror of
https://github.com/BlackMATov/ecs.hpp.git
synced 2025-12-13 02:26:27 +07:00
operator accessories for component wrappers
This commit is contained in:
@@ -991,6 +991,14 @@ namespace ecs_hpp
|
|||||||
|
|
||||||
T* find() noexcept;
|
T* find() noexcept;
|
||||||
const T* find() const noexcept;
|
const T* find() const noexcept;
|
||||||
|
|
||||||
|
T& operator*();
|
||||||
|
const T& operator*() const;
|
||||||
|
|
||||||
|
T* operator->() noexcept;
|
||||||
|
const T* operator->() const noexcept;
|
||||||
|
|
||||||
|
explicit operator bool() const noexcept;
|
||||||
private:
|
private:
|
||||||
entity owner_;
|
entity owner_;
|
||||||
};
|
};
|
||||||
@@ -1047,6 +1055,10 @@ namespace ecs_hpp
|
|||||||
|
|
||||||
const T& get() const;
|
const T& get() const;
|
||||||
const T* find() const noexcept;
|
const T* find() const noexcept;
|
||||||
|
|
||||||
|
const T& operator*() const;
|
||||||
|
const T* operator->() const noexcept;
|
||||||
|
explicit operator bool() const noexcept;
|
||||||
private:
|
private:
|
||||||
const_entity owner_;
|
const_entity owner_;
|
||||||
};
|
};
|
||||||
@@ -1689,6 +1701,31 @@ namespace ecs_hpp
|
|||||||
return detail::as_const(owner_).template find_component<T>();
|
return detail::as_const(owner_).template find_component<T>();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
template < typename T >
|
||||||
|
T& component<T>::operator*() {
|
||||||
|
return get();
|
||||||
|
}
|
||||||
|
|
||||||
|
template < typename T >
|
||||||
|
const T& component<T>::operator*() const {
|
||||||
|
return get();
|
||||||
|
}
|
||||||
|
|
||||||
|
template < typename T >
|
||||||
|
T* component<T>::operator->() noexcept {
|
||||||
|
return find();
|
||||||
|
}
|
||||||
|
|
||||||
|
template < typename T >
|
||||||
|
const T* component<T>::operator->() const noexcept {
|
||||||
|
return find();
|
||||||
|
}
|
||||||
|
|
||||||
|
template < typename T >
|
||||||
|
component<T>::operator bool() const noexcept {
|
||||||
|
return exists();
|
||||||
|
}
|
||||||
|
|
||||||
template < typename T >
|
template < typename T >
|
||||||
bool operator<(const component<T>& l, const component<T>& r) noexcept {
|
bool operator<(const component<T>& l, const component<T>& r) noexcept {
|
||||||
return l.owner() < r.owner();
|
return l.owner() < r.owner();
|
||||||
@@ -1751,6 +1788,21 @@ namespace ecs_hpp
|
|||||||
return detail::as_const(owner_).template find_component<T>();
|
return detail::as_const(owner_).template find_component<T>();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
template < typename T >
|
||||||
|
const T& const_component<T>::operator*() const {
|
||||||
|
return get();
|
||||||
|
}
|
||||||
|
|
||||||
|
template < typename T >
|
||||||
|
const T* const_component<T>::operator->() const noexcept {
|
||||||
|
return find();
|
||||||
|
}
|
||||||
|
|
||||||
|
template < typename T >
|
||||||
|
const_component<T>::operator bool() const noexcept {
|
||||||
|
return exists();
|
||||||
|
}
|
||||||
|
|
||||||
template < typename T >
|
template < typename T >
|
||||||
bool operator<(const const_component<T>& l, const const_component<T>& r) noexcept {
|
bool operator<(const const_component<T>& l, const const_component<T>& r) noexcept {
|
||||||
return l.owner() < r.owner();
|
return l.owner() < r.owner();
|
||||||
|
|||||||
@@ -29,4 +29,11 @@ add_executable(${PROJECT_NAME} ${UNTESTS_SOURCES})
|
|||||||
target_link_libraries(${PROJECT_NAME}
|
target_link_libraries(${PROJECT_NAME}
|
||||||
Catch2
|
Catch2
|
||||||
ecs.hpp)
|
ecs.hpp)
|
||||||
|
target_compile_options(${PROJECT_NAME}
|
||||||
|
PRIVATE
|
||||||
|
$<$<CXX_COMPILER_ID:MSVC>:
|
||||||
|
/W4>
|
||||||
|
PRIVATE
|
||||||
|
$<$<OR:$<CXX_COMPILER_ID:GNU>,$<CXX_COMPILER_ID:Clang>,$<CXX_COMPILER_ID:AppleClang>>:
|
||||||
|
-Wall -Wextra -Wpedantic>)
|
||||||
add_test(${PROJECT_NAME} ${PROJECT_NAME})
|
add_test(${PROJECT_NAME} ${PROJECT_NAME})
|
||||||
|
|||||||
@@ -518,6 +518,54 @@ TEST_CASE("registry") {
|
|||||||
|
|
||||||
REQUIRE_FALSE(c1.remove());
|
REQUIRE_FALSE(c1.remove());
|
||||||
}
|
}
|
||||||
|
{
|
||||||
|
using namespace ecs::detail;
|
||||||
|
|
||||||
|
ecs::registry w;
|
||||||
|
ecs::entity e1 = w.create_entity();
|
||||||
|
|
||||||
|
ecs::component<position_c> c1 = w.wrap_component<position_c>(e1);
|
||||||
|
ecs::const_component<position_c> c2 = w.wrap_component<position_c>(e1);
|
||||||
|
|
||||||
|
REQUIRE_FALSE(c1);
|
||||||
|
REQUIRE_FALSE(as_const(c1));
|
||||||
|
REQUIRE_FALSE(c2);
|
||||||
|
REQUIRE_FALSE(as_const(c2));
|
||||||
|
|
||||||
|
REQUIRE_THROWS_AS(*c1, std::logic_error);
|
||||||
|
REQUIRE_THROWS_AS(*as_const(c1), std::logic_error);
|
||||||
|
REQUIRE_THROWS_AS(*c2, std::logic_error);
|
||||||
|
REQUIRE_THROWS_AS(*as_const(c2), std::logic_error);
|
||||||
|
|
||||||
|
c1.assign(1,2);
|
||||||
|
|
||||||
|
REQUIRE(c1);
|
||||||
|
REQUIRE(as_const(c1));
|
||||||
|
REQUIRE(c2);
|
||||||
|
REQUIRE(as_const(c2));
|
||||||
|
|
||||||
|
REQUIRE(*c1 == position_c(1,2));
|
||||||
|
REQUIRE(*as_const(c1) == position_c(1,2));
|
||||||
|
REQUIRE(*c2 == position_c(1,2));
|
||||||
|
REQUIRE(*as_const(c2) == position_c(1,2));
|
||||||
|
|
||||||
|
REQUIRE(c1->x == 1);
|
||||||
|
REQUIRE(c1->y == 2);
|
||||||
|
REQUIRE(as_const(c1)->x == 1);
|
||||||
|
REQUIRE(as_const(c1)->y == 2);
|
||||||
|
|
||||||
|
REQUIRE(c2->x == 1);
|
||||||
|
REQUIRE(c2->y == 2);
|
||||||
|
REQUIRE(as_const(c2)->x == 1);
|
||||||
|
REQUIRE(as_const(c2)->y == 2);
|
||||||
|
|
||||||
|
c1.remove();
|
||||||
|
|
||||||
|
REQUIRE_FALSE(c1);
|
||||||
|
REQUIRE_FALSE(as_const(c1));
|
||||||
|
REQUIRE_FALSE(c2);
|
||||||
|
REQUIRE_FALSE(as_const(c2));
|
||||||
|
}
|
||||||
}
|
}
|
||||||
SECTION("prototypes") {
|
SECTION("prototypes") {
|
||||||
{
|
{
|
||||||
|
|||||||
Reference in New Issue
Block a user