Files
Renderive/YSGraphic_Core/base/Global.h
T
2026-07-23 09:35:29 +08:00

115 lines
3.7 KiB
C++

#pragma once
#include "../GlobalTypes.h"
#include "Core/Base/global_include.h"
#include <functional>
#include <memory>
#include <QThread>
#include <qrgb.h>
extern int qInitResources_resource();
namespace asio {
class io_context;
}
namespace YSG {
struct TimerThreadPrivate;
class LIB_DECL TimerThread : public QThread {
Q_OBJECT
public:
QList<Plot*> mPlots;
TimerThread();
~TimerThread() override;
void run() override;
void post(std::function<void()> task);
void postCpu(std::function<void()> task);
void removePlot(Plot* plot);
void shutdown();
bool isSchedulerThread() const;
asio::io_context& ioContext();
private:
void joinCpuPool();
void postAndWait(std::function<void()> task);
void removePlotOnScheduler(Plot* plot);
void shutdownOnScheduler();
void stopSchedulerOnScheduler();
std::unique_ptr<TimerThreadPrivate> d;
};
class LIB_DECL Global : public QObject, public Psc::Singleton<Global> {
public:
int r = qInitResources_resource();
int mTimerId{};
QMap<QString, TimerThread*> mTimerThreadMap;
int mInterval = 10;
void startAllTimeThread();
void timerEvent(QTimerEvent *event) override;
QVector<QRgb> mColorMap = getTurboColorGradient();
};
}
#define QLOG_POS QString(__func__) + QString(" ") + __FILE__ + QString(":") + QString::number(__LINE__)
template<typename, typename = void>
struct has_qdebug_stream : std::false_type {};
template<typename T>
struct has_qdebug_stream<T,
std::void_t<
decltype(
std::declval<QDebug&>() << std::declval<T>()
)
>
> : std::true_type {};
template <typename T>
QString to_qstring(const T& value)
{
using C = std::decay_t<T>;
if constexpr (has_qdebug_stream<C>::value)
{
QString s;
QDebug dbg(&s);
dbg << value;
return s;
}
else
{
static_assert(sizeof(C) == 0,
"Psc: to_qstring type error");
}
return "";
}
// // 通用模板
// template <typename T>
// QString to_qstring(const T& value) {
// static_assert(std::is_arithmetic<T>::value, "T must be an arithmetic type");
// return QString::number(value);
// }
//
// inline QString to_qstring(const char* value) {
// return value;
// }
//
// inline QString to_qstring(const QString& value) {
// return value;
// }
// 特化模板:处理 bool 类型
template <>
inline QString to_qstring(const bool& value) {
return value ? QStringLiteral("true") : QStringLiteral("false");
}
#define VAR_QSTR(P) ("[" + to_qstring(#P) + "]:" + to_qstring(P) + to_qstring(", "))
#define VAR_QSTR_1(P1) VAR_QSTR(P1)
#define VAR_QSTR_2(P1, P2) VAR_QSTR_1(P1) + VAR_QSTR(P2)
#define VAR_QSTR_3(P1, P2, P3) VAR_QSTR_2(P1, P2) + VAR_QSTR(P3)
#define VAR_QSTR_4(P1, P2, P3, P4) VAR_QSTR_3(P1, P2, P3) + VAR_QSTR(P4)
#define VAR_QSTR_5(P1, P2, P3, P4, P5) VAR_QSTR_4(P1, P2, P3, P4) + VAR_QSTR(P5)
#define VAR_QSTR_6(P1, P2, P3, P4, P5, P6) VAR_QSTR_5(P1, P2, P3, P4, P5) + VAR_QSTR(P6)
#define VAR_QSTR_7(P1, P2, P3, P4, P5, P6, P7) VAR_QSTR_6(P1, P2, P3, P4, P5, P6) + VAR_QSTR(P7)
#define VAR_QSTR_8(P1, P2, P3, P4, P5, P6, P7, P8) VAR_QSTR_7(P1, P2, P3, P4, P5, P6, P7) + VAR_QSTR(P8)
#define VAR_QSTR_9(P1, P2, P3, P4, P5, P6, P7, P8, P9) VAR_QSTR_8(P1, P2, P3, P4, P5, P6, P7, P8) + VAR_QSTR(P9)
#define VAR_QSTR_10(P1, P2, P3, P4, P5, P6, P7, P8, P9, P10) VAR_QSTR_9(P1, P2, P3, P4, P5, P6, P7, P8, P9) + VAR_QSTR(P10)