soui 5.0.0.1
Soui5 Doc
 
Loading...
Searching...
No Matches
SObjectFactory.h
Go to the documentation of this file.
1/**
2 * Copyright (C) 2014-2050
3 * All rights reserved.
4 *
5 * @file SObjectFactory.h
6 * @brief Object Factory Management
7 * @version v1.0
8 * @author SOUI group
9 * @date 2014/08/02
10 *
11 * Description: Manages object factories for creating and managing objects in SOUI.
12 */
13
14#ifndef __SOBJECTFACTORY__H__
15#define __SOBJECTFACTORY__H__
16
17#include <core/SCmnMap.h>
18#include <helper/obj-ref-impl.hpp>
19#include <interface/SObjFactory-i.h>
20
21SNSBEGIN
22
23/**
24 * @brief Creates a new object information structure.
25 * @param name Name of the object.
26 * @param type Type of the object.
27 * @param alise Alias for the object (optional).
28 * @return New object information structure.
29 */
30SOUI_EXP SObjectInfo ObjInfo_New(LPCWSTR name, int type, LPCWSTR alise = NULL);
31
32/**
33 * @brief Checks if the object information is valid.
34 * @param pObjInfo Pointer to the object information structure.
35 * @return TRUE if valid, FALSE otherwise.
36 */
37SOUI_EXP BOOL ObjInfo_IsValid(const SObjectInfo *pObjInfo);
38
39/**
40 * @class CElementTraits< SObjectInfo >
41 * @brief SObjectInfo Hash and Comparison Traits
42 *
43 * Description: Provides hash and comparison functions for SObjectInfo used in the factory map.
44 */
45template <>
46class CElementTraits<SObjectInfo> : public CElementTraitsBase<SObjectInfo> {
47 public:
48 /**
49 * @brief Computes the hash value for an SObjectInfo.
50 * @param objInfo Object information structure.
51 * @return Hash value.
52 */
53 static ULONG Hash(INARGTYPE objInfo)
54 {
55 ULONG uRet = CElementTraits<SStringW>::Hash(objInfo.szName);
56 uRet = (uRet << 5) + objInfo.nType;
57 return uRet;
58 }
59
60 /**
61 * @brief Compares two SObjectInfo structures.
62 * @param obj1 First object information structure.
63 * @param obj2 Second object information structure.
64 * @return TRUE if the structures are equal, FALSE otherwise.
65 */
66 static bool CompareElements(INARGTYPE obj1, INARGTYPE obj2)
67 {
68 return obj1.nType == obj2.nType && _wcsicmp(obj1.szName, obj2.szName) == 0;
69 }
70};
71
72/**
73 * @class TplSObjectFactory
74 * @brief Template-based object factory.
75 * @tparam T Type of the object to be created.
76 *
77 * Description: Implements the IObjectFactory interface for creating objects of type T.
78 */
79template <typename T>
80class TplSObjectFactory : public TObjRefImpl<IObjectFactory> {
81 public:
82 /**
83 * @brief Default constructor.
84 */
86 {
87 }
88
89 /**
90 * @brief Called when the final reference is released.
91 */
92 STDMETHOD_(void, OnFinalRelease)(THIS)
93 {
94 delete this;
95 }
96
97 /**
98 * @brief Gets the base class name of the object.
99 * @return Base class name.
100 */
101 STDMETHOD_(LPCWSTR, BaseClassName)(CTHIS) SCONST OVERRIDE
102 {
103 return T::BaseClassName();
104 }
105
106 /**
107 * @brief Creates a new object of type T.
108 * @return Pointer to the new object.
109 */
110 STDMETHOD_(IObject *, NewObject)(CTHIS) SCONST OVERRIDE
111 {
112 return new T;
113 }
114
115 /**
116 * @brief Clones the object factory.
117 * @return Pointer to the cloned factory.
118 */
119 STDMETHOD_(IObjectFactory *, Clone)(CTHIS) SCONST OVERRIDE
120 {
121 return new TplSObjectFactory<T>();
122 }
123
124 /**
125 * @brief Gets the object information for the factory.
126 * @return Object information structure.
127 */
128 STDMETHOD_(SObjectInfo, GetObjectInfo)(CTHIS) SCONST OVERRIDE
129 {
130 return ObjInfo_New(T::GetClassName(), T::GetClassType(), T::GetClassAlise());
131 }
132};
133
134typedef IObjectFactory *SObjectFactoryPtr;
135
136/**
137 * @class SObjectFactoryMgr
138 * @brief Manager for object factories.
139 *
140 * Description: Manages the registration, unregistration, and creation of objects using factories.
141 */
142class SOUI_EXP SObjectFactoryMgr : public SCmnMap<SObjectFactoryPtr, SObjectInfo> {
143 public:
144 /**
145 * @brief Constructor for SObjectFactoryMgr.
146 */
147 SObjectFactoryMgr(void);
148
149 /**
150 * @brief Registers an object factory.
151 * @param objFactory Pointer to the object factory.
152 * @param bReplace Flag to replace existing factory if it exists.
153 * @return TRUE if registration is successful, FALSE otherwise.
154 */
155 BOOL RegisterFactory(const IObjectFactory *objFactory, BOOL bReplace = false);
156
157 /**
158 * @brief Unregisters an object factory.
159 * @param objInfo Object information structure.
160 * @return TRUE if unregistration is successful, FALSE otherwise.
161 */
162 BOOL UnregisterFactory(const SObjectInfo &objInfo);
163
164 /**
165 * @brief Sets default attributes for a given object.
166 * @param pObject Pointer to the object.
167 */
168 void SetSwndDefAttr(IObject *pObject) const;
169
170 /**
171 * @brief Gets the base object information from a given object information.
172 * @param objInfo Object information structure.
173 * @return Base object information structure.
174 */
175 SObjectInfo BaseObjectInfoFromObjectInfo(const SObjectInfo &objInfo);
176
177 /**
178 * @brief Template method to register a factory for a specific object type.
179 * @tparam T Type of the object.
180 * @return TRUE if registration is successful, FALSE otherwise.
181 */
182 template <class T>
184 {
186 return RegisterFactory(&fac);
187 }
188
189 /**
190 * @brief Template method to unregister a factory for a specific object type.
191 * @tparam T Type of the object.
192 * @return TRUE if unregistration is successful, FALSE otherwise.
193 */
194 template <class T>
196 {
197 return UnregisterFactory(ObjInfo_New(T::GetClassName(), T::GetClassType()));
198 }
199
200 protected:
201 /**
202 * @brief Creates an object based on the given object information.
203 * @param objInfo Object information structure.
204 * @return Pointer to the created object.
205 */
206 virtual IObject *CreateObject(const SObjectInfo &objInfo) const;
207
208 /**
209 * @brief Handles the creation of an unknown object.
210 * @param objInfo Object information structure.
211 * @return Pointer to the created object.
212 */
213 virtual IObject *OnCreateUnknownObject(const SObjectInfo &objInfo) const;
214
215 /**
216 * @brief Static callback for when a factory is removed.
217 * @param obj Pointer to the object factory.
218 */
219 static void OnFactoryRemoved(const SObjectFactoryPtr &obj);
220};
221
222SNSEND
223
224#endif // __SOBJECTFACTORY__H__
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.
static bool CompareElements(INARGTYPE obj1, INARGTYPE obj2)
Compares two SObjectInfo structures.
static ULONG Hash(INARGTYPE objInfo)
Computes the hash value for an SObjectInfo.
SCmnMap(void(*funOnKeyRemoved)(const SObjectFactoryPtr &)=NULL)
Definition SCmnMap.h:28
SObjectFactoryMgr(void)
Constructor for SObjectFactoryMgr.
BOOL UnregisterFactory(const SObjectInfo &objInfo)
Unregisters an object factory.
BOOL TplUnregisterFactory()
Template method to unregister a factory for a specific object type.
BOOL TplRegisterFactory()
Template method to register a factory for a specific object type.
BOOL RegisterFactory(const IObjectFactory *objFactory, BOOL bReplace=false)
Registers an object factory.
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.
Template-based object factory.
IObject * NewObject() SCONST OVERRIDE
Creates a new object of type T.
TplSObjectFactory()
Default constructor.
void OnFinalRelease()
Called when the final reference is released.
IObjectFactory * Clone() SCONST OVERRIDE
Clones the object factory.
LPCWSTR BaseClassName() SCONST OVERRIDE
Gets the base class name of the object.
SObjectInfo GetObjectInfo() SCONST OVERRIDE
Gets the object information for the factory.