Files
VoidLoaderApi/include/Mod/Mod.hpp
T
2025-10-20 00:53:04 +03:00

64 lines
1.8 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
// detection helpers
template<class X, class = void>
struct _has_ctor_ctx : std::false_type {};
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>; };
template<class T>
concept HasStaticVersion =
requires { { T::Version() } -> std::same_as<std::string_view>; };
template <class 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);
}
static inline const Registration _vl_reg VL_MOD_SECTION {
kModRegMagic,
&_vl_factory,
typeid(T).name(),
T::Name().data(),
T::Version().data()
};
public:
// T provides: T(const Context&), onStart(), onStop()
};
} // namespace vl::log