source: trunk/src/dict.cpp @ 758

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

ps3: start a minimal PS3 port using PSGL, but not CG yet.

  • Property svn:keywords set to Id
File size: 2.7 KB
Line 
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
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        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
55private:
56    Entity **entities;
57    int maxid, nentities;
58};
59
60/*
61 * Public Dict class
62 */
63
64Dict::Dict()
65  : data(new DictData())
66{
67}
68
69Dict::~Dict()
70{
71    delete data;
72}
73
74int 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
128void 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
138void Dict::SetEntity(int id, Entity *entity)
139{
140    Ticker::Ref(entity);
141    data->entities[id] = entity;
142}
143
144Entity *Dict::GetEntity(int id)
145{
146    return data->entities[id];
147}
148
149} /* namespace lol */
150
Note: See TracBrowser for help on using the repository browser.