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
125extern ZFLIB_ZFCore void **_ZFP_ZFStyleableDefaultRefAccess(ZF_IN const zfstring &name);
126typedef void (*_ZFP_ZFStyleableDefaultDeleteCallback)(ZF_IN void *instance);
127zfclassLikePOD ZFLIB_ZFCore _ZFP_ZFStyleableDefaultDeleteCallbackHolder {
128public:
129 _ZFP_ZFStyleableDefaultDeleteCallbackHolder(
130 ZF_IN _ZFP_ZFStyleableDefaultDeleteCallback deleteCallback
131 , ZF_IN void *instance
132 )
133 : deleteCallback(deleteCallback)
134 , instance(instance)
135 {
136 }
137 ~_ZFP_ZFStyleableDefaultDeleteCallbackHolder(void) {
138 if(this->deleteCallback && this->instance) {
139 this->deleteCallback(this->instance);
140 }
141 }
142private:
143 _ZFP_ZFStyleableDefaultDeleteCallback deleteCallback;
144 void *instance;
145};
146
147#define _ZFP_ZFSTYLE_DEFAULT_DECLARE(YourStyle) \
148 public: \
149 zfclass _ZFP_ZFStyleableDefault_##YourStyle; \
150 public: \
151 \
152 static zfanyT<YourStyle> DefaultStyle(void); \
153 private: \
154 static void _ZFP_ZFStyleablEnumDefaultStyle(ZF_IN YourStyle *newInstance); \
155 static zfauto &_ZFP_ZFStyleableDefaultCleaner(void); \
156 static void _ZFP_ZFStyleableDefaultOnDelete(ZF_IN void *instance); \
157 public:
158#define _ZFP_ZFSTYLE_DEFAULT_DEFINE(YourStyle) \
159 zfclass YourStyle::_ZFP_ZFStyleableDefault_##YourStyle : zfextend YourStyle { \
160 ZFOBJECT_DECLARE(_ZFP_ZFStyleableDefault_##YourStyle, YourStyle) \
161 public: \
162 zfoverride \
163 virtual zfbool styleableIsDefaultStyle(void) { \
164 return zftrue; \
165 } \
166 }; \
167 zfanyT<YourStyle> YourStyle::DefaultStyle(void) { \
168 static void **holder = _ZFP_ZFStyleableDefaultRefAccess(#YourStyle); \
169 if(*holder == zfnull) { \
170 ZFCoreMutexLocker(); \
171 if(ZFFrameworkStateCheck(_ZFP_ZFStyleableDefault_level) == ZFFrameworkStateNotAvailable) { \
172 return zfnull; \
173 } \
174 zfauto obj = _ZFP_ZFStyleableDefault_##YourStyle::ClassData()->newInstance(); \
175 if(obj != zfnull) { \
176 zfself::_ZFP_ZFStyleablEnumDefaultStyle(obj.to<YourStyle *>()); \
177 } \
178 } \
179 return (YourStyle *)(*holder); \
180 } \
181 ZFMETHOD_USER_REGISTER_DETAIL_0(YourStyle, \
182 public, ZFMethodTypeStatic, s, \
183 zfanyT<YourStyle>, DefaultStyle) { \
184 return YourStyle::DefaultStyle(); \
185 } \
186 void YourStyle::_ZFP_ZFStyleablEnumDefaultStyle(ZF_IN YourStyle *newInstance) { \
187 if(ZFFrameworkStateCheck(_ZFP_ZFStyleableDefault_level) == ZFFrameworkStateNotAvailable) { \
188 return; \
189 } \
190 void **holder = _ZFP_ZFStyleableDefaultRefAccess(#YourStyle); \
191 if(*holder == (void *)newInstance) { \
192 return; \
193 } \
194 zfauto &cleanerRef = _ZFP_ZFStyleableDefaultCleaner(); \
195 zfauto cleanerOld = cleanerRef; \
196 cleanerRef = zfnull; \
197 if(newInstance != zfnull) { \
198 *holder = (void *)zfobjRetain(newInstance); \
199 cleanerRef = zfobj<ZFValue>(*holder, YourStyle::_ZFP_ZFStyleableDefaultOnDelete); \
200 ZFObjectGlobalInstanceAdd(cleanerRef, _ZFP_ZFStyleableDefault_level); \
201 } \
202 if(cleanerOld != zfnull) { \
203 ZFObjectGlobalInstanceRemove(cleanerOld, _ZFP_ZFStyleableDefault_level); \
204 *holder = (void *)newInstance; \
205 } \
206 } \
207 zfauto &YourStyle::_ZFP_ZFStyleableDefaultCleaner(void) { \
208 static zfauto _cleaner; \
209 return _cleaner; \
210 } \
211 void YourStyle::_ZFP_ZFStyleableDefaultOnDelete(ZF_IN void *instance) { \
212 YourStyle::_ZFP_ZFStyleableDefaultCleaner() = zfnull; \
213 void **holder = _ZFP_ZFStyleableDefaultRefAccess(#YourStyle); \
214 *holder = zfnull; \
215 zfobjRelease((YourStyle *)instance); \
216 }
217
250#define ZFSTYLE_DEFAULT_DECLARE(YourStyle) \
251 _ZFP_ZFSTYLE_DEFAULT_DECLARE(YourStyle)
252
253#define ZFSTYLE_DEFAULT_DEFINE(YourStyle) \
254 _ZFP_ZFSTYLE_DEFAULT_DEFINE(YourStyle)
255
265
273#define ZFSTYLE_DEFAULT_AUTO_COPY() \
274 ZFINTERFACE_ON_INIT_DECLARE() { \
275 if(!this->styleableIsDefaultStyle()) { \
276 zfanyT<ZFStyleable> defaultStyle = this->defaultStyle(); \
277 if(defaultStyle != zfnull) { \
278 this->styleableCopyFrom(defaultStyle); \
279 ZFStyleDefaultApplyAutoCopy(this); \
280 } \
281 } \
282 }
283
284// ============================================================
285// style holder
372 ZF_IN const zfstring &styleKey
373 , ZF_IN ZFStyleable *styleValue
374 );
389 , ZF_IN_OUT ZFCoreArray<zfauto> &styleValue
390 );
394 );
399 return ret;
400}
401
404 );
409 return ret;
410}
411
416
425
429#define ZFStyleUpdateBlock() _ZFP_ZFStyleUpdateBlock ZFUniqueName(_ZFP_ZFStyleUpdateBlock)
430zfclassLikePOD _ZFP_ZFStyleUpdateBlock {
431public:
432 _ZFP_ZFStyleUpdateBlock(void) {
434 }
435 ~_ZFP_ZFStyleUpdateBlock(void) {
437 }
438};
439
446ZFEVENT_GLOBAL(ZFLIB_ZFCore, ZFStyleOnUpdateBegin)
462ZFEVENT_GLOBAL(ZFLIB_ZFCore, ZFStyleOnInvalid)
464
465// ============================================================
488#define ZFSTYLE_DECODER_DEFINE(registerSig, decodeAction, ...) \
489 _ZFP_ZFSTYLE_DECODER_DEFINE(registerSig, decodeAction, ##__VA_ARGS__)
490#define _ZFP_ZFSTYLE_DECODER_DEFINE(registerSig, decodeAction, ...) \
491 ZF_STATIC_REGISTER_INIT(ZFStyleDecoder_##registerSig) { \
492 _ZFP_ZFStyleDecoderRegister(zftext(#registerSig), zfself::_ZFP_decode); \
493 } \
494 ZF_STATIC_REGISTER_DESTROY(ZFStyleDecoder_##registerSig) { \
495 _ZFP_ZFStyleDecoderUnregister(zftext(#registerSig)); \
496 } \
497 static zfbool _ZFP_decode( \
498 ZF_OUT zfauto &ret \
499 , ZF_IN const zfstring &styleKey \
500 ) { \
501 decodeAction __VA_ARGS__ \
502 } \
503 ZF_STATIC_REGISTER_END(ZFStyleDecoder_##registerSig)
504typedef zfbool (*_ZFP_ZFStyleDecoder)(
505 ZF_OUT zfauto &ret
506 , ZF_IN const zfstring &styleKey
507 );
508extern ZFLIB_ZFCore void _ZFP_ZFStyleDecoderRegister(
509 ZF_IN const zfstring &registerSig
510 , ZF_IN _ZFP_ZFStyleDecoder decoder
511 );
512extern ZFLIB_ZFCore void _ZFP_ZFStyleDecoderUnregister(ZF_IN const zfstring &registerSig);
513
515#endif // #ifndef _ZFI_ZFStyleable_h_
516
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
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:357
serializable interface
ZFCoreArray< zfauto > ZFStyleGetAllValue(void)
see ZFStyleGetAll
Definition ZFStyleable.h:406
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:396
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:348
base class of all objects
Definition ZFObjectCore.h:196
info for a property for ZFObject, see ZFPROPERTY_RETAIN for more info
Definition ZFProperty.h:27
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:113
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:24