Awali
Another Weighted Automata library
option.hh
Go to the documentation of this file.
1 // This file is part of Awali.
2 // Copyright 2016-2022 Sylvain Lombardy, Victor Marsault, Jacques Sakarovitch
3 //
4 // Awali is a free software: you can redistribute it and/or modify
5 // it under the terms of the GNU General Public License as published by
6 // the Free Software Foundation, either version 3 of the License, or
7 // (at your option) any later version.
8 //
9 // This program is distributed in the hope that it will be useful,
10 // but WITHOUT ANY WARRANTY; without even the implied warranty of
11 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 // GNU General Public License for more details.
13 //
14 // You should have received a copy of the GNU General Public License
15 // along with this program. If not, see <http://www.gnu.org/licenses/>.
16 
17 #ifndef AWALI_DYN_OPTIONS_OPTION_HH
18 #define AWALI_DYN_OPTIONS_OPTION_HH
19 
20 #include <unordered_map>
21 #include <typeinfo>
22 
23 #include <awali/dyn/core/any.hh>
24 #include <awali/common/priority.hh>
25 #include <awali/common/enums.hh>
29 
30 namespace awali {
31  namespace dyn {
32  namespace internal {
33 
34  template<typename T>
35  struct acceptor_t {
36  virtual bool is_valid(T const&) {
37  return true;
38  }
39  virtual std::string acceptable_values() {
40  return ("any value of type "+awali::internal::demangle(typeid(T).name()));
41  }
42  virtual ~acceptor_t() = default;
43  };
44 
45  template<>
46  struct acceptor_t<bool> {
47  virtual bool is_valid(bool const&) {
48  return true;
49  }
50  std::string acceptable_values() {
51  return ("boolean");
52  }
53  virtual ~acceptor_t() = default;
54  };
55 
56  template<>
57  struct acceptor_t<std::string> {
58  virtual bool is_valid(std::string const&) {
59  return true;
60  }
61  virtual std::string acceptable_values() {
62  return ("any string");
63  }
64  virtual ~acceptor_t() = default;
65  };
66 
70  extern size_t next_id;
71 
72  template<typename T>
73  class option_t {
74  public:
75  using value_t = T;
76  const size_t id;
77 
78  private:
79  std::string option_name;
80  acceptor_t<value_t> acceptor;
81  value_t default_value;
82  bool default_set_by_user = false;
83 
84  option_t(option_t<T> other, value_t def) :
85  id(other.id),
86  option_name(other.option_name),
87  acceptor(other.acceptor),
88  default_value(other.default_set_by_user?other.def:def),
89  default_set_by_user(other.was_user_set)
90  {}
91 
92  template<typename X, typename P>
94  throw std::runtime_error("Wrong value type for option "+option_name
95  + ". Expecting: "
96  + acceptor.acceptable_values()
97  + "." );
98  }
99 
100  template<typename X=T, typename P>
102  make(const char* value, priority::TWO<P>)
103  { return make(std::string(value), priority::value); }
104 
105  template<typename X=T, typename P>
106  auto make(std::string const& value, priority::THREE<P>)
107  -> decltype(
108  awali::internal::make_enum<X>(value),
109  internal::option_value_pair_t(id, default_value)
110  ) {
111  if (value == "default")
112  return {id, default_value};
113  return {id, awali::internal::make_enum<X>(value)};
114  }
115 
116  template<typename P>
117  internal::option_value_pair_t make(const T& value, priority::FOUR<P>) {
118  if (!acceptor.is_valid(value))
119  make(value, priority::one);
120  return {id, value};
121  }
122 
123 
124 
125  public:
126  option_t(std::string name, T def, acceptor_t<T> acc = acceptor_t<T>())
127  : id(internal::next_id++), option_name(std::move(name)), acceptor(acc),
128  default_value(def)
129  {}
130 
131  bool acceptable(T value) {
132  return acceptor(value);
133  }
134 
137  return {id, true};
138  throw std::runtime_error("Using option without argument is only possible if they expect boolean values.");
139  }
140 
141 
160  return option_t(*this, new_default_value);
161  }
162 
163 
164  /* Globally sets the default value of this option.
165  */
166  void set_default_value(value_t new_default_value) {
167  default_value = new_default_value;
168  default_set_by_user = true;
169  }
170 
172  return default_value;
173  }
174 
175  public:
176 
177  template<typename X>
179  return make(value, priority::value);
180  }
181 
186  value_t of_string(std::string const& str) {
188  return (value_t) pair.value;
189  }
190 
191  };
192 
193  } // end of namespace awali::dyn::internal
194  } // end of namespace awali::dyn
195 } //end of namespace awali
196 
197 
199 
200 
201 
202 
203 #endif
Definition: option.hh:73
value_t of_string(std::string const &str)
Create of value of the type expected by this option_t, from string str.
Definition: option.hh:186
const size_t id
Definition: option.hh:76
option_t(std::string name, T def, acceptor_t< T > acc=acceptor_t< T >())
Definition: option.hh:126
value_t get_default_value()
Definition: option.hh:171
T value_t
Definition: option.hh:75
internal::option_value_pair_t operator=(const X &value)
Definition: option.hh:178
void set_default_value(value_t new_default_value)
Definition: option.hh:166
bool acceptable(T value)
Definition: option.hh:131
option_t< value_t > with_default_value(value_t new_default_value)
Creates a phony option_t that overrides system default value (but not user defined default,...
Definition: option.hh:159
size_t next_id
Global variable that assign a unique identifier to each instance of option<T> independentely of type ...
std::string demangle(const char *name)
Definition: demangle.hxx:48
static constexpr TOP< void > value
Definition: priority.hh:93
static constexpr ONE< void > one
Definition: priority.hh:76
Definition: priority.hh:52
pair_automaton< Aut > pair(const Aut &aut, bool keep_initials=false)
Definition: synchronizing_word.hh:266
Main namespace of Awali.
Definition: ato.hh:22
std::string acceptable_values()
Definition: option.hh:50
virtual bool is_valid(bool const &)
Definition: option.hh:47
virtual std::string acceptable_values()
Definition: option.hh:61
virtual bool is_valid(std::string const &)
Definition: option.hh:58
Definition: option.hh:35
virtual bool is_valid(T const &)
Definition: option.hh:36
virtual std::string acceptable_values()
Definition: option.hh:39