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