simap/mapmap.hpp

40 lines
791 B
C++

#ifndef MAPMAP_HPP
#define MAPMAP_HPP
#include <map>
#include <cassert>
#include <memory>
struct mapmap_instance {
std::map<const char *, void *> m;
};
static inline mapmap_instance mapmap_create() {
mapmap_instance ret;
return ret;
}
static inline void* mapmap(void *amap_instance, AMAP_OP op, const char *key, void *ptr) {
mapmap_instance *map = (mapmap_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::map<const char *, void *>());
return (void *)((uint8_t)(NULL) - 1L);
}
}
#endif // MAPMAP_HPP