xo-facet: + FacetRegistry tidy

This commit is contained in:
Roland Conybeare 2026-01-08 19:46:32 -05:00
commit b1ec26a9fd
2 changed files with 39 additions and 51 deletions

View file

@ -53,16 +53,6 @@ namespace xo {
return s_instance;
}
/** Number of registered (facet, repr) pairs **/
std::size_t size() const { return registry_.size(); }
/** Check if implementation is registered **/
bool contains(typeseq facet_id,
typeseq repr_id) const
{
return registry_.find(key_type(facet_id, repr_id)) != registry_.end();
}
/** Type-safe registration
*
* Registers the compile-time FacetImplementation<AFacet, DRepr>
@ -72,14 +62,35 @@ namespace xo {
* @tparam DRepr data representation type
**/
template <typename AFacet, typename DRepr>
void register_impl() {
static void register_impl() {
static FacetImplType<AFacet, DRepr> impl;
this->_register_impl(typeseq::id<AFacet>(),
instance()._register_impl(typeseq::id<AFacet>(),
typeseq::id<DRepr>(),
&impl);
}
/** Convenience function for runtime lookup
*
* @tparam AFacet abstract facet type
* @param repr_id typeseq for data representation
* @return pointer to AFacet implementation, or nullptr
**/
template <typename AFacet>
static inline const AFacet * impl_for(typeseq repr_id) {
return FacetRegistry::instance().lookup<AFacet>(repr_id);
}
/** Number of registered (facet, repr) pairs **/
std::size_t size() const { return registry_.size(); }
/** Check if implementation is registered **/
bool contains(typeseq facet_id,
typeseq repr_id) const
{
return registry_.find(key_type(facet_id, repr_id)) != registry_.end();
}
/** Type-safe lookup
*
* @tparam AFacet abstract facet type
@ -126,27 +137,6 @@ namespace xo {
std::unordered_map<key_type, const void *, KeyHash> registry_;
};
/** Convenience function for runtime lookup
*
* @tparam AFacet abstract facet type
* @param repr_id typeseq for data representation
* @return pointer to AFacet implementation, or nullptr
**/
template <typename AFacet>
inline const AFacet * runtime_impl_for(typeseq repr_id) {
return FacetRegistry::instance().lookup<AFacet>(repr_id);
}
/** Convenience function for registration
*
* @tparam AFacet abstract facet type
* @tparam DRepr data representation type
**/
template <typename AFacet, typename DRepr>
inline void register_facet_impl() {
FacetRegistry::instance().register_impl<AFacet, DRepr>();
}
} /*namespace facet*/
} /*namespace xo*/

View file

@ -21,8 +21,6 @@ namespace xo {
using xo::facet::valid_facet_implementation;
using xo::facet::FacetImplementation;
using xo::facet::FacetRegistry;
using xo::facet::runtime_impl_for;
using xo::facet::register_facet_impl;
using xo::facet::DVariantPlaceholder;
using xo::facet::OObject;
using xo::facet::valid_object_router;
@ -412,8 +410,8 @@ namespace xo {
auto & registry = FacetRegistry::instance();
// register implementations
register_facet_impl<AComplex, DRectCoords>();
register_facet_impl<AComplex, DPolarCoords>();
FacetRegistry::register_impl<AComplex, DRectCoords>();
FacetRegistry::register_impl<AComplex, DPolarCoords>();
REQUIRE(registry.contains(typeseq::id<AComplex>(),
typeseq::id<DRectCoords>()));
@ -424,17 +422,17 @@ namespace xo {
TEST_CASE("registry-lookup-1", "[facet][registry]")
{
// ensure registered
register_facet_impl<AComplex, DRectCoords>();
register_facet_impl<AComplex, DPolarCoords>();
FacetRegistry::register_impl<AComplex, DRectCoords>();
FacetRegistry::register_impl<AComplex, DPolarCoords>();
// runtime lookup using typeseq
typeseq rect_id = typeseq::id<DRectCoords>();
typeseq polar_id = typeseq::id<DPolarCoords>();
const AComplex * rect_impl = runtime_impl_for<AComplex>(rect_id);
const AComplex * polar_impl = runtime_impl_for<AComplex>(polar_id);
const AComplex * rect_impl = FacetRegistry::impl_for<AComplex>(rect_id);
const AComplex * polar_impl = FacetRegistry::impl_for<AComplex>(polar_id);
REQUIRE(rect_impl != nullptr);
REQUIRE( rect_impl != nullptr);
REQUIRE(polar_impl != nullptr);
// use implementations
@ -454,8 +452,8 @@ namespace xo {
// given obj<AComplex> with unknown repr type,
// look up implementation at runtime
register_facet_impl<AComplex, DRectCoords>();
register_facet_impl<AComplex, DPolarCoords>();
FacetRegistry::register_impl<AComplex, DRectCoords>();
FacetRegistry::register_impl<AComplex, DPolarCoords>();
// create type-erased objects
DRectCoords z1{1.0, 0.0};
@ -469,8 +467,8 @@ namespace xo {
typeseq repr2 = obj2._typeseq();
// lookup implementations
const AComplex * impl1 = runtime_impl_for<AComplex>(repr1);
const AComplex * impl2 = runtime_impl_for<AComplex>(repr2);
const AComplex * impl1 = FacetRegistry::impl_for<AComplex>(repr1);
const AComplex * impl2 = FacetRegistry::impl_for<AComplex>(repr2);
REQUIRE(impl1 != nullptr);
REQUIRE(impl2 != nullptr);
@ -485,7 +483,7 @@ namespace xo {
// lookup for unregistered type returns nullptr
struct DUnknown {};
const AComplex * impl = runtime_impl_for<AComplex>(typeseq::id<DUnknown>());
const AComplex * impl = FacetRegistry::impl_for<AComplex>(typeseq::id<DUnknown>());
REQUIRE(impl == nullptr);
}