2 (C) 1997-98 AROS - The Amiga Research OS
5 Desc: Demo of new OOP system.
11 #include "sysdep/hashed_strings.h"
18 #define ClassID ClassNode.ln_Name
20 static struct Bucket
*CopyBucket(struct Bucket
*old_b
);
21 static VOID
FreeBucket(struct Bucket
*b
);
23 /**********************
25 **********************/
27 static ULONG
NumNewMethods(Class
*cl
, struct InterfaceDescr
*ifDescr
)
30 ULONG numnewmethods
= 0;
32 EnterFunc(bug("NumNewMethods(cl=%s, ifDescr=%p)\n", cl
->ClassID
, ifDescr
));
36 ht
= (struct Bucket
**)cl
->SuperClass
->HashTable
;
37 for (; ifDescr
->MethodTable
; ifDescr
++)
41 for (i
= 0; i
< ifDescr
->NumMethods
; i
++)
44 if (!HashLookupStr(ht
, (IPTR
)ifDescr
->MethodTable
[i
].MethodID
))
56 /* This is rootclass, count the methods */
57 ht
= (struct Bucket
**)cl
->HashTable
;
58 for (; ifDescr
->MethodTable
; ifDescr
++)
60 numnewmethods
+= ifDescr
->NumMethods
;
63 ReturnInt ("NumNewMethods", ULONG
, numnewmethods
);
66 /***************************
67 ** AllocDispatchTales() **
68 ***************************/
70 BOOL
AllocDispatchTables(Class
*cl
, struct InterfaceDescr
*ifDescr
)
74 EnterFunc(bug("AllocDispatchTables(cl=%s,ifDescr=%p)\n",
75 cl
->ClassID
, ifDescr
));
77 entries
= NumNewMethods(cl
, ifDescr
);
79 cl
->HashTable
= (struct MethodBucket
**)NewHash(entries
);
82 /* Save hashtable mask for speed */
83 cl
->HashMask
= HashMask(cl
->HashTable
);
87 /* Copy the superclass hash table */
88 if ( !CopyHash((struct Bucket
**)cl
->HashTable
89 ,(struct Bucket
**)cl
->SuperClass
->HashTable
97 /* Insert our own methods */
98 for (; ifDescr
->MethodTable
; ifDescr
++)
100 struct MethodDescr
*mtab
= ifDescr
->MethodTable
;
103 for (i
= 0; i
< ifDescr
->NumMethods
; i
++)
105 struct MethodBucket
*b
;
107 /* Method existed in superclass ? */
108 b
= (struct MethodBucket
*)HashLookupStr((struct Bucket
**)cl
->HashTable
109 ,(IPTR
)mtab
[i
].MethodID
);
112 b
->MethodFunc
= mtab
[i
].MethodFunc
;
117 /* Must allocate new bucket */
118 struct MethodBucket
*new_b
;
120 D(bug("Inserting method %s\n", mtab
[i
].MethodID
));
122 new_b
= (struct MethodBucket
*)malloc( sizeof (struct MethodBucket
) );
127 /* Initialize bucket */
128 new_b
->MethodID
= mtab
[i
].MethodID
;
129 new_b
->MethodFunc
= mtab
[i
].MethodFunc
;
132 /* Add bucket to hashtable */
133 InsertBucket((struct Bucket
**)cl
->HashTable
, (struct Bucket
*)new_b
);
136 } /* for (each method in methodtable) */
138 } /* for (each interface) */
141 ReturnBool ("AllocDispatchTables", TRUE
);
143 FreeHash((struct Bucket
**)cl
->HashTable
, FreeBucket
);
144 ReturnBool ("AllocDispatchTables", FALSE
);
148 VOID
FreeDispatchTables(Class
*cl
)
150 FreeHash((struct Bucket
**)cl
->HashTable
, FreeBucket
);
156 /**************************
157 ** Hash handling hooks **
158 **************************/
159 #define MB(x) ((struct MethodBucket *)x)
160 static struct Bucket
*CopyBucket(struct Bucket
*old_b
)
162 struct MethodBucket
*new_b
;
164 new_b
= (struct MethodBucket
*)malloc(sizeof (struct MethodBucket
) );
167 new_b
->MethodID
= MB(old_b
)->MethodID
;
168 new_b
->MethodFunc
= MB(old_b
)->MethodFunc
;
169 new_b
->mClass
= MB(old_b
)->mClass
;
170 return ((struct Bucket
*)new_b
);
175 static VOID
FreeBucket(struct Bucket
*b
)