40 lines
821 B
C++
40 lines
821 B
C++
#ifndef UNOMAP_HPP
|
|
#define UNOMAP_HPP
|
|
|
|
#include <unordered_map>
|
|
#include <cassert>
|
|
#include <memory>
|
|
|
|
struct unomap_instance {
|
|
std::unordered_map<const char *, void *> m;
|
|
};
|
|
|
|
static inline unomap_instance unomap_create() {
|
|
unomap_instance ret;
|
|
return ret;
|
|
}
|
|
|
|
static inline void* unomap(void *amap_instance, AMAP_OP op, const char *key, void *ptr) {
|
|
unomap_instance *map = (unomap_instance *) amap_instance;
|
|
if(op == AMAP_GET) {
|
|
try {
|
|
return map->m[key];
|
|
} catch(...) {
|
|
return ptr;
|
|
}
|
|
} else if(op == AMAP_SET) {
|
|
try {
|
|
map->m[key] = ptr;
|
|
return map; // non-null
|
|
} catch(...) {
|
|
return NULL;
|
|
}
|
|
} else { // if(op == AMAP_ERASE) {
|
|
assert(op == AMAP_ERASE);
|
|
map->m = std::move(std::unordered_map<const char *, void *>());
|
|
return (void *)((uint8_t)(NULL) - 1L);
|
|
}
|
|
}
|
|
|
|
#endif // MAPMAP_HPP
|