enforce ctx in constructor

Signed-off-by: Faumaray <faumaray@gmail.com>
This commit is contained in:
faumaray committed 2025-10-20 00:43:10 +03:00
1 parent c2f423e4a5
commit 9abecd9fe9
3 files changed
+29 -22

No files matched your search

-4
View File
@@ -9,11 +9,7 @@ public:
virtual ~Base() = default;
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
+24 -10
View File
@@ -14,29 +14,43 @@ namespace vl::mod {
#define VL_MOD_SECTION __attribute__((used))
#endif
// detection helpers
template <class T>
concept HasCtorWithContext =
std::is_constructible_v<T, const Context&>;
template <class T>
concept HasStaticName =
requires { { T::Name() } -> std::same_as<std::string_view>; };
template <class T>
concept HasStaticVersion =
requires { { T::Version() } -> std::same_as<std::string_view>; };
template <class T>
class Mod : public Base {
private:
// ---- 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&)"
);
static_assert(HasCtorWithContext<T>,
"Any class derived from Mod<T> must have a constructor: T(const vl::log::Context&)");
static_assert(HasStaticName<T>,
"Any class derived from Mod<T> must declare: static constexpr std::string_view Name()");
static_assert(HasStaticVersion<T>,
"Any class derived from Mod<T> must declare: static constexpr std::string_view Version()");
// factory used by the registry
private:
static std::unique_ptr<Base> _vl_factory(const Context& ctx) {
return std::make_unique<T>(ctx);
}
// auto-registration record (ELF)
static inline const Registration _vl_reg VL_MOD_SECTION {
kModRegMagic,
&_vl_factory,
typeid(T).name()
typeid(T).name(),
T::Name().data(),
T::Version().data()
};
public:
// ModBase behavior implemented by T through overrides
// T provides: T(const Context&), onStart(), onStop()
};
} // namespace vl::log
+5 -8
View File
@@ -8,15 +8,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 {
// magic helps Core validate the record
const void* magic;
// factory that constructs a ModBase-derived object with Context
std::unique_ptr<Base> (*factory)(const Context&);
// an optional RTTI-ish string (e.g., typeid(T).name())
const char* typeName;
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 kModRegMagicValue = 0xBADC0FFEEULL;
inline const void* kModRegMagic = reinterpret_cast<const void*>(kModRegMagicValue);
inline constexpr std::uintptr_t kModRegMagicRaw = 0xBADC0FFEEULL;
inline const void* kModRegMagic = reinterpret_cast<const void*>(kModRegMagicRaw);
} // namespace vl::log