#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() -- 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 #include #include #include 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 = ""); // 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 T get(const std::string& key, const T& fallback) const; // Thieu key hoac sai kieu -> nem ConfigError kem duong dan day du. template T require(const std::string& key) const; // Doc chinh node nay (khi da di toi la). template T as() const; template 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 UnknownKeys(const std::vector& 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& known) const; // Danh sach moi key cua node map nay. std::vector 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> AsStringMap() const; private: struct Impl; std::unique_ptr impl_; explicit ConfigNode(std::unique_ptr 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(const std::string&, const T&) const; \ extern template T ConfigNode::require(const std::string&) const; \ extern template T ConfigNode::as() const; \ extern template T ConfigNode::as(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 ConfigNode::get>( const std::string&, const std::vector&) const; extern template std::vector ConfigNode::require>( const std::string&) const; extern template std::vector ConfigNode::get>( const std::string&, const std::vector&) const; extern template std::vector ConfigNode::require>( const std::string&) const; } // namespace vslam #endif // VSLAM_CONFIG_H