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.
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 globalstd::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. Theregistry
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 ofstd::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.