default to identity-hash
[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     return ( type && type->itemkey )? type->itemkey( type, item ) : item;
25 }
26
27 /**
28  * \brief Trampoline function for the ItemKeyFun.haskey callback.
29  */
30 int ItemKeyFun_haskey(void *this,void *item,void *key) {
31     ItemKeyFun *type = (ItemKeyFun*) this;
32     if ( type && type->haskey ) {
33         return type->haskey( this, item, key );
34     }
35     void *ikey = ItemKeyFun_itemkey( this, item );
36     int n = ikey == key;
37     ItemKeyFun_releasekey( this, ikey );
38     return n;
39 }
40
41 /**
42  * \brief Trampoline function for the ItemKeyFun.releasekey callback.
43  */
44 void ItemKeyFun_releasekey(void *this,void *key) {
45     ItemKeyFun *type = (ItemKeyFun*) this;
46     if( type && type->releasekey ) {
47         type->releasekey( type, key );
48     }
49 }
50
51 /**
52  * \brief Trampoline function for the ItemKeyFun.tostring callback.
53  */
54 int ItemKeyFun_tostring(void *this,void *item,char *buffer,int limit) {
55     ItemKeyFun *type = (ItemKeyFun*) this;
56     if( type && type->tostring ) {
57         return type->tostring( type, item, buffer, limit );
58     }
59     void *key = ItemKeyFun_itemkey( this, item );
60     int n = snprintf( buffer, limit, "{%p/%p@%p}", type, key, item );
61     ItemKeyFun_releasekey( this, key );
62     return n;
63 }