kunlang_tbox/include/config/TboxConfigClient.h

49 lines
1.2 KiB
C
Raw Normal View History

2026-02-06 09:03:24 +08:00
#pragma once
#include <optional>
#include <string>
#include <string_view>
#include <unordered_map>
#include "nlohmann/json.hpp"
// 前置声明,避免头文件膨胀
namespace httplib
{
class Client;
}
class TboxConfigClient
{
public:
explicit TboxConfigClient(std::string server = "127.0.0.1", int port = 18080);
// ================= 拉取配置 =================
bool fetch();
uint64_t version() const;
bool isDirty() const;
// ================= 读取 =================
std::optional<std::string> getString(std::string_view semantic) const;
std::optional<int> getInt(std::string_view semantic) const;
std::optional<bool> getBool(std::string_view semantic) const;
// ================= 修改(仅本地 staging =================
void setString(std::string_view semantic, const std::string& v);
void setInt(std::string_view semantic, int v);
void setBool(std::string_view semantic, bool v);
// ================= 提交并确认 =================
bool commitAndFetch();
private:
std::string server_;
int port_;
uint64_t version_{0};
bool dirty_{false};
std::unordered_map<std::string, nlohmann::json> cache_;
std::unordered_map<std::string, nlohmann::json> pending_;
};