1 /*
2  * Distributed under the Boost Software License, Version 1.0.
3  *    (See accompanying file LICENSE_1_0.txt or copy at
4  *          http://www.boost.org/LICENSE_1_0.txt)
5  */
6 module glib.gtypes;
7 
8 import std.bitmanip;
9 import core.stdc.config;
10 
11 
12 enum GLIB_MAJOR_VERSION = 2;
13 enum GLIB_MINOR_VERSION = 42;
14 enum GLIB_MICRO_VERSION = 1;
15 
16 
17 alias gchar = char;
18 alias gshort = short;
19 alias glong = c_long;
20 alias gint = int;
21 alias gboolean = gint;
22 
23 alias guchar = ubyte;
24 alias gushort = ushort;
25 alias gulong = c_ulong;
26 alias guint = uint;
27 
28 alias gfloat = float;
29 alias gdouble = double;
30 
31 alias gint8 = byte;
32 alias guint8 = ubyte;
33 alias gint16 = short;
34 alias guint16 = ushort;
35 alias gint32 = int;
36 alias guint32 = uint;
37 alias gint64 = long;
38 alias guint64 = ulong;
39 
40 alias gssize = ptrdiff_t;
41 alias gsize = size_t;
42 
43 alias goffset = gint64;
44 
45 alias gpointer = void*;
46 alias gconstpointer = const(void)*;
47 
48 alias gintptr = ptrdiff_t;
49 alias guintptr = size_t;
50 
51 alias GPid = int;
52 
53 
54 enum float G_MINFLOAT = float.min_normal;
55 enum float G_MAXFLOAT = float.max;
56 enum double G_MINDOUBLE	= double.min_normal;
57 enum double G_MAXDOUBLE	= double.max;
58 enum short G_MINSHORT = short.min;
59 enum short G_MAXSHORT = short.max;
60 enum ushort G_MAXUSHORT = ushort.max;
61 enum int G_MININT = int.min;
62 enum int G_MAXINT = int.max;
63 enum uint G_MAXUINT = uint.max;
64 enum c_long G_MINLONG = c_long.min;
65 enum c_long G_MAXLONG = c_long.max;
66 enum c_ulong G_MAXULONG	= c_ulong.max;
67 
68 enum gint8 G_MININT8 = gint8.min;
69 enum gint8 G_MAXINT8 = gint8.max;
70 enum guint8 G_MAXUINT8 = guint8.max;
71 
72 enum gint16 G_MININT16 = gint16.min;
73 enum gint16 G_MAXINT16 = gint16.max;
74 enum guint16 G_MAXUINT16 = guint16.max;
75 
76 enum gint32 G_MININT32 = gint32.min;
77 enum gint32 G_MAXINT32 = gint32.max;
78 enum guint32 G_MAXUINT32 = guint32.max;
79 
80 enum gint64 G_MININT64 = gint64.min;
81 enum gint64 G_MAXINT64 = gint64.max;
82 enum guint64 G_MAXUINT64 = guint64.max;
83 
84 enum gsize G_MAXSIZE = gsize.max;
85 enum gssize G_MINSSIZE = gssize.min;
86 enum gssize G_MAXSSIZE = gssize.max;
87 
88 enum goffset G_MINOFFSET = goffset.min;
89 enum goffset G_MAXOFFSET = goffset.max;
90 
91 enum size_t GLIB_SIZEOF_VOID_P = (void*).sizeof;
92 enum size_t GLIB_SIZEOF_LONG = c_long.sizeof;
93 enum size_t GLIB_SIZEOF_SIZE_T = size_t.sizeof;
94 enum size_t GLIB_SIZEOF_SSIZE_T = ptrdiff_t.sizeof;
95 
96 
97 enum FALSE = 0;
98 enum TRUE = 1;
99 enum NULL = null;
100 
101 
102 extern (C) {
103     alias GCompareFunc = gint function(gconstpointer a, gconstpointer b);
104     alias GCompareDataFunc = gint function(gconstpointer a, gconstpointer b,
105                                            gpointer user_data);
106     alias GEqualFunc = gboolean function(gconstpointer a, gconstpointer b);
107     alias GDestroyNotify = void function(gpointer data);
108     alias GFunc = void function(gpointer data, gpointer user_data);
109     alias GHashFunc = guint function (gconstpointer key);
110     alias GHFunc = void function(gpointer key, gpointer value,
111                                  gpointer user_data);
112 
113     alias GFreeFunc = void function(gpointer data);
114 
115     alias GTranslateFunc = const(gchar)* function(const(gchar)*str,
116                                                   gpointer data);
117 }
118 enum real G_E = 2.7182818284590452353602874713526624977572470937000;
119 enum real G_LN2 = 0.69314718055994530941723212145817656807550013436026;
120 enum real G_LN10 = 2.3025850929940456840179914546843642076011014886288;
121 enum real G_PI = 3.1415926535897932384626433832795028841971693993751;
122 enum real G_PI_2 = 1.5707963267948966192313216916397514420985846996876;
123 enum real G_PI_4 = 0.78539816339744830961566084581987572104929234984378;
124 enum real G_SQRT2 = 1.4142135623730950488016887242096980785696718753769;
125 
126 enum G_LITTLE_ENDIAN = 1234;
127 enum G_BIG_ENDIAN =    4321;
128 version (LittleEndian) enum G_BYTE_ORDER = G_LITTLE_ENDIAN;
129 else enum G_BYTE_ORDER = G_BIG_ENDIAN;
130 
131 
132 
133 guint16 GUINT16_SWAP_LE_BE(V)(V val) {
134     return
135         cast(guint16)((cast(guint16)val >> 8) | (cast(guint16)val << 8));
136 }
137 
138 guint32 GUINT32_SWAP_LE_BE(V)(V val) {
139     return
140         (((cast(guint32)val) & 0x000000ff) << 24) |
141         (((cast(guint32)val) & 0x0000ff00) << 8) |
142         (((cast(guint32)val) & 0x00ff0000) >> 8) |
143         (((cast(guint32)val) & 0xff000000) >> 24);
144 }
145 
146 guint64 GUINT64_SWAP_LE_BE(V)(V val) {
147     return
148         (((cast(guint64)val) & 0x00000000000000ffL) << 56) |
149         (((cast(guint64)val) & 0x000000000000ff00L) << 40) |
150         (((cast(guint64)val) & 0x0000000000ff0000L) << 24) |
151         (((cast(guint64)val) & 0x00000000ff000000L) << 8)  |
152         (((cast(guint64)val) & 0x000000ff00000000L) >> 8)  |
153         (((cast(guint64)val) & 0x0000ff0000000000L) >> 24) |
154         (((cast(guint64)val) & 0x00ff000000000000L) >> 40) |
155         (((cast(guint64)val) & 0xff00000000000000L) >> 56);
156 }
157 
158 template GUINT16_SWAP_LE_BE_CONSTANT(guint16 val)
159 {
160     const guint16 GUINT16_SWAP_LE_BE_CONSTANT =
161         (cast(guint16)val >> 8) | (cast(guint16)val << 8);
162 }
163 
164 template GUINT32_SWAP_LE_BE_CONSTANT(guint32 val)
165 {
166     const guint32 GUINT32_SWAP_LE_BE_CONSTANT =
167         (((cast(guint32)val) & 0x000000ff) << 24) |
168         (((cast(guint32)val) & 0x0000ff00) << 8) |
169         (((cast(guint32)val) & 0x00ff0000) >> 8) |
170         (((cast(guint32)val) & 0xff000000) >> 24);
171 }
172 
173 template GUINT64_SWAP_LE_BE_CONSTANT(guint64 val)
174 {
175     const guint64 GUINT64_SWAP_LE_BE_CONSTANT =
176         (((cast(guint64)val) & 0x00000000000000ffL) << 56) |
177         (((cast(guint64)val) & 0x000000000000ff00L) << 40) |
178         (((cast(guint64)val) & 0x0000000000ff0000L) << 24) |
179         (((cast(guint64)val) & 0x00000000ff000000L) << 8)  |
180         (((cast(guint64)val) & 0x000000ff00000000L) >> 8)  |
181         (((cast(guint64)val) & 0x0000ff0000000000L) >> 24) |
182         (((cast(guint64)val) & 0x00ff000000000000L) >> 40) |
183         (((cast(guint64)val) & 0xff00000000000000L) >> 56);
184 }
185 
186 
187 auto GINT16_TO_LE(gint16 val) {
188     version(LittleEndian) return val;
189     else return cast(gint16) GUINT16_SWAP_LE_BE(val);
190 }
191 auto GINT16_TO_BE(gint16 val) {
192     version(BigEndian) return val;
193     else return cast(gint16) GUINT16_SWAP_LE_BE(val);
194 }
195 
196 auto GUINT16_TO_LE(guint16 val) {
197     version(LittleEndian) return val;
198     else return GUINT16_SWAP_LE_BE(val);
199 }
200 auto GUINT16_TO_BE(guint16 val) {
201     version(BigEndian) return val;
202     else return GUINT16_SWAP_LE_BE(val);
203 }
204 
205 
206 
207 auto GINT32_TO_LE(gint32 val) {
208     version(LittleEndian) return val;
209     else return cast(gint32) GUINT32_SWAP_LE_BE(val);
210 }
211 auto GINT32_TO_BE(gint32 val) {
212     version(BigEndian) return val;
213     else return cast(gint32) GUINT32_SWAP_LE_BE(val);
214 }
215 
216 auto GUINT32_TO_LE(guint32 val) {
217     version(LittleEndian) return val;
218     else return GUINT32_SWAP_LE_BE(val);
219 }
220 auto GUINT32_TO_BE(guint32 val) {
221     version(BigEndian) return val;
222     else return GUINT32_SWAP_LE_BE(val);
223 }
224 
225 
226 
227 auto GINT64_TO_LE(gint64 val) {
228     version(LittleEndian) return val;
229     else return cast(gint64) GUINT64_SWAP_LE_BE(val);
230 }
231 auto GINT64_TO_BE(gint64 val) {
232     version(BigEndian) return val;
233     else return cast(gint64) GUINT64_SWAP_LE_BE(val);
234 }
235 
236 auto GUINT64_TO_LE(guint64 val) {
237     version(LittleEndian) return val;
238     else return GUINT64_SWAP_LE_BE(val);
239 }
240 auto GUINT64_TO_BE(guint64 val) {
241     version(BigEndian) return val;
242     else return GUINT64_SWAP_LE_BE(val);
243 }
244 
245 static if (c_ulong.sizeof == 4) {
246     alias GLONG_TO_LE = GINT32_TO_LE;
247     alias GLONG_TO_BE = GINT32_TO_BE;
248     alias GULONG_TO_LE = GUINT32_TO_LE;
249     alias GULONG_TO_BE = GUINT32_TO_BE;
250 }
251 else {
252     static assert(c_ulong.sizeof == 8);
253     alias GLONG_TO_LE = GINT64_TO_LE;
254     alias GLONG_TO_BE = GINT64_TO_BE;
255     alias GULONG_TO_LE = GUINT64_TO_LE;
256     alias GULONG_TO_BE = GUINT64_TO_BE;
257 }
258 
259 static if (size_t.sizeof == 4) {
260     alias GSSIZE_TO_LE = GINT32_TO_LE;
261     alias GSSIZE_TO_BE = GINT32_TO_BE;
262     alias GSIZE_TO_LE = GUINT32_TO_LE;
263     alias GSIZE_TO_BE = GUINT32_TO_BE;
264 }
265 else {
266     static assert(size_t.sizeof == 8);
267     alias GSSIZE_TO_LE = GINT64_TO_LE;
268     alias GSSIZE_TO_BE = GINT64_TO_BE;
269     alias GSIZE_TO_LE = GUINT64_TO_LE;
270     alias GSIZE_TO_BE = GUINT64_TO_BE;
271 }
272 
273 alias GINT_TO_LE = GINT32_TO_LE;
274 alias GINT_TO_BE = GINT32_TO_BE;
275 alias GUINT_TO_LE = GUINT32_TO_LE;
276 alias GUINT_TO_BE = GUINT32_TO_BE;
277 
278 
279 
280 
281 auto GINT16_FROM_LE(V)(V val) { return (GINT16_TO_LE (val)); }
282 auto GUINT16_FROM_LE(V)(V val) { return (GUINT16_TO_LE (val)); }
283 auto GINT16_FROM_BE(V)(V val) { return (GINT16_TO_BE (val)); }
284 auto GUINT16_FROM_BE(V)(V val) { return (GUINT16_TO_BE (val)); }
285 auto GINT32_FROM_LE(V)(V val) { return (GINT32_TO_LE (val)); }
286 auto GUINT32_FROM_LE(V)(V val) { return (GUINT32_TO_LE (val)); }
287 auto GINT32_FROM_BE(V)(V val) { return (GINT32_TO_BE (val)); }
288 auto GUINT32_FROM_BE(V)(V val) { return (GUINT32_TO_BE (val)); }
289 
290 auto GINT64_FROM_LE(V)(V val) { return (GINT64_TO_LE (val)); }
291 auto GUINT64_FROM_LE(V)(V val) { return (GUINT64_TO_LE (val)); }
292 auto GINT64_FROM_BE(V)(V val) { return (GINT64_TO_BE (val)); }
293 auto GUINT64_FROM_BE(V)(V val) { return (GUINT64_TO_BE (val)); }
294 
295 auto GLONG_FROM_LE(V)(V val) { return (GLONG_TO_LE (val)); }
296 auto GULONG_FROM_LE(V)(V val) { return (GULONG_TO_LE (val)); }
297 auto GLONG_FROM_BE(V)(V val) { return (GLONG_TO_BE (val)); }
298 auto GULONG_FROM_BE(V)(V val) { return (GULONG_TO_BE (val)); }
299 
300 auto GINT_FROM_LE(V)(V val) { return (GINT_TO_LE (val)); }
301 auto GUINT_FROM_LE(V)(V val) { return (GUINT_TO_LE (val)); }
302 auto GINT_FROM_BE(V)(V val) { return (GINT_TO_BE (val)); }
303 auto GUINT_FROM_BE(V)(V val) { return (GUINT_TO_BE (val)); }
304 
305 auto GSIZE_FROM_LE(V)(V val) { return (GSIZE_TO_LE (val)); }
306 auto GSSIZE_FROM_LE(V)(V val) { return (GSSIZE_TO_LE (val)); }
307 auto GSIZE_FROM_BE(V)(V val) { return (GSIZE_TO_BE (val)); }
308 auto GSSIZE_FROM_BE(V)(V val) { return (GSSIZE_TO_BE (val)); }
309 
310 auto g_ntohl(V)(V val) { return (GUINT32_FROM_BE (val)); }
311 auto g_ntohs(V)(V val) { return (GUINT16_FROM_BE (val)); }
312 auto g_htonl(V)(V val) { return (GUINT32_TO_BE (val)); }
313 auto g_htons(V)(V val) { return (GUINT16_TO_BE (val)); }
314 
315 
316 
317 /* IEEE Standard 754 Single Precision Storage Format (gfloat):
318  *
319  *        31 30           23 22            0
320  * +--------+---------------+---------------+
321  * | s 1bit | e[30:23] 8bit | f[22:0] 23bit |
322  * +--------+---------------+---------------+
323  * B0------------------->B1------->B2-->B3-->
324  *
325  * IEEE Standard 754 Double Precision Storage Format (gdouble):
326  *
327  *        63 62            52 51            32   31            0
328  * +--------+----------------+----------------+ +---------------+
329  * | s 1bit | e[62:52] 11bit | f[51:32] 20bit | | f[31:0] 32bit |
330  * +--------+----------------+----------------+ +---------------+
331  * B0--------------->B1---------->B2--->B3---->  B4->B5->B6->B7->
332  */
333 /* subtract from biased_exponent to form base2 exponent (normal numbers) */
334 
335 union GFloatIEEE754
336 {
337     struct MPN {
338         version(LittleEndian) {
339             mixin(bitfields!(
340                         guint, "mantissa", 23,
341                         guint, "biased_exponent", 8,
342                         guint, "sign", 1));
343         }
344         else {
345             mixin(bitfields!(
346                         guint, "sign", 1,
347                         guint, "biased_exponent", 8,
348                         guint, "mantissa", 23));
349         }
350     }
351 
352     gfloat v_float;
353     MPN mpn;
354 }
355 
356 union GDoubleIEEE754
357 {
358     struct MPN {
359         version(LittleEndian) {
360             mixin(bitfields!(
361                         guint, "mantissa_low", 32,
362                         guint, "mantissa_high", 20,
363                         guint, "biased_exponent", 11,
364                         guint, "sign", 1));
365         }
366         else {
367             mixin(bitfields!(
368                         guint, "sign", 1,
369                         guint, "biased_exponent", 11,
370                         guint, "mantissa_high", 20,
371                         guint, "mantissa_low", 32));
372         }
373     }
374 
375     gdouble v_double;
376     MPN mpn;
377 }
378 
379 
380 
381 enum G_IEEE754_FLOAT_BIAS = 127;
382 enum G_IEEE754_DOUBLE_BIAS = 1023;
383 
384 enum real G_LOG_2_BASE_10 = 0.30102999566398119521;
385 
386 
387 struct GTimeVal
388 {
389   glong tv_sec;
390   glong tv_usec;
391 }
392