|
1 /* Copyright (c) 2009 The Khronos Group Inc. |
|
2 * |
|
3 * Permission is hereby granted, free of charge, to any person obtaining a |
|
4 * copy of this software and/or associated documentation files (the |
|
5 * "Materials"), to deal in the Materials without restriction, including |
|
6 * without limitation the rights to use, copy, modify, merge, publish, |
|
7 * distribute, sublicense, and/or sell copies of the Materials, and to |
|
8 * permit persons to whom the Materials are furnished to do so, subject to |
|
9 * the following conditions: |
|
10 * |
|
11 * The above copyright notice and this permission notice shall be included |
|
12 * in all copies or substantial portions of the Materials. |
|
13 * |
|
14 * THE MATERIALS ARE PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, |
|
15 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF |
|
16 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. |
|
17 * IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY |
|
18 * CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, |
|
19 * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE |
|
20 * MATERIALS OR THE USE OR OTHER DEALINGS IN THE MATERIALS. |
|
21 */ |
|
22 |
|
23 #ifndef OWFOBJECT_H_ |
|
24 #define OWFOBJECT_H_ |
|
25 |
|
26 #include <stdlib.h> |
|
27 |
|
28 #include "owftypes.h" |
|
29 #include "owfdebug.h" |
|
30 |
|
31 |
|
32 #ifdef __cplusplus |
|
33 extern "C" |
|
34 { |
|
35 #endif |
|
36 |
|
37 |
|
38 typedef void (*DESTRUCTOR)(void*); |
|
39 typedef void (*CONSTRUCTOR)(void*); |
|
40 |
|
41 typedef struct { |
|
42 const char* type; |
|
43 DESTRUCTOR destructor; |
|
44 OWFuint32 referenceCount; |
|
45 void* payload; |
|
46 } OWF_OBJECT; |
|
47 |
|
48 #define TYPE(x) "" # x "_type" |
|
49 |
|
50 /*! returns field offset within structure */ |
|
51 #define FOFF(x,y) ((OWFuint32) &(((x*) 0x1000)->y) - 0x1000) |
|
52 |
|
53 /*! returns pointer to wrapper object which holds object x */ |
|
54 #define _O(x) ((OWF_OBJECT*) ((OWFuint32)(x) -\ |
|
55 FOFF(OWF_OBJECT,payload))) |
|
56 |
|
57 /*! create new object instance */ |
|
58 #define CREATE(x) (x*) OWF_Object_Construct(sizeof(x), TYPE(x), \ |
|
59 & x##_Ctor, & x##_Dtor) |
|
60 |
|
61 #define REFCOUNT(x) ((x) ? _O(x)->referenceCount : 0xFFFFFFFF) |
|
62 |
|
63 /*! get the type of the contained object */ |
|
64 #define TYPEOF(x) (_O(x)->type) |
|
65 |
|
66 /*! increment object reference count */ |
|
67 #define ADDREF(x,y) \ |
|
68 if (y) {\ |
|
69 _O(y)->referenceCount++; \ |
|
70 DPRINT(("ADDREF: ref count of %s(%p) is now %d\n", TYPEOF(y), y, \ |
|
71 REFCOUNT(y)));\ |
|
72 }\ |
|
73 x = y; |
|
74 |
|
75 /*! decrement object reference count, call destructor if count reaches zero */ |
|
76 #define REMREF(x) \ |
|
77 if (x) {\ |
|
78 --_O(x)->referenceCount; \ |
|
79 DPRINT(("REMREF: ref count of %s(%p) is now %d\n", TYPEOF(x), x, \ |
|
80 REFCOUNT(x))); \ |
|
81 if (0 == _O(x)->referenceCount) {\ |
|
82 if (_O(x)->destructor) {\ |
|
83 _O(x)->destructor(x);\ |
|
84 }\ |
|
85 xfree(_O(x));\ |
|
86 }\ |
|
87 x = NULL;\ |
|
88 } |
|
89 |
|
90 #define DESTROY(x) \ |
|
91 REMREF(x); \ |
|
92 x = NULL; |
|
93 |
|
94 OWF_API_CALL void* |
|
95 OWF_Object_Construct(size_t size, |
|
96 const char* type, |
|
97 CONSTRUCTOR ctor, |
|
98 DESTRUCTOR dtor); |
|
99 |
|
100 |
|
101 #ifdef __cplusplus |
|
102 } |
|
103 #endif |
|
104 |
|
105 |
|
106 #endif /* OWFOBJECT_H_ */ |