enforce ctx in constructor
Signed-off-by: Faumaray <faumaray@gmail.com>
This commit is contained in:
2 files changed
+42
-20
No files matched your search
+40
-15
@@ -13,33 +13,58 @@ namespace vl::mod {
|
|||||||
#define VL_MOD_SECTION __attribute__((used))
|
#define VL_MOD_SECTION __attribute__((used))
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
// ---- Contract Concepts ----
|
// ---- Contract concepts ----
|
||||||
|
|
||||||
|
// must have T(const Context&)
|
||||||
|
template<class T>
|
||||||
|
concept HasCtorWithContext =
|
||||||
|
requires(const Context& ctx) {
|
||||||
|
{ T(ctx) };
|
||||||
|
};
|
||||||
|
|
||||||
template <class T>
|
// must have static constexpr std::string_view Name()
|
||||||
|
template<class T>
|
||||||
|
concept HasStaticName =
|
||||||
|
requires {
|
||||||
|
{ T::Name() } -> std::same_as<std::string_view>;
|
||||||
|
};
|
||||||
|
|
||||||
|
// must have static constexpr std::string_view Version()
|
||||||
|
template<class T>
|
||||||
|
concept HasStaticVersion =
|
||||||
|
requires {
|
||||||
|
{ T::Version() } -> std::same_as<std::string_view>;
|
||||||
|
};
|
||||||
|
|
||||||
|
// must have lifecycle methods
|
||||||
|
template<class T>
|
||||||
|
concept HasLifecycle =
|
||||||
|
requires(T& obj) {
|
||||||
|
{ obj.onStart() } -> std::same_as<void>;
|
||||||
|
{ obj.onStop() } -> std::same_as<void>;
|
||||||
|
};
|
||||||
|
|
||||||
|
// ---- CRTP base with delayed constraints ----
|
||||||
|
template<class T>
|
||||||
|
requires HasCtorWithContext<T> &&
|
||||||
|
HasStaticName<T> &&
|
||||||
|
HasStaticVersion<T> &&
|
||||||
|
HasLifecycle<T>
|
||||||
class Mod : public Base {
|
class Mod : public Base {
|
||||||
private:
|
private:
|
||||||
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);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// NOTE: no typeid(T), no T::Name()/Version() here — T is incomplete now.
|
||||||
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(),
|
|
||||||
T::Name().data(),
|
|
||||||
T::Version().data()
|
|
||||||
};
|
};
|
||||||
|
|
||||||
public:
|
public:
|
||||||
Mod(const Context& ctx) : m_ctx(ctx){};
|
// no extra virtuals; T provides onStart()/onStop() and ctor(Context)
|
||||||
virtual void onStart() = 0;
|
~Mod() override = default;
|
||||||
virtual void onStop() = 0;
|
|
||||||
virtual ~Mod() = default;
|
|
||||||
virtual constexpr std::string_view Name() = 0;
|
|
||||||
virtual constexpr std::string_view Version() = 0;
|
|
||||||
Context m_ctx;
|
|
||||||
// T provides: T(const Context&), onStart(), onStop()
|
|
||||||
};
|
};
|
||||||
|
|
||||||
} // namespace vl::log
|
} // namespace vl::mod
|
||||||
@@ -5,15 +5,12 @@
|
|||||||
|
|
||||||
namespace vl::mod {
|
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 {
|
struct Registration {
|
||||||
const void* magic;
|
const void* magic;
|
||||||
std::unique_ptr<Base> (*factory)(const Context&);
|
std::unique_ptr<Base> (*factory)(const Context&);
|
||||||
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 kModRegMagicRaw = 0xBADC0FFEEULL;
|
inline constexpr std::uintptr_t kModRegMagicRaw = 0xBADC0FFEEULL;
|
||||||
inline const void* kModRegMagic = reinterpret_cast<const void*>(kModRegMagicRaw);
|
inline const void* kModRegMagic = reinterpret_cast<const void*>(kModRegMagicRaw);
|
||||||
|
|
||||||
} // namespace vl::log
|
} // namespace vl::log
|
||||||
Reference in new issue
Block a user