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 <boost/format.hpp>
00026 
00027 #include "sbuild-error.h"
00028 #include "sbuild-log.h"
00029 
00030 namespace sbuild
00031 {
00032 
00037   class parse_value
00038   {
00039   public:
00041     typedef runtime_error_custom<parse_value> error;
00042 
00047     parse_value (std::string const& value);
00048 
00050     virtual ~parse_value ();
00051 
00057     template <typename T>
00058     operator T (void)
00059     {
00060       T tmp;
00061 
00062       if (parse(tmp) == false)
00063         {
00064           boost::format fmt("Could not parse value \"%1%\"");
00065           fmt % this->value;
00066           throw error(fmt);
00067         }
00068 
00069       return tmp;
00070     }
00071 
00072   private:
00078     bool
00079     parse (bool& parsed_value) const;
00080 
00086     bool
00087     parse (std::string& parsed_value) const;
00088 
00094     template <typename T>
00095     bool
00096     parse (T& parsed_value) const
00097     {
00098       std::istringstream is(this->value);
00099       is.imbue(std::locale("C"));
00100       T tmpval;
00101       if (is >> tmpval)
00102         {
00103           parsed_value = tmpval;
00104           log_debug(DEBUG_NOTICE) << "value=" << parsed_value << std::endl;
00105           return true;
00106         }
00107       log_debug(DEBUG_NOTICE) << "parse error" << std::endl;
00108       return false;
00109     }
00110 
00111     std::string value;
00112   };
00113 
00114 }
00115 
00116 #endif /* SBUILD_PARSE_VALUE_H */
00117 
00118 /*
00119  * Local Variables:
00120  * mode:C++
00121  * End:
00122  */

Generated on Sun Apr 30 14:07:01 2006 for schroot by  doxygen 1.4.6