destringify.hh

Go to the documentation of this file.
00001 /* vim: set sw=4 sts=4 et foldmethod=syntax : */
00002 
00003 /*
00004  * Copyright (c) 2006 Stephen Bennett
00005  *
00006  * This file is part of the Paludis package manager. Paludis is free software;
00007  * you can redistribute it and/or modify it under the terms of the GNU General
00008  * Public License version 2, as published by the Free Software Foundation.
00009  *
00010  * Paludis is distributed in the hope that it will be useful, but WITHOUT ANY
00011  * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
00012  * FOR A PARTICULAR PURPOSE.  See the GNU General Public License for more
00013  * details.
00014  *
00015  * You should have received a copy of the GNU General Public License along with
00016  * this program; if not, write to the Free Software Foundation, Inc., 59 Temple
00017  * Place, Suite 330, Boston, MA  02111-1307  USA
00018  */
00019 
00020 #ifndef PALUDIS_GUARD_PALUDIS_DESTRINGIFY_HH
00021 #define PALUDIS_GUARD_PALUDIS_DESTRINGIFY_HH 1
00022 
00023 #include <paludis/util/exception.hh>
00024 #include <sstream>
00025 #include <string>
00026 
00027 /** \file
00028  * Destringify functions.
00029  *
00030  * \ingroup g_strings
00031  *
00032  * \section Examples
00033  *
00034  * - None at this time.
00035  */
00036 
00037 namespace paludis
00038 {
00039     /**
00040      * Default exception type thrown by destringify when it fails.
00041      *
00042      * \ingroup g_strings
00043      * \ingroup g_exceptions
00044      * \nosubgrouping
00045      */
00046     class PALUDIS_VISIBLE DestringifyError :
00047         public Exception
00048     {
00049         public:
00050             ///\name Basic operations
00051             ///\{
00052 
00053             DestringifyError(const std::string & str) throw ();
00054 
00055             ///\}
00056     };
00057 
00058     /**
00059      * For internal use by destringify.
00060      *
00061      * \ingroup g_strings
00062      */
00063     namespace destringify_internals
00064     {
00065         /**
00066          * Basic destringifier.
00067          *
00068          * \ingroup g_strings
00069          */
00070         template <typename Type_, typename Exception_>
00071         struct Destringifier
00072         {
00073             /**
00074              * Do the destringification.
00075              */
00076             static Type_ do_destringify(const std::string & s)
00077             {
00078                 std::istringstream ss(s);
00079                 Type_ t;
00080                 ss >> t;
00081                 if (! ss.eof() || ss.fail())
00082                     throw Exception_(s);
00083                 return t;
00084             }
00085         };
00086 
00087         /**
00088          * Specialised destringify for std::string.
00089          *
00090          * \ingroup g_strings
00091          */
00092         template <typename Exception_>
00093         struct Destringifier<std::string, Exception_>
00094         {
00095             /**
00096              * Do the destringification.
00097              */
00098             static std::string do_destringify(const std::string & s)
00099             {
00100                 return s;
00101             }
00102         };
00103 
00104         /**
00105          * Specialised destringify for bool.
00106          *
00107          * \ingroup g_strings
00108          */
00109         template <typename Exception_>
00110         struct Destringifier<bool, Exception_>
00111         {
00112             /**
00113              * Do the destringification.
00114              */
00115             static bool do_destringify(const std::string & s)
00116             {
00117                 /* Don't use boolalpha on std::istringstream here, since it's way too difficult
00118                  * to test for errors and eof. See gcc bug 37958. */
00119                 if (s == "true")
00120                     return true;
00121                 else if (s == "false")
00122                     return false;
00123                 else
00124                 {
00125                     std::istringstream ss(s);
00126                     int i;
00127                     ss >> i;
00128                     if (ss.eof() && ! ss.bad())
00129                         return i > 0;
00130                     else
00131                         throw Exception_(s);
00132                 }
00133             }
00134         };
00135 
00136         /**
00137          * Specialised destringify for char.
00138          *
00139          * \ingroup g_strings
00140          */
00141         template <typename Exception_>
00142         struct Destringifier<char, Exception_>
00143         {
00144             /**
00145              * Do the destringification.
00146              */
00147             static char do_destringify(const std::string & s)
00148             {
00149                 if (s.length() == 1)
00150                     return s[0];
00151                 else
00152                     throw Exception_(s);
00153             }
00154         };
00155     }
00156 
00157     /**
00158      * Extract a value of some type from a string.
00159      *
00160      * \ingroup g_strings
00161      */
00162     template <typename Type_, typename Exception_>
00163     Type_ destringify(const std::string & s)
00164     {
00165         if (s == "")
00166             throw Exception_("");
00167 
00168         return destringify_internals::Destringifier<Type_, Exception_>::do_destringify(s);
00169     }
00170 
00171     template <typename Type_>
00172     Type_ destringify(const std::string & s)
00173     {
00174         return destringify<Type_, DestringifyError>(s);
00175     }
00176 }
00177 
00178 #endif

Generated on Mon Sep 21 10:36:08 2009 for paludis by  doxygen 1.5.4