Awali
Another Weighted Automata library
value.hh
Go to the documentation of this file.
1 // This file is part of Awali.
2 // Copyright 2016-2021 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 DYN_VALUE_HH
18 #define DYN_VALUE_HH
19 
23 
24 #include<typeinfo>
25 
26 namespace awali { namespace dyn {
27  namespace internal {
28 
29  template<typename T> T any_cast(const any_t& a);
30  template<typename T>
31  struct Value : untyped_value {
32  Value(const T& val) : val(val) {}
33  T val;
34 
35  Value* clone() const override {
36  return new Value(val);
37  }
38 
39  std::ostream& output(std::ostream& o) const override {
40  return (o << val);
41  }
42 
43  bool less(const untyped_value& uv) const override;
44 
45  bool equal(const untyped_value& uv) const override {
46  const Value& v=dynamic_cast<const Value&>(uv);
47  return (val == v.val);
48  }
49 
50  std::ostream& real_type_name(std::ostream& o) const override {
51  return o << awali::internal::demangle(typeid(val).name());
52  }
53 
54  std::ostream& real_type_id(std::ostream& o) const override {
55  return o << typeid(val).hash_code();
56  }
57 
58  virtual ~Value() {}
59  };
60 
61  template<typename T>
62  bool Value<T>::less(const untyped_value& uv) const {
63  const Value& v=dynamic_cast<const Value&>(uv);
64  return (val < v.val);
65  }
66 
67  template<>
68  inline
69  bool Value<std::string>::less(const untyped_value& uv) const {
70  const Value& v=dynamic_cast<const Value&>(uv);
71  if(val.length() == v.val.length())
72  return (val < v.val);
73  else
74  return (val.length() < v.val.length());
75  }
76 
77  template<>
78  inline
79  bool Value<std::basic_string<int>>::less(const untyped_value& uv) const {
80  const Value& v=dynamic_cast<const Value&>(uv);
81  if(val.length() == v.val.length())
82  return (val < v.val);
83  else
84  return (val.length() < v.val.length());
85  }
86 
87 }}}//end of namespace awali::dyn::internal, awali::dyn, and awali
88 
89 #endif //DYN_VALUE_HH
T any_cast(const any_t &a)
Definition: any.hh:181
static std::string demangle(const char *name)
Definition: demangle.hxx:48
Main namespace of Awali.
Definition: ato.hh:22
Definition: value.hh:31
bool equal(const untyped_value &uv) const override
Definition: value.hh:45
Value * clone() const override
Definition: value.hh:35
std::ostream & real_type_id(std::ostream &o) const override
Definition: value.hh:54
Value(const T &val)
Definition: value.hh:32
bool less(const untyped_value &uv) const override
Definition: value.hh:62
T val
Definition: value.hh:33
std::ostream & output(std::ostream &o) const override
Definition: value.hh:39
virtual ~Value()
Definition: value.hh:58
std::ostream & real_type_name(std::ostream &o) const override
Definition: value.hh:50
Definition: untyped_value.hh:26