From c2f423e4a5af0e6b2a3d8e196343048f4d2c534a Mon Sep 17 00:00:00 2001 From: Faumaray Date: Mon, 20 Oct 2025 00:40:41 +0300 Subject: [PATCH] enforce ctx in constructor Signed-off-by: Faumaray --- include/Mod/Base.hpp | 2 +- include/Mod/Mod.hpp | 21 ++++++++++++--------- 2 files changed, 13 insertions(+), 10 deletions(-) diff --git a/include/Mod/Base.hpp b/include/Mod/Base.hpp index 12d0746..9debe23 100644 --- a/include/Mod/Base.hpp +++ b/include/Mod/Base.hpp @@ -8,7 +8,7 @@ class Base { public: virtual ~Base() = default; - virtual void onStart(Context* /*ctx*/) = 0; + virtual void onStart() = 0; virtual void onStop() = 0; diff --git a/include/Mod/Mod.hpp b/include/Mod/Mod.hpp index efe7733..a3089dc 100644 --- a/include/Mod/Mod.hpp +++ b/include/Mod/Mod.hpp @@ -1,39 +1,42 @@ #pragma once #include #include +#include #include +#include #include namespace vl::mod { -// ---- platform section helpers (ELF) ---- #if defined(__ELF__) #define VL_MOD_SECTION __attribute__((section("vlmods"), used)) #else - // Fallback: keep the variable, but discovery code will need a different path on non-ELF. #define VL_MOD_SECTION __attribute__((used)) #endif -// CRTP base: derive as `class MyMod : public Mod { ... };` -// Requires: `MyMod::MyMod(const Context&)` constructor. template class Mod : public Base { private: - // Factory thunk that Core will call via the registration record. + // ---- ENFORCE REQUIRED CONSTRUCTOR: T(const Context&) ---- + static_assert( + std::is_constructible_v, + "Any class derived from Mod must have a constructor: T(const Context&)" + ); + + // factory used by the registry static std::unique_ptr _vl_factory(const Context& ctx) { return std::make_unique(ctx); } - // One registration record per T in a well-known section. - // This creates *no* runtime cost for mods beyond a single static object. - static inline const Registration _vl_reg VL_MOD_SECTION = { + // auto-registration record (ELF) + static inline const Registration _vl_reg VL_MOD_SECTION { kModRegMagic, &_vl_factory, typeid(T).name() }; public: - // The rest (onStart/onStop/name/version/ctor) is implemented by T. + // ModBase behavior implemented by T through overrides }; } // namespace vl::log