Upstream tarball 9890
[amule.git] / src / libs / common / Format.h
blobffa1c64ffa718ae83d124000a22c598096c8d9c8
1 //
2 // This file is part of the aMule Project.
3 //
4 // Copyright (c) 2005-2008 aMule Team ( admin@amule.org / http://www.amule.org )
5 //
6 // Any parts of this program derived from the xMule, lMule or eMule project,
7 // or contributed by third-party developers are copyrighted by their
8 // respective authors.
9 //
10 // This program is free software; you can redistribute it and/or modify
11 // it under the terms of the GNU General Public License as published by
12 // the Free Software Foundation; either version 2 of the License, or
13 // (at your option) any later version.
15 // This program is distributed in the hope that it will be useful,
16 // but WITHOUT ANY WARRANTY; without even the implied warranty of
17 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 // GNU General Public License for more details.
19 //
20 // You should have received a copy of the GNU General Public License
21 // along with this program; if not, write to the Free Software
22 // Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
25 #ifndef FORMAT_H
26 #define FORMAT_H
29 #include "MuleDebug.h"
31 #ifdef __GNUC__
32 #define GCC_VERSION (__GNUC__ * 10000 + __GNUC_MINOR__ * 100 + __GNUC_PATCHLEVEL__)
33 #else
34 /* Assume that other compilers don't have this bug */
35 #define GCC_VERSION 99999
36 #endif
39 /**
40 * Classes that implement this interface are usable as
41 * arguments for %s format strings. This is needed because
42 * CFormat objects are typically created as temporary objects,
43 * which makes external declarations of operator% impossible,
44 * due to the fact that a non-const reference to a temporary
45 * object is prohibited, as in:
46 * CFormat& operator%(CFormat& fmt, ...)
48 * With this approch, it is possible to use CFormat as usual:
49 * CFormat(...) % <some CPrintable object>;
51 class CPrintable
53 public:
54 /** Must return a "pretty" string representation of the object. */
55 virtual wxString GetPrintableString() const = 0;
57 protected:
58 virtual ~CPrintable() {}
64 /**
65 * This class offers a typesafe alternative to wxString::Format.
67 * Unlike normal format and printf, this class does not care about the
68 * type of integer values passed to it, and will handle a value correctly,
69 * even if the format-string and the actual type dissagree. Therefore, it
70 * is suggested that only %i and %u be used for integers, for the sake of
71 * clarity, though %i alone is enough.
73 * The other integer type-fields are supported, but will have no inpact on
74 * how the value is represented. The only exception to this is the 'o', 'x'
75 * and 'X' fields, which will always be considered to be unsigned!
77 * CFormat lacks the following capabilities:
78 * * The "*" width-modifier, because only one argument is fed at a time.
79 * * The "p" type, could be implemented using void* or templates.
80 * * The "n" type, just unsafe, wont be implemented.
81 * * The Long Double type, which is extremly slow and shouldn't be used.
83 * CFormat has been implemented against the description of printf found
84 * in the "man 3 printf" manual page.
86 class CFormat
88 public:
89 /**
90 * Constructor.
92 * @param str The format-string to be used.
94 CFormat(const wxChar* str);
96 /**
97 * Constructor.
98 * This form is required to construct from a plain char *
99 * with wx 2.9
101 * @param str The format-string to be used.
103 CFormat(const wxString& str);
106 * Returns true if the resulting string is ready for use.
108 * For a string to be ready to use, all format fields must have
109 * had a value assigned through the operator% functions.
111 bool IsReady() const;
115 * Feeds an value into the format-string.
117 * Passing an type that isn't compatible with the current format
118 * field results in field being skipped, and an exception is raised.
119 * Passing any type to an CFormat with no free fields results an
120 * assertion, and the argument being ignored.
122 * Special rules apply to integers, see above.
124 // \{
125 CFormat& operator%(wxChar value);
126 CFormat& operator%(signed short value);
127 CFormat& operator%(unsigned short value);
128 CFormat& operator%(signed int value);
129 CFormat& operator%(unsigned int value);
130 CFormat& operator%(signed long value);
131 CFormat& operator%(unsigned long value);
132 CFormat& operator%(signed long long value);
133 CFormat& operator%(unsigned long long value);
134 CFormat& operator%(double value);
135 CFormat& operator%(const wxChar* value);
136 CFormat& operator%(const wxString& value);
137 CFormat& operator%(const CPrintable& value);
138 CFormat& operator%(void * value);
139 // \}
143 * Returns the resulting string, should only be used when all arguments have been given.
145 wxString GetString() const;
148 * Implicit conversion to wxString.
150 operator wxString() const { return GetString(); };
152 private:
154 * Sets the value of the current field, and locates
155 * the next format field in the string.
157 void SetCurrentField(const wxString& value);
160 * Returns the current format-field, or an empty
161 * string if no field was found.
163 wxString GetCurrentField();
166 * Returns the next field modified to fit the given integer type.
168 * @param fieldType A modifier and type for an integer value.
169 * @return The resulting format-string.
171 * This function is used to generate the integer-type independant
172 * fields, by modifying the existing format-string to fit the type
173 * of the integer value that has been passed to it.
175 wxString GetIntegerField(const wxChar* fieldType);
177 //! Index to the current format field.
178 size_t m_fieldStart;
179 //! Length of the current format field.
180 size_t m_fieldLength;
181 //! The number of fields to skip in GetCurrentField
182 size_t m_skipCount;
184 //! The format-string fed to the parser.
185 wxString m_format;
186 //! The current result of arguments fed to the parser.
187 wxString m_result;
192 ////////////////////////////////////////////////////////////////////////////////
194 inline CFormat& CFormat::operator%(signed short value)
196 return *this % (signed long long)value;
200 inline CFormat& CFormat::operator%(unsigned short value)
202 return *this % (unsigned long long)value;
206 inline CFormat& CFormat::operator%(signed int value)
208 return *this % (signed long long)value;
212 inline CFormat& CFormat::operator%(unsigned int value)
214 return *this % (unsigned long long)value;
218 inline CFormat& CFormat::operator%(signed long value)
220 return *this % (signed long long)value;
224 inline CFormat& CFormat::operator%(unsigned long value)
226 return *this % (unsigned long long)value;
230 inline CFormat& CFormat::operator%(const wxChar* val)
232 return *this % wxString(val);
235 inline CFormat& CFormat::operator%(const CPrintable& value)
237 return *this % value.GetPrintableString();
240 #if wxCHECK_VERSION(2, 9, 0)
241 #define WXLONGLONGFMTSPEC wxT(wxLongLongFmtSpec)
242 #else
243 #define WXLONGLONGFMTSPEC wxLongLongFmtSpec
244 #endif
246 #endif
247 // File_checked_for_headers