69 lines
1.9 KiB
C++
69 lines
1.9 KiB
C++
#pragma once
|
|
#include <Mod/Base.hpp>
|
|
#include <Mod/Registration.hpp>
|
|
#include <Mod/Context.hpp>
|
|
#include <memory>
|
|
#include <type_traits>
|
|
#include <typeinfo>
|
|
namespace vl::mod {
|
|
|
|
#if defined(__ELF__)
|
|
#define VL_MOD_SECTION __attribute__((section("vlmods"), used))
|
|
#else
|
|
#define VL_MOD_SECTION __attribute__((used))
|
|
#endif
|
|
|
|
// --- Contract concepts (NV2 + C2 + lifecycle) ---
|
|
|
|
// Must have T(const Context&)
|
|
template<class T>
|
|
concept HasCtorWithContext =
|
|
requires(const Context& ctx) { { T{ctx} }; };
|
|
|
|
// Must provide compile-time identity (SV1)
|
|
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>; };
|
|
|
|
// Must implement lifecycle
|
|
template<class T>
|
|
concept HasLifecycle =
|
|
requires(T& o) {
|
|
{ o.onStart() } -> std::same_as<void>;
|
|
{ o.onStop() } -> std::same_as<void>;
|
|
};
|
|
|
|
// --- CRTP base ---
|
|
// NOTE: constraints placed on the template parameter itself (prevents
|
|
// GCC from evaluating them before T is complete).
|
|
template<class T>
|
|
requires HasCtorWithContext<T> &&
|
|
HasStaticName<T> &&
|
|
HasStaticVersion<T> &&
|
|
HasLifecycle<T>
|
|
class Mod : public Base {
|
|
private:
|
|
static std::unique_ptr<Base> _vl_factory(const Context& ctx) {
|
|
return std::make_unique<T>(ctx);
|
|
}
|
|
|
|
// Registration only stores a factory pointer — no early T usage.
|
|
static inline const Registration _vl_reg VL_MOD_SECTION {
|
|
kModRegMagic,
|
|
&_vl_factory
|
|
};
|
|
|
|
public:
|
|
// R2: runtime identity getters implemented here using NV2 statics
|
|
std::string_view name() const noexcept override { return T::Name(); }
|
|
std::string_view version() const noexcept override { return T::Version(); }
|
|
|
|
~Mod() override = default;
|
|
};
|
|
|
|
} // namespace vl::mod
|