ScummVM API documentation
id_man.h
1/* ScummVM - Graphic Adventure Engine
2 *
3 * ScummVM is the legal property of its developers, whose names
4 * are too numerous to list here. Please refer to the COPYRIGHT
5 * file distributed with this source distribution.
6 *
7 * This program is free software: you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation, either version 3 of the License, or
10 * (at your option) any later version.
11 *
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
16 *
17 * You should have received a copy of the GNU General Public License
18 * along with this program. If not, see <http://www.gnu.org/licenses/>.
19 *
20 */
21
22#ifndef ULTIMA8_MISC_IDMAN_H
23#define ULTIMA8_MISC_IDMAN_H
24
25#include "common/array.h"
26
27namespace Common {
28class ReadStream;
29class WriteStream;
30}
31
32namespace Ultima {
33namespace Ultima8 {
34
35//
36// idMan. Used to allocate and keep track of unused ids.
37// Supposed to be used by Kernel and World for pID and ObjId
38//
39// The idMan itself uses a nifty linked list that is also an array.
40// As such it has the advantages of both. Adds and removals are fast,
41// As is checking to see if an ID is used.
42//
43// idMan works as a LILO (last in last out) for id recycling. So an id that has
44// been cleared will not be used until all other currently unused ids have been
45// allocated.
46//
47
48class idMan {
49 uint16 _begin;
50 uint16 _end;
51 uint16 _maxEnd;
52 uint16 _startCount;
53
54 uint16 _usedCount;
55
57 uint16 _first;
58 uint16 _last;
59public:
63 idMan(uint16 begin, uint16 max_end, uint16 startcount = 0);
64 ~idMan();
65
67 bool isFull() const {
68 return _first == 0 && _end >= _maxEnd;
69 }
70
72 void clearAll(uint16 new_max = 0);
73
76 uint16 getNewID();
77
81 bool reserveID(uint16 id);
82
84 void clearID(uint16 id);
85
87 bool isIDUsed(uint16 id) const {
88 return id >= _begin && id <= _end && _ids[id] == 0 && id != _last;
89 }
90
96 void setNewMax(uint16 maxEnd) {
97 _maxEnd = maxEnd;
98 }
99
100 void save(Common::WriteStream *ws) const;
101 bool load(Common::ReadStream *rs, uint32 version);
102
103private:
106 void expand();
107};
108
109} // End of namespace Ultima8
110} // End of namespace Ultima
111
112#endif
Definition: stream.h:385
Definition: stream.h:77
Definition: id_man.h:48
void clearAll(uint16 new_max=0)
clear all IDs, reset size to the startcount, and set max_end to new_max
bool isFull() const
check if this idMan is full
Definition: id_man.h:67
bool isIDUsed(uint16 id) const
check if an ID is in use
Definition: id_man.h:87
bool reserveID(uint16 id)
void clearID(uint16 id)
release an id
idMan(uint16 begin, uint16 max_end, uint16 startcount=0)
void setNewMax(uint16 maxEnd)
Definition: id_man.h:96
Definition: algorithm.h:29