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 onStop() = 0;
virtual std::string_view name() const noexcept = 0;
virtual std::string_view version() const noexcept = 0;
};
} // namespace vl::log
+18 -20
View File
@@ -13,38 +13,33 @@ namespace vl::mod {
#define VL_MOD_SECTION __attribute__((used))
#endif
// ---- Contract concepts ----
// --- Contract concepts (NV2 + C2 + lifecycle) ---
// must have T(const Context&)
// Must have T(const Context&)
template<class T>
concept HasCtorWithContext =
requires(const Context& ctx) {
{ T(ctx) };
};
requires(const Context& ctx) { { T{ctx} }; };
// must have static constexpr std::string_view Name()
// Must provide compile-time identity (SV1)
template<class T>
concept HasStaticName =
requires {
{ T::Name() } -> std::same_as<std::string_view>;
};
requires { { T::Name() } -> std::same_as<std::string_view>; };
// must have static constexpr std::string_view Version()
template<class T>
concept HasStaticVersion =
requires {
{ T::Version() } -> std::same_as<std::string_view>;
};
requires { { T::Version() } -> std::same_as<std::string_view>; };
// must have lifecycle methods
// Must implement lifecycle
template<class T>
concept HasLifecycle =
requires(T& obj) {
{ obj.onStart() } -> std::same_as<void>;
{ obj.onStop() } -> std::same_as<void>;
requires(T& o) {
{ o.onStart() } -> 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>
requires HasCtorWithContext<T> &&
HasStaticName<T> &&
@@ -56,14 +51,17 @@ private:
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 {
kModRegMagic,
&_vl_factory
};
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;
};