ZFFramework
Loading...
Searching...
No Matches
Using object

like many other application framework, ZFFramework use single inheritance, most of elements inherit from ZFObject
and, we use retain count logic to manage memory, just like Object-C
to create an object, you must use one of alloc function supplied by ZFFramework:

// all object can only be allocated in heap,
// so always use pointer here
ZFObject *obj;
// use zfnull to stands for null object,
// which typically same as NULL in C/C++ world
obj = zfnull;
// allocate an object,
// the newly allocated object would have 1 as retain count
// retain an object,
// which would increase the object's retain count by 1,
// would be 2 here in this example
zfRetain(obj);
// release an object,
// which would decrease the object's retain count by 1,
// would be 1 here in this example
zfRelease(obj);
// continue to release the object,
// decreasing the retain count to 0,
// and the object would be deleted finally
zfRelease(obj);
// after the object being deleted,
// set it to null is recommended,
// just like it was in the C/C++ world
obj = zfnull;
#define zfnull
same as NULL, defined for future use
Definition ZFCoreTypeDef_CoreType.h:50
#define zfRelease(obj)
release an object, see ZFObject
Definition ZFObjectRetain.h:140
#define zfAlloc(T_ZFObject,...)
alloc an object, see ZFObject
Definition ZFObjectRetain.h:96
#define zfRetain(obj)
retain an object, see ZFObject
Definition ZFObjectRetain.h:120
base class of all objects
Definition ZFObjectCore.h:214


once allocated, you may use object's member method just like normal C++ object:

obj->func();

Advanced

tired of writing retain and release everywhere? we also supply some utility methods to make it easier to manage allocated object:

{
// similar to shared_ptr in C++ world
} // obj would be released automatically after this code block
{
// allocate an object which looks like being allocated in stack
} // obj would be released automatically after this code block
// allocate an temp object which would be released automatically
// after end of this code line
MyObject *obj = zfAlloc(MyObject);
// release the object automatically after end of this code line
func(zflineRelease(obj));
obj = zfAlloc(MyObject);
// most powerful auto release logic similar to autorelease in Object-C
// however, requires ZFThread,
// and may affect performance for a little
func(zfautoRelease(obj)); // obj would be released at future
#define zflineRelease(obj)
convenient method to release the object after code line end
Definition ZFObjectAutoPtr.h:89
#define zfautoRelease(obj)
make object autorelease, which would be released by the owner thread
Definition ZFThread_zfautoRelease.h:38
a ZFObject holder which would release content object automatically when destroyed
Definition zfautoFwd.h:34
util class to alloc and hold ZFObject type
Definition ZFObjectAutoPtr.h:163


it's recommended to use zfauto for API design when your function needs to return an allocated object

zfauto myFunc(void) {
return obj;
}