Awali
Another Weighted Automata library
value.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_VALUE_HH
18 #define AWAlI_DYN_OPTIONS_VALUE_HH
19 
20 
21 namespace awali {
22  namespace dyn {
23  namespace internal {
24 
27  virtual opt_untyped_value* clone() const = 0;
28  virtual ~opt_untyped_value() {}
29  };
30 
31  template<typename T>
33  T value;
34 
35  opt_typed_value(const T& val) : value(val) {}
36 
37  opt_typed_value* clone() const override {
38  return new opt_typed_value(value);
39  }
40  };
41 
42 
43  struct opt_any_t {
44  private:
45  opt_untyped_value* value;
46 
47  public:
48 
49  template<typename T>
50  opt_any_t(const T& val) : value(new opt_typed_value<T>(val)) {}
51 
52  opt_any_t(char const* s) : value(new opt_typed_value<std::string>(s)) {}
53 
54  opt_any_t(opt_any_t const& a) : value(a.value->clone()) {}
55 
57  if(&t!=this) {
58  delete value;
59  value=t.value->clone();
60  }
61  return (*this);
62  }
63 
65  delete(value);
66  }
67 
68 
69  template<typename T>
70  explicit
71  operator T() const {
72  const opt_typed_value<T>& test
73  = dynamic_cast<const opt_typed_value<T>&>(*(this->value));
74  return test.value;
75  }
76 
77  };
78 
80  size_t id;
83  };
84 
85  }
86  }
87 } //end of namespaces awali::dyn::internal, awali::dyn, and awali.
88 #endif
Main namespace of Awali.
Definition: ato.hh:22
Definition: value.hh:43
opt_any_t(char const *s)
Definition: value.hh:52
~opt_any_t()
Definition: value.hh:64
opt_any_t(opt_any_t const &a)
Definition: value.hh:54
opt_any_t & operator=(opt_any_t const &t)
Definition: value.hh:56
opt_any_t(const T &val)
Definition: value.hh:50
opt_typed_value(const T &val)
Definition: value.hh:35
T value
Definition: value.hh:33
opt_typed_value * clone() const override
Definition: value.hh:37
virtual ~opt_untyped_value()
Definition: value.hh:28
virtual opt_untyped_value * clone() const =0
opt_untyped_value()
Definition: value.hh:26
internal::opt_any_t value
Definition: value.hh:81
size_t id
Definition: value.hh:80
option_value_pair_t(size_t i, internal::opt_any_t v)
Definition: value.hh:82