Implement string entity description in ecs
This commit is contained in:
parent
fbf78f1dc4
commit
655dd2778f
8 changed files with 80 additions and 4 deletions
|
|
@ -107,7 +107,7 @@ namespace psemek::ecs
|
|||
{
|
||||
if (auto ptr = get_if<Component>())
|
||||
return *ptr;
|
||||
throw component_not_found_exception(typeid(std::remove_const_t<Component>), table_->entity_handles()[row_]);
|
||||
throw component_not_found_exception(typeid(std::remove_const_t<Component>), table_->entity_handles()[row_], table_->describe(row_));
|
||||
}
|
||||
|
||||
template <typename Component>
|
||||
|
|
@ -115,7 +115,7 @@ namespace psemek::ecs
|
|||
{
|
||||
if (auto ptr = get_if<Component const>())
|
||||
return *ptr;
|
||||
throw component_not_found_exception(typeid(std::remove_const_t<Component>), table_->entity_handles()[row_]);
|
||||
throw component_not_found_exception(typeid(std::remove_const_t<Component>), table_->entity_handles()[row_], table_->describe(row_));
|
||||
}
|
||||
|
||||
template <typename Component>
|
||||
|
|
|
|||
|
|
@ -93,6 +93,15 @@ namespace psemek::ecs
|
|||
*/
|
||||
accessor get(handle entity);
|
||||
|
||||
/** Compute a string representation of an entity, based on the contained components.
|
||||
* A component has to implement a to_string() method in order to provide extra info.
|
||||
*
|
||||
* @param entity A handle to the entity to describe
|
||||
* @return A string representation of an entity
|
||||
* @pre The entity was previously obtained by a `create()` call and is `alive()`
|
||||
*/
|
||||
std::string describe(handle entity) const;
|
||||
|
||||
/** Check if the entity can be cloned.
|
||||
*
|
||||
* An entity can be cloned if all of its components are copy-constructible.
|
||||
|
|
|
|||
|
|
@ -1,6 +1,7 @@
|
|||
#pragma once
|
||||
|
||||
#include <psemek/ecs/detail/stride.hpp>
|
||||
#include <psemek/ecs/detail/describe.hpp>
|
||||
#include <psemek/util/uuid.hpp>
|
||||
#include <psemek/util/assert.hpp>
|
||||
|
||||
|
|
@ -53,6 +54,8 @@ namespace psemek::ecs::detail
|
|||
|
||||
virtual std::size_t memory_usage() const = 0;
|
||||
|
||||
virtual std::string describe(std::uint32_t row) const = 0;
|
||||
|
||||
virtual ~column() = default;
|
||||
|
||||
protected:
|
||||
|
|
@ -87,6 +90,8 @@ namespace psemek::ecs::detail
|
|||
|
||||
std::size_t memory_usage() const override;
|
||||
|
||||
std::string describe(std::uint32_t row) const override;
|
||||
|
||||
~column_impl() override;
|
||||
|
||||
private:
|
||||
|
|
@ -113,6 +118,8 @@ namespace psemek::ecs::detail
|
|||
|
||||
std::size_t memory_usage() const override;
|
||||
|
||||
std::string describe(std::uint32_t row) const override;
|
||||
|
||||
~column_impl() override;
|
||||
};
|
||||
|
||||
|
|
@ -208,6 +215,12 @@ namespace psemek::ecs::detail
|
|||
return sizeof(Component) * row_count_;
|
||||
}
|
||||
|
||||
template <typename Component, bool Empty>
|
||||
std::string column_impl<Component, Empty>::describe(std::uint32_t row) const
|
||||
{
|
||||
return detail::describe(reinterpret_cast<Component const *>(data_)[row]);
|
||||
}
|
||||
|
||||
template <typename Component, bool Empty>
|
||||
column_impl<Component, Empty>::~column_impl()
|
||||
{
|
||||
|
|
@ -286,6 +299,12 @@ namespace psemek::ecs::detail
|
|||
return sizeof(Component);
|
||||
}
|
||||
|
||||
template <typename Component>
|
||||
std::string column_impl<Component, true>::describe(std::uint32_t) const
|
||||
{
|
||||
return detail::describe(*reinterpret_cast<Component const *>(data_));
|
||||
}
|
||||
|
||||
template <typename Component>
|
||||
column_impl<Component, true>::~column_impl()
|
||||
{
|
||||
|
|
|
|||
26
libs/ecs/include/psemek/ecs/detail/describe.hpp
Normal file
26
libs/ecs/include/psemek/ecs/detail/describe.hpp
Normal file
|
|
@ -0,0 +1,26 @@
|
|||
#pragma once
|
||||
|
||||
#include <psemek/util/type_name.hpp>
|
||||
|
||||
#include <string>
|
||||
|
||||
namespace psemek::ecs::detail
|
||||
{
|
||||
|
||||
template <typename Component>
|
||||
std::string describe(Component const & component)
|
||||
{
|
||||
if constexpr (requires {component.to_string();})
|
||||
{
|
||||
return component.to_string();
|
||||
}
|
||||
else
|
||||
{
|
||||
auto name = util::type_name<Component>();
|
||||
if (auto pos = name.find_last_of(':'); pos != std::string::npos)
|
||||
name = name.substr(pos + 1);
|
||||
return name;
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
|
@ -12,6 +12,7 @@
|
|||
#include <memory>
|
||||
#include <vector>
|
||||
#include <optional>
|
||||
#include <string>
|
||||
|
||||
namespace psemek::ecs::detail
|
||||
{
|
||||
|
|
@ -81,6 +82,8 @@ namespace psemek::ecs::detail
|
|||
|
||||
std::size_t memory_usage() const;
|
||||
|
||||
std::string describe(std::uint32_t row) const;
|
||||
|
||||
protected:
|
||||
std::size_t hash_;
|
||||
std::vector<std::unique_ptr<detail::column>> columns_;
|
||||
|
|
|
|||
|
|
@ -13,8 +13,8 @@ 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))
|
||||
component_not_found_exception(std::type_info const & type, handle const & handle, std::string const & description, boost::stacktrace::stacktrace stacktrace = {})
|
||||
: util::exception(util::to_string("Component ", util::type_name(type), " not found for entity ", handle, " <", description, ">"), std::move(stacktrace))
|
||||
, type_(type)
|
||||
, handle_(handle)
|
||||
{}
|
||||
|
|
|
|||
|
|
@ -27,6 +27,13 @@ namespace psemek::ecs
|
|||
return {data.table, data.row};
|
||||
}
|
||||
|
||||
std::string container::describe(handle entity) const
|
||||
{
|
||||
assert(alive(entity));
|
||||
auto const data = entity_list_.get_entities()[entity.id];
|
||||
return data.table->describe(data.row);
|
||||
}
|
||||
|
||||
bool container::can_clone(handle entity) const
|
||||
{
|
||||
return entity_list_.get_entities()[entity.id].table->non_copyable_components().empty();
|
||||
|
|
|
|||
|
|
@ -186,4 +186,16 @@ namespace psemek::ecs::detail
|
|||
return result;
|
||||
}
|
||||
|
||||
std::string table::describe(std::uint32_t row) const
|
||||
{
|
||||
std::string result;
|
||||
for (std::size_t i = 0; i < columns_.size(); ++i)
|
||||
{
|
||||
if (i > 0)
|
||||
result += ";";
|
||||
result += columns_[i]->describe(row);
|
||||
}
|
||||
return result;
|
||||
}
|
||||
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Reference in a new issue