inherit.c
Go to the documentation of this file.00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029 #ifdef HAVE_CONFIG_H
00030 # include <config.h>
00031 #endif
00032
00033 #define DISABLE_DEBUGLOG
00034
00035 #include "inherit_p.h"
00036 #include <gwenhywfar/misc.h>
00037 #include <gwenhywfar/debug.h>
00038 #include <gwenhywfar/gwenhywfarapi.h>
00039
00040 #include <string.h>
00041
00042
00043
00044 GWEN_LIST_FUNCTIONS(GWEN_INHERITDATA, GWEN_InheritData)
00045
00046
00047
00048 GWEN_INHERITDATA *GWEN_InheritData_new(const char *t,
00049 uint32_t id,
00050 void *data,
00051 void *baseData,
00052 GWEN_INHERIT_FREEDATAFN freeDataFn){
00053 GWEN_INHERITDATA *d;
00054
00055 assert(t);
00056 GWEN_NEW_OBJECT(GWEN_INHERITDATA, d);
00057 GWEN_LIST_INIT(GWEN_INHERITDATA, d);
00058 d->typeName=strdup(t);
00059 d->id=id;
00060 d->data=data;
00061 d->baseData=baseData;
00062 d->freeDataFn=freeDataFn;
00063
00064 DBG_VERBOUS(GWEN_LOGDOMAIN,
00065 "Created inheritance for type \"%s\" (%08x)", t, id);
00066 return d;
00067 }
00068
00069
00070
00071 void GWEN_InheritData_free(GWEN_INHERITDATA *d) {
00072 if (d) {
00073 if (d->freeDataFn)
00074 d->freeDataFn(d->baseData, d->data);
00075 free(d->typeName);
00076 GWEN_LIST_FINI(GWEN_INHERITDATA, d);
00077 GWEN_FREE_OBJECT(d);
00078 }
00079 }
00080
00081
00082
00083 void GWEN_InheritData_freeData(GWEN_INHERITDATA *d) {
00084 if (d) {
00085 DBG_VERBOUS(GWEN_LOGDOMAIN,
00086 "Freeing data for type \"%s\"",
00087 d->typeName);
00088 if (d->freeDataFn)
00089 d->freeDataFn(d->baseData, d->data);
00090 d->freeDataFn=NULL;
00091 d->data=NULL;
00092 }
00093 }
00094
00095
00096
00097 void GWEN_InheritData_clear(GWEN_INHERITDATA *d){
00098 assert(d);
00099 d->freeDataFn=0;
00100 d->data=0;
00101 }
00102
00103
00104
00105 const char *GWEN_InheritData_GetTypeName(const GWEN_INHERITDATA *d){
00106 assert(d);
00107 return d->typeName;
00108 }
00109
00110
00111
00112 uint32_t GWEN_InheritData_GetId(const GWEN_INHERITDATA *d){
00113 assert(d);
00114 return d->id;
00115 }
00116
00117
00118
00119 void *GWEN_InheritData_GetData(const GWEN_INHERITDATA *d){
00120 assert(d);
00121 return d->data;
00122 }
00123
00124
00125
00126 GWEN_INHERIT_FREEDATAFN
00127 GWEN_InheritData_GetFreeDataFn(const GWEN_INHERITDATA *d){
00128 assert(d);
00129 return d->freeDataFn;
00130 }
00131
00132
00133
00134
00135
00136 uint32_t GWEN_Inherit_MakeId(const char *typeName){
00137 unsigned int i, j;
00138 uint32_t result;
00139
00140 result=0;
00141 j=strlen(typeName);
00142 for (i=0; i<j; i++) {
00143 uint32_t tmpResult;
00144 unsigned char c;
00145
00146 tmpResult=result<<8;
00147 c=((result>>24)&0xff);
00148 result=tmpResult|c;
00149 result^=(unsigned char)(typeName[i]);
00150 }
00151
00152 DBG_VERBOUS(GWEN_LOGDOMAIN,
00153 "Id for type \"%s\" is \"%08x\"",
00154 typeName, result);
00155 return result;
00156 }
00157
00158
00159
00160 void *GWEN_Inherit_FindData(GWEN_INHERITDATA_LIST *l,
00161 uint32_t id,
00162 int wantCreate){
00163 GWEN_INHERITDATA *ih;
00164
00165 assert(l);
00166
00167 DBG_VERBOUS(GWEN_LOGDOMAIN,
00168 "Searching for inheritance id \"%08x\"", id);
00169 ih=GWEN_InheritData_List_First(l);
00170 while(ih) {
00171 DBG_VERBOUS(GWEN_LOGDOMAIN,
00172 "Checking type \"%s\" (%08x) against %08x",
00173 ih->typeName, ih->id, id);
00174 if (ih->id==id)
00175 return ih->data;
00176 ih=GWEN_InheritData_List_Next(ih);
00177 }
00178 if (!wantCreate) {
00179 DBG_WARN(GWEN_LOGDOMAIN,
00180 "Type \"%08x\" not derived from this base type", id);
00181 }
00182 return 0;
00183 }
00184
00185
00186
00187 GWEN_INHERITDATA *GWEN_Inherit_FindEntry(GWEN_INHERITDATA_LIST *l,
00188 uint32_t id,
00189 int wantCreate){
00190 GWEN_INHERITDATA *ih;
00191
00192 assert(l);
00193
00194 DBG_VERBOUS(GWEN_LOGDOMAIN, "Searching for inheritance id \"%08x\"", id);
00195 ih=GWEN_InheritData_List_First(l);
00196 while(ih) {
00197 DBG_VERBOUS(GWEN_LOGDOMAIN, "Checking type \"%s\" (%08x) against %08x",
00198 ih->typeName, ih->id, id);
00199 if (ih->id==id)
00200 return ih;
00201 ih=GWEN_InheritData_List_Next(ih);
00202 }
00203 if (!wantCreate) {
00204 DBG_WARN(GWEN_LOGDOMAIN,
00205 "Type \"%08x\" not derived from this base type", id);
00206 }
00207 return 0;
00208 }
00209
00210
00211
00212
00213