enforce ctx in constructor
Signed-off-by: Faumaray <faumaray@gmail.com>
This commit is contained in:
1 file changed
+29
-21
+29
-21
@@ -14,35 +14,40 @@ namespace vl::mod {
|
||||
#define VL_MOD_SECTION __attribute__((used))
|
||||
#endif
|
||||
|
||||
// detection helpers
|
||||
template<class X, class = void>
|
||||
struct _has_ctor_ctx : std::false_type {};
|
||||
// ---- Contract Concepts ----
|
||||
template<class T>
|
||||
concept HasCtorWithContext =
|
||||
requires(const Context& ctx) {
|
||||
{ T(ctx) };
|
||||
};
|
||||
|
||||
template<class X>
|
||||
struct _has_ctor_ctx<X, std::void_t<decltype(X(std::declval<const Context&>()))>> : std::true_type {};
|
||||
|
||||
template<class X>
|
||||
inline constexpr bool has_ctor_ctx_v = _has_ctor_ctx<X>::value;
|
||||
|
||||
// -------- Detection: T must provide Name()/Version() returning std::string_view (SV1) --------
|
||||
template<class T>
|
||||
concept HasStaticName =
|
||||
requires { { T::Name() } -> std::same_as<std::string_view>; };
|
||||
requires {
|
||||
{ T::Name() } -> std::same_as<std::string_view>;
|
||||
};
|
||||
|
||||
template<class T>
|
||||
concept HasStaticVersion =
|
||||
requires { { T::Version() } -> std::same_as<std::string_view>; };
|
||||
requires {
|
||||
{ T::Version() } -> std::same_as<std::string_view>;
|
||||
};
|
||||
|
||||
template <class T>
|
||||
template<class T>
|
||||
concept HasLifecycle =
|
||||
requires(T obj) {
|
||||
{ obj.onStart() } -> std::same_as<void>;
|
||||
{ obj.onStop() } -> std::same_as<void>;
|
||||
};
|
||||
|
||||
template <
|
||||
class T
|
||||
> requires
|
||||
HasCtorWithContext<T> &&
|
||||
HasStaticName<T> &&
|
||||
HasStaticVersion<T> &&
|
||||
HasLifecycle<T>
|
||||
class Mod : public Base {
|
||||
// Compile-time contract:
|
||||
static_assert(has_ctor_ctx_v<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()");
|
||||
|
||||
private:
|
||||
static std::unique_ptr<Base> _vl_factory(const Context& ctx) {
|
||||
return std::make_unique<T>(ctx);
|
||||
@@ -57,6 +62,9 @@ private:
|
||||
};
|
||||
|
||||
public:
|
||||
virtual void onStart() = 0;
|
||||
virtual void onStop() = 0;
|
||||
virtual ~Mod() = default;
|
||||
// T provides: T(const Context&), onStart(), onStop()
|
||||
};
|
||||
|
||||
|
||||
Reference in new issue
Block a user