You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

74 lines
1.9 KiB

3 days ago
#ifndef BASECONTROLLER_H
#define BASECONTROLLER_H
#include "Controller.h"
#ifndef _MSC_VER
#include <cxxabi.h>
#endif
#include "Core/HttpManager.h"
template<typename T>
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<char, decltype(&std::free)>
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<T>(); }
[[nodiscard]] const QString &className() const
{
static QString class_name = getClassname(typeid(T).name());
return class_name;
}
template<typename D>
typename std::enable_if<std::is_default_constructible<D>::value, void>::type registerClass()
{
HttpManager::instance().registerCtl(className(),
[]() -> BaseController * { return new T(); });
}
template<typename D>
typename std::enable_if<!std::is_default_constructible<D>::value, void>::type registerClass()
{}
};
static Allocator allocator;
};
template<typename T>
typename BaseController<T>::Allocator BaseController<T>::allocator;
#endif // BASECONTROLLER_H