2 * Copyright 2008, 2009 David Hilvert <dhilvert@gmail.com>
4 * This file is part of libale.
6 * libale is free software: you can redistribute it and/or modify it under the
7 * terms of the GNU Affero General Public License as published by the Free
8 * Software Foundation, either version 3 of the License, or (at your option)
11 * libale is distributed in the hope that it will be useful, but WITHOUT ANY
12 * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
13 * FOR A PARTICULAR PURPOSE. See the GNU Affero General Public License for
16 * You should have received a copy of the GNU Affero General Public License
17 * along with libale. If not, see <http://www.gnu.org/licenses/>.
25 * Abstract types used internally by host code (cf. ale_context_get_*_type for
26 * preferred computational types).
29 typedef double ale_pos
;
30 typedef double ale_accum
;
31 typedef double ale_real
;
33 typedef struct { ale_pos x
[3]; } point
;
35 static point
point2(ale_pos x0
, ale_pos x1
) {
42 static point
point3(ale_pos x0
, ale_pos x1
, ale_pos x2
) {
50 static point
point_posinf(int dim
) {
54 /* Division logic from ALE d2::point::posinf */
61 for (d
= 0; d
< dim
&& d
< 3; d
++)
67 static point
point_neginf(int dim
) {
69 point p
= point_posinf(dim
);
71 for (d
= 0; d
< dim
&& d
< 3; d
++)
78 * Macros for common API type elements and functions.
81 #define TYPE_COMMON_ELEMENTS \
82 unsigned int refcount;
84 #define TYPE_STRUCTURE(TYPENAME, TYPE_ELEMENTS) \
85 struct _ ## TYPENAME { \
86 TYPE_COMMON_ELEMENTS \
90 #define TYPE_COMMON_FUNCTIONS(TYPENAME, DESTRUCTOR) \
91 int TYPENAME ## _valid(TYPENAME xarg) { \
92 if (xarg == NULL || xarg->refcount == 0) \
97 int TYPENAME ## _retain(TYPENAME xarg) { \
98 if (!TYPENAME ## _valid(xarg)) \
99 return ALE_UNSPECIFIED_FAILURE; \
100 if (!(xarg->refcount + 1 > 0)) \
101 return ALE_UNSPECIFIED_FAILURE; \
103 return ALE_SUCCESS; \
106 static TYPENAME TYPENAME ## _alloc() { \
107 TYPENAME retval = (TYPENAME) malloc(sizeof(struct _ ## TYPENAME)); \
109 retval->refcount = 1; \
113 static void TYPENAME ## _free(TYPENAME this) { \
117 int TYPENAME ## _release(TYPENAME xarg) { \
118 if (!TYPENAME ## _valid(xarg)) \
119 return ALE_UNSPECIFIED_FAILURE; \
121 if (xarg->refcount == 0) {\
122 TYPENAME ## _free(xarg); \
125 return ALE_SUCCESS; \
128 #define TYPE(TYPENAME, TYPE_ELEMENTS, DESTRUCTOR) \
129 TYPE_STRUCTURE(TYPENAME, TYPE_ELEMENTS) \
130 TYPE_COMMON_FUNCTIONS(TYPENAME, DESTRUCTOR)
133 * Macros for API parameter operations.
136 #define PARAMETER_R(OBJECT_TYPE, PARAMETER_NAME, PARAMETER_TYPE) \
137 PARAMETER_TYPE OBJECT_TYPE ## _get_ ## PARAMETER_NAME (OBJECT_TYPE object) { \
138 if (!OBJECT_TYPE ## _valid(object)) \
139 return ((PARAMETER_TYPE) 0); \
140 return object->PARAMETER_NAME; \
143 #define PARAMETER_W(OBJECT_TYPE, PARAMETER_NAME, PARAMETER_TYPE) \
144 int OBJECT_TYPE ## _set_ ## PARAMETER_NAME (OBJECT_TYPE object, PARAMETER_TYPE value){ \
145 if (!OBJECT_TYPE ## _valid(object)) \
146 return ALE_UNSPECIFIED_FAILURE; \
147 object->PARAMETER_NAME = value; \
148 return ALE_SUCCESS; \
151 #define PARAMETER_RW(OBJECT_TYPE, PARAMETER_NAME, PARAMETER_TYPE) \
152 PARAMETER_R(OBJECT_TYPE, PARAMETER_NAME, PARAMETER_TYPE) \
153 PARAMETER_W(OBJECT_TYPE, PARAMETER_NAME, PARAMETER_TYPE)
156 * Detemine type size.
159 static int type_size(int type
) {
160 if (type
== ALE_TYPE_UINT_8
)
162 if (type
== ALE_TYPE_UINT_16
)
164 if (type
== ALE_TYPE_FLOAT_32
|| type
== ALE_TYPE_UINT_32
)
166 if (type
== ALE_TYPE_FLOAT_64
|| type
== ALE_TYPE_UINT_64
)