1 #ifndef INCLUDED_HASHITEM_H_
2 #define INCLUDED_HASHITEM_H_
3
4 #include "../root/root.h"
5
6 /* =========================================================================
7 `private' functions are called hi_...() and are declared in hashitem.ih.
8
9 A corresponding approach is followed for other structs, mentioned below.
10
11 public functions are called hashitem...()
12
13 */
14 typedef struct
15 {
16 SymbolType d_type; /* type of symbol */
17 char *d_key; /* ascii-z */
18
19 void *d_value; /* points to the value */
20 void (*d_destructor)(void *); /* knows how to destroy d_value */
21 }
22 HashItem;
23
24 HashItem *hashitem_new_destructor(SymbolType type, char const *key,
25 void *value, void (*destructor)(void *));
26 HashItem *hashitem_new(char const *key, SymbolType type);
27 void hashitem_delete(HashItem **hashitem);
28
29 void hashitem_changekey(HashItem *hashitem, char const *key);
30 Result hashitem_pop(HashItem *item);
31 void hashitem_set(HashItem *item, void *value,
32 void (*destructor)(void *));
33
34 /*
35 Internal HashItem use only. Not used outside of this directory, needed here
36 to allow proper compilation of the static inline functions below
37 */
38
39 #include <string.h>
40
41 /* public interface continues from here */
42
43
hashitem_key(HashItem const * item)44 static inline char const *hashitem_key(HashItem const *item)
45 {
46 return item->d_key;
47 }
48
hashitem_setType(register HashItem * item,SymbolType type)49 static inline void hashitem_setType(register HashItem *item, SymbolType type)
50 {
51 item->d_type = type;
52 }
53 /* caller may not free */
54 /* the returned info */
55 /* modifying its contents */
56 /* is ok */
hashitem_value(register HashItem * item)57 static inline void *hashitem_value(register HashItem *item)
58 {
59 return item != PFAILED ? item->d_value : PFAILED;
60 }
61
hashitem_fullType(register HashItem * item)62 static inline SymbolType hashitem_fullType(register HashItem *item)
63 {
64 return item == PFAILED ? UNDEFINED_SYMBOL : item->d_type;
65 }
66
hashitem_iskeytype(HashItem const * hashitem,char const * key,SymbolType type)67 static inline bool hashitem_iskeytype(HashItem const *hashitem,
68 char const *key, SymbolType type)
69 {
70 return (hashitem->d_type & type) && !strcmp(hashitem->d_key, key);
71 }
72
hashitem_type(register HashItem * item)73 static inline SymbolType hashitem_type(register HashItem *item)
74 {
75 return item == PFAILED ? UNDEFINED_SYMBOL : (item->d_type & ANY);
76 }
77
78 #endif
79