paludis Version 3.0.2
The one true package mangler
stringify.hh
Go to the documentation of this file.
1/* vim: set sw=4 sts=4 et foldmethod=syntax : */
2
3/*
4 * Copyright (c) 2005, 2006, 2007, 2008, 2009, 2010 Ciaran McCreesh
5 *
6 * This file is part of the Paludis package manager. Paludis is free software;
7 * you can redistribute it and/or modify it under the terms of the GNU General
8 * Public License version 2, as published by the Free Software Foundation.
9 *
10 * Paludis is distributed in the hope that it will be useful, but WITHOUT ANY
11 * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
12 * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
13 * details.
14 *
15 * You should have received a copy of the GNU General Public License along with
16 * this program; if not, write to the Free Software Foundation, Inc., 59 Temple
17 * Place, Suite 330, Boston, MA 02111-1307 USA
18 */
19
20#ifndef PALUDIS_GUARD_PALUDIS_STRINGIFY_HH
21#define PALUDIS_GUARD_PALUDIS_STRINGIFY_HH 1
22
24#include <paludis/util/wrapped_value-fwd.hh>
25#include <memory>
26#include <sstream>
27#include <string>
28
29/** \file
30 * Stringify functions.
31 *
32 * \ingroup g_strings
33 *
34 * \section Examples
35 *
36 * - None at this time.
37 */
38
39namespace paludis
40{
41 /**
42 * For use by stringify.
43 *
44 * \ingroup g_strings
45 */
47 {
48 /**
49 * Check that T_ is a sane type to be stringified.
50 *
51 * \ingroup g_strings
52 */
53 template <typename T_>
54 struct CheckType
55 {
56 /// Yes, we are a sane type.
57 enum { value = 0 } Value;
58 };
59
60 /**
61 * Check that T_ is a sane type to be stringified, which it isn't
62 * if it's a pointer unless it's a char * pointer.
63 *
64 * \ingroup g_strings
65 */
66 template <typename T_>
67 struct CheckType<T_ *>
68 {
69 };
70
71 /**
72 * Check that T_ is a sane type to be stringified, which it isn't
73 * if it's a CountedPtr.
74 *
75 * \ingroup g_strings
76 */
77 template <typename T_>
78 struct CheckType<std::shared_ptr<T_> >
79 {
80 };
81
82 /**
83 * Check that T_ is a sane type to be stringified, which it isn't
84 * if it's a pointer unless it's a char * pointer.
85 *
86 * \ingroup g_strings
87 */
88 template <>
89 struct CheckType<char *>
90 {
91 /// Yes, we are a sane type.
92 enum { value = 0 } Value;
93 };
94 }
95
96 template <typename T_> inline std::string stringify(const T_ & item);
97
98 namespace stringify_internals
99 {
100 /**
101 * Internal function to convert item to a string, to make
102 * function pointers work more sensibly. May be overloaded,
103 * but should not be called directly.
104 *
105 * \ingroup g_strings
106 */
107 template <typename T_>
108 std::string
109 real_stringify(const T_ & item)
110 {
111 /* check that we're not trying to stringify a pointer or somesuch */
112 int check_for_stringifying_silly_things
113 PALUDIS_ATTRIBUTE((unused)) = CheckType<T_>::value;
114
115 std::ostringstream s;
116 s << item;
117 return s.str();
118 }
119
120 inline std::string
121 real_stringify(const std::string & item)
122 {
123 return item;
124 }
125
126 inline std::string
127 real_stringify(const char & item)
128 {
129 return std::string(1, item);
130 }
131
132 inline std::string
133 real_stringify(const unsigned char & item)
134 {
135 return std::string(1, item);
136 }
137
138 inline std::string
139 real_stringify(const bool & item)
140 {
141 return item ? "true" : "false";
142 }
143
144 inline std::string
145 real_stringify(const char * const item)
146 {
147 return std::string(item);
148 }
149
150 template <typename Tag_>
151 inline std::string
152 real_stringify(const WrappedValue<Tag_> & v)
153 {
154 return stringify(v.value());
155 }
156 }
157
158 /**
159 * Convert item to a string. To customise for new types, overload
160 * stringify_internals::real_stringify, not this function.
161 *
162 * \ingroup g_strings
163 */
164 template <typename T_>
165 inline std::string
166 stringify(const T_ & item)
167 {
169 }
170}
171
172#endif
std::string stringify(const T_ &item)
Definition stringify.hh:166
std::string real_stringify(const T_ &item)
Definition stringify.hh:109
#define PALUDIS_ATTRIBUTE(x)
Definition attributes.hh:53
Definition stringify.hh:47
Definition about_metadata-fwd.hh:24
Definition stringify.hh:55