libVFRendering  0.14.0

A vector field rendering library
Options.hxx
1 #ifndef VECTORFIELDRENDERING_OPTIONS_HXX
2 #define VECTORFIELDRENDERING_OPTIONS_HXX
3 
4 #include <vector>
5 #include <unordered_map>
6 #include <memory>
7 #include <iostream>
8 
9 namespace VFRendering {
10 namespace Utilities {
11 
12 class Options {
13 private:
14  template<int i>
15  struct Option;
16 
17 public:
18 
19  template<int i>
20  struct Type {
21  typedef decltype(Option<i>().default_value) type;
22  };
23 
24  Options();
25 
26  template<int index>
27  static Options withOption(typename Type<index>::type value);
28 
29  template<int index>
30  typename Type<index>::type get() const;
31 
32  template<int index>
33  bool has() const;
34 
35  template<int index>
36  void set(typename Type<index>::type value);
37 
38  template<int index>
39  void clear();
40 
41  std::vector<int> keys() const;
42 
43  std::vector<int> update(const Options &other);
44 
45 private:
46  struct IStorableOption {
47  virtual ~IStorableOption() {}
48  };
49 
50  template<int i>
51  struct StorableOption : public IStorableOption {
52  StorableOption(const typename Type<i>::type& value) : m_value(value) {}
53  typename Type<i>::type m_value;
54  };
55 
56  std::unordered_map<int, std::shared_ptr<IStorableOption>> m_options;
57 };
58 
59 
60 template<int index>
61 Options Options::withOption(typename Type<index>::type value) {
62  Options options;
63  options.set<index>(value);
64  return options;
65 }
66 
67 template<int index>
68 typename Options::template Type<index>::type Options::get() const {
69  if (has<index>()) {
70  if (auto storableOption = static_cast<StorableOption<index> *>(m_options.at(index).get())) {
71  return storableOption->m_value;
72  }
73  }
74  return Option<index>().default_value;
75 }
76 
77 template<int index>
78 bool Options::has() const {
79  return (m_options.find(index) != m_options.end());
80 }
81 
82 template<int index>
83 void Options::set(typename Type<index>::type value) {
84  m_options[index] = std::make_shared<StorableOption<index>>(value);
85 }
86 
87 template<int index>
88 void Options::clear() {
89  m_options.erase(index);
90 }
91 
92 }
93 }
94 
95 #endif
VFRendering::Utilities::Options
Definition: Options.hxx:12
VFRendering::Utilities::Options::Type
Definition: Options.hxx:20