7 #include "spdlog/spdlog.h"
14 throw std::logic_error(
"Cannot enable tracking of id, because no `Statistics` object has been assigned.");
24 auto dist = std::distance(begin(names), std::find_if(begin(names), end(names), [&](
auto&& v){
return v.Name == str; }));
26 throw std::invalid_argument(
"No statistics of the given name has been declared.");
42 throw std::invalid_argument(
"A stat with the given id already exists");
45 throw std::invalid_argument(
"Currently, stats must be declared consecutively!");
49 if(meta.
Name == old.Name) {
50 throw std::invalid_argument(
"A stat with the given name already exists");
62 throw std::invalid_argument(
"Cannot register stat. Already registered!");
79 throw std::invalid_argument(
"No Statistics registered for the given name");
89 throw std::invalid_argument(
"A tag with the given id already exists");
92 throw std::invalid_argument(
"Currently, tags must be declared consecutively!");
96 if(old.get_name() == name) {
97 throw std::invalid_argument(
"A tag with the given name already exists");
123 return std::any_of(begin(
m_MetaData), end(
m_MetaData), [&](
auto&& v){
return v.Name == name; });
127 #include "nlohmann/json.hpp"
132 [[nodiscard]] std::unique_ptr<Statistics>
clone()
const override {
134 __builtin_unreachable();
138 __builtin_unreachable();
143 __builtin_unreachable();
146 void record_int(
long integer)
override { LastValue = integer; }
167 DOCTEST_REQUIRE_THROWS(collection.
get_stat(
"stat"));
170 DOCTEST_REQUIRE_THROWS(collection.
enable(
"unknown"));
171 DOCTEST_REQUIRE_THROWS(collection.
disable(
"unknown"));
173 DOCTEST_REQUIRE_THROWS(collection.
register_stat(
"unknown",
nullptr));
174 DOCTEST_REQUIRE_THROWS(collection.
get_stat(
"unknown"));
199 CHECK_FALSE(collection.
has_stat(
"stat2"));
201 auto stat = std::make_unique<MockStat>();
205 DOCTEST_REQUIRE_THROWS(collection.
register_stat(
"stat", std::make_unique<MockStat>()));
210 auto second = std::make_unique<MockStat>();
211 auto* ptr = second.get();
215 CHECK(ptr == &collection.
get_stat(
"stat"));
223 collection.
register_stat(
"stat", std::make_unique<MockStat>());
234 collection.
enable(
"stat");
250 collection.
register_stat(
"stat", std::make_unique<MockStat>());
251 const auto& stat =
dynamic_cast<const MockStat&
>(collection.
get_stat(
"stat"));
255 CHECK(stat.LastValue == 5);
259 CHECK(stat.LastValue == 8);
263 collection.
record(
stat_id_t{0}, [](){ DOCTEST_FAIL(
"callable was called for disabled stat");
return 0; });
298 CHECK(foreign_tag.get_value() == 25);
constexpr T to_index() const
! Explicitly convert to an integer.
This class manages a collection of named Statistics objects.
void declare_tag(tag_id_t index, std::string name)
Declares a new tag value.
std::vector< StatisticMetaData > m_MetaData
TagContainer get_tag_by_name(const std::string &name) const
Gets the tag with the given name.
bool is_enabled(stat_id_t stat) const
Quickly checks whether collection of data is enabled for the given statistics.
std::vector< std::unique_ptr< Statistics > > m_Statistics
const Statistics & get_stat(const std::string &name) const
Returns the Statistics object corresponding to the slot name.
std::vector< bool > m_Enabled
void provide_tags(const StatisticsCollection &other)
Registers all the tags of the other collection as read-only tags in this collection.
void disable(stat_id_t stat)
Explicitly disable the collection of statistics for the given index.
const std::vector< StatisticMetaData > & get_statistics_meta() const
Gets a vector with the declarations for all statistics.
const std::vector< TagContainer > & get_all_tags() const
Gets a vector which contains all the tags owned by this collection.
bool is_enabled_by_name(const std::string &name) const
Checks whether gathering data is enabled based on the statistic's name.
void declare_stat(stat_id_t index, StatisticMetaData meta)
Declares a new statistics. Defines the corresponding index and name.
std::vector< TagContainer > m_TagValues
void enable(stat_id_t stat)
Explicitly enable the collection of statistics for the given index.
bool has_stat(const std::string &name) const
Returns whether a stat with the given name is declared.
void set_tag(tag_id_t tag, int value)
Sets the tag to the given integer value.
void record(stat_id_t stat, T &&value)
Records an already computed value.
void register_stat(const std::string &name, std::unique_ptr< Statistics > stat)
Registers a Statistics object to the named slot.
std::unordered_map< std::string, TagContainer > m_TagLookup
TODO maybe we should solve this with a variant which does the dispatch of expected type and tag.
A tag container combines a name with a shared pointer, which points to the tag value.
static TagContainer create_full_container(std::string name)
int get_value() const
Returns the current value of the tag. Requires the container to not be empty.
const std::string & get_name() const
returns the name of the associated tag
TEST_CASE("check errors for stats")
stat_id_t str_to_id(const std::string &str, const std::vector< StatisticMetaData > &names)
constexpr auto ssize(const C &c) -> std::common_type_t< std::ptrdiff_t, std::make_signed_t< decltype(c.size())>>
signed size free function. Taken from https://en.cppreference.com/w/cpp/iterator/size
void merge(const Statistics &other) override
Merges this statistics of another one of the same type and settings.
void record_int(long integer) override
nlohmann::json to_json() const override
Converts the statistics current value into a json object.
std::unique_ptr< Statistics > clone() const override