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