1#ifndef __SNAMEDVALUE__H__
2#define __SNAMEDVALUE__H__
4#include "helper/SColor.h"
5#include "layout/SLayoutSize.h"
22 wchar_t strName[MAX_NAME + 1];
34template <
class T,
class ValueParser>
51 if (xmlStr.type() != node_element)
54 NAMEDVALUE namedValue;
55 wcscpy_s(namedValue.strName, MAX_NAME, xmlStr.name());
56 if (ValueParser::ParseValue(xmlStr.attribute(L
"value").as_string(), namedValue.value))
58 m_lstNamedValue.Add(namedValue);
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);
66 qsort(m_lstNamedValue.GetData(), m_lstNamedValue.GetCount(),
sizeof(NAMEDVALUE),
Compare);
80 static const T nullValue = ValueParser::GetNullValue();
93 wcscpy_s(target.strName, MAX_NAME, strName);
94 NAMEDVALUE *pFind = (NAMEDVALUE *)bsearch(&target, m_lstNamedValue.GetData(), m_lstNamedValue.GetCount(),
sizeof(NAMEDVALUE),
Compare);
108 SASSERT(idx >= 0 && idx < (
int)m_lstNamedValue.GetCount());
109 return m_lstNamedValue[idx].value;
119 if (m_lstNamedValue.IsEmpty())
121 m_lstNamedValue.Copy(src.m_lstNamedValue);
122 return src.m_lstNamedValue.GetCount();
126 SArray<NAMEDVALUE> data;
127 for (
size_t i = 0; i < src.m_lstNamedValue.GetCount(); i++)
129 NAMEDVALUE *pFind = (NAMEDVALUE *)bsearch(&src.m_lstNamedValue[i], m_lstNamedValue.GetData(), m_lstNamedValue.GetCount(),
sizeof(NAMEDVALUE),
Compare);
132 data.Add(src.m_lstNamedValue[i]);
135 m_lstNamedValue.Append(data);
136 qsort(m_lstNamedValue.GetData(), m_lstNamedValue.GetCount(),
sizeof(NAMEDVALUE),
Compare);
137 return data.GetCount();
147 return (UINT)m_lstNamedValue.GetCount();
157 static int Compare(
const void *p1,
const void *p2)
159 const NAMEDVALUE *pData1 = (
const NAMEDVALUE *)p1;
160 const NAMEDVALUE *pData2 = (
const NAMEDVALUE *)p2;
161 return wcscmp(pData1->strName, pData2->strName);
164 SArray<NAMEDVALUE> m_lstNamedValue;
200 void Init2(
const NAMEDVALUE *pValue,
int nCount, BOOL bSorted);
209 void Init3(
const LPCWSTR *pNames,
const int *nIDs,
int nCount, BOOL bSorted);
245 BOOL
Get(
const SStringW &strValue, COLORREF &cr)
const;
252 COLORREF
Get(
int idx)
const
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.
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.
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.
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.
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.
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.
virtual BOOL Init(SXmlNode xmlNode)
Initializes the named values from an XML node.
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.
BOOL __cdecl Format(HINSTANCE hInst, UINT nFormatID,...)
Formats a string using a format string and variable arguments.
Class representing an XML node.
SXmlNode next_sibling() const
Gets the next sibling node in the children list of the parent node.
SXmlNode first_child() const
Gets the first child node of the node.
Structure to hold a named value.