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:
|
public:
|
||||||
virtual ~Base() = default;
|
virtual ~Base() = default;
|
||||||
|
|
||||||
virtual void onStart(Context* /*ctx*/) = 0;
|
virtual void onStart() = 0;
|
||||||
|
|
||||||
virtual void onStop() = 0;
|
virtual void onStop() = 0;
|
||||||
|
|
||||||
|
|||||||
+12
-9
@@ -1,39 +1,42 @@
|
|||||||
#pragma once
|
#pragma once
|
||||||
#include <Mod/Base.hpp>
|
#include <Mod/Base.hpp>
|
||||||
#include <Mod/Registration.hpp>
|
#include <Mod/Registration.hpp>
|
||||||
|
#include <Mod/Context.hpp>
|
||||||
#include <memory>
|
#include <memory>
|
||||||
|
#include <type_traits>
|
||||||
#include <typeinfo>
|
#include <typeinfo>
|
||||||
|
|
||||||
namespace vl::mod {
|
namespace vl::mod {
|
||||||
|
|
||||||
// ---- platform section helpers (ELF) ----
|
|
||||||
#if defined(__ELF__)
|
#if defined(__ELF__)
|
||||||
#define VL_MOD_SECTION __attribute__((section("vlmods"), used))
|
#define VL_MOD_SECTION __attribute__((section("vlmods"), used))
|
||||||
#else
|
#else
|
||||||
// Fallback: keep the variable, but discovery code will need a different path on non-ELF.
|
|
||||||
#define VL_MOD_SECTION __attribute__((used))
|
#define VL_MOD_SECTION __attribute__((used))
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
// CRTP base: derive as `class MyMod : public Mod<MyMod> { ... };`
|
|
||||||
// Requires: `MyMod::MyMod(const Context&)` constructor.
|
|
||||||
template <class T>
|
template <class T>
|
||||||
class Mod : public Base {
|
class Mod : public Base {
|
||||||
private:
|
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) {
|
static std::unique_ptr<Base> _vl_factory(const Context& ctx) {
|
||||||
return std::make_unique<T>(ctx);
|
return std::make_unique<T>(ctx);
|
||||||
}
|
}
|
||||||
|
|
||||||
// One registration record per T in a well-known section.
|
// auto-registration record (ELF)
|
||||||
// This creates *no* runtime cost for mods beyond a single static object.
|
static inline const Registration _vl_reg VL_MOD_SECTION {
|
||||||
static inline const Registration _vl_reg VL_MOD_SECTION = {
|
|
||||||
kModRegMagic,
|
kModRegMagic,
|
||||||
&_vl_factory,
|
&_vl_factory,
|
||||||
typeid(T).name()
|
typeid(T).name()
|
||||||
};
|
};
|
||||||
|
|
||||||
public:
|
public:
|
||||||
// The rest (onStart/onStop/name/version/ctor) is implemented by T.
|
// ModBase behavior implemented by T through overrides
|
||||||
};
|
};
|
||||||
|
|
||||||
} // namespace vl::log
|
} // namespace vl::log
|
||||||
Reference in new issue
Block a user