enforce ctx in constructor

Signed-off-by: Faumaray <faumaray@gmail.com>
This commit is contained in:
faumaray committed 2025-10-20 01:09:53 +03:00
1 parent 766c612cf8
commit 02a077431d
2 files changed
+42 -20

No files matched your search

+40 -15
View File
@@ -13,33 +13,58 @@ namespace vl::mod {
#define VL_MOD_SECTION __attribute__((used))
#endif
// ---- Contract Concepts ----
// ---- Contract concepts ----
// must have T(const Context&)
template<class T>
concept HasCtorWithContext =
requires(const Context& ctx) {
{ T(ctx) };
};
template <class T>
// must have static constexpr std::string_view Name()
template<class T>
concept HasStaticName =
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>;
};
// must have lifecycle methods
template<class T>
concept HasLifecycle =
requires(T& obj) {
{ obj.onStart() } -> std::same_as<void>;
{ obj.onStop() } -> std::same_as<void>;
};
// ---- CRTP base with delayed constraints ----
template<class T>
requires HasCtorWithContext<T> &&
HasStaticName<T> &&
HasStaticVersion<T> &&
HasLifecycle<T>
class Mod : public Base {
private:
static std::unique_ptr<Base> _vl_factory(const Context& ctx) {
return std::make_unique<T>(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
+2 -5
View File
@@ -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<Base> (*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<const void*>(kModRegMagicRaw);
} // namespace vl::log