1 #ifndef SLAM_CTOR_UTILS_PROPERTIES_PROVIDER_H 2 #define SLAM_CTOR_UTILS_PROPERTIES_PROVIDER_H 5 #include <unordered_map> 18 using str = std::string;
21 virtual int get_int(
const std::string &
id,
int dflt)
const = 0;
22 virtual double get_dbl(
const std::string &
id,
double dflt)
const = 0;
23 virtual str get_str(
const std::string &
id,
const str &dflt)
const = 0;
24 virtual unsigned get_uint(
const std::string &
id,
unsigned dflt)
const = 0;
25 virtual bool get_bool(
const std::string &
id,
bool dflt)
const = 0;
31 void set_property(
const std::string &
id,
const std::string &value) {
36 return _props.find(
id) != _props.end();
39 int get_int(
const std::string &
id,
int dflt)
const override {
40 return has_property(
id) ? std::stoi(_props.at(
id)) : dflt;
43 double get_dbl(
const std::string &
id,
double dflt)
const override {
44 return has_property(
id) ? std::stod(_props.at(
id)) : dflt;
48 return has_property(
id) ? _props.at(
id) : dflt;
51 unsigned get_uint(
const std::string &
id,
unsigned dflt)
const override {
52 return has_property(
id) ? std::stoul(_props.at(
id)) : dflt;
55 bool get_bool(
const std::string &
id,
bool dflt)
const override {
56 if (!has_property(
id)) {
return dflt; }
57 auto value = _props.at(
id);
58 return !(value ==
"false" || value ==
"0");
68 std::unordered_map<std::string, std::string>
_props;
73 static constexpr
char Comment_Marker =
'#';
74 static constexpr
char Include_Open_Marker =
'<';
75 static constexpr
char Include_Close_Marker =
'>';
76 static constexpr
char Id_Value_Delimiter =
'=';
82 _props += parse_file(path);
85 int get_int(
const std::string &
id,
int dflt)
const override {
86 return _props.get_int(
id, dflt);
89 double get_dbl(
const std::string &
id,
double dflt)
const override {
90 return _props.get_dbl(
id, dflt);
94 return _props.get_str(
id, dflt);
97 unsigned get_uint(
const std::string &
id,
unsigned dflt)
const override {
98 return _props.get_uint(
id, dflt);
101 bool get_bool(
const std::string &
id,
bool dflt)
const override {
102 return _props.get_bool(
id, dflt);
110 std::ifstream file(path);
112 std::cout <<
"[WARN][FilePropertiesProvider] File " << path
113 <<
"does not exist." << std::endl;
117 auto line =
unsigned{0};
120 std::getline(file, buf);
121 if (!file) {
break; }
124 if (buf.empty()) {
continue; }
125 if (buf.front() == Comment_Marker) {
continue; }
126 if (buf.front() == Include_Open_Marker) {
128 if (buf.back() == Include_Close_Marker) {
130 auto path_copy = std::unique_ptr<char[]>{
131 new char[path.length() + 1]()
133 std::strncpy(path_copy.get(), path.c_str(), path.length());
134 constexpr
char Unix_Path_Separator =
'/';
135 auto fname_dir = std::string{dirname(path_copy.get())} +
142 auto included_path = fname_dir + std::string{std::next(buf.begin()),
143 std::prev(buf.end())};
144 append_file_content(included_path);
146 std::cout <<
"[Warn][FilePropertiesProvider] Line: " << line
147 <<
". Include error: last char is not '>'" << std::endl;
152 auto delim_i = buf.find(Id_Value_Delimiter);
153 if (delim_i == std::string::npos) {
154 std::cout <<
"[WARN][FilePropertiesProvider] Unable to parse " 155 << path <<
" entry. Line: " << line
156 <<
", unable to find delimiter (" << Id_Value_Delimiter
157 <<
")." << std::endl;
161 auto id = buf.substr(0, delim_i);
162 auto value = buf.substr(delim_i + 1, buf.size() - delim_i - 1);
165 if (props.has_property(
id)) {
166 std::cout <<
"[WARN][FilePropertiesProvider] Reset property " 167 <<
id <<
"." << std::endl;
169 props.set_property(
id, value);
unsigned get_uint(const std::string &id, unsigned dflt) const override
MapPropertiesProvider & operator+=(const MapPropertiesProvider &that)
virtual str get_str(const std::string &id, const str &dflt) const =0
virtual int get_int(const std::string &id, int dflt) const =0
unsigned get_uint(const std::string &id, unsigned dflt) const override
MapPropertiesProvider parse_file(const std::string &path)
virtual double get_dbl(const std::string &id, double dflt) const =0
bool get_bool(const std::string &id, bool dflt) const override
MapPropertiesProvider _props
void set_property(const std::string &id, const std::string &value)
std::unordered_map< std::string, std::string > _props
virtual unsigned get_uint(const std::string &id, unsigned dflt) const =0
void append_file_content(const std::string &path)
str get_str(const std::string &id, const str &dflt) const override
str get_str(const std::string &id, const str &dflt) const override
bool has_property(const std::string &id) const
virtual bool get_bool(const std::string &id, bool dflt) const =0
double get_dbl(const std::string &id, double dflt) const override
int get_int(const std::string &id, int dflt) const override
int get_int(const std::string &id, int dflt) const override
bool get_bool(const std::string &id, bool dflt) const override
double get_dbl(const std::string &id, double dflt) const override