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
// release an object,
// which would decrease the object's retain count by 1,
// would be 1 here in this example
// continue to release the object,
// decreasing the retain count to 0,
// and the object would be deleted finally
// 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:88
#define zfobjRetain(obj)
retain an object, see ZFObject
Definition ZFObjectRetain.h:128
#define zfobjAlloc(T_ZFObject,...)
alloc an object, see ZFObject
Definition ZFObjectRetain.h:104
#define zfobjRelease(obj)
release an object, see ZFObject
Definition ZFObjectRetain.h:148
base class of all objects
Definition ZFObjectCore.h:196


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
obj = zfobjAlloc(MyObject);
// most powerful auto release logic similar to autorelease in Object-C
// however, requires ZFThread,
// and may affect performance for a little
func(zfobjAutoRelease(obj)); // obj would be released at future
#define zfobjAutoRelease(obj)
make object autorelease, which would be released by the owner thread
Definition ZFThread_zfobjAutoRelease.h:38
type restrict version of zfauto
Definition zfautoFwd.h:108
util class to alloc and hold ZFObject type
Definition ZFObjectAutoPtr.h:79


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

zfauto myFunc(void) {
return obj;
}
a ZFObject holder which would release content object automatically when destroyed
Definition zfautoFwd.h:34