Support retrieving ECS table statistics

This commit is contained in:
Nikita Lisitsa 2024-11-29 01:15:35 +03:00
parent 95b6651fc3
commit bfa0491f39
7 changed files with 79 additions and 3 deletions

View file

@ -11,6 +11,7 @@
#include <psemek/ecs/detail/index_container.hpp>
#include <psemek/ecs/accessor.hpp>
#include <psemek/ecs/exceptions.hpp>
#include <psemek/ecs/statistics.hpp>
#include <psemek/util/range.hpp>
#include <psemek/util/object_pool.hpp>
@ -396,6 +397,8 @@ namespace psemek::ecs
std::size_t table_count();
struct statistics statistics();
private:
detail::entity_list entity_list_;
detail::table_container table_container_;

View file

@ -54,6 +54,7 @@ namespace psemek::ecs::detail
virtual std::size_t memory_usage() const = 0;
virtual std::string describe() const = 0;
virtual std::string describe(std::uint32_t row) const = 0;
virtual ~column() = default;
@ -90,6 +91,7 @@ namespace psemek::ecs::detail
std::size_t memory_usage() const override;
std::string describe() const override;
std::string describe(std::uint32_t row) const override;
~column_impl() override;
@ -118,6 +120,7 @@ namespace psemek::ecs::detail
std::size_t memory_usage() const override;
std::string describe() const override;
std::string describe(std::uint32_t row) const override;
~column_impl() override;
@ -215,6 +218,12 @@ namespace psemek::ecs::detail
return sizeof(Component) * row_count_;
}
template <typename Component, bool Empty>
std::string column_impl<Component, Empty>::describe() const
{
return detail::describe<Component>();
}
template <typename Component, bool Empty>
std::string column_impl<Component, Empty>::describe(std::uint32_t row) const
{
@ -299,6 +308,12 @@ namespace psemek::ecs::detail
return sizeof(Component);
}
template <typename Component>
std::string column_impl<Component, true>::describe() const
{
return detail::describe<Component>();
}
template <typename Component>
std::string column_impl<Component, true>::describe(std::uint32_t) const
{

View file

@ -8,11 +8,11 @@ namespace psemek::ecs::detail
{
template <typename Component>
std::string describe(Component const & component)
std::string describe()
{
if constexpr (requires {component.to_string();})
if constexpr (requires {Component::to_string();})
{
return component.to_string();
return Component::to_string();
}
else
{
@ -23,4 +23,17 @@ namespace psemek::ecs::detail
}
}
template <typename Component>
std::string describe(Component const & component)
{
if constexpr (requires {component.to_string();})
{
return component.to_string();
}
else
{
return describe<Component>();
}
}
}

View file

@ -81,6 +81,7 @@ namespace psemek::ecs::detail
std::size_t memory_usage() const;
std::string describe() const;
std::string describe(std::uint32_t row) const;
protected:

View file

@ -0,0 +1,20 @@
#pragma once
#include <string>
#include <vector>
namespace psemek::ecs
{
struct table_statistics
{
std::string description;
std::size_t entity_count;
};
struct statistics
{
std::vector<table_statistics> tables;
};
}

View file

@ -138,4 +138,16 @@ namespace psemek::ecs
return table_container_.table_count();
}
statistics container::statistics()
{
struct statistics result;
table_container_.apply([&](detail::table & table)
{
result.tables.push_back({table.describe(), table.row_count()});
}, {}, {});
return result;
}
}

View file

@ -188,6 +188,18 @@ namespace psemek::ecs::detail
return result;
}
std::string table::describe() const
{
std::string result;
for (std::size_t i = 0; i < columns_.size(); ++i)
{
if (i > 0)
result += ";";
result += columns_[i]->describe();
}
return result;
}
std::string table::describe(std::uint32_t row) const
{
std::string result;