enforce ctx in constructor

Signed-off-by: Faumaray <faumaray@gmail.com>
This commit is contained in:
faumaray committed 2025-10-20 01:14:13 +03:00
1 parent 02a077431d
commit 0b5400e6f3
2 files changed
+21 -20

No files matched your search

+3
View File
@@ -10,6 +10,9 @@ public:
virtual void onStart() = 0; virtual void onStart() = 0;
virtual void onStop() = 0; virtual void onStop() = 0;
virtual std::string_view name() const noexcept = 0;
virtual std::string_view version() const noexcept = 0;
}; };
} // namespace vl::log } // namespace vl::log
+18 -20
View File
@@ -13,38 +13,33 @@ namespace vl::mod {
#define VL_MOD_SECTION __attribute__((used)) #define VL_MOD_SECTION __attribute__((used))
#endif #endif
// ---- Contract concepts ---- // --- Contract concepts (NV2 + C2 + lifecycle) ---
// must have T(const Context&) // Must have T(const Context&)
template<class T> template<class T>
concept HasCtorWithContext = concept HasCtorWithContext =
requires(const Context& ctx) { requires(const Context& ctx) { { T{ctx} }; };
{ T(ctx) };
};
// must have static constexpr std::string_view Name() // Must provide compile-time identity (SV1)
template<class T> template<class T>
concept HasStaticName = concept HasStaticName =
requires { requires { { T::Name() } -> std::same_as<std::string_view>; };
{ T::Name() } -> std::same_as<std::string_view>;
};
// must have static constexpr std::string_view Version()
template<class T> template<class T>
concept HasStaticVersion = concept HasStaticVersion =
requires { requires { { T::Version() } -> std::same_as<std::string_view>; };
{ T::Version() } -> std::same_as<std::string_view>;
};
// must have lifecycle methods // Must implement lifecycle
template<class T> template<class T>
concept HasLifecycle = concept HasLifecycle =
requires(T& obj) { requires(T& o) {
{ obj.onStart() } -> std::same_as<void>; { o.onStart() } -> std::same_as<void>;
{ obj.onStop() } -> std::same_as<void>; { o.onStop() } -> std::same_as<void>;
}; };
// ---- CRTP base with delayed constraints ---- // --- CRTP base ---
// NOTE: constraints placed on the template parameter itself (prevents
// GCC from evaluating them before T is complete).
template<class T> template<class T>
requires HasCtorWithContext<T> && requires HasCtorWithContext<T> &&
HasStaticName<T> && HasStaticName<T> &&
@@ -56,14 +51,17 @@ private:
return std::make_unique<T>(ctx); return std::make_unique<T>(ctx);
} }
// NOTE: no typeid(T), no T::Name()/Version() here — T is incomplete now. // Registration only stores a factory pointer — no early T usage.
static inline const Registration _vl_reg VL_MOD_SECTION { static inline const Registration _vl_reg VL_MOD_SECTION {
kModRegMagic, kModRegMagic,
&_vl_factory &_vl_factory
}; };
public: public:
// no extra virtuals; T provides onStart()/onStop() and ctor(Context) // R2: runtime identity getters implemented here using NV2 statics
std::string_view name() const noexcept override { return T::Name(); }
std::string_view version() const noexcept override { return T::Version(); }
~Mod() override = default; ~Mod() override = default;
}; };