]> git.evergreen-ils.org Git - OpenSRF.git/blob - src/libstack/osrf_hash.h
added connection oriented statefull session handling to the server stack
[OpenSRF.git] / src / libstack / osrf_hash.h
1 #ifndef OSRF_HASH_H
2 #define OSRF_HASH_H
3
4 #include "opensrf/utils.h"
5 #include "opensrf/string_array.h"
6 #include "osrf_list.h"
7
8 /* 0x100 is a good size for small hashes */
9 #define OSRF_HASH_LIST_SIZE 0x100  /* size of the main hash list */
10
11 /* used internally */
12 #define OSRF_HASH_NODE_FREE(h, n) \
13         if(h && n) { \
14                 if(h->freeItem) h->freeItem(n->key, n->item);\
15                 free(n->key); free(n); \
16         }
17
18
19 struct __osrfHashStruct {
20         osrfList* hash; /* this hash */
21         void (*freeItem) (char* key, void* item);       /* callback for freeing stored items */
22         unsigned int size;
23         osrfStringArray* keys;
24 };
25 typedef struct __osrfHashStruct osrfHash;
26
27 struct _osrfHashNodeStruct {
28         char* key;
29         void* item;
30 };
31 typedef struct _osrfHashNodeStruct osrfHashNode;
32
33
34 struct __osrfHashIteratorStruct {
35         char* current;
36         int currentIdx;
37         osrfHash* hash;
38         osrfStringArray* keys;
39 };
40 typedef struct __osrfHashIteratorStruct osrfHashIterator;
41
42 osrfHashNode* osrfNewHashNode(char* key, void* item);
43 void* osrfHashNodeFree(osrfHash*, osrfHashNode*);
44
45 /**
46   Allocates a new hash object
47   */
48 osrfHash* osrfNewHash();
49
50 /**
51   Sets the given key with the given item
52   if "freeItem" is defined and an item already exists at the given location, 
53   then old item is freed and the new item is put into place.
54   if "freeItem" is not defined and an item already exists, the old item
55   is returned.
56   @return The old item if exists and there is no 'freeItem', returns NULL
57   otherwise
58   */
59 void* osrfHashSet( osrfHash* hash, void* item, const char* key, ... );
60
61 /**
62   Removes an item from the hash.
63   if 'freeItem' is defined it is used and NULL is returned,
64   else the freed item is returned
65   */
66 void* osrfHashRemove( osrfHash* hash, const char* key, ... );
67
68 void* osrfHashGet( osrfHash* hash, const char* key, ... );
69
70
71 /**
72   @return A list of strings representing the keys of the hash. 
73   caller is responsible for freeing the returned string array 
74   with osrfStringArrayFree();
75   */
76 osrfStringArray* osrfHashKeys( osrfHash* hash );
77
78 osrfStringArray* osrfHashKeysInc( osrfHash* hash );
79
80 /**
81   Frees a hash
82   */
83 void osrfHashFree( osrfHash* hash );
84
85 /**
86   @return The number of items in the hash
87   */
88 unsigned long osrfHashGetCount( osrfHash* hash );
89
90
91
92
93 /**
94   Creates a new list iterator with the given list
95   */
96 osrfHashIterator* osrfNewHashIterator( osrfHash* hash );
97
98 int osrfHashIteratorHasNext( osrfHashIterator* itr );
99
100 /**
101   Returns the next non-NULL item in the list, return NULL when
102   the end of the list has been reached
103   */
104 void* osrfHashIteratorNext( osrfHashIterator* itr );
105
106 /**
107   Deallocates the given list
108   */
109 void osrfHashIteratorFree( osrfHashIterator* itr );
110
111 void osrfHashIteratorReset( osrfHashIterator* itr );
112
113 #endif