Vaucanson  1.4.1
int_letter.hxx
1 // int_letter.hxx: this file is part of the Vaucanson project.
2 //
3 // Vaucanson, a generic library for finite state machines.
4 //
5 // Copyright (C) 2004, 2005, 2007, 2008, 2009 The Vaucanson Group.
6 //
7 // This program is free software; you can redistribute it and/or
8 // modify it under the terms of the GNU General Public License
9 // as published by the Free Software Foundation; either version 2
10 // of the License, or (at your option) any later version.
11 //
12 // The complete GNU General Public Licence Notice can be found as the
13 // `COPYING' file in the root directory.
14 //
15 // The Vaucanson Group consists of people listed in the `AUTHORS' file.
16 //
17 #ifndef VCSN_ALGEBRA_IMPLEMENTATION_LETTER_INT_LETTER_HXX
18 # define VCSN_ALGEBRA_IMPLEMENTATION_LETTER_INT_LETTER_HXX
19 
20 # include <string>
21 # include <sstream>
22 # include <utility>
23 # include <climits>
24 
25 # include <vaucanson/algebra/implementation/letter/int_letter.hh>
26 
27 namespace vcsn
28 {
29  namespace algebra
30  {
31  template <>
32  struct letter_traits<int>
33  {
34  // This letter type needs more than one char to be displayed.
35  typedef misc::false_t is_char_letter;
36 
37  enum
38  {
39  // Here we use UINT_MAX and not UINT_MAX + 1 because an enum cannot
40  // hold UINT_MAX + 1.
41  cardinal = UINT_MAX
42  };
43 
44  // We can not "project" an int letter.
45  typedef undefined_type first_projection_t;
46  typedef undefined_type second_projection_t;
47 
48  static
49  std::pair<bool, int>
50  literal_to_letter(const std::string& str)
51  {
52  std::stringstream sstr(str);
53 
54  // Disallow a leading '+' because it would be too confusing:
55  // would '2+4' mean '2#+4' or '(2)+(4)'?
56  if (str[0] == '+')
57  return std::make_pair(false, 0);
58 
59  int ret = 0;
60  // Do not skip spaces when translating numbers, otherwise a
61  // blank string such as " " would be translated into 0 without
62  // any problem.
63  sstr >> std::noskipws >> ret;
64 
65  // Check if something is left in the stream.
66  if (!sstr.eof())
67  return std::make_pair(false, 0);
68 
69  return std::make_pair(true, ret);
70  }
71 
72  static
73  std::string
74  letter_to_literal(const int& c)
75  {
76  std::stringstream sstr;
77  sstr << c;
78  return sstr.str();
79  }
80 
81  // An int is "simple" with dimension 1.
82  static std::string kind() { return "simple"; }
83  static int dim() { return 1; }
84 
85  };
86 
87  } // ! algebra
88 
89 } // ! vcsn
90 
91 #endif // ! VCSN_ALGEBRA_IMPLEMENTATION_LETTER_INT_LETTER_HXX