ZFFramework
 
Loading...
Searching...
No Matches
ZFStyleable.h
Go to the documentation of this file.
1
5
6#ifndef _ZFI_ZFStyleable_h_
7#define _ZFI_ZFStyleable_h_
8
9#include "ZFSerializable.h"
10#include "ZFCopyable.h"
11#include "ZFObjectObserver.h"
13
14// ============================================================
15// ZFStyleable
16zfclassFwd _ZFP_ZFStyleKeyHolder;
30zfinterface ZFLIB_ZFCore ZFStyleable : zfextend ZFInterface {
31 ZFINTERFACE_DECLARE(ZFStyleable, ZFInterface)
32
33public:
42
43public:
50 zffinal void styleableCopyFrom(ZF_IN ZFObject *anotherStyleable);
51
52public:
57 return zffalse;
58 }
59
65 );
70
71protected:
76 ZF_IN ZFObject *anotherStyleable
77 , ZF_IN const ZFProperty *property
78 );
83 virtual void styleableOnCopyFrom(ZF_IN ZFObject *anotherStyleable);
84
85public:
92 ZF_IN const zfstring &propertyName
93 , ZF_IN const zfstring &styleKey
94 );
96 zffinal const zfstring &propStyle(ZF_IN const zfstring &propertyName);
97private:
98 friend zfclassFwd _ZFP_ZFStyleKeyHolder;
99};
100
101// ============================================================
102// ZFStyle
110zfclass ZFLIB_ZFCore ZFStyle : zfextend ZFObject, zfimplement ZFStyleable, zfimplement ZFSerializable, zfimplement ZFCopyable {
111 ZFOBJECT_DECLARE(ZFStyle, ZFObject)
112 ZFIMPLEMENT_DECLARE(ZFStyleable, ZFSerializable, ZFCopyable)
113
114protected:
116 virtual void copyableOnCopyFrom(ZF_IN ZFObject *anotherObj) {
117 zfsuperI(ZFCopyable)::copyableOnCopyFrom(anotherObj);
118 this->styleableCopyFrom(zfcast(zfself *, anotherObj));
119 }
120};
121
122// ============================================================
123// DefaultStyle
124#define _ZFP_ZFStyleableDefault_level ZFLevelZFFrameworkHigh
125zfclassLikePOD ZFLIB_ZFCore _ZFP_ZFStyleableDefaultPointerHolder {
126public:
127 void *d;
128 _ZFP_ZFStyleableDefaultPointerHolder(void) : d(zfnull) {}
129};
130
131extern ZFLIB_ZFCore _ZFP_ZFStyleableDefaultPointerHolder *_ZFP_ZFStyleableDefaultRefAccess(ZF_IN const zfstring &name);
132typedef void (*_ZFP_ZFStyleableDefaultDeleteCallback)(ZF_IN void *instance);
133zfclassLikePOD ZFLIB_ZFCore _ZFP_ZFStyleableDefaultDeleteCallbackHolder {
134public:
135 _ZFP_ZFStyleableDefaultDeleteCallbackHolder(
136 ZF_IN _ZFP_ZFStyleableDefaultDeleteCallback deleteCallback
137 , ZF_IN void *instance
138 )
139 : deleteCallback(deleteCallback)
140 , instance(instance)
141 {
142 }
143 ~_ZFP_ZFStyleableDefaultDeleteCallbackHolder(void) {
144 if(this->deleteCallback && this->instance) {
145 this->deleteCallback(this->instance);
146 }
147 }
148private:
149 _ZFP_ZFStyleableDefaultDeleteCallback deleteCallback;
150 void *instance;
151};
152
153#define _ZFP_ZFSTYLE_DEFAULT_DECLARE(YourStyle) \
154 public: \
155 zfclass _ZFP_ZFStyleableDefault_##YourStyle; \
156 public: \
157 \
158 static zfanyT<YourStyle> DefaultStyle(void); \
159 private: \
160 static void _ZFP_ZFStyleablEnumDefaultStyle(ZF_IN YourStyle *newInstance); \
161 static const ZFCorePointer *&_ZFP_ZFStyleableDefaultCleaner(void); \
162 static void _ZFP_ZFStyleableDefaultOnDelete(ZF_IN void *instance); \
163 public:
164#define _ZFP_ZFSTYLE_DEFAULT_DEFINE(YourStyle) \
165 zfclass YourStyle::_ZFP_ZFStyleableDefault_##YourStyle : zfextend YourStyle { \
166 ZFOBJECT_DECLARE(_ZFP_ZFStyleableDefault_##YourStyle, YourStyle) \
167 public: \
168 zfoverride \
169 virtual zfbool styleableIsDefaultStyle(void) { \
170 return zftrue; \
171 } \
172 }; \
173 zfanyT<YourStyle> YourStyle::DefaultStyle(void) { \
174 static _ZFP_ZFStyleableDefaultPointerHolder *holder = _ZFP_ZFStyleableDefaultRefAccess(#YourStyle); \
175 if(holder->d == zfnull) { \
176 ZFCoreMutexLocker(); \
177 if(ZFFrameworkStateCheck(_ZFP_ZFStyleableDefault_level) == ZFFrameworkStateNotAvailable) { \
178 return zfnull; \
179 } \
180 zfauto obj = _ZFP_ZFStyleableDefault_##YourStyle::ClassData()->newInstance(); \
181 if(obj != zfnull) { \
182 zfself::_ZFP_ZFStyleablEnumDefaultStyle(obj.to<YourStyle *>()); \
183 } \
184 } \
185 return (YourStyle *)holder->d; \
186 } \
187 ZFMETHOD_USER_REGISTER_DETAIL_0({ \
188 return YourStyle::DefaultStyle(); \
189 }, YourStyle, \
190 public, ZFMethodTypeStatic, s, \
191 zfanyT<YourStyle>, DefaultStyle) \
192 void YourStyle::_ZFP_ZFStyleablEnumDefaultStyle(ZF_IN YourStyle *newInstance) { \
193 if(ZFFrameworkStateCheck(_ZFP_ZFStyleableDefault_level) == ZFFrameworkStateNotAvailable) { \
194 return; \
195 } \
196 _ZFP_ZFStyleableDefaultPointerHolder *holder = _ZFP_ZFStyleableDefaultRefAccess(#YourStyle); \
197 if(holder->d == newInstance) { \
198 return; \
199 } \
200 const ZFCorePointer *&cleanerRef = _ZFP_ZFStyleableDefaultCleaner(); \
201 const ZFCorePointer *cleanerOld = cleanerRef; \
202 const ZFCorePointer *cleanerNew = zfnull; \
203 cleanerRef = zfnull; \
204 if(newInstance != zfnull) { \
205 holder->d = zfRetain(newInstance); \
206 cleanerNew = ZFObjectGlobalInstanceAdd(ZFCorePointerForObject<_ZFP_ZFStyleableDefaultDeleteCallbackHolder *>( \
207 zfnew(_ZFP_ZFStyleableDefaultDeleteCallbackHolder, YourStyle::_ZFP_ZFStyleableDefaultOnDelete, holder->d)), \
208 _ZFP_ZFStyleableDefault_level); \
209 cleanerRef = cleanerNew; \
210 } \
211 if(cleanerOld != zfnull) { \
212 ZFObjectGlobalInstanceRemove(cleanerOld, _ZFP_ZFStyleableDefault_level); \
213 holder->d = newInstance; \
214 cleanerRef = cleanerNew; \
215 } \
216 } \
217 const ZFCorePointer *&YourStyle::_ZFP_ZFStyleableDefaultCleaner(void) { \
218 static const ZFCorePointer *_cleaner = zfnull; \
219 return _cleaner; \
220 } \
221 void YourStyle::_ZFP_ZFStyleableDefaultOnDelete(ZF_IN void *instance) { \
222 YourStyle::_ZFP_ZFStyleableDefaultCleaner() = zfnull; \
223 _ZFP_ZFStyleableDefaultPointerHolder *holder = _ZFP_ZFStyleableDefaultRefAccess(#YourStyle); \
224 holder->d = zfnull; \
225 zfRelease((YourStyle *)instance); \
226 }
227
260#define ZFSTYLE_DEFAULT_DECLARE(YourStyle) \
261 _ZFP_ZFSTYLE_DEFAULT_DECLARE(YourStyle)
262
263#define ZFSTYLE_DEFAULT_DEFINE(YourStyle) \
264 _ZFP_ZFSTYLE_DEFAULT_DEFINE(YourStyle)
265
275
283#define ZFSTYLE_DEFAULT_AUTO_COPY() \
284 ZFINTERFACE_ON_INIT_DECLARE() { \
285 if(!this->styleableIsDefaultStyle()) { \
286 zfanyT<ZFStyleable> defaultStyle = this->defaultStyle(); \
287 if(defaultStyle != zfnull) { \
288 this->styleableCopyFrom(defaultStyle); \
289 ZFStyleDefaultApplyAutoCopy(this); \
290 } \
291 } \
292 }
293
294// ============================================================
295// style holder
382 ZF_IN const zfstring &styleKey
383 , ZF_IN ZFStyleable *styleValue
384 );
399 , ZF_IN_OUT ZFCoreArray<zfauto> &styleValue
400 );
404 );
409 return ret;
410}
411
414 );
419 return ret;
420}
421
426
435
439#define ZFStyleUpdateBlock() _ZFP_ZFStyleUpdateBlock ZFUniqueName(_ZFP_ZFStyleUpdateBlock)
440zfclassLikePOD _ZFP_ZFStyleUpdateBlock {
441public:
442 _ZFP_ZFStyleUpdateBlock(void) {
444 }
445 ~_ZFP_ZFStyleUpdateBlock(void) {
447 }
448};
449
466ZFEVENT_GLOBAL(ZFLIB_ZFCore, ZFStyleOnInvalid)
468
469// ============================================================
492#define ZFSTYLE_DECODER_DEFINE(registerSig, decodeAction, ...) \
493 _ZFP_ZFSTYLE_DECODER_DEFINE(registerSig, decodeAction, ##__VA_ARGS__)
494#define _ZFP_ZFSTYLE_DECODER_DEFINE(registerSig, decodeAction, ...) \
495 ZF_STATIC_REGISTER_INIT(ZFStyleDecoder_##registerSig) { \
496 _ZFP_ZFStyleDecoderRegister(zftext(#registerSig), zfself::_ZFP_decode); \
497 } \
498 ZF_STATIC_REGISTER_DESTROY(ZFStyleDecoder_##registerSig) { \
499 _ZFP_ZFStyleDecoderUnregister(zftext(#registerSig)); \
500 } \
501 static zfbool _ZFP_decode( \
502 ZF_OUT zfauto &ret \
503 , ZF_IN const zfstring &styleKey \
504 ) { \
505 decodeAction __VA_ARGS__ \
506 } \
507 ZF_STATIC_REGISTER_END(ZFStyleDecoder_##registerSig)
508typedef zfbool (*_ZFP_ZFStyleDecoder)(
509 ZF_OUT zfauto &ret
510 , ZF_IN const zfstring &styleKey
511 );
512extern ZFLIB_ZFCore void _ZFP_ZFStyleDecoderRegister(
513 ZF_IN const zfstring &registerSig
514 , ZF_IN _ZFP_ZFStyleDecoder decoder
515 );
516extern ZFLIB_ZFCore void _ZFP_ZFStyleDecoderUnregister(ZF_IN const zfstring &registerSig);
517
519#endif // #ifndef _ZFI_ZFStyleable_h_
520
interface shows a ZFObject is copyable
#define ZFLIB_ZFCore
used to export symbols
Definition ZFCoreEnvDef.h:30
#define zffinal
dummy macro shows that a method or class is designed must not to be overrided
Definition ZFCoreTypeDef_ClassType.h:63
#define zfclassLikePOD
shows the class is not a POD type, but you may use it like a POD except memset it to 0
Definition ZFCoreTypeDef_ClassType.h:41
#define zfextend
dummy macro shows class inherit from another
Definition ZFCoreTypeDef_ClassType.h:53
#define zfoverride
dummy macro shows that method override parent's method
Definition ZFCoreTypeDef_ClassType.h:58
#define ZF_IN
dummy macro that shows the param used as required input
Definition ZFCoreTypeDef_ClassType.h:184
#define ZF_OUT
dummy macro that shows the param used as required output
Definition ZFCoreTypeDef_ClassType.h:192
#define ZF_IN_OUT
dummy macro that shows the param used as required input and output
Definition ZFCoreTypeDef_ClassType.h:200
#define zfclassFwd
forward declaration of a class type
Definition ZFCoreTypeDef_ClassType.h:31
_ZFT_t_zfbool zfbool
bool type
Definition ZFCoreTypeDef_CoreType.h:103
#define zffalse
bool false type
Definition ZFCoreTypeDef_CoreType.h:111
#define zfnull
same as NULL, defined for future use
Definition ZFCoreTypeDef_CoreType.h:88
zft_zfstring< zfchar > zfstring
see zft_zfstring
Definition ZFCoreTypeDef_StringType.h:15
#define ZF_NAMESPACE_GLOBAL_BEGIN
begin namespace ZFFramework
Definition ZFNamespace.h:97
#define ZF_NAMESPACE_END(NameSpace)
end namespace
Definition ZFNamespace.h:60
#define ZF_NAMESPACE_GLOBAL_END
end namespace ZFFramework
Definition ZFNamespace.h:98
#define ZF_NAMESPACE_BEGIN(NameSpace)
begin namespace
Definition ZFNamespace.h:40
#define zfclass
same as class, shows that this class is a ZFObject type
Definition ZFObjectClassTypeFwd.h:38
#define ZFOBJECT_DECLARE(ChildClass, SuperClass,...)
necessary for every class inherit from ZFObject
Definition ZFObjectDeclare.h:126
#define zfsuperI(T_SuperType)
class ref to proper super type, see ZFObject for more info
Definition ZFObjectDeclare.h:26
#define ZFIMPLEMENT_DECLARE(ImplementedInterfaces,...)
see ZFINTERFACE_DECLARE
Definition ZFObjectInterface.h:169
#define zfimplement
shows class implement from interface, see ZFInterface
Definition ZFObjectInterface.h:24
#define zfinterface
shows the type is an interface, see ZFInterface
Definition ZFObjectInterface.h:20
#define ZFINTERFACE_DECLARE(InterfaceName, ParentInterface,...)
see ZFInterface
Definition ZFObjectInterface.h:100
observer types for ZFObject
#define ZFEVENT_GLOBAL(ZFLIB_, YourEvent)
declare a observer event in global scope, see ZFEVENT
Definition ZFObjectObserver.h:351
serializable interface
ZFCoreArray< zfauto > ZFStyleGetAllValue(void)
see ZFStyleGetAll
Definition ZFStyleable.h:416
void ZFStyleSet(const zfstring &styleKey, ZFStyleable *styleValue)
used to store style holder
void ZFStyleRemoveAll(void)
remove all styles, see ZFStyleSet
void ZFStyleGetAllKeyT(ZFCoreArray< zfstring > &styleKey)
see ZFStyleGetAll
void ZFStyleUpdateBegin()
see ZFStyleSet
void ZFStyleGetAllValueT(ZFCoreArray< zfauto > &styleValue)
see ZFStyleGetAll
zfauto ZFStyleGet(const zfstring &styleKey)
see ZFStyleSet
ZFCoreArray< zfstring > ZFStyleGetAllKey(void)
see ZFStyleGetAll
Definition ZFStyleable.h:406
void ZFStyleUpdateEnd()
see ZFStyleSet
void ZFStyleDefaultApplyAutoCopy(ZFStyleable *style)
util method to setup automatically copy style from ZFStyleable::defaultStyle if its property value ch...
void ZFStyleGetAll(ZFCoreArray< zfstring > &styleKey, ZFCoreArray< zfauto > &styleValue)
get all styles, for debug use only, see ZFStyleSet
light weight array
Definition ZFCoreArray.h:331
base class of all objects
Definition ZFObjectCore.h:205
info for a property for ZFObject, see ZFPROPERTY_RETAIN for more info
Definition ZFProperty.h:28
virtual void copyableOnCopyFrom(ZFObject *anotherObj)
called by copy to copy contents from anotherObj
Definition ZFStyleable.h:116
ZFStyle zfself
class ref to self
Definition ZFStyleable.h:111
styleable element that can apply style from another object
Definition ZFStyleable.h:30
const zfstring & styleKey(void)
see ZFStyleSet
ZFCoreArray< const ZFProperty * > styleablePropertyGetAll(void)
return a list of styleable property, for debug use only
virtual zfbool styleableIsDefaultStyle(void)
true if this object is defaultStyle
Definition ZFStyleable.h:56
virtual zfanyT< ZFStyleable > defaultStyle(void)
return default style of this instance
void styleablePropertyGetAllT(ZFCoreArray< const ZFProperty * > &ret)
return a list of styleable property, for debug use only
virtual void styleableOnCopyFrom(ZFObject *anotherStyleable)
for subclass to achieve custom style copy step, called by styleableCopyFrom, see ZFStyleable
void styleableCopyFrom(ZFObject *anotherStyleable)
copy style from another styleable, see ZFStyleable
void propStyle(const zfstring &propertyName, const zfstring &styleKey)
see ZFStyleSet
const zfstring & propStyle(const zfstring &propertyName)
see ZFStyleSet
virtual void styleableOnCopyPropertyFrom(ZFObject *anotherStyleable, const ZFProperty *property)
copy property with styleable logic
void styleKey(const zfstring &styleKey)
see ZFStyleSet
see zfany
Definition zfany.h:109
a ZFObject holder which would release content object automatically when destroyed
Definition zfautoFwd.h:34
Definition ZFApp.h:13
#define zfcast(T_To, obj)
safely cast ZFObject types, return null if not desired type
Definition zfcast.h:28