1 |
/* PEAK Library |
2 |
* |
3 |
* Copyright (c) 2003 |
4 |
* Stephane Thiell <mbuna@bugged.org>. All rights reserved. |
5 |
* |
6 |
* Redistribution and use in source and binary forms, with or without |
7 |
* modification, are permitted provided that the following conditions |
8 |
* are met: |
9 |
* |
10 |
* 1. Redistributions of source code must retain the above copyright |
11 |
* notice, this list of conditions and the following disclaimer. |
12 |
* |
13 |
* 2. Redistributions in binary form must reproduce the above copyright |
14 |
* notice, this list of conditions and the following disclaimer in the |
15 |
* documentation and/or other materials provided with the distribution. |
16 |
* |
17 |
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS |
18 |
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED |
19 |
* TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR |
20 |
* PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR |
21 |
* CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, |
22 |
* EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, |
23 |
* PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; |
24 |
* OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, |
25 |
* WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR |
26 |
* OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF |
27 |
* ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
28 |
* |
29 |
* $Id: runtime.h,v 1.2 2005/01/27 16:31:50 mbuna Exp $ |
30 |
*/ |
31 |
#ifndef INCLUDED_PEAK_RUNTIME_H_ |
32 |
#define INCLUDED_PEAK_RUNTIME_H_ |
33 |
|
34 |
#include <sys/types.h> |
35 |
#include <sys/time.h> |
36 |
#include <stdarg.h> |
37 |
#include <stdlib.h> |
38 |
|
39 |
#include <peak/stdint.h> |
40 |
|
41 |
typedef void (*peak_runtime_init)(void *p, va_list vp, void *ctcx); |
42 |
typedef void (*peak_runtime_finalize)(void *p); |
43 |
|
44 |
struct _peak_runtime_class |
45 |
{ |
46 |
const char *name; |
47 |
int size; |
48 |
const struct _peak_runtime_class *base; |
49 |
peak_runtime_init init; |
50 |
peak_runtime_finalize finalize; |
51 |
}; |
52 |
|
53 |
typedef const struct _peak_runtime_class peak_runtime_class; |
54 |
|
55 |
#define PEAK_CLASS(name) (&_peak_##name##_class) |
56 |
|
57 |
#define PEAK_CLASS_BASE_DECLARE(name) \ |
58 |
static peak_runtime_class _peak_##name##_class = \ |
59 |
{ \ |
60 |
"peak_" #name, \ |
61 |
sizeof(struct __peak_##name), \ |
62 |
NULL, \ |
63 |
(peak_runtime_init)__peak_##name##_init, \ |
64 |
(peak_runtime_finalize)__peak_##name##_finalize \ |
65 |
} |
66 |
|
67 |
#define PEAK_CLASS_DECLARE(name, base) \ |
68 |
static peak_runtime_class _peak_##name##_class = \ |
69 |
{ \ |
70 |
"peak_" #name, \ |
71 |
sizeof(struct __peak_##name), \ |
72 |
&_peak_##base##_class, \ |
73 |
(peak_runtime_init)__peak_##name##_init, \ |
74 |
(peak_runtime_finalize)__peak_##name##_finalize \ |
75 |
} |
76 |
|
77 |
#define PEAK_CLASS_BASE_DECLARE_VIRTUAL(name) \ |
78 |
extern peak_runtime_class _peak_##name##_class |
79 |
|
80 |
#define PEAK_CLASS_BASE_DEFINE_VIRTUAL(name) \ |
81 |
peak_runtime_class _peak_##name##_class = \ |
82 |
{ \ |
83 |
"peak_" #name, \ |
84 |
sizeof(struct __peak_##name), \ |
85 |
NULL, \ |
86 |
(peak_runtime_init)__peak_##name##_init, \ |
87 |
(peak_runtime_finalize)__peak_##name##_finalize \ |
88 |
} |
89 |
|
90 |
#define PEAK_CLASS_DECLARE_VIRTUAL(name, base) \ |
91 |
extern peak_runtime_class _peak_##name##_class |
92 |
|
93 |
#define PEAK_CLASS_DEFINE_VIRTUAL(name, base) \ |
94 |
peak_runtime_class _peak_##name##_class = \ |
95 |
{ \ |
96 |
"peak_" #name, \ |
97 |
sizeof(struct __peak_##name), \ |
98 |
&_peak_##base##_class, \ |
99 |
(peak_runtime_init)__peak_##name##_init, \ |
100 |
(peak_runtime_finalize)__peak_##name##_finalize \ |
101 |
} |
102 |
|
103 |
#define PEAK_CLASS_CONSTRUCT0(name) \ |
104 |
(peak_##name)peak_construct(&_peak_##name##_class) |
105 |
#define PEAK_CLASS_CONSTRUCT1(name, arg1) \ |
106 |
(peak_##name)peak_construct(&_peak_##name##_class, arg1) |
107 |
#define PEAK_CLASS_CONSTRUCT2(name, arg1, arg2) \ |
108 |
(peak_##name)peak_construct(&_peak_##name##_class, arg1, arg2) |
109 |
#define PEAK_CLASS_CONSTRUCT3(name, arg1, arg2, arg3) \ |
110 |
(peak_##name)peak_construct(&_peak_##name##_class, arg1, arg2, arg3) |
111 |
#define PEAK_CLASS_CONSTRUCT4(name, arg1, arg2, arg3, arg4) \ |
112 |
(peak_##name)peak_construct(&_peak_##name##_class, arg1, arg2, arg3, arg4) |
113 |
#define PEAK_CLASS_CONSTRUCT5(name, arg1, arg2, arg3, arg4, arg5) \ |
114 |
(peak_##name)peak_construct(&_peak_##name##_class, arg1, arg2, arg3, \ |
115 |
arg4, arg5) |
116 |
#define PEAK_CLASS_CONSTRUCT6(name, arg1, arg2, arg3, arg4, arg5, arg6) \ |
117 |
(peak_##name)peak_construct(&_peak_##name##_class, arg1, arg2, arg3, \ |
118 |
arg4, arg5, arg6) |
119 |
#define PEAK_CLASS_CONSTRUCT7(name, arg1, arg2, arg3, arg4, arg5, arg6, arg7) \ |
120 |
(peak_##name)peak_construct(&_peak_##name##_class, arg1, arg2, arg3, \ |
121 |
arg4, arg5, arg6, arg7) |
122 |
|
123 |
|
124 |
/* _rc -> refcount |
125 |
* bit 31 -> constant flag |
126 |
* |
127 |
*/ |
128 |
struct _peak_runtime_base |
129 |
{ |
130 |
peak_runtime_class * _cls; |
131 |
uint32_t _rc; |
132 |
}; |
133 |
|
134 |
typedef struct _peak_runtime_base peak_runtime_base; |
135 |
|
136 |
#define PEAK_STRUCT_RT_HEADER peak_runtime_base _rt |
137 |
|
138 |
|
139 |
#if defined(__cplusplus) |
140 |
extern "C" { |
141 |
#endif |
142 |
|
143 |
extern void peak_ct_raise(const char *msg, int err, void *ctcx); |
144 |
|
145 |
#define PEAK_CT_RAISE(m, e) peak_ct_raise(m, e, ctcx) |
146 |
#define PEAK_CT_RAISE_IF_ERROR(r, m, e) if (r == -1) PEAK_CT_RAISE(m, e) |
147 |
|
148 |
extern void * peak_construct(peak_runtime_class * cls, ...); |
149 |
|
150 |
static inline peak_runtime_class * |
151 |
peak_get_class(void *obj) |
152 |
{ |
153 |
return ((peak_runtime_base *)obj)->_cls; |
154 |
} |
155 |
|
156 |
#if defined(__cplusplus) |
157 |
} |
158 |
#endif |
159 |
|
160 |
|
161 |
#endif /* INCLUDED_PEAK_RUNTIME_H_ */ |