sbuild-parse-value.h

Go to the documentation of this file.
00001 /* Copyright © 2005-2006  Roger Leigh <rleigh@debian.org>
00002  *
00003  * schroot is free software; you can redistribute it and/or modify it
00004  * under the terms of the GNU General Public License as published by
00005  * the Free Software Foundation; either version 2 of the License, or
00006  * (at your option) any later version.
00007  *
00008  * schroot is distributed in the hope that it will be useful, but
00009  * WITHOUT ANY WARRANTY; without even the implied warranty of
00010  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00011  * General Public License for more details.
00012  *
00013  * You should have received a copy of the GNU General Public License
00014  * along with this program; if not, write to the Free Software
00015  * Foundation, Inc., 59 Temple Place, Suite 330, Boston,
00016  * MA  02111-1307  USA
00017  *
00018  *********************************************************************/
00019 
00020 #ifndef SBUILD_PARSE_VALUE_H
00021 #define SBUILD_PARSE_VALUE_H
00022 
00023 #include <sbuild/sbuild-parse-error.h>
00024 #include <sbuild/sbuild-log.h>
00025 
00026 #include <string>
00027 #include <sstream>
00028 
00029 namespace sbuild
00030 {
00031 
00036   class parse_value
00037   {
00038   public:
00040     typedef parse_error error;
00041 
00046     parse_value (std::string const& value);
00047 
00049     virtual ~parse_value ();
00050 
00056     template <typename T>
00057     operator T (void)
00058     {
00059       T tmp;
00060 
00061       if (parse(tmp) == false)
00062         {
00063           throw error(parse_error::BAD_VALUE, this->value);
00064         }
00065 
00066       return tmp;
00067     }
00068 
00069   private:
00075     bool
00076     parse (bool& parsed_value) const;
00077 
00083     bool
00084     parse (std::string& parsed_value) const;
00085 
00091     template <typename T>
00092     bool
00093     parse (T& parsed_value) const
00094     {
00095       std::istringstream is(this->value);
00096       is.imbue(std::locale("C"));
00097       T tmpval;
00098       if (is >> tmpval)
00099         {
00100           parsed_value = tmpval;
00101           log_debug(DEBUG_NOTICE) << "value=" << parsed_value << std::endl;
00102           return true;
00103         }
00104       log_debug(DEBUG_NOTICE) << "parse error" << std::endl;
00105       return false;
00106     }
00107 
00108     std::string value;
00109   };
00110 
00111 }
00112 
00113 #endif /* SBUILD_PARSE_VALUE_H */
00114 
00115 /*
00116  * Local Variables:
00117  * mode:C++
00118  * End:
00119  */

Generated on Mon Jun 26 12:50:25 2006 for schroot by  doxygen 1.4.7