00001 /* 00002 * $Id: casts.h 13160 2007-03-20 15:15:45Z cbiere $ 00003 * 00004 * Copyright (c) 2006 Christian Biere 00005 * 00006 *---------------------------------------------------------------------- 00007 * This file is part of gtk-gnutella. 00008 * 00009 * gtk-gnutella is free software; you can redistribute it and/or modify 00010 * it under the terms of the GNU General Public License as published by 00011 * the Free Software Foundation; either version 2 of the License, or 00012 * (at your option) any later version. 00013 * 00014 * gtk-gnutella is distributed in the hope that it will be useful, 00015 * but WITHOUT ANY WARRANTY; without even the implied warranty of 00016 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 00017 * GNU General Public License for more details. 00018 * 00019 * You should have received a copy of the GNU General Public License 00020 * along with gtk-gnutella; if not, write to the Free Software 00021 * Foundation, Inc.: 00022 * 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA 00023 *---------------------------------------------------------------------- 00024 */ 00025 00035 #ifndef _casts_h_ 00036 #define _casts_h_ 00037 00038 /* @note This file is only for inclusion by common.h. */ 00039 00045 static inline G_GNUC_CONST WARN_UNUSED_RESULT ALWAYS_INLINE gboolean * 00046 deconstify_gboolean(const gboolean *p) 00047 { 00048 return (gboolean *) p; 00049 } 00050 00051 static inline G_GNUC_CONST WARN_UNUSED_RESULT ALWAYS_INLINE gchar * 00052 deconstify_gchar(const gchar *p) 00053 { 00054 return (gchar *) p; 00055 } 00056 00057 static inline G_GNUC_CONST WARN_UNUSED_RESULT ALWAYS_INLINE guint32 * 00058 deconstify_guint32(const guint32 *p) 00059 { 00060 return (guint32 *) p; 00061 } 00062 00063 static inline G_GNUC_CONST WARN_UNUSED_RESULT ALWAYS_INLINE gpointer 00064 deconstify_gpointer(gconstpointer p) 00065 { 00066 return (gpointer) p; 00067 } 00068 00069 static inline G_GNUC_CONST WARN_UNUSED_RESULT ALWAYS_INLINE gconstpointer 00070 cast_to_gconstpointer(gconstpointer p) 00071 { 00072 return p; 00073 } 00074 00075 static inline G_GNUC_CONST WARN_UNUSED_RESULT ALWAYS_INLINE gpointer 00076 cast_to_gpointer(gpointer p) 00077 { 00078 return p; 00079 } 00080 00081 static inline G_GNUC_CONST WARN_UNUSED_RESULT ALWAYS_INLINE gchar * 00082 cast_to_gchar_ptr(gpointer p) 00083 { 00084 return p; 00085 } 00086 00087 static inline G_GNUC_CONST WARN_UNUSED_RESULT ALWAYS_INLINE guchar * 00088 cast_to_guchar_ptr(gpointer p) 00089 { 00090 return p; 00091 } 00092 00096 static inline G_GNUC_CONST WARN_UNUSED_RESULT ALWAYS_INLINE gulong 00097 cast_ptr_to_uintptr(gpointer p) 00098 { 00099 gulong u = (gulong) p; 00100 STATIC_ASSERT(sizeof u >= sizeof p); 00101 return u; 00102 } 00103 00107 static inline G_GNUC_CONST WARN_UNUSED_RESULT ALWAYS_INLINE gpointer 00108 cast_uintptr_to_ptr(gulong u) 00109 { 00110 gpointer p = (gpointer) u; 00111 STATIC_ASSERT(sizeof p >= sizeof u); 00112 return p; 00113 } 00114 00115 typedef void (*func_ptr_t)(void); 00116 00117 static inline G_GNUC_CONST WARN_UNUSED_RESULT ALWAYS_INLINE gpointer 00118 cast_func_to_gpointer(func_ptr_t f) 00119 { 00120 return (gpointer) f; 00121 } 00122 00123 static inline G_GNUC_CONST WARN_UNUSED_RESULT ALWAYS_INLINE func_ptr_t 00124 cast_gpointer_to_func(gconstpointer p) 00125 { 00126 return (func_ptr_t) p; 00127 } 00128 00129 static inline size_t G_GNUC_CONST WARN_UNUSED_RESULT ALWAYS_INLINE 00130 ptr_diff(gconstpointer a, gconstpointer b) 00131 { 00132 return (gchar *) a - (gchar *) b; 00133 } 00134 00140 static inline off_t G_GNUC_CONST WARN_UNUSED_RESULT ALWAYS_INLINE 00141 filesize_to_off_t(filesize_t pos) 00142 { 00143 off_t offset = pos > OFF_T_MAX ? (off_t) -1 : (off_t) pos; 00144 00145 /* Handle -1 explicitly just in case there might be platform with 00146 * an non-standard unsigned off_t. 00147 */ 00148 if ((off_t) -1 == offset || offset < 0) { 00149 return (off_t) -1; 00150 } 00151 return offset; 00152 } 00153 00159 #if defined(UINTMAX_C) 00160 #define UNSIGNED(value) ((value) + (UINTMAX_C(0))) 00161 #elif defined(UINTMAX_MAX) 00162 #define UNSIGNED(value) ((value) + (UINTMAX_MAX - UINTMAX_MAX)) 00163 #else 00164 #define UNSIGNED(value) ((value) + ((guint64)0U)) 00165 #endif /* UINTMAX_C */ 00166 00167 #endif /* _casts_h_ */ 00168 00169 /* vi: set ts=4 sw=4 cindent: */
1.3.9.1