properties_providers.h
Go to the documentation of this file.
1 #ifndef SLAM_CTOR_UTILS_PROPERTIES_PROVIDER_H
2 #define SLAM_CTOR_UTILS_PROPERTIES_PROVIDER_H
3 
4 #include <string>
5 #include <unordered_map>
6 #include <fstream>
7 #include <algorithm>
8 #include <sstream>
9 
10 // TODO: replace with filesystem on standard update
11 #include <libgen.h>
12 #include <cstring>
13 #include <memory>
14 /* #include <experimental/filesystem> */
15 
17 protected:
18  using str = std::string;
19 public:
20  // TODO: use templates?
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;
26 };
27 
29 public:
30 
31  void set_property(const std::string &id, const std::string &value) {
32  _props[id] = value;
33  }
34 
35  bool has_property(const std::string &id) const {
36  return _props.find(id) != _props.end();
37  }
38 
39  int get_int(const std::string &id, int dflt) const override {
40  return has_property(id) ? std::stoi(_props.at(id)) : dflt;
41  }
42 
43  double get_dbl(const std::string &id, double dflt) const override {
44  return has_property(id) ? std::stod(_props.at(id)) : dflt;
45  }
46 
47  str get_str(const std::string &id, const str &dflt) const override {
48  return has_property(id) ? _props.at(id) : dflt;
49  }
50 
51  unsigned get_uint(const std::string &id, unsigned dflt) const override {
52  return has_property(id) ? std::stoul(_props.at(id)) : dflt;
53  }
54 
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");
59  }
60 
62  _props.insert(that._props.begin(), that._props.end());
63  return *this;
64  }
65 
66 private:
67 
68  std::unordered_map<std::string, std::string> _props;
69 };
70 
72 protected:
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 = '=';
77 public:
78 
80 
81  void append_file_content(const std::string &path) {
82  _props += parse_file(path);
83  }
84 
85  int get_int(const std::string &id, int dflt) const override {
86  return _props.get_int(id, dflt);
87  }
88 
89  double get_dbl(const std::string &id, double dflt) const override {
90  return _props.get_dbl(id, dflt);
91  }
92 
93  str get_str(const std::string &id, const str &dflt) const override {
94  return _props.get_str(id, dflt);
95  }
96 
97  unsigned get_uint(const std::string &id, unsigned dflt) const override {
98  return _props.get_uint(id, dflt);
99  }
100 
101  bool get_bool(const std::string &id, bool dflt) const override {
102  return _props.get_bool(id, dflt);
103  }
104 
105 private:
106 
107  MapPropertiesProvider parse_file(const std::string &path) {
108  auto props = MapPropertiesProvider{};
109 
110  std::ifstream file(path);
111  if (!file) {
112  std::cout << "[WARN][FilePropertiesProvider] File " << path
113  << "does not exist." << std::endl;
114  return props;
115  }
116 
117  auto line = unsigned{0};
118  while (true) {
119  std::string buf;
120  std::getline(file, buf);
121  if (!file) { break; }
122 
123  ++line;
124  if (buf.empty()) { continue; }
125  if (buf.front() == Comment_Marker) { continue; }
126  if (buf.front() == Include_Open_Marker) {
127  // TODO: circular includes detection
128  if (buf.back() == Include_Close_Marker) {
129  // NB: portability - unix specific; use <filesystem> when available
130  auto path_copy = std::unique_ptr<char[]>{
131  new char[path.length() + 1]()
132  };
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())} +
136  Unix_Path_Separator;
137  /*
138  auto fname_dir = static_cast<std::string>(
139  std::experimental::filesystem::path{fname}.remove_filename());
140  */
141 
142  auto included_path = fname_dir + std::string{std::next(buf.begin()),
143  std::prev(buf.end())};
144  append_file_content(included_path);
145  } else {
146  std::cout << "[Warn][FilePropertiesProvider] Line: " << line
147  << ". Include error: last char is not '>'" << std::endl;
148  }
149  continue;
150  }
151 
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;
158  continue;
159  }
160 
161  auto id = buf.substr(0, delim_i);
162  auto value = buf.substr(delim_i + 1, buf.size() - delim_i - 1);
163  //TODO: trim id and value
164 
165  if (props.has_property(id)) {
166  std::cout << "[WARN][FilePropertiesProvider] Reset property "
167  << id << "." << std::endl;
168  }
169  props.set_property(id, value);
170  }
171  return props;
172  }
173 
174 private:
176 };
177 
178 
179 #endif
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


slam_constructor
Author(s): JetBrains Research, OSLL team
autogenerated on Mon Jun 10 2019 15:08:25