Virtual Testbed
Ship dynamics simulator for extreme conditions
options.hh
1 #ifndef VTESTBED_ASSETS_OPTIONS_HH
2 #define VTESTBED_ASSETS_OPTIONS_HH
3 
4 #include <any>
5 #include <string>
6 #include <unordered_map>
7 
8 namespace vtb {
9 
10  namespace assets {
11 
12  class Options: public std::unordered_map<std::string,std::any> {
13 
14  private:
16 
17  public:
18  using key_type = typename base_type::key_type;
19  using value_type = typename base_type::value_type;
20  using mapped_type = typename base_type::mapped_type;
21  using hasher = typename base_type::hasher;
22  using key_equal = typename base_type::key_equal;
23  using allocator_type = typename base_type::allocator_type;
24  using pointer = typename base_type::pointer;
25  using const_pointer = typename base_type::const_pointer;
26  using reference = typename base_type::reference;
27  using const_reference = typename base_type::const_reference;
28  using iterator = typename base_type::iterator;
29  using const_iterator = typename base_type::const_iterator;
30  using local_iterator = typename base_type::local_iterator;
31  using const_local_iterator = typename base_type::const_local_iterator;
32  using size_type = typename base_type::size_type;
33  using difference_type = typename base_type::difference_type;
34 
35  public:
36  using base_type::base_type;
37  using base_type::operator=;
38  using base_type::operator[];
39 
40  inline const std::any&
41  get(const std::string& key, const std::any& default_value) const {
42  auto result = this->find(key);
43  if (result == this->end()) { return default_value; }
44  return result->second;
45  }
46 
47  template <class T> inline T
48  get(const std::string& key, T default_value) const {
49  auto result = this->find(key);
50  if (result == this->end()) { return default_value; }
51  return std::any_cast<T>(result->second);
52  }
53 
54  template <class T> inline T
55  get(const std::string& key) const {
56  auto result = this->find(key);
57  if (result == this->end()) { throw std::invalid_argument(key); }
58  return std::any_cast<T>(result->second);
59  }
60 
61  inline std::string
62  string(const std::string& key) const {
63  return get<std::string>(key);
64  }
65 
66  void read(const std::string& text);
67  void read(const std::string& key, const std::string& value);
68 
69  };
70 
71  }
72 
73 }
74 
75 #endif // vim:filetype=cpp
Main namespace.
Definition: convert.hh:9