enforce ctx in constructor
Signed-off-by: Faumaray <faumaray@gmail.com>
This commit is contained in:
3 files changed
+29
-22
No files matched your search
+24
-10
@@ -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
|
||||
Reference in new issue
Block a user