sl@0: /* Copyright (c) 2009 The Khronos Group Inc. sl@0: * sl@0: * Permission is hereby granted, free of charge, to any person obtaining a sl@0: * copy of this software and/or associated documentation files (the sl@0: * "Materials"), to deal in the Materials without restriction, including sl@0: * without limitation the rights to use, copy, modify, merge, publish, sl@0: * distribute, sublicense, and/or sell copies of the Materials, and to sl@0: * permit persons to whom the Materials are furnished to do so, subject to sl@0: * the following conditions: sl@0: * sl@0: * The above copyright notice and this permission notice shall be included sl@0: * in all copies or substantial portions of the Materials. sl@0: * sl@0: * THE MATERIALS ARE PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, sl@0: * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF sl@0: * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. sl@0: * IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY sl@0: * CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, sl@0: * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE sl@0: * MATERIALS OR THE USE OR OTHER DEALINGS IN THE MATERIALS. sl@0: */ sl@0: sl@0: #ifndef OWFOBJECT_H_ sl@0: #define OWFOBJECT_H_ sl@0: sl@0: #include sl@0: sl@0: #include "owftypes.h" sl@0: #include "owfdebug.h" sl@0: sl@0: sl@0: #ifdef __cplusplus sl@0: extern "C" sl@0: { sl@0: #endif sl@0: sl@0: sl@0: typedef void (*DESTRUCTOR)(void*); sl@0: typedef void (*CONSTRUCTOR)(void*); sl@0: sl@0: typedef struct { sl@0: const char* type; sl@0: DESTRUCTOR destructor; sl@0: OWFuint32 referenceCount; sl@0: void* payload; sl@0: } OWF_OBJECT; sl@0: sl@0: #define TYPE(x) "" # x "_type" sl@0: sl@0: /*! returns field offset within structure */ sl@0: #define FOFF(x,y) ((OWFuint32) &(((x*) 0x1000)->y) - 0x1000) sl@0: sl@0: /*! returns pointer to wrapper object which holds object x */ sl@0: #define _O(x) ((OWF_OBJECT*) ((OWFuint32)(x) -\ sl@0: FOFF(OWF_OBJECT,payload))) sl@0: sl@0: /*! create new object instance */ sl@0: #define CREATE(x) (x*) OWF_Object_Construct(sizeof(x), TYPE(x), \ sl@0: & x##_Ctor, & x##_Dtor) sl@0: sl@0: #define REFCOUNT(x) ((x) ? _O(x)->referenceCount : 0xFFFFFFFF) sl@0: sl@0: /*! get the type of the contained object */ sl@0: #define TYPEOF(x) (_O(x)->type) sl@0: sl@0: /*! increment object reference count */ sl@0: #define ADDREF(x,y) \ sl@0: if (y) {\ sl@0: _O(y)->referenceCount++; \ sl@0: DPRINT(("ADDREF: ref count of %s(%p) is now %d\n", TYPEOF(y), y, \ sl@0: REFCOUNT(y)));\ sl@0: }\ sl@0: x = y; sl@0: sl@0: /*! decrement object reference count, call destructor if count reaches zero */ sl@0: #define REMREF(x) \ sl@0: if (x) {\ sl@0: --_O(x)->referenceCount; \ sl@0: DPRINT(("REMREF: ref count of %s(%p) is now %d\n", TYPEOF(x), x, \ sl@0: REFCOUNT(x))); \ sl@0: if (0 == _O(x)->referenceCount) {\ sl@0: if (_O(x)->destructor) {\ sl@0: _O(x)->destructor(x);\ sl@0: }\ sl@0: xfree(_O(x));\ sl@0: }\ sl@0: x = NULL;\ sl@0: } sl@0: sl@0: #define DESTROY(x) \ sl@0: REMREF(x); \ sl@0: x = NULL; sl@0: sl@0: OWF_API_CALL void* sl@0: OWF_Object_Construct(size_t size, sl@0: const char* type, sl@0: CONSTRUCTOR ctor, sl@0: DESTRUCTOR dtor); sl@0: sl@0: sl@0: #ifdef __cplusplus sl@0: } sl@0: #endif sl@0: sl@0: sl@0: #endif /* OWFOBJECT_H_ */