#include "vslam/config.h" #include #include #include #include "vslam/log.h" namespace vslam { struct ConfigNode::Impl { YAML::Node node; std::string path; // "safety.scan.stop_distance" std::string origin; // ten file Impl() = default; Impl(YAML::Node n, std::string p, std::string o) : node(std::move(n)), path(std::move(p)), origin(std::move(o)) {} }; namespace { std::string JoinPath(const std::string& base, const std::string& key) { return base.empty() ? key : base + "." + key; } // Thong bao loi luon co ba manh: file, duong dan key day du, va ly do. // Thieu bat cu manh nao la nguoi doc phai di tim, va do chinh la thu lam // viec sua config trong ban ROS ton thoi gian. // // Nhan chuoi chu khong nhan ConfigNode::Impl: Impl la kieu private, ham tu do // trong namespace vo danh khong goi ten no duoc. std::string Where(const std::string& origin, const std::string& path, const std::string& key) { std::ostringstream oss; oss << origin << ": " << JoinPath(path, key); return oss.str(); } } // namespace ConfigNode::ConfigNode() : impl_(std::make_unique()) {} ConfigNode::ConfigNode(std::unique_ptr impl) : impl_(std::move(impl)) {} ConfigNode::ConfigNode(const ConfigNode& o) : impl_(std::make_unique(*o.impl_)) {} ConfigNode::ConfigNode(ConfigNode&&) noexcept = default; ConfigNode& ConfigNode::operator=(const ConfigNode& o) { if (this != &o) impl_ = std::make_unique(*o.impl_); return *this; } ConfigNode& ConfigNode::operator=(ConfigNode&&) noexcept = default; ConfigNode::~ConfigNode() = default; ConfigNode ConfigNode::LoadFile(const std::string& path) { try { YAML::Node root = YAML::LoadFile(path); return ConfigNode(std::make_unique(std::move(root), "", path)); } catch (const YAML::BadFile&) { throw ConfigError("khong mo duoc file cau hinh: " + path); } catch (const YAML::Exception& e) { throw ConfigError("YAML sai cu phap trong " + path + ": " + e.what()); } } ConfigNode ConfigNode::LoadString(const std::string& yaml, std::string origin) { try { YAML::Node root = YAML::Load(yaml); return ConfigNode(std::make_unique(std::move(root), "", std::move(origin))); } catch (const YAML::Exception& e) { throw ConfigError("YAML sai cu phap trong " + origin + ": " + e.what()); } } ConfigNode ConfigNode::operator[](const std::string& key) const { // Key thieu tra node Null thay vi nem, de chuoi a["b"]["c"] khong no giua // chung. Nguoi goi kiem IsNull() hoac dung require() o buoc cuoi. if (!impl_->node || !impl_->node.IsMap()) { return ConfigNode(std::make_unique(YAML::Node(YAML::NodeType::Undefined), JoinPath(impl_->path, key), impl_->origin)); } return ConfigNode(std::make_unique(impl_->node[key], JoinPath(impl_->path, key), impl_->origin)); } ConfigNode ConfigNode::at(size_t index) const { std::ostringstream oss; oss << impl_->path << "[" << index << "]"; if (!impl_->node || !impl_->node.IsSequence() || index >= impl_->node.size()) { return ConfigNode(std::make_unique(YAML::Node(YAML::NodeType::Undefined), oss.str(), impl_->origin)); } return ConfigNode(std::make_unique(impl_->node[index], oss.str(), impl_->origin)); } bool ConfigNode::IsNull() const { return !impl_->node || impl_->node.IsNull() || impl_->node.Type() == YAML::NodeType::Undefined; } bool ConfigNode::IsMap() const { return impl_->node && impl_->node.IsMap(); } bool ConfigNode::IsSequence() const { return impl_->node && impl_->node.IsSequence(); } bool ConfigNode::IsScalar() const { return impl_->node && impl_->node.IsScalar(); } size_t ConfigNode::size() const { return impl_->node ? impl_->node.size() : 0; } const std::string& ConfigNode::path() const { return impl_->path; } const std::string& ConfigNode::origin() const { return impl_->origin; } // --- Doc gia tri ----------------------------------------------------------- template T ConfigNode::get(const std::string& key, const T& fallback) const { const ConfigNode child = (*this)[key]; if (child.IsNull()) return fallback; try { return child.impl_->node.template as(); } catch (const YAML::Exception&) { // Sai KIEU luon la loi, khac han voi THIEU key. Thieu key co the co y // (dung default); sai kieu thi khong bao gio co y. throw ConfigError(Where(impl_->origin, impl_->path, key) + ": sai kieu du lieu"); } } template T ConfigNode::require(const std::string& key) const { const ConfigNode child = (*this)[key]; if (child.IsNull()) { throw ConfigError(Where(impl_->origin, impl_->path, key) + ": thieu key bat buoc"); } try { return child.impl_->node.template as(); } catch (const YAML::Exception&) { throw ConfigError(Where(impl_->origin, impl_->path, key) + ": sai kieu du lieu"); } } template T ConfigNode::as() const { if (IsNull()) { throw ConfigError(impl_->origin + ": " + impl_->path + ": thieu gia tri"); } try { return impl_->node.template as(); } catch (const YAML::Exception&) { throw ConfigError(impl_->origin + ": " + impl_->path + ": sai kieu du lieu"); } } template T ConfigNode::as(const T& fallback) const { if (IsNull()) return fallback; try { return impl_->node.template as(); } catch (const YAML::Exception&) { throw ConfigError(impl_->origin + ": " + impl_->path + ": sai kieu du lieu"); } } // --- Kiem key la ----------------------------------------------------------- std::vector ConfigNode::Keys() const { std::vector out; if (!IsMap()) return out; for (const auto& kv : impl_->node) { out.push_back(kv.first.as()); } return out; } std::vector ConfigNode::UnknownKeys( const std::vector& known) const { std::vector out; for (const auto& k : Keys()) { if (std::find(known.begin(), known.end(), k) == known.end()) { out.push_back(JoinPath(impl_->path, k)); } } return out; } bool ConfigNode::WarnUnknownKeys(const std::vector& known) const { const auto unknown = UnknownKeys(known); for (const auto& k : unknown) { // ERROR chu khong phai WARN: mot key la gan nhu luon la go sai ten cua // mot key that, nghia la gia tri nguoi dung DINH dat dang khong co tac // dung. "stop_distence" thay vi "stop_distance" van cho robot chay, chi // la chay voi khoang cach dung mac dinh. VSLAM_ERROR(impl_->origin << ": key khong nhan ra: " << k); } return unknown.empty(); } std::vector> ConfigNode::AsStringMap() const { std::vector> out; if (!IsMap()) return out; for (const auto& kv : impl_->node) { // Giu nguyen van ca khoa lan gia tri. Khoi `rtabmap:` trong slam.yaml di // thang vao rtabmap::ParametersMap, noi moi gia tri deu la chuoi -- kiem // kieu o day se lam hong "true"/"0.05" thanh bool/double roi in lai khac. out.emplace_back(kv.first.as(), kv.second.as()); } return out; } // --- Khoi tao tuong minh --------------------------------------------------- #define VSLAM_CONFIG_DEFINE(T) \ template T ConfigNode::get(const std::string&, const T&) const; \ template T ConfigNode::require(const std::string&) const; \ template T ConfigNode::as() const; \ template T ConfigNode::as(const T&) const; VSLAM_CONFIG_DEFINE(bool) VSLAM_CONFIG_DEFINE(int) VSLAM_CONFIG_DEFINE(unsigned int) VSLAM_CONFIG_DEFINE(float) VSLAM_CONFIG_DEFINE(double) VSLAM_CONFIG_DEFINE(std::string) #undef VSLAM_CONFIG_DEFINE template std::vector ConfigNode::get>( const std::string&, const std::vector&) const; template std::vector ConfigNode::require>( const std::string&) const; template std::vector ConfigNode::get>( const std::string&, const std::vector&) const; template std::vector ConfigNode::require>( const std::string&) const; } // namespace vslam