完成框架目录和模块重构;新增核心、单例、工具、加密及框架 SDK;统一任务、相机和 RPC 接口,并增加离线检测、相机标定、备份恢复、版本管理、崩溃守护和心跳监控功能。
97 lines
3.2 KiB
C++
97 lines
3.2 KiB
C++
#pragma once
|
|
|
|
#include <list>
|
|
#include <map>
|
|
#include <string>
|
|
#include <unordered_map>
|
|
#include <utility>
|
|
#include <vector>
|
|
|
|
#include <QObject>
|
|
#include <QMutex>
|
|
#include <QString>
|
|
#include <QtCore/qglobal.h>
|
|
|
|
#include <nlohmannjson.hpp>
|
|
#include <eui_frameworkSdk/TranslationInterfaces.h>
|
|
|
|
#define translationLRUCacheCapacity 3000
|
|
using json = nlohmann::json;
|
|
|
|
#ifndef EUI_SERVICE_API
|
|
# if defined(EUI_SERVICE_LIBRARY)
|
|
# define EUI_SERVICE_API Q_DECL_EXPORT
|
|
# else
|
|
# define EUI_SERVICE_API Q_DECL_IMPORT
|
|
# endif
|
|
#endif
|
|
|
|
class EUI_SERVICE_API translationLRUCache {
|
|
public:
|
|
explicit translationLRUCache(int capacity);
|
|
|
|
bool contains(const std::string& key, std::string& value);
|
|
void put(const std::string& key, const std::string& value);
|
|
bool remove(const std::string& key);
|
|
int size();
|
|
int capacity();
|
|
bool isEmpty();
|
|
void clear();
|
|
|
|
private:
|
|
using EntryList = std::list<std::pair<std::string, std::string>>;
|
|
|
|
QMutex mutex;
|
|
int maxCapacity;
|
|
// 标准 LRU 结构:链表头部为最近使用项,哈希表提供 O(1) 查找
|
|
EntryList entries;
|
|
std::unordered_map<std::string, EntryList::iterator> index;
|
|
};
|
|
|
|
class Translation : public eui_frameworkSdk::ITranslation {
|
|
public:
|
|
Translation();
|
|
private:
|
|
translationLRUCache translatedCache{ translationLRUCacheCapacity };
|
|
std::vector<std::string> sections;
|
|
std::vector<std::map<std::string, std::map<std::string, std::string>, std::less<>>> basicTranslations;
|
|
std::string currentLanguage = "zh-CN";
|
|
std::string translationFilePath_;
|
|
mutable QMutex mutex;
|
|
|
|
Translation(const Translation&) = delete;
|
|
Translation& operator=(const Translation&) = delete;
|
|
|
|
public:
|
|
EUI_SERVICE_API bool loadFromFile(const std::string& filePath);
|
|
EUI_SERVICE_API bool loadFromString(const std::string& jsonString);
|
|
EUI_SERVICE_API bool loadFromJson(const json& j);
|
|
EUI_SERVICE_API bool setLanguage(const std::string& lang) override;
|
|
EUI_SERVICE_API std::string getCurrentLanguage() const override;
|
|
EUI_SERVICE_API std::string translate(const std::string& key) override;
|
|
EUI_SERVICE_API std::string translate(const std::string& key, const std::string& lang) override;
|
|
EUI_SERVICE_API std::vector<std::pair<std::string, std::string>> getAllTranslateResult(const std::string& key);
|
|
EUI_SERVICE_API void addSection(const std::string& lang);
|
|
// 返回副本:返回引用会在 addSection 触发扩容时悬空
|
|
EUI_SERVICE_API std::vector<std::string> getSections() const override;
|
|
EUI_SERVICE_API bool hasTranslation(const std::string& key) const;
|
|
EUI_SERVICE_API std::vector<std::string> getAllKeys();
|
|
EUI_SERVICE_API bool saveToFile(const std::string& filePath);
|
|
|
|
private:
|
|
json originalJson;
|
|
|
|
bool parseJson_unsafe(const json& j);
|
|
void syncJson_unsafe();
|
|
std::string translate_unsafe(const std::string& key);
|
|
std::string translate_unsafe(const std::string& key, const std::string& lang);
|
|
bool addSection_unsafe(const std::string& lang);
|
|
};
|
|
|
|
EUI_SERVICE_API Translation& appTranslation();
|
|
EUI_SERVICE_API eui_frameworkSdk::ITranslation& appTranslationService();
|
|
|
|
#define TR(key) QString::fromStdString(appTranslation().translate(key))
|
|
|
|
|