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

71 lines
1.7 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 ----
// must have T(const Context&)
template<class T>
concept HasCtorWithContext =
requires(const Context& ctx) {
{ T(ctx) };
};
// 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 {
private:
static std::unique_ptr<Base> _vl_factory(const Context& 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 {
kModRegMagic,
&_vl_factory
};
public:
// no extra virtuals; T provides onStart()/onStop() and ctor(Context)
~Mod() override = default;
};
} // namespace vl::mod