diff --git a/include/Mod/Mod.hpp b/include/Mod/Mod.hpp index fc286be..26044fc 100644 --- a/include/Mod/Mod.hpp +++ b/include/Mod/Mod.hpp @@ -13,33 +13,58 @@ namespace vl::mod { #define VL_MOD_SECTION __attribute__((used)) #endif -// ---- Contract Concepts ---- +// ---- Contract concepts ---- +// must have T(const Context&) +template +concept HasCtorWithContext = + requires(const Context& ctx) { + { T(ctx) }; + }; -template +// must have static constexpr std::string_view Name() +template +concept HasStaticName = + requires { + { T::Name() } -> std::same_as; + }; + +// must have static constexpr std::string_view Version() +template +concept HasStaticVersion = + requires { + { T::Version() } -> std::same_as; + }; + +// must have lifecycle methods +template +concept HasLifecycle = + requires(T& obj) { + { obj.onStart() } -> std::same_as; + { obj.onStop() } -> std::same_as; + }; + +// ---- CRTP base with delayed constraints ---- +template + requires HasCtorWithContext && + HasStaticName && + HasStaticVersion && + HasLifecycle class Mod : public Base { private: static std::unique_ptr _vl_factory(const Context& ctx) { return std::make_unique(ctx); } + // NOTE: no typeid(T), no T::Name()/Version() here — T is incomplete now. static inline const Registration _vl_reg VL_MOD_SECTION { kModRegMagic, - &_vl_factory, - typeid(T).name(), - T::Name().data(), - T::Version().data() + &_vl_factory }; public: - Mod(const Context& ctx) : m_ctx(ctx){}; - virtual void onStart() = 0; - virtual void onStop() = 0; - virtual ~Mod() = default; - virtual constexpr std::string_view Name() = 0; - virtual constexpr std::string_view Version() = 0; - Context m_ctx; - // T provides: T(const Context&), onStart(), onStop() + // no extra virtuals; T provides onStart()/onStop() and ctor(Context) + ~Mod() override = default; }; -} // namespace vl::log +} // namespace vl::mod diff --git a/include/Mod/Registration.hpp b/include/Mod/Registration.hpp index a615e30..1fa0f15 100644 --- a/include/Mod/Registration.hpp +++ b/include/Mod/Registration.hpp @@ -5,15 +5,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 { const void* magic; std::unique_ptr (*factory)(const Context&); - 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 kModRegMagicRaw = 0xBADC0FFEEULL; inline const void* kModRegMagic = reinterpret_cast(kModRegMagicRaw); + } // namespace vl::log