Remove second template parameter from class GUIList
[openttd/fttd.git] / src / strings_func.h
blob862a5fa7287e9f0667dec9682bcf761f2236113e
1 /* $Id$ */
3 /*
4 * This file is part of OpenTTD.
5 * OpenTTD is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, version 2.
6 * OpenTTD is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
7 * See the GNU General Public License for more details. You should have received a copy of the GNU General Public License along with OpenTTD. If not, see <http://www.gnu.org/licenses/>.
8 */
10 /** @file strings_func.h Functions related to OTTD's strings. */
12 #ifndef STRINGS_FUNC_H
13 #define STRINGS_FUNC_H
15 #include "strings_type.h"
16 #include "string.h"
17 #include "gfx_type.h"
19 class StringParameters {
20 StringParameters *parent; ///< If not NULL, this instance references data from this parent instance.
21 uint64 *data; ///< Array with the actual data.
22 WChar *type; ///< Array with type information about the data. Can be NULL when no type information is needed. See #StringControlCode.
24 public:
25 uint offset; ///< Current offset in the data/type arrays.
26 uint num_param; ///< Length of the data array.
28 /** Create a new StringParameters instance. */
29 StringParameters(uint64 *data, uint num_param, WChar *type) :
30 parent(NULL),
31 data(data),
32 type(type),
33 offset(0),
34 num_param(num_param)
35 { }
37 /** Create a new StringParameters instance. */
38 template <size_t Tnum_param>
39 StringParameters(int64 (&data)[Tnum_param]) :
40 parent(NULL),
41 data((uint64 *)data),
42 type(NULL),
43 offset(0),
44 num_param(Tnum_param)
46 assert_compile(sizeof(data[0]) == sizeof(uint64));
49 /**
50 * Create a new StringParameters instance that can reference part of the data of
51 * the given partent instance.
53 StringParameters(StringParameters &parent, uint size) :
54 parent(&parent),
55 data(parent.data + parent.offset),
56 offset(0),
57 num_param(size)
59 assert(size <= parent.GetDataLeft());
60 if (parent.type == NULL) {
61 this->type = NULL;
62 } else {
63 this->type = parent.type + parent.offset;
67 ~StringParameters()
69 if (this->parent != NULL) {
70 this->parent->offset += this->num_param;
74 void ClearTypeInformation();
76 int64 GetInt64(WChar type = 0);
78 /** Read an int32 from the argument array. @see GetInt64. */
79 int32 GetInt32(WChar type = 0)
81 return (int32)this->GetInt64(type);
84 /** Get a pointer to the current element in the data array. */
85 uint64 *GetDataPointer() const
87 return &this->data[this->offset];
90 /** Return the amount of elements which can still be read. */
91 uint GetDataLeft() const
93 return this->num_param - this->offset;
96 /** Get a pointer to a specific element in the data array. */
97 uint64 *GetPointerToOffset(uint offset) const
99 assert(offset < this->num_param);
100 return &this->data[offset];
103 /** Does this instance store information about the type of the parameters. */
104 bool HasTypeInformation() const
106 return this->type != NULL;
109 /** Get the type of a specific element. */
110 WChar GetTypeAtOffset(uint offset) const
112 assert(offset < this->num_param);
113 assert(this->HasTypeInformation());
114 return this->type[offset];
117 void SetParam(uint n, uint64 v)
119 assert(n < this->num_param);
120 this->data[n] = v;
123 uint64 GetParam(uint n) const
125 assert(n < this->num_param);
126 return this->data[n];
129 extern StringParameters _global_string_params;
131 const char *GetStringPtr(StringID string);
133 void AppendString (stringb *buf, StringID string);
135 static inline void GetString (stringb *buf, StringID string)
137 buf->clear();
138 AppendString (buf, string);
141 template <uint N>
142 static inline void GetString (char (*buf) [N], StringID string)
144 stringb tmp (N, &(*buf)[0]);
145 GetString (&tmp, string);
148 template <uint N>
149 static inline void GetString (char (&buf) [N], StringID string)
151 GetString (&buf, string);
154 void AppendStringWithArgs (stringb *buf, StringID string, StringParameters *args, uint case_index = 0, bool game_script = false);
156 uint ConvertKmhishSpeedToDisplaySpeed(uint speed);
157 uint ConvertDisplaySpeedToKmhishSpeed(uint speed);
160 * Set a string parameter \a v at index \a n in the global string parameter array.
161 * @param n Index of the string parameter.
162 * @param v Value of the string parameter.
164 static inline void SetDParam(uint n, uint64 v)
166 _global_string_params.SetParam(n, v);
169 void SetDParamMaxValue(uint n, uint64 max_value, uint min_count = 0, FontSize size = FS_NORMAL);
170 void SetDParamMaxDigits(uint n, uint count, FontSize size = FS_NORMAL);
172 void SetDParamStr(uint n, const char *str);
174 void CopyInDParam(int offs, const uint64 *src, int num);
175 void CopyOutDParam(uint64 *dst, int offs, int num);
176 void CopyOutDParam(uint64 *dst, const char **strings, StringID string, int num);
179 * Get the current string parameter at index \a n from the global string parameter array.
180 * @param n Index of the string parameter.
181 * @return Value of the requested string parameter.
183 static inline uint64 GetDParam(uint n)
185 return _global_string_params.GetParam(n);
188 extern TextDirection _current_text_dir; ///< Text direction of the currently selected language
190 void InitializeLanguagePacks();
191 const char *GetCurrentLanguageIsoCode();
193 int CDECL StringIDSorter(const StringID *a, const StringID *b);
196 * A searcher for missing glyphs.
198 class MissingGlyphSearcher {
199 public:
200 const FontSize default_size; ///< default font size of the string
201 const bool monospace; ///< whether to search for a monospace font
203 /** Construct an instance. */
204 CONSTEXPR MissingGlyphSearcher (FontSize size, bool mono)
205 : default_size(size), monospace(mono)
209 /** Make sure everything gets destructed right. */
210 virtual ~MissingGlyphSearcher() {}
213 * Get the next string to search through.
214 * @return The next string or NULL if there is none.
216 virtual const char *NextString() = 0;
219 * Get the default (font) size of the string.
220 * @return The font size.
222 FontSize DefaultSize() const
224 return this->default_size;
228 * Reset the search, i.e. begin from the beginning again.
230 virtual void Reset() = 0;
233 * Whether to search for a monospace font or not.
234 * @return True if searching for monospace.
236 bool Monospace() const
238 return this->monospace;
241 bool FindMissingGlyphs (void);
244 void CheckForMissingGlyphs(bool base_font = true, MissingGlyphSearcher *search = NULL);
246 #endif /* STRINGS_FUNC_H */