stringify.hh

Go to the documentation of this file.
00001 /* vim: set sw=4 sts=4 et foldmethod=syntax : */
00002 
00003 /*
00004  * Copyright (c) 2005, 2006, 2007, 2008, 2009 Ciaran McCreesh
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_STRINGIFY_HH
00021 #define PALUDIS_GUARD_PALUDIS_STRINGIFY_HH 1
00022 
00023 #include <paludis/util/attributes.hh>
00024 #include <paludis/util/validated-fwd.hh>
00025 #include <tr1/memory>
00026 #include <sstream>
00027 #include <string>
00028 
00029 /** \file
00030  * Stringify functions.
00031  *
00032  * \ingroup g_strings
00033  *
00034  * \section Examples
00035  *
00036  * - None at this time.
00037  */
00038 
00039 namespace paludis
00040 {
00041     /**
00042      * For use by stringify.
00043      *
00044      * \ingroup g_strings
00045      */
00046     namespace stringify_internals
00047     {
00048         /**
00049          * Check that T_ is a sane type to be stringified.
00050          *
00051          * \ingroup g_strings
00052          */
00053         template <typename T_>
00054         struct CheckType
00055         {
00056             /// Yes, we are a sane type.
00057             enum { value = 0 } Value;
00058         };
00059 
00060         /**
00061          * Check that T_ is a sane type to be stringified, which it isn't
00062          * if it's a pointer unless it's a char * pointer.
00063          *
00064          * \ingroup g_strings
00065          */
00066         template <typename T_>
00067         struct CheckType<T_ *>
00068         {
00069         };
00070 
00071         /**
00072          * Check that T_ is a sane type to be stringified, which it isn't
00073          * if it's a CountedPtr.
00074          *
00075          * \ingroup g_strings
00076          */
00077         template <typename T_>
00078         struct CheckType<std::tr1::shared_ptr<T_> >
00079         {
00080         };
00081 
00082         /**
00083          * Check that T_ is a sane type to be stringified, which it isn't
00084          * if it's a pointer unless it's a char * pointer.
00085          *
00086          * \ingroup g_strings
00087          */
00088         template <>
00089         struct CheckType<char *>
00090         {
00091             /// Yes, we are a sane type.
00092             enum { value = 0 } Value;
00093         };
00094     }
00095 
00096     template <typename T_> inline std::string stringify(const T_ & item);
00097 
00098     namespace stringify_internals
00099     {
00100         /**
00101          * Internal function to convert item to a string, to make
00102          * function pointers work more sensibly.  May be overloaded,
00103          * but should not be called directly.
00104          *
00105          * \ingroup g_strings
00106          */
00107         template <typename T_>
00108         std::string
00109         real_stringify(const T_ & item)
00110         {
00111             /* check that we're not trying to stringify a pointer or somesuch */
00112             int check_for_stringifying_silly_things
00113                 PALUDIS_ATTRIBUTE((unused)) = CheckType<T_>::value;
00114 
00115             std::ostringstream s;
00116             s << item;
00117             return s.str();
00118         }
00119 
00120         inline std::string
00121         real_stringify(const std::string & item)
00122         {
00123             return item;
00124         }
00125 
00126         inline std::string
00127         real_stringify(const char & item)
00128         {
00129             return std::string(1, item);
00130         }
00131 
00132         inline std::string
00133         real_stringify(const unsigned char & item)
00134         {
00135             return std::string(1, item);
00136         }
00137 
00138         inline std::string
00139         real_stringify(const bool & item)
00140         {
00141             return item ? "true" : "false";
00142         }
00143 
00144         inline std::string
00145         real_stringify(const char * const item)
00146         {
00147             return std::string(item);
00148         }
00149 
00150         template <typename D_, typename V_, bool c_, typename C_>
00151         inline std::string
00152         real_stringify(const Validated<D_, V_, c_, C_> & v)
00153         {
00154             return stringify(v.data());
00155         }
00156     }
00157 
00158     /**
00159      * Convert item to a string.  To customise for new types, overload
00160      * stringify_internals::real_stringify, not this function.
00161      *
00162      * \ingroup g_strings
00163      */
00164     template <typename T_>
00165     inline std::string
00166     stringify(const T_ & item)
00167     {
00168         return stringify_internals::real_stringify(item);
00169     }
00170 }
00171 
00172 #endif

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