source: trunk/src/dict.cpp @ 2370

Last change on this file since 2370 was 2216, checked in by touky, 7 years ago

New year copyright update.

  • Property svn:keywords set to Id
File size: 3.0 KB
RevLine 
[153]1//
[221]2// Lol Engine
[153]3//
[2216]4// Copyright: (c) 2010-2013 Sam Hocevar <sam@hocevar.net>
[221]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
[2183]8//   http://www.wtfpl.net/ for more details.
[221]9//
[153]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
[1173]20#if defined _WIN32 || defined _XBOX
[172]21#   define strcasecmp _stricmp
22#endif
23
[758]24using namespace std;
25
[686]26namespace lol
27{
28
[153]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    {
[658]46#if !LOL_RELEASE
[153]47        if (nentities)
[734]48            Log::Error("still %i entities in dict\n", nentities);
[225]49#endif
[153]50    }
51
52private:
[1985]53    Array<Entity *> m_entities;
54    int nentities;
[153]55};
56
57/*
58 * Public Dict class
59 */
60
61Dict::Dict()
[259]62  : data(new DictData())
[153]63{
64}
65
66Dict::~Dict()
67{
68    delete data;
69}
70
71int Dict::MakeSlot(char const *name)
72{
[958]73    int slotid, empty = -1;
[153]74
75    /* If the entry is already registered, remember its ID. Look for an
76     * empty slot at the same time. */
[1985]77    for (slotid = 0; slotid < data->m_entities.Count(); slotid++)
[153]78    {
[1985]79        Entity *e = data->m_entities[slotid];
[153]80        if (!e)
[420]81        {
[958]82            empty = slotid;
[420]83            break;
84        }
[419]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        }
[153]101    }
102
103    /* If this is a new entry, create a new slot for it. */
[1985]104    if (slotid == data->m_entities.Count() || !data->m_entities[slotid])
[153]105    {
[1985]106        if (slotid == data->m_entities.Count())
[153]107        {
[1985]108            empty = data->m_entities.Count();
109            data->m_entities.Push(NULL);
[153]110        }
111
[1985]112        data->m_entities[empty] = NULL;
[958]113        slotid = empty;
[420]114        data->nentities++;
[153]115    }
116    else
117    {
[1985]118        Ticker::Ref(data->m_entities[slotid]);
[153]119    }
120
[958]121    return slotid;
[153]122}
123
[958]124void Dict::RemoveSlot(int slotid)
[153]125{
[1985]126    if (Ticker::Unref(data->m_entities[slotid]) == 0)
[225]127    {
[1985]128        data->m_entities[slotid] = NULL;
[420]129        data->nentities--;
[225]130    }
[153]131}
132
[789]133void Dict::RemoveSlot(Entity *entity)
134{
[1985]135    for (int slotid = 0; slotid < data->m_entities.Count(); slotid++)
136        if (data->m_entities[slotid] == entity)
[789]137        {
[958]138            RemoveSlot(slotid);
[789]139            return;
140        }
[153]141
[789]142#if !LOL_RELEASE
[1942]143    Log::Error("removing unregistered entity %p (%s)\n",
144               entity, entity->GetName());
[789]145#endif
146}
147
[958]148void Dict::SetEntity(int slotid, Entity *entity)
[153]149{
[170]150    Ticker::Ref(entity);
[1985]151    data->m_entities[slotid] = entity;
[153]152}
153
[958]154Entity *Dict::GetEntity(int slotid)
[153]155{
[1985]156    return data->m_entities[slotid];
[153]157}
158
[686]159} /* namespace lol */
160
Note: See TracBrowser for help on using the repository browser.