Files
vslam_cpp/libs/vslam_common/include/vslam/config.h
trung09122002 48bd5377dc upload
2026-09-18 18:14:42 +07:00

135 lines
4.9 KiB
C++

#ifndef VSLAM_CONFIG_H
#define VSLAM_CONFIG_H
// Doc cau hinh YAML. Bao yaml-cpp de khong ro kieu cua no ra API cong khai.
//
// HAI THU MA BAN ROS THIEU, va ca hai deu da gay ton that that:
//
// 1. require<T>() -- nem loi kem TEN KEY DAY DU khi thieu.
// Hien tai pnh_.param("stop_distance", stop_distance_, 0.35) khien mot key
// go sai AM THAM roi ve default C++ va khong ai biet. Voi tham so an toan
// (stop_distance, min_front_rays) hay hinh hoc (wheel_separation) thi day
// la kieu loi im lang nguy hiem nhat: he van chay, chi la chay sai.
//
// 2. UnknownKeys() -- liet ke key CO trong file nhung KHONG co trong danh
// sach biet truoc. Bat duoc "stop_distence". Kiem o startup, log ERROR.
//
// Quy tac: dung require() cho moi thu lien quan an toan, hinh hoc, va duong
// dan. Dung get(key, fallback) chi cho tuy chon thuc su co default hop ly.
#include <memory>
#include <stdexcept>
#include <string>
#include <vector>
namespace vslam {
class ConfigError : public std::runtime_error {
public:
explicit ConfigError(const std::string& msg) : std::runtime_error(msg) {}
};
class ConfigNode {
public:
ConfigNode();
ConfigNode(const ConfigNode&);
ConfigNode(ConfigNode&&) noexcept;
ConfigNode& operator=(const ConfigNode&);
ConfigNode& operator=(ConfigNode&&) noexcept;
~ConfigNode();
// Nem ConfigError neu file khong doc duoc hoac YAML sai cu phap.
static ConfigNode LoadFile(const std::string& path);
static ConfigNode LoadString(const std::string& yaml, std::string origin = "<string>");
// Key thieu tra ve node Null thay vi nem -- de chuoi a["b"]["c"] khong no
// giua chung. Cho phep kiem IsNull() o cuoi.
ConfigNode operator[](const std::string& key) const;
ConfigNode at(size_t index) const; // cho chuoi (sequence)
bool IsNull() const;
bool IsMap() const;
bool IsSequence() const;
bool IsScalar() const;
size_t size() const;
// Duong dan day du toi node nay, vd "safety.scan.stop_distance".
// Dung trong moi thong bao loi.
const std::string& path() const;
// Ten file goc, de thong bao loi noi ro key nam o dau.
const std::string& origin() const;
// --- Doc gia tri --------------------------------------------------------
// Thieu key -> tra `fallback`. Sai kieu -> nem ConfigError (khac han voi
// thieu key: sai kieu luon la loi cua nguoi viet config).
template <typename T>
T get(const std::string& key, const T& fallback) const;
// Thieu key hoac sai kieu -> nem ConfigError kem duong dan day du.
template <typename T>
T require(const std::string& key) const;
// Doc chinh node nay (khi da di toi la).
template <typename T>
T as() const;
template <typename T>
T as(const T& fallback) const;
// --- Kiem key la ---------------------------------------------------------
// Tra ve cac key CO trong node nay nhung KHONG nam trong `known`.
// Rong nghia la khong co typo.
std::vector<std::string> UnknownKeys(const std::vector<std::string>& known) const;
// Tien loi: goi UnknownKeys() roi log ERROR cho tung key la.
// Tra ve false neu co key la.
bool WarnUnknownKeys(const std::vector<std::string>& known) const;
// Danh sach moi key cua node map nay.
std::vector<std::string> Keys() const;
// Cac cap key/value dang chuoi, giu nguyen van. Dung cho khoi `rtabmap:`
// trong slam.yaml -- 26 key cua RTAB-Map di thang vao ParametersMap ma
// khong qua kiem kieu nao o day.
std::vector<std::pair<std::string, std::string>> AsStringMap() const;
private:
struct Impl;
std::unique_ptr<Impl> impl_;
explicit ConfigNode(std::unique_ptr<Impl> impl);
};
// Khai bao tuong minh cac kieu duoc ho tro. Dinh nghia o config.cc.
// Them kieu moi thi phai them ca o day va o .cc -- co y, de khong ai vo tinh
// doc mot kieu ma yaml-cpp chuyen doi theo cach bat ngo.
#define VSLAM_CONFIG_DECLARE(T) \
extern template T ConfigNode::get<T>(const std::string&, const T&) const; \
extern template T ConfigNode::require<T>(const std::string&) const; \
extern template T ConfigNode::as<T>() const; \
extern template T ConfigNode::as<T>(const T&) const;
VSLAM_CONFIG_DECLARE(bool)
VSLAM_CONFIG_DECLARE(int)
VSLAM_CONFIG_DECLARE(unsigned int)
VSLAM_CONFIG_DECLARE(float)
VSLAM_CONFIG_DECLARE(double)
VSLAM_CONFIG_DECLARE(std::string)
#undef VSLAM_CONFIG_DECLARE
// Chuoi thi khai bao rieng vi khong dung duoc trong macro (co dau phay).
extern template std::vector<double> ConfigNode::get<std::vector<double>>(
const std::string&, const std::vector<double>&) const;
extern template std::vector<double> ConfigNode::require<std::vector<double>>(
const std::string&) const;
extern template std::vector<std::string> ConfigNode::get<std::vector<std::string>>(
const std::string&, const std::vector<std::string>&) const;
extern template std::vector<std::string> ConfigNode::require<std::vector<std::string>>(
const std::string&) const;
} // namespace vslam
#endif // VSLAM_CONFIG_H