diff --git a/include/Mod/Mod.hpp b/include/Mod/Mod.hpp index eabef58..da432dd 100644 --- a/include/Mod/Mod.hpp +++ b/include/Mod/Mod.hpp @@ -14,35 +14,40 @@ namespace vl::mod { #define VL_MOD_SECTION __attribute__((used)) #endif -// detection helpers -template -struct _has_ctor_ctx : std::false_type {}; +// ---- Contract Concepts ---- +template +concept HasCtorWithContext = + requires(const Context& ctx) { + { T(ctx) }; + }; -template -struct _has_ctor_ctx()))>> : std::true_type {}; - -template -inline constexpr bool has_ctor_ctx_v = _has_ctor_ctx::value; - -// -------- Detection: T must provide Name()/Version() returning std::string_view (SV1) -------- template concept HasStaticName = - requires { { T::Name() } -> std::same_as; }; + requires { + { T::Name() } -> std::same_as; + }; template concept HasStaticVersion = - requires { { T::Version() } -> std::same_as; }; + requires { + { T::Version() } -> std::same_as; + }; -template +template +concept HasLifecycle = + requires(T obj) { + { obj.onStart() } -> std::same_as; + { obj.onStop() } -> std::same_as; + }; + +template < + class T +> requires + HasCtorWithContext && + HasStaticName && + HasStaticVersion && + HasLifecycle class Mod : public Base { - // Compile-time contract: - static_assert(has_ctor_ctx_v, - "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()"); - private: static std::unique_ptr _vl_factory(const Context& ctx) { return std::make_unique(ctx); @@ -57,6 +62,9 @@ private: }; public: + virtual void onStart() = 0; + virtual void onStop() = 0; + virtual ~Mod() = default; // T provides: T(const Context&), onStart(), onStop() };