soui 5.0.0.1
Soui5 Doc
 
Loading...
Searching...
No Matches
Sobject.hpp
1#ifndef __SOBJECT_I_H_
2#define __SOBJECT_I_H_
3
5#include <string/strcpcvt.h>
6#include <xml/SXml.h>
7
8SNSBEGIN
9
10#ifndef ENABLE_SOBJ_XML
11#ifdef _DEBUG
12#define ENABLE_SOBJ_XML 1
13#else
14#define ENABLE_SOBJ_XML 0
15#endif
16#endif
17
18/**
19 * @class SObjectImpl
20 * @brief Template class for implementing the SObject interface.
21 *
22 * @details This class provides runtime type identification and attribute dispatching based on XML descriptions.
23 * @tparam T Base class that implements the IObject interface.
24 */
25template<class T>
26class SObjectImpl : public T
27{
28public:
29 /**
30 * @brief Retrieves the class type.
31 * @return Class type identifier.
32 */
33 static int GetClassType() {
34 return T::GetClassType();
35 }
36
37 /**
38 * @brief Retrieves the class name.
39 * @return Class name as a wide string.
40 */
41 static LPCWSTR GetClassName() {
42 return T::GetClassName();
43 }
44
45 /**
46 * @brief Retrieves the class alias.
47 * @return Class alias as a wide string.
48 */
49 static LPCWSTR GetClassAlias() {
50 return T::GetClassAlias();
51 }
52
53public:
54 /**
55 * @brief Marks an attribute as handled.
56 * @param xmlAttr XML attribute to mark.
57 * @param bHandled TRUE if the attribute is handled, FALSE otherwise.
58 */
59 static void MarkAttributeHandled(SXmlAttr xmlAttr, bool bHandled) {
60 xmlAttr.set_userdata(bHandled ? 1 : 0);
61 }
62
63 /**
64 * @brief Checks if an attribute is handled.
65 * @param xmlAttr XML attribute to check.
66 * @return TRUE if the attribute is handled, FALSE otherwise.
67 */
68 static bool IsAttributeHandled(SXmlAttr xmlAttr) {
69 return xmlAttr.get_userdata() == 1 ? true : false;
70 }
71
72 /**
73 * @brief Constructor.
74 */
76
77public:
78 /**
79 * @brief Initializes the object from an XML node.
80 * @param pXmlNode Pointer to the XML node containing the object's attributes.
81 * @return TRUE if initialization is successful, FALSE otherwise.
82 */
83 STDMETHOD_(BOOL, InitFromXml)(THIS_ IXmlNode * pXmlNode) OVERRIDE {
84 SXmlNode xmlNode(pXmlNode);
85
86 if (!xmlNode) return FALSE;
87#if ENABLE_SOBJ_XML
88 {
89 pXmlNode->ToString(&m_strXml);
90 }
91#endif
92
93 // Set current object's attributes
94 for (SXmlAttr attr = xmlNode.first_attribute(); attr; attr = attr.next_attribute()) {
95 if (IsAttributeHandled(attr)) continue; // Ignore already processed attributes
96 SetAttribute(attr.name(), attr.value(), TRUE);
97 }
98
99 // Call initialization finished interface
100 OnInitFinished(pXmlNode);
101 return TRUE;
102 }
103
104 /**
105 * @brief Retrieves the object's name.
106 * @return Object's name as a wide string.
107 */
108 STDMETHOD_(LPCWSTR, GetName)(THIS) SCONST OVERRIDE {
109 return m_strName;
110 }
111
112 /**
113 * @brief Retrieves the object's name in ANSI format.
114 * @return Object's name as an ANSI string.
115 */
116 STDMETHOD_(LPCSTR, GetNameA)(THIS) SCONST OVERRIDE {
117 static SStringA str;
118 str = S_CW2A(m_strName, CP_UTF8);
119 return str.c_str();
120 }
121
122 /**
123 * @brief Sets the object's name.
124 * @param pszName Name to set.
125 */
126 STDMETHOD_(void, SetName)(THIS_ LPCWSTR pszName) OVERRIDE {
127 m_strName = pszName;
128 }
129
130 /**
131 * @brief Retrieves the object's ID.
132 * @return Object's ID.
133 */
134 STDMETHOD_(int, GetID)(THIS) SCONST OVERRIDE {
135 return m_nID;
136 }
137
138 /**
139 * @brief Sets the object's ID.
140 * @param nID ID to set.
141 */
142 STDMETHOD_(void, SetID)(THIS_ int nID) OVERRIDE {
143 m_nID = nID;
144 }
145
146 /**
147 * @brief Handles attribute processing after setting an attribute.
148 * @param strAttribName Attribute name.
149 * @param strValue Attribute value.
150 * @param bLoading TRUE if the object is being loaded, FALSE otherwise.
151 * @param hr Result of the attribute setting.
152 * @return Result of the attribute processing.
153 */
154 STDMETHOD_(HRESULT, AfterAttribute)(THIS_ LPCWSTR strAttribName, LPCWSTR strValue, BOOL bLoading, HRESULT hr) OVERRIDE {
155 UNREFERENCED_PARAMETER(strAttribName);
156 UNREFERENCED_PARAMETER(strValue);
157 UNREFERENCED_PARAMETER(bLoading);
158 return hr;
159 }
160
161 /**
162 * @brief Sets an attribute using ANSI strings.
163 * @param pszAttr Attribute name.
164 * @param pszValue Attribute value.
165 * @param bLoading TRUE if the object is being loaded, FALSE otherwise.
166 * @return Result of setting the attribute.
167 */
168 STDMETHOD_(HRESULT, SetAttributeA)(THIS_ LPCSTR pszAttr, LPCSTR pszValue, BOOL bLoading) OVERRIDE {
169 return SetAttribute(S_CA2W(pszAttr, CP_UTF8), S_CA2W(pszValue, CP_UTF8), bLoading);
170 }
171
172 /**
173 * @brief Sets an attribute using wide strings.
174 * @param pszAttr Attribute name.
175 * @param pszValue Attribute value.
176 * @param bLoading TRUE if the object is being loaded, FALSE otherwise.
177 * @return Result of setting the attribute.
178 */
179 STDMETHOD_(HRESULT, SetAttribute)(THIS_ LPCWSTR pszAttr, LPCWSTR pszValue, BOOL bLoading) OVERRIDE {
180 SStringW strName(pszAttr), strValue(pszValue);
181 if (m_attrHandler) {
182 HRESULT hr = m_attrHandler(this, &strName, &strValue, bLoading);
183 if (SUCCEEDED(hr))
184 return hr;
185 }
186
187 return SetAttribute(strName, strValue, bLoading);
188 }
189
190 /**
191 * @brief Sets an attribute using IStringW objects.
192 * @param strAttr Attribute name.
193 * @param strValue Attribute value.
194 * @param bLoading TRUE if the object is being loaded, FALSE otherwise.
195 * @return Result of setting the attribute.
196 */
197 STDMETHOD_(HRESULT, ISetAttribute)(THIS_ const IStringW *strAttr, const IStringW *strValue, BOOL bLoading) OVERRIDE {
198 if (m_attrHandler) {
199 HRESULT hr = m_attrHandler(this, strAttr, strValue, bLoading);
200 if (SUCCEEDED(hr))
201 return hr;
202 }
203
204 return SetAttribute(SStringW(strAttr), SStringW(strValue), bLoading);
205 }
206
207 /**
208 * @brief Retrieves the object's class name.
209 * @return Class name as a wide string.
210 */
211 STDMETHOD_(LPCWSTR, GetObjectClass)(THIS_) SCONST OVERRIDE {
212 return GetClassName();
213 }
214
215 /**
216 * @brief Retrieves the object's type.
217 * @return Object type identifier.
218 */
219 STDMETHOD_(int, GetObjectType)(THIS) SCONST OVERRIDE {
220 return GetClassType();
221 }
222
223 /**
224 * @brief Checks if the object is of a specific class.
225 * @param lpszName Class name to check.
226 * @return TRUE if the object is of the specified class, FALSE otherwise.
227 */
228 STDMETHOD_(BOOL, IsClass)(THIS_ LPCWSTR lpszName) SCONST OVERRIDE {
229 return wcscmp(lpszName, GetClassName()) == 0;
230 }
231
232 /**
233 * @brief Retrieves an attribute value.
234 * @param strAttr Attribute name.
235 * @param pValue Pointer to store the attribute value.
236 * @return TRUE if the attribute is retrieved successfully, FALSE otherwise.
237 */
238 STDMETHOD_(BOOL, GetAttribute)(THIS_ LPCWSTR strAttr, IStringW *pValue) SCONST OVERRIDE {
239 UNREFERENCED_PARAMETER(strAttr);
240 UNREFERENCED_PARAMETER(pValue);
241 return FALSE;
242 }
243
244 /**
245 * @brief Handles initialization completion.
246 * @param xmlNode XML node containing the object's attributes.
247 */
248 STDMETHOD_(void, OnInitFinished)(THIS_ IXmlNode *xmlNode) OVERRIDE {
249 UNREFERENCED_PARAMETER(xmlNode);
250 }
251
252 /**
253 * @brief Sets the attribute handler.
254 * @param attrHandler Function pointer to the attribute handler.
255 */
256 STDMETHOD_(void, SetAttrHandler)(THIS_ FunAttrHandler attrHandler) OVERRIDE {
257 m_attrHandler = attrHandler;
258 }
259
260public:
261 /**
262 * @brief Sets an attribute using wide strings.
263 * @param strAttr Attribute name.
264 * @param strValue Attribute value.
265 * @param bLoading TRUE if the object is being loaded, FALSE otherwise.
266 * @return Result of setting the attribute.
267 */
268 virtual HRESULT SetAttribute(const SStringW &strAttr, const SStringW &strValue, BOOL bLoading) {
269 return DefAttributeProc(strAttr, strValue, bLoading);
270 }
271
272 /**
273 * @brief Default attribute processing.
274 * @param strAttr Attribute name.
275 * @param strValue Attribute value.
276 * @param bLoading TRUE if the object is being loaded, FALSE otherwise.
277 * @return Result of default attribute processing.
278 */
279 virtual HRESULT DefAttributeProc(const SStringW &strAttr, const SStringW &strValue, BOOL bLoading) {
280 if (strAttr.CompareNoCase(L"name") == 0)
281 m_strName = strValue;
282 else if (strAttr.CompareNoCase(L"id") == 0)
283 m_nID = _wtoi(strValue);
284 return E_FAIL;
285 }
286
287#if ENABLE_SOBJ_XML
288public:
289 SStringW m_strXml; /**< XML string for debugging purposes */
290#endif // ENABLE_SOBJ_XML
291
292protected:
293 SStringW m_strName; /**< Object's name */
294 int m_nID; /**< Object's ID */
295
296 FunAttrHandler m_attrHandler; /**< Attribute handler function */
297};
298
299/**
300 * @typedef SObject
301 * @brief Typedef for SObjectImpl<IObject>.
302 */
304
305SNSEND
306
307#endif // __SOBJECT_I_H_
SOUI系统中的对象基类
Template class for implementing the SObject interface.
Definition Sobject.hpp:27
void SetName(LPCWSTR pszName) OVERRIDE
Definition Sobject.hpp:126
static int GetClassType()
Retrieves the class type.
Definition Sobject.hpp:33
BOOL GetAttribute(LPCWSTR strAttr, IStringW *pValue) SCONST OVERRIDE
Definition Sobject.hpp:238
virtual HRESULT DefAttributeProc(const SStringW &strAttr, const SStringW &strValue, BOOL bLoading)
Default attribute processing.
Definition Sobject.hpp:279
FunAttrHandler m_attrHandler
Definition Sobject.hpp:296
BOOL IsClass(LPCWSTR lpszName) SCONST OVERRIDE
Definition Sobject.hpp:228
HRESULT SetAttribute(LPCWSTR pszAttr, LPCWSTR pszValue, BOOL bLoading) OVERRIDE
Sets an attribute using wide strings.
Definition Sobject.hpp:179
LPCSTR GetNameA() SCONST OVERRIDE
Definition Sobject.hpp:116
virtual HRESULT SetAttribute(const SStringW &strAttr, const SStringW &strValue, BOOL bLoading)
Sets an attribute using wide strings.
Definition Sobject.hpp:268
void SetID(int nID) OVERRIDE
Definition Sobject.hpp:142
static LPCWSTR GetClassAlias()
Retrieves the class alias.
Definition Sobject.hpp:49
static void MarkAttributeHandled(SXmlAttr xmlAttr, bool bHandled)
Marks an attribute as handled.
Definition Sobject.hpp:59
HRESULT ISetAttribute(const IStringW *strAttr, const IStringW *strValue, BOOL bLoading) OVERRIDE
Definition Sobject.hpp:197
int GetID() SCONST OVERRIDE
Definition Sobject.hpp:134
LPCWSTR GetName() SCONST OVERRIDE
Definition Sobject.hpp:108
static bool IsAttributeHandled(SXmlAttr xmlAttr)
Checks if an attribute is handled.
Definition Sobject.hpp:68
BOOL InitFromXml(IXmlNode *pXmlNode) OVERRIDE
Definition Sobject.hpp:83
int GetObjectType() SCONST OVERRIDE
Definition Sobject.hpp:219
void OnInitFinished(IXmlNode *xmlNode) OVERRIDE
Handles initialization completion.
Definition Sobject.hpp:248
HRESULT SetAttributeA(LPCSTR pszAttr, LPCSTR pszValue, BOOL bLoading) OVERRIDE
Definition Sobject.hpp:168
void SetAttrHandler(FunAttrHandler attrHandler) OVERRIDE
Definition Sobject.hpp:256
HRESULT AfterAttribute(LPCWSTR strAttribName, LPCWSTR strValue, BOOL bLoading, HRESULT hr) OVERRIDE
Definition Sobject.hpp:154
SObjectImpl()
Constructor.
Definition Sobject.hpp:75
LPCWSTR GetObjectClass() SCONST OVERRIDE
Definition Sobject.hpp:211
static LPCWSTR GetClassName()
Retrieves the class name.
Definition Sobject.hpp:41
A class representing an ASCII string.
Definition sstringa.h:96
const char * c_str() SCONST
Retrieves a C-style string representation of the string.
A class representing an ASCII string.
Definition sstringw.h:96
int CompareNoCase(const wchar_t *psz) SCONST
Compares the string with another string, ignoring case.
Definition sstringw.cpp:929
Class representing an XML attribute.
Definition SXml.h:20
BOOL set_userdata(int data) OVERRIDE
Sets user data for the attribute.
Definition SXml.cpp:53
int get_userdata() SCONST OVERRIDE
Gets user data for the attribute.
Definition SXml.cpp:58
Class representing an XML node.
Definition SXml.h:352
SXmlAttr first_attribute() const
Gets the first attribute of the node.
Definition SXml.cpp:373
Interface for XML nodes.
Definition sxml-i.h:128
Bitfield structure for font style attributes.