cutes/dictionary.h

47 lines
1.9 KiB
C
Raw Normal View History

#ifndef CUTES_DICTIONARY_H
#define CUTES_DICTIONARY_H
2024-01-25 13:08:23 +00:00
#include "list.h"
typedef struct Dictionary {
List list;
size_t element_size;
} Dictionary;
// returns a newly initialized dictionary
extern Dictionary dictionary_new(size_t element_size);
// gets a voidptr to the value identified by key
extern void* dictionary_get_raw(Dictionary* self, const char* key);
// inserts or overwrites a new entry identified by key
extern int dictionary_set_raw(Dictionary* self, const char* key, void* data);
// remove entry identified by key from dictionary
extern int dictionary_erase(Dictionary* self, const char* key);
// clear the contents of the dictionary
// erases all heap-allocated memory, the dictionary can be safely freed after
extern void dictionary_empty(Dictionary* self);
// find out if the dictionary contains a value identified by this key
// returns 1 if the key was found
// returns 0 if the key was not found
extern int dictionary_has_key(Dictionary* self, const char* key);
// try to find value identified as key in the dictionary.
// return 1 if the value was found, 0 if not.
// if dictionary_try_get returns 0, memory at out will be unchanged,
// if dictionary_try_get returns 1, found value will be copied into out
extern int dictionary_try_get(Dictionary* self, const char* key, void* out);
// insert or override value identified by key from a value type (supports rvalues)
#define dictionary_set_value(Type_, Dict_, Key_, Value_)\
{ Type_ __value = (Type_)Value_; dictionary_set_raw(Dict_, Key_, &__value); }
// initialize dictionary to fit values of Type_
#define dictionary_from_type(Type_)\
dictionary_new(sizeof(Type_))
// get a pointer to value identified by Key_
#define dictionary_get_as(Type_, Dict_, Key_)\
(Type_*)dictionary_get_raw(Dict_, Key_)
// get dereferenced value identified by Key_
#define dictionary_get_value(Type_, Dict_, Key_)\
*((Type_*)dictionary_get_raw(Dict_, Key_))
#endif // !CUTES_DICTIONARY_H