source: trunk/src/dict.cpp @ 2506

Last change on this file since 2506 was 2506, checked in by sam, 7 years ago

base: start removing occurrences of NULL on our long journey to nullptr.

  • Property svn:keywords set to Id
File size: 3.0 KB
Line 
1//
2// Lol Engine
3//
4// Copyright: (c) 2010-2013 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://www.wtfpl.net/ 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 || defined _XBOX
21#   define strcasecmp _stricmp
22#endif
23
24using namespace std;
25
26namespace lol
27{
28
29/*
30 * Dict implementation class
31 */
32
33class DictData
34{
35    friend class Dict;
36
37public:
38    DictData() :
39        nentities(0)
40    {
41        /* Nothing to do */
42    }
43
44    ~DictData()
45    {
46#if !LOL_RELEASE
47        if (nentities)
48            Log::Error("still %i entities in dict\n", nentities);
49#endif
50    }
51
52private:
53    Array<Entity *> m_entities;
54    int nentities;
55};
56
57/*
58 * Public Dict class
59 */
60
61Dict::Dict()
62  : data(new DictData())
63{
64}
65
66Dict::~Dict()
67{
68    delete data;
69}
70
71int Dict::MakeSlot(char const *name)
72{
73    int slotid, empty = -1;
74
75    /* If the entry is already registered, remember its ID. Look for an
76     * empty slot at the same time. */
77    for (slotid = 0; slotid < data->m_entities.Count(); slotid++)
78    {
79        Entity *e = data->m_entities[slotid];
80        if (!e)
81        {
82            empty = slotid;
83            break;
84        }
85        else
86        {
87            char const *oldname = e->GetName();
88            if (*oldname == '<')
89            {
90                while (*oldname && *oldname != '>')
91                    oldname++;
92                while (*oldname == '>')
93                    oldname++;
94                while (*oldname == ' ')
95                    oldname++;
96            }
97
98            if (!strcasecmp(name, oldname))
99                break;
100        }
101    }
102
103    /* If this is a new entry, create a new slot for it. */
104    if (slotid == data->m_entities.Count() || !data->m_entities[slotid])
105    {
106        if (slotid == data->m_entities.Count())
107        {
108            empty = data->m_entities.Count();
109            data->m_entities.Push(nullptr);
110        }
111
112        data->m_entities[empty] = nullptr;
113        slotid = empty;
114        data->nentities++;
115    }
116    else
117    {
118        Ticker::Ref(data->m_entities[slotid]);
119    }
120
121    return slotid;
122}
123
124void Dict::RemoveSlot(int slotid)
125{
126    if (Ticker::Unref(data->m_entities[slotid]) == 0)
127    {
128        data->m_entities[slotid] = nullptr;
129        data->nentities--;
130    }
131}
132
133void Dict::RemoveSlot(Entity *entity)
134{
135    for (int slotid = 0; slotid < data->m_entities.Count(); slotid++)
136        if (data->m_entities[slotid] == entity)
137        {
138            RemoveSlot(slotid);
139            return;
140        }
141
142#if !LOL_RELEASE
143    Log::Error("removing unregistered entity %p (%s)\n",
144               entity, entity->GetName());
145#endif
146}
147
148void Dict::SetEntity(int slotid, Entity *entity)
149{
150    Ticker::Ref(entity);
151    data->m_entities[slotid] = entity;
152}
153
154Entity *Dict::GetEntity(int slotid)
155{
156    return data->m_entities[slotid];
157}
158
159} /* namespace lol */
160
Note: See TracBrowser for help on using the repository browser.