| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143 |
- // 配置访问器实现 —— 语义与 Python `portcv.config.Config` 对齐(见 config_lite.hpp)。
- #include "portcv/config_lite.hpp"
- #include <cstddef>
- #include <string>
- #include <utility>
- #include <vector>
- namespace portcv {
- namespace {
- /// 缺失值单例(`get()` 查不到时返回它,避免返回临时对象造成悬垂引用)。
- const json::Value& missing() {
- static const json::Value value;
- return value;
- }
- /// 按 `a.b.c` 逐段深入;任一段缺失 / 中间不是对象 → 返回 nullptr。
- const json::Value* find(const json::Value& root, const std::string& path) {
- const json::Value* node = &root;
- std::size_t start = 0;
- while (true) {
- const std::size_t dot = path.find('.', start);
- const std::string part =
- dot == std::string::npos ? path.substr(start) : path.substr(start, dot - start);
- if (part.empty()) { return nullptr; } // 路径写错("a..b" / 结尾多一个点)
- if (!node->is_object() || !node->has(part)) { return nullptr; }
- node = &node->member(part);
- if (dot == std::string::npos) { return node; }
- start = dot + 1;
- }
- }
- } // namespace
- // ---------------------------------------------------------------- 构造
- bool Config::from_text(const std::string& text, Config* out, std::string* error) {
- if (out == nullptr) {
- if (error != nullptr) { *error = "Config::from_text: 输出参数为空指针"; }
- return false;
- }
- std::string local_error;
- json::Value root = json::parse(text, &local_error);
- if (!local_error.empty()) {
- if (error != nullptr) { *error = local_error; }
- return false;
- }
- if (!root.is_object()) {
- if (error != nullptr) { *error = "配置文件的根必须是 JSON 对象 { ... }"; }
- return false;
- }
- out->root_ = static_cast<json::Value&&>(root);
- if (error != nullptr) { error->clear(); }
- return true;
- }
- bool Config::from_file(const std::string& path, Config* out, std::string* error) {
- std::string local_error;
- std::string text;
- if (!json::read_file(path, &text, &local_error)) {
- if (error != nullptr) { *error = local_error; }
- return false;
- }
- if (!Config::from_text(text, out, &local_error)) {
- if (error != nullptr) { *error = path + ":" + local_error; }
- return false;
- }
- if (error != nullptr) { error->clear(); }
- return true;
- }
- // ---------------------------------------------------------------- 访问
- bool Config::has(const std::string& path) const {
- const json::Value* value = find(root_, path);
- return value != nullptr && !value->is_null();
- }
- const json::Value& Config::get(const std::string& path) const {
- const json::Value* value = find(root_, path);
- return value != nullptr ? *value : missing();
- }
- double Config::number(const std::string& path, double fallback) const {
- const json::Value& value = get(path);
- return value.is_number() ? value.number : fallback;
- }
- int Config::integer(const std::string& path, int fallback) const {
- const json::Value& value = get(path);
- if (!value.is_number()) { return fallback; }
- return static_cast<int>(value.number < 0.0 ? value.number - 0.5 : value.number + 0.5);
- }
- bool Config::boolean(const std::string& path, bool fallback) const {
- const json::Value& value = get(path);
- if (value.is_bool()) { return value.boolean; }
- if (value.is_number()) { return value.number != 0.0; } // 与 Python bool(value) 同口径
- return fallback;
- }
- std::string Config::text(const std::string& path, const std::string& fallback) const {
- const json::Value& value = get(path);
- return value.is_string() ? value.text : fallback;
- }
- std::vector<double> Config::numbers(const std::string& path) const {
- std::vector<double> out;
- const json::Value& value = get(path);
- if (!value.is_array()) { return out; }
- out.reserve(value.items.size());
- for (const json::Value& item : value.items) {
- out.push_back(item.is_number() ? item.number : 0.0);
- }
- return out;
- }
- std::vector<std::string> Config::strings(const std::string& path) const {
- std::vector<std::string> out;
- const json::Value& value = get(path);
- if (!value.is_array()) { return out; }
- out.reserve(value.items.size());
- for (const json::Value& item : value.items) {
- if (item.is_string()) { out.push_back(item.text); }
- }
- return out;
- }
- std::vector<std::pair<double, double>> Config::points(const std::string& path) const {
- std::vector<std::pair<double, double>> out;
- const json::Value& value = get(path);
- if (!value.is_array()) { return out; }
- out.reserve(value.items.size());
- for (const json::Value& item : value.items) {
- if (item.is_array() && item.items.size() >= 2 && item.items[0].is_number() &&
- item.items[1].is_number()) {
- out.emplace_back(item.items[0].number, item.items[1].number);
- }
- }
- return out;
- }
- } // namespace portcv
|