C Standard Library Extensions
1.1
|
00001 /* $Id: cxmacros.h,v 1.7 2011/02/21 14:15:31 rpalsa Exp $ 00002 * 00003 * This file is part of the ESO C Extension Library 00004 * Copyright (C) 2001-2011 European Southern Observatory 00005 * 00006 * This program is free software; you can redistribute it and/or modify 00007 * it under the terms of the GNU General Public License as published by 00008 * the Free Software Foundation; either version 2 of the License, or 00009 * (at your option) any later version. 00010 * 00011 * This program is distributed in the hope that it will be useful, 00012 * but WITHOUT ANY WARRANTY; without even the implied warranty of 00013 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 00014 * GNU General Public License for more details. 00015 * 00016 * You should have received a copy of the GNU General Public License 00017 * along with this program; if not, write to the Free Software 00018 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA 00019 */ 00020 00021 /* 00022 * $Author: rpalsa $ 00023 * $Date: 2011/02/21 14:15:31 $ 00024 * $Revision: 1.7 $ 00025 * $Name: cpl-6_0 $ 00026 */ 00027 00028 00029 /* 00030 * This file MUST not include any other cext header file. 00031 */ 00032 00033 #ifndef CX_MACROS_H 00034 #define CX_MACROS_H 00035 00036 00037 /* 00038 * Get the system's definition of NULL from stddef.h 00039 */ 00040 00041 #include <stddef.h> 00042 00043 00044 /* 00045 * An alias for __extension__ 00046 */ 00047 00048 #if __GNUC__ > 2 || (__GNUC__ == 2 && __GNUC_MINOR__ >= 8) 00049 # define CX_GNUC_EXTENSION __extension__ 00050 #else 00051 # define CX_GNUC_EXTENSION 00052 #endif 00053 00054 00055 /* 00056 * Macros for the GNU compiler function attributes 00057 */ 00058 00059 #if __GNUC__ > 2 || (__GNUC__ == 2 && __GNUC_MINOR__ >= 96) 00060 # define CX_GNUC_PURE __attribute__((__pure__)) 00061 # define CX_GNUC_MALLOC __attribute__((__malloc__)) 00062 #else 00063 # define G_GNUC_PURE 00064 # define G_GNUC_MALLOC 00065 #endif 00066 00067 #if __GNUC__ > 2 || (__GNUC__ == 2 && __GNUC_MINOR__ > 4) 00068 # define CX_GNUC_PRINTF(fmt_idx, arg_idx) \ 00069 __attribute__((__format__ (__printf__, fmt_idx, arg_idx))) 00070 # define CX_GNUC_SCANF(fmt_idx, arg_idx) \ 00071 __attribute__((__format__ (__scanf__, fmt_idx, arg_idx))) 00072 # define CX_GNUC_FORMAT(arg_idx) __attribute__((__format_arg__ (arg_idx))) 00073 # define CX_GNUC_NORETURN __attribute__((__noreturn__)) 00074 # define CX_GNUC_CONST __attribute__((__const__)) 00075 # define CX_GNUC_UNUSED __attribute__((__unused__)) 00076 #else 00077 # define CX_GNUC_PRINTF(fmt_idx, arg_idx) 00078 # define CX_GNUC_SCANF(fmt_idx, arg_idx) 00079 # define CX_GNUC_FORMAT(arg_idx) 00080 # define CX_GNUC_NORETURN 00081 # define CX_GNUC_CONST 00082 # define CX_GNUC_UNUSED 00083 #endif 00084 00085 00086 /* 00087 * Wrap the gcc __PRETTY_FUNCTION__ and __FUNCTION__ variables with macros. 00088 */ 00089 00090 #if defined (__GNUC__) && (__GNUC__ < 3) 00091 # define CX_GNUC_FUNCTION __FUNCTION__ 00092 # define CX_GNUC_PRETTY_FUNCTION __PRETTY_FUNCTION__ 00093 #else /* !__GNUC__ */ 00094 # define CX_GNUC_FUNCTION "" 00095 # define CX_GNUC_PRETTY_FUNCTION "" 00096 #endif /* !__GNUC__ */ 00097 00098 #define CX_STRINGIFY(macro) CX_STRINGIFY_ARG(macro) 00099 #define CX_STRINGIFY_ARG(contents) #contents 00100 00101 00102 /* 00103 * String identifier for the current code position 00104 */ 00105 00106 #if defined (__GNUC__) && (__GNUC__ < 3) 00107 # define CX_CODE_POS __FILE__ ":" CX_STRINGIFY(__LINE__) ":" __PRETTY_FUNCTION__ "()" 00108 #else 00109 # define CX_CODE_POS __FILE__ ":" CX_STRINGIFY(__LINE__) 00110 #endif 00111 00112 00113 /* 00114 * Current function identifier 00115 */ 00116 #if defined (__GNUC__) 00117 # define CX_FUNC_NAME ((const char*) (__PRETTY_FUNCTION__)) 00118 #elif defined (__STDC_VERSION__) && __STDC_VERSION__ >= 19901L 00119 # define CX_FUNC_NAME ((const char*) (__func__)) 00120 #else 00121 # define CX_FUNC_NAME ((const char*) ("???")) 00122 #endif 00123 00124 00125 /* 00126 * C code guard 00127 */ 00128 00129 #undef CX_BEGIN_DECLS 00130 #undef CX_END_DECLS 00131 00132 #ifdef __cplusplus 00133 # define CX_BEGIN_DECLS extern "C" { 00134 # define CX_END_DECLS } 00135 #else 00136 # define CX_BEGIN_DECLS /* empty */ 00137 # define CX_END_DECLS /* empty */ 00138 #endif 00139 00140 00141 /* 00142 * Some popular macros. If the system provides already a definition for some 00143 * of them this definition is used, assuming the definition is correct. 00144 */ 00145 00146 #ifndef NULL 00147 # ifdef __cplusplus 00148 # define NULL (0L) 00149 # else /* !__cplusplus */ 00150 # define NULL ((void *) 0) 00151 # endif /* !__cplusplus */ 00152 #endif 00153 00154 #ifndef FALSE 00155 # define FALSE (0) 00156 #endif 00157 00158 #ifndef TRUE 00159 # define TRUE (!FALSE) 00160 #endif 00161 00162 #ifndef CX_MIN 00163 # define CX_MIN(a, b) ((a) < (b) ? (a) : (b)) 00164 #endif 00165 00166 #ifndef CX_MAX 00167 # define CX_MAX(a, b) ((a) > (b) ? (a) : (b)) 00168 #endif 00169 00170 #ifndef CX_ABS 00171 # define CX_ABS(a) ((a) < (0) ? -(a) : (a)) 00172 #endif 00173 00174 #ifndef CX_CLAMP 00175 # define CX_CLAMP(a, low, high) (((a) > (high)) ? (high) : (((a) < (low)) ? (low) : (a))) 00176 #endif 00177 00178 00179 /* 00180 * Number of elements in an array 00181 */ 00182 00183 #define CX_N_ELEMENTS(array) (sizeof (array) / sizeof ((array)[0])) 00184 00185 #endif /* CX_MACROS_H */