soui 5.0.0.1
Soui5 Doc
 
Loading...
Searching...
No Matches
SNamedValue.h
1#ifndef __SNAMEDVALUE__H__
2#define __SNAMEDVALUE__H__
3
4#include "helper/SColor.h"
5#include "layout/SLayoutSize.h"
6#include <stdlib.h>
7
8SNSBEGIN
9
10#define MAX_NAME 50
11
12/**
13 * @struct TNAMEDVALUE
14 * @brief Structure to hold a named value.
15 *
16 * @details This structure contains a name (up to `MAX_NAME` characters) and a value of type `T`.
17 * @tparam T Type of the value.
18 */
19template <class T>
21{
22 wchar_t strName[MAX_NAME + 1]; // Name of the value
23 T value; // Value associated with the name
24};
25
26/**
27 * @class SNamedValue
28 * @brief Template class for managing named values.
29 *
30 * @details This class manages a list of named values, allowing for initialization from XML, retrieval by name, and merging of values.
31 * @tparam T Type of the value.
32 * @tparam ValueParser Class responsible for parsing the value from a string.
33 */
34template <class T, class ValueParser>
36 public:
37 typedef TNAMEDVALUE<T> NAMEDVALUE; // Type alias for TNAMEDVALUE
38
39 /**
40 * @brief Initializes the named values from an XML node.
41 * @param xmlNode XML node containing the named values.
42 * @return TRUE if initialization is successful, FALSE otherwise.
43 */
44 virtual BOOL Init(SXmlNode xmlNode)
45 {
46 if (!xmlNode)
47 return FALSE;
48
49 for (SXmlNode xmlStr = xmlNode.first_child(); xmlStr; xmlStr = xmlStr.next_sibling())
50 {
51 if (xmlStr.type() != node_element)
52 continue;
53
54 NAMEDVALUE namedValue;
55 wcscpy_s(namedValue.strName, MAX_NAME, xmlStr.name());
56 if (ValueParser::ParseValue(xmlStr.attribute(L"value").as_string(), namedValue.value))
57 {
58 m_lstNamedValue.Add(namedValue);
59 }
60 else
61 {
62 SStringW msg = SStringW().Format(L"parse value failed, name=%s,value=%s", xmlStr.name(), xmlStr.attribute(L"value").as_string());
63 SASSERT_MSGW(false, msg);
64 }
65 }
66 qsort(m_lstNamedValue.GetData(), m_lstNamedValue.GetCount(), sizeof(NAMEDVALUE), Compare);
67 return TRUE;
68 }
69
70 /**
71 * @brief Converts a string name to its corresponding value.
72 * @param strName Name of the value.
73 * @return Value associated with the name, or a null value if not found.
74 */
75 T String2Value(const SStringW &strName) const
76 {
77 T ret;
78 if (FindValue(strName, ret))
79 return ret;
80 static const T nullValue = ValueParser::GetNullValue();
81 return nullValue;
82 }
83
84 /**
85 * @brief Finds the value associated with a given name.
86 * @param strName Name of the value.
87 * @param value Variable to store the found value.
88 * @return TRUE if the value is found, FALSE otherwise.
89 */
90 BOOL FindValue(const SStringW &strName, T &value) const
91 {
92 NAMEDVALUE target;
93 wcscpy_s(target.strName, MAX_NAME, strName);
94 NAMEDVALUE *pFind = (NAMEDVALUE *)bsearch(&target, m_lstNamedValue.GetData(), m_lstNamedValue.GetCount(), sizeof(NAMEDVALUE), Compare);
95 if (!pFind)
96 return FALSE;
97 value = pFind->value;
98 return TRUE;
99 }
100
101 /**
102 * @brief Retrieves the value at a specific index.
103 * @param idx Index of the value.
104 * @return Value at the specified index.
105 */
106 T GetAt(const int idx) const
107 {
108 SASSERT(idx >= 0 && idx < (int)m_lstNamedValue.GetCount());
109 return m_lstNamedValue[idx].value;
110 }
111
112 /**
113 * @brief Merges the named values from another SNamedValue object.
114 * @param src Source SNamedValue object.
115 * @return Number of new values added.
116 */
117 size_t Merge(const SNamedValue &src)
118 {
119 if (m_lstNamedValue.IsEmpty())
120 {
121 m_lstNamedValue.Copy(src.m_lstNamedValue);
122 return src.m_lstNamedValue.GetCount();
123 }
124 else
125 {
126 SArray<NAMEDVALUE> data;
127 for (size_t i = 0; i < src.m_lstNamedValue.GetCount(); i++)
128 {
129 NAMEDVALUE *pFind = (NAMEDVALUE *)bsearch(&src.m_lstNamedValue[i], m_lstNamedValue.GetData(), m_lstNamedValue.GetCount(), sizeof(NAMEDVALUE), Compare);
130 if (!pFind)
131 {
132 data.Add(src.m_lstNamedValue[i]);
133 }
134 }
135 m_lstNamedValue.Append(data);
136 qsort(m_lstNamedValue.GetData(), m_lstNamedValue.GetCount(), sizeof(NAMEDVALUE), Compare);
137 return data.GetCount();
138 }
139 }
140
141 /**
142 * @brief Retrieves the number of named values.
143 * @return Number of named values.
144 */
145 UINT GetCount() const
146 {
147 return (UINT)m_lstNamedValue.GetCount();
148 }
149
150 protected:
151 /**
152 * @brief Comparison function for sorting and searching.
153 * @param p1 Pointer to the first NAMEDVALUE.
154 * @param p2 Pointer to the second NAMEDVALUE.
155 * @return Comparison result.
156 */
157 static int Compare(const void *p1, const void *p2)
158 {
159 const NAMEDVALUE *pData1 = (const NAMEDVALUE *)p1;
160 const NAMEDVALUE *pData2 = (const NAMEDVALUE *)p2;
161 return wcscmp(pData1->strName, pData2->strName);
162 }
163
164 SArray<NAMEDVALUE> m_lstNamedValue; // Array to store named values
165};
166
167/**
168 * @class SIntParser
169 * @brief Parser for integer values.
170 */
171class SOUI_EXP SIntParser {
172 public:
173 /**
174 * @brief Parses a string to an integer value.
175 * @param strValue String representation of the value.
176 * @param value Variable to store the parsed integer value.
177 * @return TRUE if parsing is successful, FALSE otherwise.
178 */
179 static bool ParseValue(const SStringW &strValue, int &value);
180
181 /**
182 * @brief Retrieves the null value for integers.
183 * @return Null value for integers.
184 */
185 static int GetNullValue();
186};
187
188/**
189 * @class SNamedID
190 * @brief Manages named integer IDs.
191 */
192class SOUI_EXP SNamedID : public SNamedValue<int, SIntParser> {
193 public:
194 /**
195 * @brief Initializes named IDs from an array.
196 * @param pValue Array of named values.
197 * @param nCount Number of elements in the array.
198 * @param bSorted TRUE if the array is already sorted, FALSE otherwise.
199 */
200 void Init2(const NAMEDVALUE *pValue, int nCount, BOOL bSorted);
201
202 /**
203 * @brief Initializes named IDs from arrays of names and IDs.
204 * @param pNames Array of names.
205 * @param nIDs Array of IDs.
206 * @param nCount Number of elements in the arrays.
207 * @param bSorted TRUE if the arrays are already sorted, FALSE otherwise.
208 */
209 void Init3(const LPCWSTR *pNames, const int *nIDs, int nCount, BOOL bSorted);
210};
211
212/**
213 * @class SColorParser
214 * @brief Parser for color values.
215 */
216class SOUI_EXP SColorParser {
217 public:
218 /**
219 * @brief Parses a string to a color value.
220 * @param strValue String representation of the color.
221 * @param value Variable to store the parsed color value.
222 * @return TRUE if parsing is successful, FALSE otherwise.
223 */
224 static bool ParseValue(const SStringW &strValue, COLORREF &value);
225
226 /**
227 * @brief Retrieves the null value for colors.
228 * @return Null value for colors.
229 */
230 static COLORREF GetNullValue();
231};
232
233/**
234 * @class SNamedColor
235 * @brief Manages named colors.
236 */
237class SOUI_EXP SNamedColor : public SNamedValue<COLORREF, SColorParser> {
238 public:
239 /**
240 * @brief Retrieves a color value by name, automatically converting named colors.
241 * @param strValue Name of the color.
242 * @param cr Variable to store the retrieved color value.
243 * @return TRUE if the color is found, FALSE otherwise.
244 */
245 BOOL Get(const SStringW &strValue, COLORREF &cr) const;
246
247 /**
248 * @brief Retrieves a color value by index.
249 * @param idx Index of the color.
250 * @return Color value at the specified index.
251 */
252 COLORREF Get(int idx) const
253 {
254 return GetAt(idx);
255 }
256};
257
258/**
259 * @class SStringParser
260 * @brief Parser for string values.
261 */
262class SOUI_EXP SStringParser {
263 public:
264 /**
265 * @brief Parses a string to a string value.
266 * @param strValue String representation of the value.
267 * @param value Variable to store the parsed string value.
268 * @return TRUE if parsing is successful, FALSE otherwise.
269 */
270 static bool ParseValue(const SStringW &strValue, SStringW &value);
271
272 /**
273 * @brief Retrieves the null value for strings.
274 * @return Null value for strings.
275 */
276 static SStringW GetNullValue();
277};
278
279/**
280 * @class SNamedString
281 * @brief Manages named strings.
282 */
283class SOUI_EXP SNamedString : public SNamedValue<SStringW, SStringParser> {
284 public:
285 /**
286 * @brief Retrieves a string value by name, automatically converting named strings.
287 * @param strValue Name of the string.
288 * @param ret Variable to store the retrieved string value.
289 * @return TRUE if the string is found, FALSE otherwise.
290 */
291 BOOL Get(const SStringW &strValue, SStringW &ret) const;
292
293 /**
294 * @brief Retrieves a string value by index.
295 * @param idx Index of the string.
296 * @return String value at the specified index.
297 */
298 SStringW Get(int idx) const
299 {
300 return GetAt(idx);
301 }
302};
303
304/**
305 * @class SNamedFont
306 * @brief Manages named fonts.
307 */
308class SOUI_EXP SNamedFont : public SNamedValue<SStringW, SStringParser> {
309 public:
310 /**
311 * @brief Retrieves a font value by name, automatically converting named fonts.
312 * @param strValue Name of the font.
313 * @param ret Variable to store the retrieved font value.
314 * @return TRUE if the font is found, FALSE otherwise.
315 */
316 BOOL Get(const SStringW &strValue, SStringW &ret) const;
317
318 /**
319 * @brief Retrieves a font value by index.
320 * @param idx Index of the font.
321 * @return Font value at the specified index.
322 */
323 SStringW Get(int idx) const
324 {
325 return GetAt(idx);
326 }
327};
328
329/**
330 * @class SDimensionParser
331 * @brief Parser for dimension values.
332 */
333class SOUI_EXP SDimensionParser {
334 public:
335 /**
336 * @brief Parses a string to a dimension value.
337 * @param strValue String representation of the dimension.
338 * @param value Variable to store the parsed dimension value.
339 * @return TRUE if parsing is successful, FALSE otherwise.
340 */
341 static bool ParseValue(const SStringW &strValue, SLayoutSize &value);
342
343 /**
344 * @brief Retrieves the null value for dimensions.
345 * @return Null value for dimensions.
346 */
347 static SLayoutSize GetNullValue();
348};
349
350/**
351 * @class SNamedDimension
352 * @brief Manages named dimensions.
353 */
354class SOUI_EXP SNamedDimension : public SNamedValue<SLayoutSize, SDimensionParser> {
355 public:
356 /**
357 * @brief Retrieves a dimension value by name, automatically converting named dimensions.
358 * @param strValue Name of the dimension.
359 * @param ret Variable to store the retrieved dimension value.
360 * @return TRUE if the dimension is found, FALSE otherwise.
361 */
362 BOOL Get(const SStringW &strValue, SLayoutSize &ret) const;
363
364 /**
365 * @brief Retrieves a dimension value by index.
366 * @param idx Index of the dimension.
367 * @return Dimension value at the specified index.
368 */
369 SLayoutSize Get(int idx) const
370 {
371 return GetAt(idx);
372 }
373};
374
375SNSEND
376
377#endif // __SNAMEDVALUE__H__
Parser for color values.
static COLORREF GetNullValue()
Retrieves the null value for colors.
static bool ParseValue(const SStringW &strValue, COLORREF &value)
Parses a string to a color value.
Parser for dimension values.
static SLayoutSize GetNullValue()
Retrieves the null value for dimensions.
static bool ParseValue(const SStringW &strValue, SLayoutSize &value)
Parses a string to a dimension value.
Parser for integer values.
static int GetNullValue()
Retrieves the null value for integers.
static bool ParseValue(const SStringW &strValue, int &value)
Parses a string to an integer value.
布局大小类
Definition SLayoutSize.h:10
Manages named colors.
BOOL Get(const SStringW &strValue, COLORREF &cr) const
Retrieves a color value by name, automatically converting named colors.
COLORREF Get(int idx) const
Retrieves a color value by index.
Manages named dimensions.
SLayoutSize Get(int idx) const
Retrieves a dimension value by index.
BOOL Get(const SStringW &strValue, SLayoutSize &ret) const
Retrieves a dimension value by name, automatically converting named dimensions.
Manages named fonts.
BOOL Get(const SStringW &strValue, SStringW &ret) const
Retrieves a font value by name, automatically converting named fonts.
SStringW Get(int idx) const
Retrieves a font value by index.
Manages named integer IDs.
void Init2(const NAMEDVALUE *pValue, int nCount, BOOL bSorted)
Initializes named IDs from an array.
void Init3(const LPCWSTR *pNames, const int *nIDs, int nCount, BOOL bSorted)
Initializes named IDs from arrays of names and IDs.
Manages named strings.
SStringW Get(int idx) const
Retrieves a string value by index.
BOOL Get(const SStringW &strValue, SStringW &ret) const
Retrieves a string value by name, automatically converting named strings.
Template class for managing named values.
Definition SNamedValue.h:35
T GetAt(const int idx) const
Retrieves the value at a specific index.
T String2Value(const SStringW &strName) const
Converts a string name to its corresponding value.
Definition SNamedValue.h:75
UINT GetCount() const
Retrieves the number of named values.
size_t Merge(const SNamedValue &src)
Merges the named values from another SNamedValue object.
BOOL FindValue(const SStringW &strName, T &value) const
Finds the value associated with a given name.
Definition SNamedValue.h:90
virtual BOOL Init(SXmlNode xmlNode)
Initializes the named values from an XML node.
Definition SNamedValue.h:44
static int Compare(const void *p1, const void *p2)
Comparison function for sorting and searching.
Parser for string values.
static SStringW GetNullValue()
Retrieves the null value for strings.
static bool ParseValue(const SStringW &strValue, SStringW &value)
Parses a string to a string value.
A class representing an ASCII string.
Definition sstringw.h:96
BOOL __cdecl Format(HINSTANCE hInst, UINT nFormatID,...)
Formats a string using a format string and variable arguments.
Definition sstringw.cpp:490
Class representing an XML node.
Definition SXml.h:352
SXmlNode next_sibling() const
Gets the next sibling node in the children list of the parent node.
Definition SXml.cpp:393
SXmlNode first_child() const
Gets the first child node of the node.
Definition SXml.cpp:383
Structure to hold a named value.
Definition SNamedValue.h:21