Vaucanson 1.4
int_letter.hxx
00001 // int_letter.hxx: this file is part of the Vaucanson project.
00002 //
00003 // Vaucanson, a generic library for finite state machines.
00004 //
00005 // Copyright (C) 2004, 2005, 2007, 2008, 2009 The Vaucanson Group.
00006 //
00007 // This program is free software; you can redistribute it and/or
00008 // modify it under the terms of the GNU General Public License
00009 // as published by the Free Software Foundation; either version 2
00010 // of the License, or (at your option) any later version.
00011 //
00012 // The complete GNU General Public Licence Notice can be found as the
00013 // `COPYING' file in the root directory.
00014 //
00015 // The Vaucanson Group consists of people listed in the `AUTHORS' file.
00016 //
00017 #ifndef VCSN_ALGEBRA_IMPLEMENTATION_LETTER_INT_LETTER_HXX
00018 # define VCSN_ALGEBRA_IMPLEMENTATION_LETTER_INT_LETTER_HXX
00019 
00020 # include <string>
00021 # include <sstream>
00022 # include <utility>
00023 # include <climits>
00024 
00025 # include <vaucanson/algebra/implementation/letter/int_letter.hh>
00026 
00027 namespace vcsn
00028 {
00029   namespace algebra
00030   {
00031     template <>
00032     struct letter_traits<int>
00033     {
00034       // This letter type needs more than one char to be displayed.
00035       typedef misc::false_t is_char_letter;
00036 
00037       enum
00038       {
00039         // Here we use UINT_MAX and not UINT_MAX + 1 because an enum cannot
00040         // hold UINT_MAX + 1.
00041         cardinal = UINT_MAX
00042       };
00043 
00044       // We can not "project" an int letter.
00045       typedef undefined_type first_projection_t;
00046       typedef undefined_type second_projection_t;
00047 
00048       static
00049       std::pair<bool, int>
00050       literal_to_letter(const std::string& str)
00051       {
00052         std::stringstream sstr(str);
00053 
00054         // Disallow a leading '+' because it would be too confusing:
00055         // would '2+4' mean '2#+4' or '(2)+(4)'?
00056         if (str[0] == '+')
00057           return std::make_pair(false, 0);
00058 
00059         int ret = 0;
00060         // Do not skip spaces when translating numbers, otherwise a
00061         // blank string such as " " would be translated into 0 without
00062         // any problem.
00063         sstr >> std::noskipws >> ret;
00064 
00065         // Check if something is left in the stream.
00066         if (!sstr.eof())
00067           return std::make_pair(false, 0);
00068 
00069         return std::make_pair(true, ret);
00070       }
00071 
00072       static
00073       std::string
00074       letter_to_literal(const int& c)
00075       {
00076         std::stringstream sstr;
00077         sstr << c;
00078         return sstr.str();
00079       }
00080 
00081       // An int is "simple" with dimension 1.
00082       static std::string kind() { return "simple"; }
00083       static int dim() { return 1; }
00084 
00085     };
00086 
00087   } // ! algebra
00088 
00089 } // ! vcsn
00090 
00091 #endif // ! VCSN_ALGEBRA_IMPLEMENTATION_LETTER_INT_LETTER_HXX