major reorganisation
[rrq/rrqmisc.git] / typing / ItemKeyFun.c
1 /**
2  * Trampoline functions for the ItemKeyFun callbacks.
3  */
4
5 #include <assert.h>
6 #include <ItemKeyFun.h>
7
8 /**
9  * \brief Trampoline function for the ItemKeyFun.hashcode callback.
10  *
11  * The default is to use the pointer itself.
12  */
13 unsigned long ItemKeyFun_hashcode(void *this,void *key) {
14     ItemKeyFun *type = (ItemKeyFun*) this;
15     assert( type );
16     return type->hashcode? type->hashcode( type, key ) : (unsigned long) key;
17 }
18
19 /**
20  * \brief Trampoline function for the ItemKeyFun.itemkey callback.
21  */
22 void *ItemKeyFun_itemkey(void *this,void *item) {
23     ItemKeyFun *type = (ItemKeyFun*) this;
24     assert( type );
25     return type->itemkey? type->itemkey( type, item ) : item;
26 }
27
28 /**
29  * \brief Trampoline function for the ItemKeyFun.haskey callback.
30  */
31 int ItemKeyFun_haskey(void *this,void *item,void *key) {
32     ItemKeyFun *type = (ItemKeyFun*) this;
33     assert( type );
34     if ( type->haskey ) {
35         return type->haskey( this, item, key );
36     }
37     void *ikey = ItemKeyFun_itemkey( this, item );
38     int n = ikey == key;
39     ItemKeyFun_releasekey( this, ikey );
40     return n;
41 }
42
43 /**
44  * \brief Trampoline function for the ItemKeyFun.releasekey callback.
45  */
46 void ItemKeyFun_releasekey(void *this,void *key) {
47     ItemKeyFun *type = (ItemKeyFun*) this;
48     assert( type );
49     if ( type->releasekey ) {
50         type->releasekey( type, key );
51     }
52 }
53
54 /**
55  * \brief Trampoline function for the ItemKeyFun.tostring callback.
56  */
57 int ItemKeyFun_tostring(void *this,void *item,char *buffer,int limit) {
58     ItemKeyFun *type = (ItemKeyFun*) this;
59     assert( type );
60     if ( type->tostring ) {
61         return type->tostring( type, item, buffer, limit );
62     }
63     void *key = ItemKeyFun_itemkey( this, item );
64     int n = snprintf( buffer, limit, "{%p/%p@%p}", type, key, item );
65     ItemKeyFun_releasekey( this, key );
66     return n;
67 }