#ifndef BASECONTROLLER_H #define BASECONTROLLER_H #include "Controller.h" #ifndef _MSC_VER #include #endif #include "Core/HttpManager.h" template class BaseController : public Controller { public: [[nodiscard]] const QString className() const override { return allocator.className(); } static QString getClassName() { return allocator.className(); } static QString getClassname(const char *mangled_name) { #ifndef _MSC_VER std::size_t len = 0; int status = 0; std::unique_ptr ptr(__cxxabiv1::__cxa_demangle(mangled_name, nullptr, &len, &status), &std::free); if (status == 0) { return {ptr.get()}; } return ""; #else auto pos = strstr(mangled_name, " "); if (pos == nullptr) return std::string{mangled_name}; else return std::string{pos + 1}; #endif } protected: BaseController() = default; ~BaseController() override = default; private: class Allocator { public: Allocator() { registerClass(); } [[nodiscard]] const QString &className() const { static QString class_name = getClassname(typeid(T).name()); return class_name; } template typename std::enable_if::value, void>::type registerClass() { HttpManager::instance().registerCtl(className(), []() -> BaseController * { return new T(); }); } template typename std::enable_if::value, void>::type registerClass() {} }; static Allocator allocator; }; template typename BaseController::Allocator BaseController::allocator; #endif // BASECONTROLLER_H