source: trunk/src/dict.cpp @ 179

Last change on this file since 179 was 179, checked in by sam, 9 years ago

Fix an erroneus warning message in the Dict destructor.

  • Property svn:keywords set to Id
File size: 1.9 KB
Line 
1//
2// Deus Hax (working title)
3// Copyright (c) 2010 Sam Hocevar <sam@hocevar.net>
4//
5
6#if defined HAVE_CONFIG_H
7#   include "config.h"
8#endif
9
10#include <cstring>
11#include <cstdio>
12#include <cstdlib>
13
14#include "core.h"
15
16#if defined WIN32
17#   define strcasecmp _stricmp
18#endif
19
20/*
21 * Dict implementation class
22 */
23
24class DictData
25{
26    friend class Dict;
27
28public:
29    DictData() :
30        entities(0),
31        nentities(0)
32    {
33        /* Nothing to do */
34    }
35
36    ~DictData()
37    {
38        if (nentities)
39            fprintf(stderr, "ERROR: still %i entities in dict\n", nentities);
40        free(entities);
41    }
42
43private:
44    Entity **entities;
45    int nentities;
46};
47
48/*
49 * Public Dict class
50 */
51
52Dict::Dict()
53{
54    data = new DictData();
55}
56
57Dict::~Dict()
58{
59    delete data;
60}
61
62int Dict::MakeSlot(char const *name)
63{
64    int id, empty = -1;
65
66    /* If the entry is already registered, remember its ID. Look for an
67     * empty slot at the same time. */
68    for (id = 0; id < data->nentities; id++)
69    {
70        Entity *e = data->entities[id];
71        if (!e)
72            empty = id;
73        else if (!strcasecmp(name, e->GetName()))
74            break;
75    }
76
77    /* If this is a new entry, create a new slot for it. */
78    if (id == data->nentities)
79    {
80        if (empty == -1)
81        {
82            empty = data->nentities++;
83            data->entities = (Entity **)realloc(data->entities,
84                                           data->nentities * sizeof(Entity *));
85        }
86
87        data->entities[empty] = NULL;
88        id = empty;
89    }
90    else
91    {
92        Ticker::Ref(data->entities[id]);
93    }
94
95    return id;
96}
97
98void Dict::RemoveSlot(int id)
99{
100    if (Ticker::Unref(data->entities[id]) == 0)
101        data->entities[id] = NULL;
102    data->nentities--;
103}
104
105
106void Dict::SetEntity(int id, Entity *entity)
107{
108    Ticker::Ref(entity);
109    data->entities[id] = entity;
110}
111
112Entity *Dict::GetEntity(int id)
113{
114    return data->entities[id];
115}
116
Note: See TracBrowser for help on using the repository browser.