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 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 #ifdef PALUDIS_HAVE_CONCEPTS
00030 #  include <concepts>
00031 #endif
00032 
00033 /** \file
00034  * Stringify functions.
00035  *
00036  * \ingroup g_strings
00037  *
00038  * \section Examples
00039  *
00040  * - None at this time.
00041  */
00042 
00043 namespace paludis
00044 {
00045 #ifdef PALUDIS_HAVE_CONCEPTS
00046     auto concept IsStreamStringifiable<typename T_>
00047     {
00048         requires ! std::Dereferenceable<T_>;
00049         std::ostream & operator<< (std::ostream &, const T_ &);
00050     };
00051 
00052     auto concept IsStringifiable<typename T_>
00053     {
00054         std::string stringify(const T_ &);
00055     };
00056 #endif
00057 
00058 #ifndef PALUDIS_HAVE_CONCEPTS
00059     /**
00060      * For use by stringify.
00061      *
00062      * \ingroup g_strings
00063      */
00064     namespace stringify_internals
00065     {
00066         /**
00067          * Check that T_ is a sane type to be stringified.
00068          *
00069          * \ingroup g_strings
00070          */
00071         template <typename T_>
00072         struct CheckType
00073         {
00074             /// Yes, we are a sane type.
00075             enum { value = 0 } Value;
00076         };
00077 
00078         /**
00079          * Check that T_ is a sane type to be stringified, which it isn't
00080          * if it's a pointer unless it's a char * pointer.
00081          *
00082          * \ingroup g_strings
00083          */
00084         template <typename T_>
00085         struct CheckType<T_ *>
00086         {
00087         };
00088 
00089         /**
00090          * Check that T_ is a sane type to be stringified, which it isn't
00091          * if it's a CountedPtr.
00092          *
00093          * \ingroup g_strings
00094          */
00095         template <typename T_>
00096         struct CheckType<std::tr1::shared_ptr<T_> >
00097         {
00098         };
00099 
00100         /**
00101          * Check that T_ is a sane type to be stringified, which it isn't
00102          * if it's a pointer unless it's a char * pointer.
00103          *
00104          * \ingroup g_strings
00105          */
00106         template <>
00107         struct CheckType<char *>
00108         {
00109             /// Yes, we are a sane type.
00110             enum { value = 0 } Value;
00111         };
00112     }
00113 #endif
00114 
00115     template <typename T_> inline std::string stringify(const T_ & item);
00116 
00117     namespace stringify_internals
00118     {
00119         /**
00120          * Internal function to convert item to a string, to make
00121          * function pointers work more sensibly.  May be overloaded,
00122          * but should not be called directly.
00123          *
00124          * \ingroup g_strings
00125          */
00126         template <typename T_>
00127 #ifdef PALUDIS_HAVE_CONCEPTS
00128             requires IsStreamStringifiable<T_>
00129 #endif
00130         std::string
00131         real_stringify(const T_ & item)
00132         {
00133 #ifndef PALUDIS_HAVE_CONCEPTS
00134             /* check that we're not trying to stringify a pointer or somesuch */
00135             int check_for_stringifying_silly_things
00136                 PALUDIS_ATTRIBUTE((unused)) = CheckType<T_>::value;
00137 #endif
00138 
00139             std::ostringstream s;
00140             s << item;
00141             return s.str();
00142         }
00143 
00144         inline std::string
00145         real_stringify(const std::string & item)
00146         {
00147             return item;
00148         }
00149 
00150         inline std::string
00151         real_stringify(const char & item)
00152         {
00153             return std::string(1, item);
00154         }
00155 
00156         inline std::string
00157         real_stringify(const unsigned char & item)
00158         {
00159             return std::string(1, item);
00160         }
00161 
00162         inline std::string
00163         real_stringify(const bool & item)
00164         {
00165             return item ? "true" : "false";
00166         }
00167 
00168         inline std::string
00169         real_stringify(const char * const item)
00170         {
00171             return std::string(item);
00172         }
00173 
00174         template <typename D_, typename V_, bool c_, typename C_>
00175         inline std::string
00176         real_stringify(const Validated<D_, V_, c_, C_> & v)
00177         {
00178             return stringify(v.data());
00179         }
00180     }
00181 
00182     /**
00183      * Convert item to a string.  To customise for new types, overload
00184      * stringify_internals::real_stringify, not this function.
00185      *
00186      * \ingroup g_strings
00187      */
00188     template <typename T_>
00189     inline std::string
00190     stringify(const T_ & item)
00191     {
00192         return stringify_internals::real_stringify(item);
00193     }
00194 }
00195 
00196 #endif

Generated on Mon Dec 22 19:43:51 2008 for paludis by  doxygen 1.5.7.1