Main Page   Class Hierarchy   Alphabetical List   Compound List   File List   Compound Members

fxdefs.h
1 /********************************************************************************
2 * *
3 * FOX Definitions, Types, and Macros *
4 * *
5 *********************************************************************************
6 * Copyright (C) 1997,2022 by Jeroen van der Zijp. All Rights Reserved. *
7 *********************************************************************************
8 * This library is free software; you can redistribute it and/or modify *
9 * it under the terms of the GNU Lesser General Public License as published by *
10 * the Free Software Foundation; either version 3 of the License, or *
11 * (at your option) any later version. *
12 * *
13 * This library is distributed in the hope that it will be useful, *
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of *
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
16 * GNU Lesser General Public License for more details. *
17 * *
18 * You should have received a copy of the GNU Lesser General Public License *
19 * along with this program. If not, see <http://www.gnu.org/licenses/> *
20 ********************************************************************************/
21 #ifndef FXDEFS_H
22 #define FXDEFS_H
23 
24 
25 
26 /******************************** Definitions ********************************/
27 
28 // Placement new
29 #include <new>
30 
31 
32 // Path separator
33 #ifdef WIN32
34 #define PATHSEP '\\'
35 #define PATHSEPSTRING "\\"
36 #define PATHLISTSEP ';'
37 #define PATHLISTSEPSTRING ";"
38 #define ISPATHSEP(c) ((c)=='\\' || (c)=='/')
39 #else
40 #define PATHSEP '/'
41 #define PATHSEPSTRING "/"
42 #define PATHLISTSEP ':'
43 #define PATHLISTSEPSTRING ":"
44 #define ISPATHSEP(c) ((c)=='/')
45 #endif
46 
47 
48 // End Of Line
49 #ifdef WIN32
50 #define ENDLINE "\r\n"
51 #else
52 #define ENDLINE "\n"
53 #endif
54 
55 
56 // Byte order
57 #if !defined(FOX_BIGENDIAN)
58 #if defined(__GNUC__)
59 #if defined(__BYTE_ORDER__)
60 #if (__BYTE_ORDER__ == __ORDER_LITTLE_ENDIAN__)
61 #define FOX_BIGENDIAN 0
62 #else
63 #define FOX_BIGENDIAN 1
64 #endif
65 #else
66 #error "FOX_BIGENDIAN macro not set"
67 #endif
68 #elif defined(_MSC_VER)
69 #define FOX_BIGENDIAN 0
70 #else
71 #error "FOX_BIGENDIAN macro not set"
72 #endif
73 #endif
74 
75 
76 // Shared library support
77 #ifdef WIN32
78 #if defined(__GNUC__)
79 #define FXLOCAL
80 #define FXEXPORT __attribute__ ((dllexport))
81 #define FXIMPORT __attribute__ ((dllimport))
82 #else
83 #define FXLOCAL
84 #define FXEXPORT __declspec(dllexport)
85 #define FXIMPORT __declspec(dllimport)
86 #endif
87 #else
88 #if defined(__GNUC__) && (__GNUC__ >= 4)
89 #define FXLOCAL __attribute__((visibility("hidden")))
90 #define FXEXPORT __attribute__((visibility("default")))
91 #define FXIMPORT __attribute__((visibility("default")))
92 #else
93 #define FXLOCAL
94 #define FXEXPORT
95 #define FXIMPORT
96 #endif
97 #endif
98 
99 
100 // Define FXAPI for DLL builds
101 #ifdef FOXDLL
102 #ifdef FOXDLL_EXPORTS
103 #define FXAPI FXEXPORT
104 #define FXTEMPLATE_EXTERN
105 #else
106 #define FXAPI FXIMPORT
107 #define FXTEMPLATE_EXTERN extern
108 #endif
109 #else
110 #define FXAPI
111 #define FXTEMPLATE_EXTERN
112 #endif
113 
114 
115 // Data alignment attribute
116 #if defined(__GNUC__)
117 #define __align(x) __attribute__((aligned(x)))
118 #elif defined(_MSC_VER)
119 #define __align(x) __declspec(align(x))
120 #else
121 #define __align(x)
122 #endif
123 
124 // Get alignment of pointer p to b=2^n bytes, returning 0..b-1
125 #define __alignment(p,b) (((FXival)(p))&((b)-1))
126 
127 // Check if pointer p is aligned to b=2^n bytes
128 #define __isaligned(p,b) (__alignment(p,b)==0)
129 
130 // Align pointer to b=2^n bytes
131 #define __alignto(p,b) ((void*)((((FXival)(p))+((FXival)((b)-1)))&~((FXival)((b)-1))))
132 
133 
134 // Thread-local storage attribute
135 #if defined(__GNUC__)
136 #define __threadlocal __thread
137 #elif defined(_MSC_VER)
138 #define __threadlocal __declspec(thread)
139 #else
140 #define __threadlocal
141 #endif
142 
143 // Non-returning function
144 #if defined(__GNUC__)
145 #define __noreturn __attribute__((__noreturn__))
146 #elif (_MSC_VER >= 1400)
147 #define __noreturn __declspec(noreturn)
148 #else
149 #define __noreturn
150 #endif
151 
152 // Branch prediction optimization
153 #if (__GNUC__ >= 3)
154 #define __likely(cond) __builtin_expect(!!(cond),1)
155 #define __unlikely(cond) __builtin_expect(!!(cond),0)
156 #else
157 #define __likely(cond) (!!(cond))
158 #define __unlikely(cond) (!!(cond))
159 #endif
160 
161 // Unreachable part of code
162 #if defined(__GNUC__) && (__GNUC__ >= 4)
163 #define __unreachable() __builtin_unreachable()
164 #elif defined(_MSC_VER)
165 #define __unreachable() __assume(false)
166 #else
167 #define __unreachable()
168 #endif
169 
170 // Prefetch address
171 #if (__GNUC__ >= 4) && (defined(__i386__) || defined(__x86_64__))
172 #define __prefetch(addr) __builtin_prefetch((addr),0)
173 #define __prefetchw(addr) __builtin_prefetch((addr),1)
174 #else
175 #define __prefetch(addr)
176 #define __prefetchw(addr)
177 #endif
178 
179 // Standard call calling sequence
180 #ifdef WIN32
181 #ifndef CALLBACK
182 #define CALLBACK __stdcall
183 #endif
184 #endif
185 
186 // C Language calling sequence
187 #ifdef WIN32
188 #ifndef CDECL
189 #define CDECL __cdecl
190 #endif
191 #else
192 #ifndef CDECL
193 #define CDECL
194 #endif
195 #endif
196 
197 // Checking printf and scanf format strings
198 #if defined(_CC_GNU_) || defined(__GNUG__) || defined(__GNUC__)
199 #define FX_PRINTF(fmt,arg) __attribute__((format(printf,fmt,arg)))
200 #define FX_SCANF(fmt,arg) __attribute__((format(scanf,fmt,arg)))
201 #define FX_FORMAT(arg) __attribute__((format_arg(arg)))
202 #else
203 #define FX_PRINTF(fmt,arg)
204 #define FX_SCANF(fmt,arg)
205 #define FX_FORMAT(arg)
206 #endif
207 
208 // Word size issues
209 #if defined(_MSC_VER) || defined(__MINGW32__) // Windows
210 #if defined(_WIN64)
211 #define LLP64 1 // Long longs and pointers are 64 bit
212 #else
213 #define ILP32 1 // Ints, longs, and pointers are 32 bit
214 #endif
215 #elif defined(__LP64__) || defined(_LP64) || (_MIPS_SZLONG == 64) || (__WORDSIZE == 64)
216 #define LP64 1 // Longs and pointers are 64 bit
217 #else
218 #define ILP32 1 // Longs, integers, and pointers are 32 bit
219 #endif
220 
221 // Suffixes for 64-bit constants
222 #if defined(LP64)
223 #define FXLONG(c) c ## L // Long suffix for 64 bit
224 #define FXULONG(c) c ## UL
225 #elif defined(_MSC_VER) && (_MSC_VER < 1900)
226 #define FXLONG(c) c ## i64 // Special suffix for 64 bit
227 #define FXULONG(c) c ## ui64
228 #else
229 #define FXLONG(c) c ## LL // Long long suffix for 64 bit
230 #define FXULONG(c) c ## ULL
231 #endif
232 
233 
234 // Raw event type
235 #ifdef WIN32
236 struct tagMSG;
237 #else
238 union _XEvent;
239 #endif
240 
241 
242 namespace FX {
243 
244 
245 /********************************* Typedefs **********************************/
246 
247 // Forward declarations
248 class FXObject;
249 class FXStream;
250 class FXString;
251 
252 
253 // Streamable types; these are fixed size!
254 typedef char FXchar;
255 typedef signed char FXschar;
256 typedef unsigned char FXuchar;
257 typedef bool FXbool;
258 typedef unsigned short FXushort;
259 typedef short FXshort;
260 typedef unsigned int FXuint;
261 typedef int FXint;
262 typedef float FXfloat;
263 typedef double FXdouble;
264 #if defined(WIN32)
265 typedef unsigned int FXwchar;
266 #if defined(_MSC_VER) && !defined(_NATIVE_WCHAR_T_DEFINED)
267 typedef unsigned short FXnchar;
268 #elif defined(__WATCOMC__) && !defined(_WCHAR_T_DEFINED)
269 typedef long char FXnchar;
270 #else
271 typedef wchar_t FXnchar;
272 #endif
273 #else
274 typedef wchar_t FXwchar;
275 typedef unsigned short FXnchar;
276 #endif
277 #if defined(LP64)
278 typedef long FXlong;
279 typedef unsigned long FXulong;
280 #elif defined(_MSC_VER) && (_MSC_VER < 1900)
281 typedef __int64 FXlong;
282 typedef unsigned __int64 FXulong;
283 #else
284 typedef long long FXlong;
285 typedef unsigned long long FXulong;
286 #endif
287 
288 // Integral types large enough to hold value of a pointer
289 #if defined(LP64) || defined(ILP32) // Long for LP64 and ILP32 models
290 typedef long FXival;
291 typedef unsigned long FXuval;
292 #elif defined(LLP64) // Long long for LLP64 models
293 #if defined(_MSC_VER) && (_MSC_VER < 1900)
294 typedef __int64 FXival;
295 typedef unsigned __int64 FXuval;
296 #else
297 typedef long long FXival;
298 typedef unsigned long long FXuval;
299 #endif
300 #endif
301 
302 // Generic void pointer
303 typedef void* FXptr;
304 
305 
306 // Handle to something in server
307 #ifdef WIN32
308 typedef void* FXID;
309 #else
310 typedef unsigned long FXID;
311 #endif
312 
313 // Time since January 1, 1970 (UTC)
314 typedef FXlong FXTime;
315 
316 // Pixel type (could be color index)
317 typedef unsigned long FXPixel;
318 
319 // RGBA pixel value
320 typedef FXuint FXColor;
321 
322 // Hot key
323 typedef FXuint FXHotKey;
324 
325 // Input source handle type
326 #ifdef WIN32
327 typedef void* FXInputHandle;
328 #else
329 typedef FXint FXInputHandle;
330 #endif
331 
332 // Process handle
333 #if defined(WIN32)
334 typedef void* FXProcessID;
335 #else
336 typedef int FXProcessID;
337 #endif
338 
339 // Thread ID type
340 #if defined(WIN32)
341 typedef void* FXThreadID;
342 #else
343 typedef unsigned long FXThreadID;
344 #endif
345 
346 // Thread-local storage key
347 typedef FXuval FXThreadStorageKey;
348 
349 // Raw event type
350 #ifdef WIN32
351 typedef tagMSG FXRawEvent;
352 #else
353 typedef _XEvent FXRawEvent;
354 #endif
355 
356 
357 // Drag and drop data type
358 #ifdef WIN32
359 typedef FXushort FXDragType;
360 #else
361 typedef FXID FXDragType;
362 #endif
363 
364 
365 // Third logic state: unknown/indeterminate
366 enum { maybe=2 };
367 
368 // A time in the far, far future
369 const FXTime forever=FXLONG(9223372036854775807);
370 
371 // Search modes for search/replace dialogs
372 enum {
373  SEARCH_BACKWARD = 1,
374  SEARCH_FORWARD = 2,
375  SEARCH_NOWRAP = 0,
376  SEARCH_WRAP = 4,
377  SEARCH_EXACT = 0,
378  SEARCH_IGNORECASE = 8,
379  SEARCH_REGEX = 16,
380  SEARCH_PREFIX = 32,
381  SEARCH_SUFFIX = 64,
382  SEARCH_WORDS = 128
383  };
384 
385 /********************************** Macros ***********************************/
386 
388 #define FXBIT(val,b) (((val)>>(b))&1)
389 
391 #define FXABS(val) (((val)>=0)?(val):-(val))
392 
394 #define FXSGN(val) (((val)<0)?-1:1)
395 
397 #define FXSGNZ(val) ((val)<0?-1:(val)>0?1:0)
398 
400 #define FXSGNX(x,b) (((FXint)((x)<<(32-(b))))>>(32-(b)))
401 
403 #define FXMAX(a,b) (((a)>(b))?(a):(b))
404 
406 #define FXMIN(a,b) (((a)>(b))?(b):(a))
407 
409 #define FXMIN3(x,y,z) ((x)<(y)?FXMIN(x,z):FXMIN(y,z))
410 
412 #define FXMAX3(x,y,z) ((x)>(y)?FXMAX(x,z):FXMAX(y,z))
413 
415 #define FXMIN4(x,y,z,w) (FXMIN(FXMIN(x,y),FXMIN(z,w)))
416 
418 #define FXMAX4(x,y,z,w) (FXMAX(FXMAX(x,y),FXMAX(z,w)))
419 
421 #define FXMINMAX(lo,hi,a,b) ((a)<(b)?((lo)=(a),(hi)=(b)):((lo)=(b),(hi)=(a)))
422 
424 #define FXCLAMP(lo,x,hi) ((x)<(lo)?(lo):((x)>(hi)?(hi):(x)))
425 
427 #define FXSWAP(a,b,t) ((t)=(a),(a)=(b),(b)=(t))
428 
430 #define FXLERP(a,b,f) ((a)+((b)-(a))*(f))
431 
433 #define STRUCTOFFSET(str,member) (((char *)(&(((str *)0)->member)))-((char *)0))
434 
436 #define ARRAYNUMBER(array) (sizeof(array)/sizeof(array[0]))
437 
439 #define CONTAINER(ptr,str,mem) ((str*)(((char*)(ptr))-STRUCTOFFSET(str,mem)))
440 
442 #define MKUINT(l,h) ((((FX::FXuint)(l))&0xffff) | (((FX::FXuint)(h))<<16))
443 
445 #define FXSEL(type,id) ((((FX::FXuint)(id))&0xffff) | (((FX::FXuint)(type))<<16))
446 
448 #define FXSELTYPE(s) ((FX::FXushort)(((s)>>16)&0xffff))
449 
451 #define FXSELID(s) ((FX::FXushort)((s)&0xffff))
452 
454 #define FXAVGCOLOR(ca,cb) (((ca)&(cb))+((((ca)^(cb))&0xFEFEFEFE)>>1))
455 
456 
457 // Definitions for big-endian machines
458 #if FOX_BIGENDIAN == 1
459 
461 #define FXRGBA(r,g,b,a) (((FX::FXuint)(FX::FXuchar)(a)) | ((FX::FXuint)(FX::FXuchar)(r)<<8) | ((FX::FXuint)(FX::FXuchar)(g)<<16) | ((FX::FXuint)(FX::FXuchar)(b)<<24))
462 
464 #define FXRGB(r,g,b) (((FX::FXuint)(FX::FXuchar)(r)<<8) | ((FX::FXuint)(FX::FXuchar)(g)<<16) | ((FX::FXuint)(FX::FXuchar)(b)<<24) | 0x000000ff)
465 
467 #define FXREDVAL(rgba) ((FX::FXuchar)(((rgba)>>8)&0xff))
468 
470 #define FXGREENVAL(rgba) ((FX::FXuchar)(((rgba)>>16)&0xff))
471 
473 #define FXBLUEVAL(rgba) ((FX::FXuchar)(((rgba)>>24)&0xff))
474 
476 #define FXALPHAVAL(rgba) ((FX::FXuchar)((rgba)&0xff))
477 
479 #define FXRGBACOMPVAL(rgba,comp) ((FX::FXuchar)(((rgba)>>((comp)<<3))&0xff))
480 
482 #define FXCOLORREF2RGB(ref) (FX::FXuint)((((ref)<<8)&0xff000000) | (((ref)<<8)&0xff0000) | (((ref)<<8)&0xff00) | 0x000000ff)
483 
485 #define FXRGB2COLORREF(rgb) (FX::FXuint)((((rgb)>>8)&0xff0000) | (((rgb)>>8)&0xff00) | (((rgb)>>8)&0xff))
486 
487 #endif
488 
489 
490 // Definitions for little-endian machines
491 #if FOX_BIGENDIAN == 0
492 
494 #define FXRGBA(r,g,b,a) (((FX::FXuint)(FX::FXuchar)(a)<<24) | ((FX::FXuint)(FX::FXuchar)(r)<<16) | ((FX::FXuint)(FX::FXuchar)(g)<<8) | ((FX::FXuint)(FX::FXuchar)(b)))
495 
497 #define FXRGB(r,g,b) (((FX::FXuint)(FX::FXuchar)(r)<<16) | ((FX::FXuint)(FX::FXuchar)(g)<<8) | ((FX::FXuint)(FX::FXuchar)(b)) | 0xff000000)
498 
500 #define FXREDVAL(rgba) ((FX::FXuchar)(((rgba)>>16)&0xff))
501 
503 #define FXGREENVAL(rgba) ((FX::FXuchar)(((rgba)>>8)&0xff))
504 
506 #define FXBLUEVAL(rgba) ((FX::FXuchar)((rgba)&0xff))
507 
509 #define FXALPHAVAL(rgba) ((FX::FXuchar)(((rgba)>>24)&0xff))
510 
512 #define FXRGBACOMPVAL(rgba,comp) ((FX::FXuchar)(((rgba)>>((3-(comp))<<3))&0xff))
513 
515 #define FXCOLORREF2RGB(ref) (FX::FXuint)((((ref)>>16)&0xff) | ((ref)&0xff00) | (((ref)<<16)&0xff0000) | 0xff000000)
516 
518 #define FXRGB2COLORREF(rgb) (FX::FXuint)((((rgb)<<16)&0xff0000) | ((rgb)&0xff00) | (((rgb)>>16)&0xff))
519 
520 #endif
521 
529 #ifndef NDEBUG
530 #define FXASSERT(exp) (__likely(exp)?((void)0):(void)FX::fxassert(#exp,__FILE__,__LINE__))
531 #else
532 #define FXASSERT(exp) ((void)0)
533 #endif
534 
535 
543 #ifndef NDEBUG
544 #define FXVERIFY(exp) (__likely(exp)?((void)0):(void)FX::fxverify(#exp,__FILE__,__LINE__))
545 #else
546 #define FXVERIFY(exp) ((void)(exp))
547 #endif
548 
549 
556 #if (defined(__cplusplus) && (__cplusplus >= 201103L)) || (defined(_MSC_VER) && (_MSC_VER >= 1600))
557 #define FXASSERT_STATIC(expr) static_assert(expr,#expr)
558 #else
559 #define FXASSERT_STATIC(expr) FXASSERT(expr)
560 #endif
561 
562 
578 #ifndef NDEBUG
579 #define FXTRACE(arguments) FX::fxtrace arguments
580 #else
581 #define FXTRACE(arguments) ((void)0)
582 #endif
583 
590 #define FXMALLOC(ptr,type,no) (FX::fxmalloc((void **)(ptr),sizeof(type)*(no)))
591 
598 #define FXCALLOC(ptr,type,no) (FX::fxcalloc((void **)(ptr),sizeof(type)*(no)))
599 
609 #define FXRESIZE(ptr,type,no) (FX::fxresize((void **)(ptr),sizeof(type)*(no)))
610 
618 #define FXMEMDUP(ptr,src,type,no) (FX::fxmemdup((void **)(ptr),(const void*)(src),sizeof(type)*(no)))
619 
626 #define FXFREE(ptr) (FX::fxfree((void **)(ptr)))
627 
628 /********************************** Globals **********************************/
629 
631 template <typename to_type>
632 static inline to_type* alias_cast(void* ptr){
633  union UNI { to_type dst[1]; };
634  return reinterpret_cast<UNI*>(ptr)->dst;
635  }
636 
638 template <typename to_type>
639 static inline const to_type* alias_cast(const void* ptr){
640  union UNI { to_type dst[1]; };
641  return reinterpret_cast<const UNI*>(ptr)->dst;
642  }
643 
645 extern FXAPI FXbool fxmalloc(void** ptr,FXuval size);
646 
648 extern FXAPI FXbool fxcalloc(void** ptr,FXuval size);
649 
651 extern FXAPI FXbool fxresize(void** ptr,FXuval size);
652 
654 extern FXAPI void fxfree(void** ptr);
655 
657 extern FXAPI FXbool fxmemdup(void** ptr,const void* src,FXuval size);
658 
660 extern FXAPI __noreturn void fxerror(const FXchar* format,...) FX_PRINTF(1,2) ;
661 
663 extern FXAPI void fxwarning(const FXchar* format,...) FX_PRINTF(1,2) ;
664 
666 extern FXAPI void fxmessage(const FXchar* format,...) FX_PRINTF(1,2) ;
667 
669 extern FXAPI void fxassert(const FXchar* expression,const FXchar* filename,unsigned int lineno);
670 
672 extern FXAPI void fxverify(const FXchar* expression,const FXchar* filename,unsigned int lineno);
673 
675 extern FXAPI void fxtrace(FXuint level,const FXchar* format,...) FX_PRINTF(2,3) ;
676 
678 extern FXAPI FXbool fxtoDOS(FXchar*& string,FXint& len);
679 
681 extern FXAPI FXbool fxfromDOS(FXchar*& string,FXint& len);
682 
684 extern FXAPI FXchar *fxstrdup(const FXchar* str);
685 
687 extern FXAPI FXuint fxstrhash(const FXchar* str);
688 
690 extern FXAPI FXival fxstrlcpy(FXchar* dst,const FXchar* src,FXival len);
691 
693 extern FXAPI FXival fxstrlcat(FXchar* dst,const FXchar* src,FXival len);
694 
696 extern FXAPI void fxrgb_to_hsv(FXfloat& h,FXfloat& s,FXfloat& v,FXfloat r,FXfloat g,FXfloat b);
697 
699 extern FXAPI void fxhsv_to_rgb(FXfloat& r,FXfloat& g,FXfloat& b,FXfloat h,FXfloat s,FXfloat v);
700 
702 extern FXAPI void fxrgb_to_hsl(FXfloat& h,FXfloat& s,FXfloat& l,FXfloat r,FXfloat g,FXfloat b);
703 
705 extern FXAPI void fxhsl_to_rgb(FXfloat& r,FXfloat& g,FXfloat& b,FXfloat h,FXfloat s,FXfloat l);
706 
708 extern FXchar* fxencode64(FXchar* dst,FXchar* dstend,const FXchar* src,const FXchar* srcend);
709 
711 extern FXchar* fxdecode64(FXchar* dst,FXchar* dstend,const FXchar* src,const FXchar* srcend);
712 
714 extern FXchar* fxencode85(FXchar* dst,FXchar* dstend,const FXchar* src,const FXchar* srcend);
715 
717 extern FXchar* fxdecode85(FXchar* dst,FXchar* dstend,const FXchar* src,const FXchar* srcend);
718 
720 extern FXAPI FXwchar fxkeysym2ucs(FXwchar sym);
721 
723 extern FXAPI FXwchar fxucs2keysym(FXwchar ucs);
724 
726 extern FXAPI FXint fxparsegeometry(const FXchar *string,FXint& x,FXint& y,FXint& w,FXint& h);
727 
729 extern FXAPI FXbool fxisconsole(const FXchar *path);
730 
732 extern FXAPI const FXuchar fxversion[3];
733 
735 extern FXAPI FXbool getTraceTopic(FXuint topic);
736 
738 extern FXAPI void setTraceTopic(FXuint topic,FXbool flag=true);
739 
741 extern FXAPI void setTraceLevel(FXuint level,FXbool flag=true);
742 
753 extern FXAPI FXbool setTraceTopics(const FXchar* topics,FXbool flag=true);
754 
756 extern FXAPI FXival fxosversion(FXchar version[],FXival len);
757 
758 
759 }
760 
761 #endif
Definition: FX4Splitter.h:28

Copyright © 1997-2022 Jeroen van der Zijp