snitch-org / snitch

Lightweight C++20 testing framework.
Boost Software License 1.0
266 stars 10 forks source link

Remove shared reporter state (+add inplace_any and type_id) #172

Closed cschreib closed 6 months ago

cschreib commented 6 months ago

This PR removes the global state for class-based reporter (see, e.g., console or xml reporters). Before this PR, calling REGISTER_REPORTER("name", Type); would create a global std::optional<Type>, which would be assigned whenever the reporter is requested. This works, but means there can only be a single instance of the reporter at any given time. Although this should be OK for 99% of use cases, this is an unnecessary limitation. The registry class should own the reporter instance.

The difficulty is we don't know what type the reporter can have, as this is an open ended set (user can add new ones we don't know about). This is normally solved by using polymorphism and std::unique_ptr<BaseType>, but we cannot do this as it requires an allocation. Here, this is instead achieved using a type-erased inplace storage, inplace_any, equivalent of std::any but without any allocation. This storage is used to manage the reporter instance, which is created on demand and destroyed when no longer used.

codecov[bot] commented 6 months ago

Codecov Report

Attention: Patch coverage is 96.72131% with 2 lines in your changes are missing coverage. Please review.

Project coverage is 94.00%. Comparing base (f324947) to head (add06dc).

Additional details and impacted files [![Impacted file tree graph](https://app.codecov.io/gh/snitch-org/snitch/pull/172/graphs/tree.svg?width=650&height=150&src=pr&token=X422DE81PN&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=snitch-org)](https://app.codecov.io/gh/snitch-org/snitch/pull/172?src=pr&el=tree&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=snitch-org) ```diff @@ Coverage Diff @@ ## main #172 +/- ## ========================================== + Coverage 93.88% 94.00% +0.11% ========================================== Files 28 29 +1 Lines 1651 1700 +49 ========================================== + Hits 1550 1598 +48 - Misses 101 102 +1 ``` | [Files](https://app.codecov.io/gh/snitch-org/snitch/pull/172?dropdown=coverage&src=pr&el=tree&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=snitch-org) | Coverage Δ | | |---|---|---| | [include/snitch/snitch\_any.hpp](https://app.codecov.io/gh/snitch-org/snitch/pull/172?src=pr&el=tree&filepath=include%2Fsnitch%2Fsnitch_any.hpp&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=snitch-org#diff-aW5jbHVkZS9zbml0Y2gvc25pdGNoX2FueS5ocHA=) | `100.00% <100.00%> (ø)` | | | [include/snitch/snitch\_registry.hpp](https://app.codecov.io/gh/snitch-org/snitch/pull/172?src=pr&el=tree&filepath=include%2Fsnitch%2Fsnitch_registry.hpp&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=snitch-org#diff-aW5jbHVkZS9zbml0Y2gvc25pdGNoX3JlZ2lzdHJ5LmhwcA==) | `86.36% <100.00%> (+3.50%)` | :arrow_up: | | [src/snitch\_registry.cpp](https://app.codecov.io/gh/snitch-org/snitch/pull/172?src=pr&el=tree&filepath=src%2Fsnitch_registry.cpp&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=snitch-org#diff-c3JjL3NuaXRjaF9yZWdpc3RyeS5jcHA=) | `95.13% <100.00%> (ø)` | | | [src/snitch\_reporter\_console.cpp](https://app.codecov.io/gh/snitch-org/snitch/pull/172?src=pr&el=tree&filepath=src%2Fsnitch_reporter_console.cpp&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=snitch-org#diff-c3JjL3NuaXRjaF9yZXBvcnRlcl9jb25zb2xlLmNwcA==) | `97.14% <100.00%> (-0.08%)` | :arrow_down: | | [include/snitch/snitch\_type\_id.hpp](https://app.codecov.io/gh/snitch-org/snitch/pull/172?src=pr&el=tree&filepath=include%2Fsnitch%2Fsnitch_type_id.hpp&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=snitch-org#diff-aW5jbHVkZS9zbml0Y2gvc25pdGNoX3R5cGVfaWQuaHBw) | `50.00% <50.00%> (ø)` | | ... and [3 files with indirect coverage changes](https://app.codecov.io/gh/snitch-org/snitch/pull/172/indirect-changes?src=pr&el=tree-more&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=snitch-org) ------ [Continue to review full report in Codecov by Sentry](https://app.codecov.io/gh/snitch-org/snitch/pull/172?dropdown=coverage&src=pr&el=continue&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=snitch-org). > **Legend** - [Click here to learn more](https://docs.codecov.io/docs/codecov-delta?utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=snitch-org) > `Δ = absolute (impact)`, `ø = not affected`, `? = missing data` > Powered by [Codecov](https://app.codecov.io/gh/snitch-org/snitch/pull/172?dropdown=coverage&src=pr&el=footer&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=snitch-org). Last update [f324947...add06dc](https://app.codecov.io/gh/snitch-org/snitch/pull/172?dropdown=coverage&src=pr&el=lastupdated&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=snitch-org). Read the [comment docs](https://docs.codecov.io/docs/pull-request-comments?utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=snitch-org).