From b1ec26a9fd79c7101c0710641b40a72c98990797 Mon Sep 17 00:00:00 2001 From: Roland Conybeare Date: Thu, 8 Jan 2026 19:46:32 -0500 Subject: [PATCH] xo-facet: + FacetRegistry tidy --- include/xo/facet/FacetRegistry.hpp | 60 +++++++++++++----------------- utest/objectmodel.test.cpp | 30 +++++++-------- 2 files changed, 39 insertions(+), 51 deletions(-) diff --git a/include/xo/facet/FacetRegistry.hpp b/include/xo/facet/FacetRegistry.hpp index 862b14c..f57f421 100644 --- a/include/xo/facet/FacetRegistry.hpp +++ b/include/xo/facet/FacetRegistry.hpp @@ -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 @@ -72,12 +62,33 @@ namespace xo { * @tparam DRepr data representation type **/ template - void register_impl() { + static void register_impl() { static FacetImplType impl; - this->_register_impl(typeseq::id(), - typeseq::id(), - &impl); + instance()._register_impl(typeseq::id(), + typeseq::id(), + &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 + static inline const AFacet * impl_for(typeseq repr_id) { + return FacetRegistry::instance().lookup(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 @@ -126,27 +137,6 @@ namespace xo { std::unordered_map 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 - inline const AFacet * runtime_impl_for(typeseq repr_id) { - return FacetRegistry::instance().lookup(repr_id); - } - - /** Convenience function for registration - * - * @tparam AFacet abstract facet type - * @tparam DRepr data representation type - **/ - template - inline void register_facet_impl() { - FacetRegistry::instance().register_impl(); - } - } /*namespace facet*/ } /*namespace xo*/ diff --git a/utest/objectmodel.test.cpp b/utest/objectmodel.test.cpp index 5566ff9..b4c1bc3 100644 --- a/utest/objectmodel.test.cpp +++ b/utest/objectmodel.test.cpp @@ -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(); - register_facet_impl(); + FacetRegistry::register_impl(); + FacetRegistry::register_impl(); REQUIRE(registry.contains(typeseq::id(), typeseq::id())); @@ -424,21 +422,21 @@ namespace xo { TEST_CASE("registry-lookup-1", "[facet][registry]") { // ensure registered - register_facet_impl(); - register_facet_impl(); + FacetRegistry::register_impl(); + FacetRegistry::register_impl(); // runtime lookup using typeseq - typeseq rect_id = typeseq::id(); + typeseq rect_id = typeseq::id(); typeseq polar_id = typeseq::id(); - const AComplex * rect_impl = runtime_impl_for(rect_id); - const AComplex * polar_impl = runtime_impl_for(polar_id); + const AComplex * rect_impl = FacetRegistry::impl_for(rect_id); + const AComplex * polar_impl = FacetRegistry::impl_for(polar_id); - REQUIRE(rect_impl != nullptr); + REQUIRE( rect_impl != nullptr); REQUIRE(polar_impl != nullptr); // use implementations - DRectCoords z1{1.0, 0.0}; + DRectCoords z1{1.0, 0.0}; DPolarCoords z2{0.0, 1.0}; REQUIRE(rect_impl->xcoord(&z1) == 1.0); @@ -454,8 +452,8 @@ namespace xo { // given obj with unknown repr type, // look up implementation at runtime - register_facet_impl(); - register_facet_impl(); + FacetRegistry::register_impl(); + FacetRegistry::register_impl(); // 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(repr1); - const AComplex * impl2 = runtime_impl_for(repr2); + const AComplex * impl1 = FacetRegistry::impl_for(repr1); + const AComplex * impl2 = FacetRegistry::impl_for(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(typeseq::id()); + const AComplex * impl = FacetRegistry::impl_for(typeseq::id()); REQUIRE(impl == nullptr); }