1 | // |
---|
2 | // Lol Engine |
---|
3 | // |
---|
4 | // Copyright: (c) 2010-2011 Sam Hocevar <sam@hocevar.net> |
---|
5 | // This program is free software; you can redistribute it and/or |
---|
6 | // modify it under the terms of the Do What The Fuck You Want To |
---|
7 | // Public License, Version 2, as published by Sam Hocevar. See |
---|
8 | // http://sam.zoy.org/projects/COPYING.WTFPL for more details. |
---|
9 | // |
---|
10 | |
---|
11 | #if defined HAVE_CONFIG_H |
---|
12 | # include "config.h" |
---|
13 | #endif |
---|
14 | |
---|
15 | #include <cstring> |
---|
16 | #include <cstdlib> |
---|
17 | |
---|
18 | #include "core.h" |
---|
19 | |
---|
20 | #if defined WIN32 |
---|
21 | # define strcasecmp _stricmp |
---|
22 | #endif |
---|
23 | |
---|
24 | using namespace std; |
---|
25 | |
---|
26 | namespace lol |
---|
27 | { |
---|
28 | |
---|
29 | /* |
---|
30 | * Dict implementation class |
---|
31 | */ |
---|
32 | |
---|
33 | class DictData |
---|
34 | { |
---|
35 | friend class Dict; |
---|
36 | |
---|
37 | public: |
---|
38 | DictData() : |
---|
39 | entities(0), |
---|
40 | maxid(0), |
---|
41 | nentities(0) |
---|
42 | { |
---|
43 | /* Nothing to do */ |
---|
44 | } |
---|
45 | |
---|
46 | ~DictData() |
---|
47 | { |
---|
48 | #if !LOL_RELEASE |
---|
49 | if (nentities) |
---|
50 | Log::Error("still %i entities in dict\n", nentities); |
---|
51 | #endif |
---|
52 | free(entities); |
---|
53 | } |
---|
54 | |
---|
55 | private: |
---|
56 | Entity **entities; |
---|
57 | int maxid, nentities; |
---|
58 | }; |
---|
59 | |
---|
60 | /* |
---|
61 | * Public Dict class |
---|
62 | */ |
---|
63 | |
---|
64 | Dict::Dict() |
---|
65 | : data(new DictData()) |
---|
66 | { |
---|
67 | } |
---|
68 | |
---|
69 | Dict::~Dict() |
---|
70 | { |
---|
71 | delete data; |
---|
72 | } |
---|
73 | |
---|
74 | int Dict::MakeSlot(char const *name) |
---|
75 | { |
---|
76 | int id, empty = -1; |
---|
77 | |
---|
78 | /* If the entry is already registered, remember its ID. Look for an |
---|
79 | * empty slot at the same time. */ |
---|
80 | for (id = 0; id < data->maxid; id++) |
---|
81 | { |
---|
82 | Entity *e = data->entities[id]; |
---|
83 | if (!e) |
---|
84 | { |
---|
85 | empty = id; |
---|
86 | break; |
---|
87 | } |
---|
88 | else |
---|
89 | { |
---|
90 | char const *oldname = e->GetName(); |
---|
91 | if (*oldname == '<') |
---|
92 | { |
---|
93 | while (*oldname && *oldname != '>') |
---|
94 | oldname++; |
---|
95 | while (*oldname == '>') |
---|
96 | oldname++; |
---|
97 | while (*oldname == ' ') |
---|
98 | oldname++; |
---|
99 | } |
---|
100 | |
---|
101 | if (!strcasecmp(name, oldname)) |
---|
102 | break; |
---|
103 | } |
---|
104 | } |
---|
105 | |
---|
106 | /* If this is a new entry, create a new slot for it. */ |
---|
107 | if (id == data->maxid || !data->entities[id]) |
---|
108 | { |
---|
109 | if (id == data->maxid) |
---|
110 | { |
---|
111 | empty = data->maxid++; |
---|
112 | data->entities = (Entity **)realloc(data->entities, |
---|
113 | data->maxid * sizeof(Entity *)); |
---|
114 | } |
---|
115 | |
---|
116 | data->entities[empty] = NULL; |
---|
117 | id = empty; |
---|
118 | data->nentities++; |
---|
119 | } |
---|
120 | else |
---|
121 | { |
---|
122 | Ticker::Ref(data->entities[id]); |
---|
123 | } |
---|
124 | |
---|
125 | return id; |
---|
126 | } |
---|
127 | |
---|
128 | void Dict::RemoveSlot(int id) |
---|
129 | { |
---|
130 | if (Ticker::Unref(data->entities[id]) == 0) |
---|
131 | { |
---|
132 | data->entities[id] = NULL; |
---|
133 | data->nentities--; |
---|
134 | } |
---|
135 | } |
---|
136 | |
---|
137 | void Dict::RemoveSlot(Entity *entity) |
---|
138 | { |
---|
139 | for (int id = 0; id < data->maxid; id++) |
---|
140 | if (data->entities[id] == entity) |
---|
141 | { |
---|
142 | RemoveSlot(id); |
---|
143 | return; |
---|
144 | } |
---|
145 | |
---|
146 | #if !LOL_RELEASE |
---|
147 | Log::Error("removing unregistered entity %p\n", entity); |
---|
148 | #endif |
---|
149 | } |
---|
150 | |
---|
151 | void Dict::SetEntity(int id, Entity *entity) |
---|
152 | { |
---|
153 | Ticker::Ref(entity); |
---|
154 | data->entities[id] = entity; |
---|
155 | } |
---|
156 | |
---|
157 | Entity *Dict::GetEntity(int id) |
---|
158 | { |
---|
159 | return data->entities[id]; |
---|
160 | } |
---|
161 | |
---|
162 | } /* namespace lol */ |
---|
163 | |
---|