aboutsummaryrefslogtreecommitdiff
path: root/src/libui_sdl/libui/darwin/map.m
blob: 46a7b8d242a8f5f066b28c4f5f0b9d89143c7501 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
// 17 august 2015
#import "uipriv_darwin.h"

// unfortunately NSMutableDictionary copies its keys, meaning we can't use it for pointers
// hence, this file
// we could expose a NSMapTable directly, but let's treat all pointers as opaque and hide the implementation, just to be safe and prevent even more rewrites later
struct mapTable {
	NSMapTable *m;
};

struct mapTable *newMap(void)
{
	struct mapTable *m;

	m = uiNew(struct mapTable);
	m->m = [[NSMapTable alloc] initWithKeyOptions:(NSPointerFunctionsOpaqueMemory | NSPointerFunctionsOpaquePersonality)
		valueOptions:(NSPointerFunctionsOpaqueMemory | NSPointerFunctionsOpaquePersonality)
		capacity:0];
	return m;
}

void mapDestroy(struct mapTable *m)
{
	if ([m->m count] != 0)
		implbug("attempt to destroy map with items inside");
	[m->m release];
	uiFree(m);
}

void *mapGet(struct mapTable *m, void *key)
{
	return NSMapGet(m->m, key);
}

void mapSet(struct mapTable *m, void *key, void *value)
{
	NSMapInsert(m->m, key, value);
}

void mapDelete(struct mapTable *m, void *key)
{
	NSMapRemove(m->m, key);
}

void mapWalk(struct mapTable *m, void (*f)(void *key, void *value))
{
	NSMapEnumerator e = NSEnumerateMapTable(m->m);
	void *k = NULL;
	void *v = NULL;
	while (NSNextMapEnumeratorPair(&e, &k, &v)) {
		f(k, v);
	}
	NSEndMapTableEnumeration(&e);
}

void mapReset(struct mapTable *m)
{
	NSResetMapTable(m->m);
}