diff --git a/include/Mod/Base.hpp b/include/Mod/Base.hpp index 9debe23..622f36f 100644 --- a/include/Mod/Base.hpp +++ b/include/Mod/Base.hpp @@ -9,11 +9,7 @@ public: virtual ~Base() = default; virtual void onStart() = 0; - virtual void onStop() = 0; - - virtual std::string_view name() const noexcept = 0; - virtual std::string_view version() const noexcept = 0; }; } // namespace vl::log diff --git a/include/Mod/Mod.hpp b/include/Mod/Mod.hpp index a3089dc..c222602 100644 --- a/include/Mod/Mod.hpp +++ b/include/Mod/Mod.hpp @@ -14,29 +14,43 @@ namespace vl::mod { #define VL_MOD_SECTION __attribute__((used)) #endif +// detection helpers +template +concept HasCtorWithContext = + std::is_constructible_v; + +template +concept HasStaticName = + requires { { T::Name() } -> std::same_as; }; + +template +concept HasStaticVersion = + requires { { T::Version() } -> std::same_as; }; + template class Mod : public Base { -private: - // ---- ENFORCE REQUIRED CONSTRUCTOR: T(const Context&) ---- - static_assert( - std::is_constructible_v, - "Any class derived from Mod must have a constructor: T(const Context&)" - ); + static_assert(HasCtorWithContext, + "Any class derived from Mod must have a constructor: T(const vl::log::Context&)"); + static_assert(HasStaticName, + "Any class derived from Mod must declare: static constexpr std::string_view Name()"); + static_assert(HasStaticVersion, + "Any class derived from Mod must declare: static constexpr std::string_view Version()"); - // factory used by the registry +private: static std::unique_ptr _vl_factory(const Context& ctx) { return std::make_unique(ctx); } - // auto-registration record (ELF) static inline const Registration _vl_reg VL_MOD_SECTION { kModRegMagic, &_vl_factory, - typeid(T).name() + typeid(T).name(), + T::Name().data(), + T::Version().data() }; public: - // ModBase behavior implemented by T through overrides + // T provides: T(const Context&), onStart(), onStop() }; } // namespace vl::log diff --git a/include/Mod/Registration.hpp b/include/Mod/Registration.hpp index 650b0ed..a615e30 100644 --- a/include/Mod/Registration.hpp +++ b/include/Mod/Registration.hpp @@ -8,15 +8,12 @@ namespace vl::mod { // Registration record stored in the module's custom section. // Core will scan this in the loaded .so to find available mods. struct Registration { - // magic helps Core validate the record const void* magic; - // factory that constructs a ModBase-derived object with Context std::unique_ptr (*factory)(const Context&); - // an optional RTTI-ish string (e.g., typeid(T).name()) - const char* typeName; + const char* typeName; // e.g., typeid(T).name() + const char* name; // COMPILE-TIME mod name (from T::Name()) + const char* version; // COMPILE-TIME mod version (from T::Version()) }; - -inline constexpr std::uintptr_t kModRegMagicValue = 0xBADC0FFEEULL; -inline const void* kModRegMagic = reinterpret_cast(kModRegMagicValue); - +inline constexpr std::uintptr_t kModRegMagicRaw = 0xBADC0FFEEULL; +inline const void* kModRegMagic = reinterpret_cast(kModRegMagicRaw); } // namespace vl::log