111 lines
2.5 KiB
C++
111 lines
2.5 KiB
C++
#pragma once
|
|
|
|
#include <psemek/ecs/detail/table.hpp>
|
|
#include <psemek/util/span.hpp>
|
|
#include <psemek/util/range.hpp>
|
|
#include <psemek/util/exception.hpp>
|
|
#include <psemek/util/type_name.hpp>
|
|
#include <psemek/util/to_string.hpp>
|
|
|
|
#include <typeindex>
|
|
|
|
namespace psemek::ecs
|
|
{
|
|
|
|
struct component_not_found_exception
|
|
: util::exception
|
|
{
|
|
component_not_found_exception(std::type_info const & type, handle const & handle, boost::stacktrace::stacktrace stacktrace = {})
|
|
: util::exception(util::to_string("Component ", util::type_name(type), " not found for entity ", handle), std::move(stacktrace))
|
|
, type_(type)
|
|
, handle_(handle)
|
|
{}
|
|
|
|
std::type_info const & type() const
|
|
{
|
|
return type_;
|
|
}
|
|
|
|
ecs::handle const & handle() const
|
|
{
|
|
return handle_;
|
|
}
|
|
|
|
private:
|
|
std::type_info const & type_;
|
|
ecs::handle handle_;
|
|
};
|
|
|
|
struct accessor
|
|
{
|
|
accessor() = default;
|
|
accessor(accessor const &) = default;
|
|
|
|
explicit operator bool() const;
|
|
|
|
/** Obtain a pointer to the specified component type
|
|
* of the accessed entity, or a null pointer if
|
|
* the entity doesn't contain this component type.
|
|
*/
|
|
template <typename Component>
|
|
Component * get_if();
|
|
|
|
/** Obtain a reference to the specified component type
|
|
* of the accessed entity.
|
|
* If the entity doesn't contain this component type,
|
|
* an exception of type `component_not_found_exception` is thrown.
|
|
*/
|
|
template <typename Component>
|
|
Component & get();
|
|
|
|
/** Check if the entity contains this component type.
|
|
*/
|
|
template <typename Component>
|
|
bool contains() const;
|
|
|
|
private:
|
|
detail::table * table_ = nullptr;
|
|
std::uint32_t row_ = 0;
|
|
|
|
friend struct entity_container;
|
|
|
|
accessor(detail::table * table, std::uint32_t row);
|
|
};
|
|
|
|
inline accessor::operator bool() const
|
|
{
|
|
return table_ != nullptr;
|
|
}
|
|
|
|
template <typename Component>
|
|
Component * accessor::get_if()
|
|
{
|
|
util::uuid const uuid = Component::uuid();
|
|
|
|
auto column = table_->column(uuid);
|
|
if (!column)
|
|
return nullptr;
|
|
|
|
return reinterpret_cast<Component *>(column->data() + detail::stride<Component>() * row_);
|
|
}
|
|
|
|
template <typename Component>
|
|
Component & accessor::get()
|
|
{
|
|
if (auto ptr = get_if<Component>())
|
|
return *ptr;
|
|
throw component_not_found_exception(typeid(Component), table_->entity_handles()[row_]);
|
|
}
|
|
|
|
template <typename Component>
|
|
bool accessor::contains() const
|
|
{
|
|
return get_if<Component>() != nullptr;
|
|
}
|
|
|
|
inline accessor::accessor(detail::table * table, std::uint32_t row)
|
|
: table_(table)
|
|
, row_(row)
|
|
{}
|
|
|
|
}
|