soui 5.0.0.1
Soui5 Doc
 
Loading...
Searching...
No Matches
SObjectFactory.cpp
1#include "souistd.h"
3#include "res.mgr/SObjDefAttr.h"
4#include "res.mgr/SUiDef.h"
5
6SNSBEGIN
7
8SObjectInfo ObjInfo_New(LPCWSTR name, int type, LPCWSTR alise)
9{
10 SObjectInfo ret;
11 SStringW strName(name);
12 strName.MakeLower();
13 SASSERT(strName.GetLength() < MAX_OBJNAME);
14 wcscpy(ret.szName, strName.c_str());
15 ret.nType = type;
16 ret.szAlise = alise;
17 return ret;
18}
19
20BOOL ObjInfo_IsValid(const SObjectInfo *pObjInfo)
21{
22 return pObjInfo->nType >= Undef && pObjInfo->szName[0] != 0;
23}
24
29
30//************************************
31// Method: RegisterFactory,注册APP自定义的窗口类
32// Access: public
33// Returns: bool
34// Qualifier:
35// Parameter: SObjectFactory * pWndFactory:窗口工厂指针
36// Parameter: bool bReplace:强制替换原有工厂标志
37//************************************
38BOOL SObjectFactoryMgr::RegisterFactory(const IObjectFactory *objFactory, BOOL bReplace)
39{
40 if (HasKey(objFactory->GetObjectInfo()))
41 {
42 if (!bReplace)
43 return FALSE;
44 RemoveKeyObject(objFactory->GetObjectInfo());
45 }
46 SObjectInfo objInfo = objFactory->GetObjectInfo();
47 AddKeyObject(objInfo, objFactory->Clone());
48 if (objInfo.szAlise)
49 {
50 SStringWList aliseList;
51 SplitString(SStringW(objInfo.szAlise), L'|', aliseList);
52 for (size_t i = 0; i < aliseList.GetCount(); i++)
53 {
54 SObjectInfo objInfoAlise = ObjInfo_New(aliseList[i], objInfo.nType);
55 AddKeyObject(objInfoAlise, objFactory->Clone());
56 }
57 }
58 return TRUE;
59}
60
61void SObjectFactoryMgr::OnFactoryRemoved(const SObjectFactoryPtr &obj)
62{
63 obj->Release();
64}
65
66//************************************
67// Method: UnregisterFactor,反注册APP自定义的窗口类
68// Access: public
69// Returns: bool
70// Qualifier:
71// Parameter: SWindowFactory * pWndFactory
72//************************************
73
74BOOL SObjectFactoryMgr::UnregisterFactory(const SObjectInfo &objInfo)
75{
76 return (BOOL)RemoveKeyObject(objInfo);
77}
78
79IObject *SObjectFactoryMgr::CreateObject(const SObjectInfo &objInfo) const
80{
81 if (!HasKey(objInfo))
82 {
83 return OnCreateUnknownObject(objInfo);
84 }
85 IObject *pRet = GetKeyObject(objInfo)->NewObject();
86 SASSERT(pRet);
87 SetSwndDefAttr(pRet);
88 return pRet;
89}
90
91SObjectInfo SObjectFactoryMgr::BaseObjectInfoFromObjectInfo(const SObjectInfo &objInfo)
92{
93 if (!HasKey(objInfo))
94 {
95 return SObjectInfo();
96 }
97
98 SStringW strBaseClass = GetKeyObject(objInfo)->BaseClassName();
99 if (strBaseClass == objInfo.szName)
100 return SObjectInfo();
101
102 return ObjInfo_New(strBaseClass, objInfo.nType);
103}
104
105void SObjectFactoryMgr::SetSwndDefAttr(IObject *pObject) const
106{
107 LPCWSTR pszClassName = pObject->GetObjectClass();
108
109 if (pObject->GetObjectType() != Window)
110 return;
111
112 //检索并设置类的默认属性
113 SXmlNode defAttr;
114 SObjDefAttr *pDefObjAttr = GETUIDEF->GetUiDef()->GetObjDefAttr();
115 if (pDefObjAttr)
116 defAttr = pDefObjAttr->GetDefAttribute(pszClassName);
117
118 if (defAttr)
119 {
120 //优先处理"class"属性
121 SXmlAttr attrClass = defAttr.attribute(L"class");
122 if (attrClass)
123 {
124 attrClass.set_userdata(1);
125 pObject->SetAttribute(attrClass.name(), attrClass.value(), TRUE);
126 }
127 for (SXmlAttr attr = defAttr.first_attribute(); attr; attr = attr.next_attribute())
128 {
129 if (attr.get_userdata())
130 continue;
131 pObject->SetAttribute(attr.name(), attr.value(), TRUE);
132 }
133 if (attrClass)
134 {
135 attrClass.set_userdata(0);
136 }
137 }
138}
139
140IObject *SObjectFactoryMgr::OnCreateUnknownObject(const SObjectInfo &objInfo) const
141{
142 SSLOGD() << "Warning: no object " << objInfo.szName << "of type:" << objInfo.nType << "in SOUI !!";
143 return NULL;
144}
145
146SNSEND
Object Factory Management.
SOUI_EXP BOOL ObjInfo_IsValid(const SObjectInfo *pObjInfo)
Checks if the object information is valid.
SNSBEGIN SOUI_EXP SObjectInfo ObjInfo_New(LPCWSTR name, int type, LPCWSTR alise=NULL)
Creates a new object information structure.
bool GetKeyObject(const SObjectInfo &key, SObjectFactoryPtr &obj) const
Definition SCmnMap.h:65
bool AddKeyObject(const SObjectInfo &key, const SObjectFactoryPtr &obj)
Definition SCmnMap.h:94
bool RemoveKeyObject(const SObjectInfo &key)
Definition SCmnMap.h:123
bool HasKey(const SObjectInfo &key) const
Definition SCmnMap.h:52
void(* m_pFunOnKeyRemoved)(const SObjectFactoryPtr &obj)
Definition SCmnMap.h:167
Class for managing default attributes of objects.
Definition SObjDefAttr.h:17
SXmlNode GetDefAttribute(LPCWSTR pszClassName)
Retrieves the default attributes for a specific class.
SObjectFactoryMgr(void)
Constructor for SObjectFactoryMgr.
BOOL UnregisterFactory(const SObjectInfo &objInfo)
Unregisters an object factory.
virtual IObject * CreateObject(const SObjectInfo &objInfo) const
Creates an object based on the given object information.
virtual IObject * OnCreateUnknownObject(const SObjectInfo &objInfo) const
Handles the creation of an unknown object.
BOOL RegisterFactory(const IObjectFactory *objFactory, BOOL bReplace=false)
Registers an object factory.
static void OnFactoryRemoved(const SObjectFactoryPtr &obj)
Static callback for when a factory is removed.
SObjectInfo BaseObjectInfoFromObjectInfo(const SObjectInfo &objInfo)
Gets the base object information from a given object information.
void SetSwndDefAttr(IObject *pObject) const
Sets default attributes for a given object.
A class representing an ASCII string.
Definition sstringw.h:96
const wchar_t * c_str() SCONST
Retrieves a C-style string representation of the string.
SStringW & MakeLower()
Converts the string to lowercase.
Definition sstringw.cpp:869
int GetLength() SCONST
Retrieves the length of the string.
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
const wchar_t * name() const
Gets the attribute name.
Definition SXml.cpp:85
const wchar_t * value() const
Gets the attribute value.
Definition SXml.cpp:90
Class representing an XML node.
Definition SXml.h:352
SXmlAttr first_attribute() const
Gets the first attribute of the node.
Definition SXml.cpp:373
SXmlAttr attribute(const wchar_t *name, bool bCaseSensitive=false) const
Gets the attribute with the specified name.
Definition SXml.cpp:428
Bitfield structure for font style attributes.