Awali
Another Weighted Automata library
to_value_of.hxx
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 COMMON_VALUE_OF_STRING_HXX
18 #define COMMON_VALUE_OF_STRING_HXX
19 
20 #include <string>
21 #include <sstream>
23 
24 namespace awali { namespace internal {
25 
26  template<typename M>
27  typename M::value_t to_value_of(M const& set, const std::string& str) {
28  size_t i= str.size();
29  try {
30  auto result = set.parse(str,i);
31  if (i==0)
32  return result;
33  }
34  catch (const parse_exception& e) { }
35  std::stringstream ss;
36  ss << "String " << str << " is not a proper representation of a value of ";
37  set.print_set(ss);
38  ss << ".";
39  throw parse_exception (ss.str());
40  }
41 
42 
43  template<typename M>
44  typename M::genset_t::word_t
45  to_word_of(M const& set, const std::string& str)
46  {
47  try {
48  auto result = get_wordset(set).parse(str);
49  return result;
50  }
51  catch (parse_exception const&) {}
52  std::stringstream ss;
53  ss << "String " << str << " is not a proper representation of a word of ";
54  set.print_set(ss);
55  ss << ".";
56  throw parse_exception (ss.str());
57  }
58 
59  template<typename M>
60  typename M::genset_t::letter_t
61  to_letter_of(M const& set, const std::string& str)
62  {
63  size_t i = str.size();
64  try {
65  auto result = set.genset().parse_one_letter(str,i);
66  if (i == 0)
67  return result;
68  }
69  catch (parse_exception const&) {}
70  std::stringstream ss;
71  ss << "String " << str << " is not a proper representation of a letter of ";
72  set.print_set(ss);
73  ss << ".";
74  throw parse_exception (ss.str());
75  }
76 
77 }} // end of namespaces awali::internal and awali::dyn
78 
79 #endif
any_t word_t
Type for words; it is an alias to any_t since the precise type depends on the context (most of the ti...
Definition: typedefs.hh:67
M::genset_t::word_t to_word_of(M const &set, const std::string &str)
Definition: to_value_of.hxx:45
M::value_t to_value_of(M const &set, const std::string &str)
Definition: to_value_of.hxx:27
M::genset_t::letter_t to_letter_of(M const &set, const std::string &str)
Definition: to_value_of.hxx:61
auto get_wordset(const L &labelset) -> typename labelset_trait< L >::wordset_t
Definition: traits.hh:232
Main namespace of Awali.
Definition: ato.hh:22
Exceptions thrown during parsing.
Definition: parse_exception.hh:26