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 <string>
00024 
00025 #include "sbuild-parse-error.h"
00026 #include "sbuild-log.h"
00027 
00028 namespace sbuild
00029 {
00030 
00035   class parse_value
00036   {
00037   public:
00039     typedef parse_error error;
00040 
00045     parse_value (std::string const& value);
00046 
00048     virtual ~parse_value ();
00049 
00055     template <typename T>
00056     operator T (void)
00057     {
00058       T tmp;
00059 
00060       if (parse(tmp) == false)
00061         {
00062           throw error(parse_error::BAD_VALUE, this->value);
00063         }
00064 
00065       return tmp;
00066     }
00067 
00068   private:
00074     bool
00075     parse (bool& parsed_value) const;
00076 
00082     bool
00083     parse (std::string& parsed_value) const;
00084 
00090     template <typename T>
00091     bool
00092     parse (T& parsed_value) const
00093     {
00094       std::istringstream is(this->value);
00095       is.imbue(std::locale("C"));
00096       T tmpval;
00097       if (is >> tmpval)
00098         {
00099           parsed_value = tmpval;
00100           log_debug(DEBUG_NOTICE) << "value=" << parsed_value << std::endl;
00101           return true;
00102         }
00103       log_debug(DEBUG_NOTICE) << "parse error" << std::endl;
00104       return false;
00105     }
00106 
00107     std::string value;
00108   };
00109 
00110 }
00111 
00112 #endif /* SBUILD_PARSE_VALUE_H */
00113 
00114 /*
00115  * Local Variables:
00116  * mode:C++
00117  * End:
00118  */

Generated on Sat Jun 17 14:41:22 2006 for schroot by  doxygen 1.4.6