enforce ctx in constructor
Signed-off-by: Faumaray <faumaray@gmail.com>
This commit is contained in:
2 files changed
+13
-10
No files matched your search
@@ -8,7 +8,7 @@ class Base {
|
||||
public:
|
||||
virtual ~Base() = default;
|
||||
|
||||
virtual void onStart(Context* /*ctx*/) = 0;
|
||||
virtual void onStart() = 0;
|
||||
|
||||
virtual void onStop() = 0;
|
||||
|
||||
|
||||
+12
-9
@@ -1,39 +1,42 @@
|
||||
#pragma once
|
||||
#include <Mod/Base.hpp>
|
||||
#include <Mod/Registration.hpp>
|
||||
#include <Mod/Context.hpp>
|
||||
#include <memory>
|
||||
#include <type_traits>
|
||||
#include <typeinfo>
|
||||
|
||||
namespace vl::mod {
|
||||
|
||||
// ---- platform section helpers (ELF) ----
|
||||
#if defined(__ELF__)
|
||||
#define VL_MOD_SECTION __attribute__((section("vlmods"), used))
|
||||
#else
|
||||
// Fallback: keep the variable, but discovery code will need a different path on non-ELF.
|
||||
#define VL_MOD_SECTION __attribute__((used))
|
||||
#endif
|
||||
|
||||
// CRTP base: derive as `class MyMod : public Mod<MyMod> { ... };`
|
||||
// Requires: `MyMod::MyMod(const Context&)` constructor.
|
||||
template <class T>
|
||||
class Mod : public Base {
|
||||
private:
|
||||
// Factory thunk that Core will call via the registration record.
|
||||
// ---- ENFORCE REQUIRED CONSTRUCTOR: T(const Context&) ----
|
||||
static_assert(
|
||||
std::is_constructible_v<T, const Context&>,
|
||||
"Any class derived from Mod<T> must have a constructor: T(const Context&)"
|
||||
);
|
||||
|
||||
// factory used by the registry
|
||||
static std::unique_ptr<Base> _vl_factory(const Context& ctx) {
|
||||
return std::make_unique<T>(ctx);
|
||||
}
|
||||
|
||||
// One registration record per T in a well-known section.
|
||||
// This creates *no* runtime cost for mods beyond a single static object.
|
||||
static inline const Registration _vl_reg VL_MOD_SECTION = {
|
||||
// auto-registration record (ELF)
|
||||
static inline const Registration _vl_reg VL_MOD_SECTION {
|
||||
kModRegMagic,
|
||||
&_vl_factory,
|
||||
typeid(T).name()
|
||||
};
|
||||
|
||||
public:
|
||||
// The rest (onStart/onStop/name/version/ctor) is implemented by T.
|
||||
// ModBase behavior implemented by T through overrides
|
||||
};
|
||||
|
||||
} // namespace vl::log
|
||||
Reference in new issue
Block a user