00001 /* 00002 * libkarmaclient - A C Library to the Karmasphere Reputation Server 00003 * Copyright (C) 2006 Karmasphere <http://www.karmasphere.com/> 00004 * - Shevek <shevek@karmasphere.com> 00005 * - Dave Stewart <dave.stewart@karmasphere.com> 00006 * 00007 * This library is free software; you can redistribute it and/or 00008 * modify it under the terms of the GNU Lesser General Public 00009 * License as published by the Free Software Foundation; either 00010 * version 2.1 of the License, or (at your option) any later version. 00011 * 00012 * This library is distributed in the hope that it will be useful, 00013 * but WITHOUT ANY WARRANTY; without even the implied warranty of 00014 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 00015 * Lesser General Public License for more details. 00016 * 00017 * You should have received a copy of the GNU Lesser General Public 00018 * License along with this library; if not, write to the Free Software 00019 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA 00020 */ 00021 00026 #ifndef INC_KS_COMMON_H 00027 #define INC_KS_COMMON_H 00028 00029 #include "ks_malloc.h" 00030 00034 typedef struct _ks_base_t ks_base_t; 00035 00039 typedef struct _ks_string_t ks_string_t; 00040 00044 typedef void (*ks_destroy_t)(ks_base_t *); 00045 00049 typedef void (*ks_bappend_t)(ks_string_t *, ks_base_t *); 00050 00054 typedef struct _ks_type_t { 00055 const char *name; 00056 ks_destroy_t destroy; 00057 ks_bappend_t bappend; 00058 } ks_type_t; 00059 00063 struct _ks_base_t { 00064 ks_type_t *type; 00065 }; 00066 00073 static inline void 00074 ks_destroy(ks_base_t *b) 00075 { 00076 b->type->destroy(b); 00077 } 00078 00086 static inline void 00087 ks_bappend(ks_string_t *r, ks_base_t *b) 00088 { 00089 b->type->bappend(r, b); 00090 } 00091 00095 typedef void (*ks_error_handler_t)(const char *); 00096 00097 void ks_error_handler_set(ks_error_handler_t); 00098 void ks_error(const char *, ...); 00099 00107 static inline ks_base_t * 00108 ks_cast_error(const char *req, ks_base_t *v) 00109 { 00110 ks_error("Error casting to %s: got %s\n", req, v->type); 00111 return v; 00112 } 00113 00114 /* XXX This doesn't allow us to cast NULL. Do better. */ 00115 #define KS_CAST(t, v) ((t ## _t *) \ 00116 ( \ 00117 ((v)->type == t ## _type()) \ 00118 ? (v) \ 00119 : ks_cast_error(#t, v) \ 00120 ) \ 00121 ) 00122 00123 #endif
1.4.4