Vowpal Wabbit
options.h
Go to the documentation of this file.
1 #pragma once
2 
3 #include <string>
4 #include <utility>
5 #include <vector>
6 #include <typeinfo>
7 #include <memory>
8 
9 namespace VW
10 {
11 namespace config
12 {
14 {
15  base_option(std::string name, size_t type_hash) : m_name(std::move(name)), m_type_hash(type_hash) {}
16 
17  std::string m_name;
18  size_t m_type_hash;
19  std::string m_help = "";
20  std::string m_short_name = "";
21  bool m_keep = false;
22 
23  virtual ~base_option() = default;
24 };
25 
26 template <typename T>
28 {
29  typed_option(const std::string& name, T& location) : base_option(name, typeid(T).hash_code()), m_location{location} {}
30 
31  static size_t type_hash() { return typeid(T).hash_code(); }
32 
34  {
35  m_default_value = std::make_shared<T>(value);
36  return *this;
37  }
38 
39  bool default_value_supplied() { return m_default_value.get() != nullptr; }
40 
41  T default_value() { return m_default_value ? *m_default_value : T(); }
42 
43  typed_option& short_name(const std::string& short_name)
44  {
45  m_short_name = short_name;
46  return *this;
47  }
48 
49  typed_option& help(const std::string& help)
50  {
51  m_help = help;
52  return *this;
53  }
54 
55  typed_option& keep(bool keep = true)
56  {
57  m_keep = keep;
58  return *this;
59  }
60 
61  bool value_supplied() { return m_value.get() != nullptr; }
62 
63  typed_option& value(T value)
64  {
65  m_value = std::make_shared<T>(value);
66  return *this;
67  }
68 
69  T value() { return m_value ? *m_value : T(); }
70 
72 
73  private:
74  // Would prefer to use std::optional (C++17) here but we are targeting C++11
75  std::shared_ptr<T> m_value{nullptr};
76  std::shared_ptr<T> m_default_value{nullptr};
77 };
78 
79 template <typename T>
80 typed_option<T> make_option(std::string name, T& location)
81 {
82  return typed_option<T>(name, location);
83 }
84 
86 {
87  option_group_definition(const std::string& name) : m_name(name) {}
88 
89  template <typename T>
91  {
92  m_options.push_back(std::make_shared<typename std::decay<T>::type>(op));
93  return *this;
94  }
95 
96  template <typename T>
98  {
99  add(std::forward<T>(op));
100  return *this;
101  }
102 
103  std::string m_name;
104  std::vector<std::shared_ptr<base_option>> m_options;
105 };
106 
107 struct options_i
108 {
109  virtual void add_and_parse(const option_group_definition& group) = 0;
110  virtual bool was_supplied(const std::string& key) = 0;
111  virtual std::string help() = 0;
112 
113  virtual std::vector<std::shared_ptr<base_option>> get_all_options() = 0;
114  virtual std::shared_ptr<base_option> get_option(const std::string& key) = 0;
115 
116  virtual void insert(const std::string& key, const std::string& value) = 0;
117  virtual void replace(const std::string& key, const std::string& value) = 0;
118 
119  template <typename T>
120  typed_option<T>& get_typed_option(const std::string& key)
121  {
122  base_option& base = *get_option(key);
124  {
125  throw std::bad_cast();
126  }
127 
128  return dynamic_cast<typed_option<T>&>(base);
129  }
130 
131  // Will throw if any options were supplied that do not having a matching argument specification.
132  virtual void check_unregistered() = 0;
133 
134  virtual ~options_i() = default;
135 };
136 
138 {
139  virtual void add(base_option& argument) = 0;
140  virtual std::string str() = 0;
141  virtual const char* data() = 0;
142  virtual size_t size() = 0;
143 };
144 
145 template <typename T>
147 {
148  return lhs.m_name == rhs.m_name && lhs.m_type_hash == rhs.m_type_hash && lhs.m_help == rhs.m_help &&
149  lhs.m_short_name == rhs.m_short_name && lhs.m_keep == rhs.m_keep && lhs.default_value() == rhs.default_value();
150 }
151 
152 template <typename T>
154 {
155  return !(lhs == rhs);
156 }
157 
158 bool operator==(const base_option& lhs, const base_option& rhs);
159 bool operator!=(const base_option& lhs, const base_option& rhs);
160 
161 inline bool operator==(const base_option& lhs, const base_option& rhs)
162 {
163  return lhs.m_name == rhs.m_name && lhs.m_type_hash == rhs.m_type_hash && lhs.m_help == rhs.m_help &&
164  lhs.m_short_name == rhs.m_short_name && lhs.m_keep == rhs.m_keep;
165 }
166 
167 inline bool operator!=(const base_option& lhs, const base_option& rhs) { return !(lhs == rhs); }
168 
169 } // namespace config
170 } // namespace VW
typed_option & default_value(T value)
Definition: options.h:33
std::vector< std::shared_ptr< base_option > > m_options
Definition: options.h:104
bool default_value_supplied()
Definition: options.h:39
bool operator!=(typed_option< T > &lhs, typed_option< T > &rhs)
Definition: options.h:153
std::string m_name
Definition: options.h:17
typed_option< T > & get_typed_option(const std::string &key)
Definition: options.h:120
typed_option & value(T value)
Definition: options.h:63
typed_option & short_name(const std::string &short_name)
Definition: options.h:43
base_option(std::string name, size_t type_hash)
Definition: options.h:15
bool operator==(typed_option< T > &lhs, typed_option< T > &rhs)
Definition: options.h:146
option_group_definition & add(T &&op)
Definition: options.h:90
int add(svm_params &params, svm_example *fec)
Definition: kernel_svm.cc:546
option_group_definition & operator()(T &&op)
Definition: options.h:97
typed_option< T > make_option(std::string name, T &location)
Definition: options.h:80
typed_option(const std::string &name, T &location)
Definition: options.h:29
std::string m_help
Definition: options.h:19
Definition: autolink.cc:11
typed_option & help(const std::string &help)
Definition: options.h:49
typed_option & keep(bool keep=true)
Definition: options.h:55
virtual ~base_option()=default
static size_t type_hash()
Definition: options.h:31
std::string m_short_name
Definition: options.h:20
option_group_definition(const std::string &name)
Definition: options.h:87