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 "ultima/shared/std/containers.h"
26 
27 namespace Ultima {
28 namespace Ultima8 {
29 
30 //
31 // idMan. Used to allocate and keep track of unused ids.
32 // Supposed to be used by Kernel and World for pID and ObjId
33 //
34 // The idMan itself uses a nifty linked list that is also an array.
35 // As such it has the advantages of both. Adds and removals are fast,
36 // As is checking to see if an ID is used.
37 //
38 // idMan works as a LILO (last in last out) for id recycling. So an id that has
39 // been cleared will not be used until all other currently unused ids have been
40 // allocated.
41 //
42 
43 class idMan {
44  uint16 _begin;
45  uint16 _end;
46  uint16 _maxEnd;
47  uint16 _startCount;
48 
49  uint16 _usedCount;
50 
51  Std::vector<uint16> _ids;
52  uint16 _first;
53  uint16 _last;
54 public:
58  idMan(uint16 begin, uint16 max_end, uint16 startcount = 0);
59  ~idMan();
60 
62  bool isFull() const {
63  return _first == 0 && _end >= _maxEnd;
64  }
65 
67  void clearAll(uint16 new_max = 0);
68 
71  uint16 getNewID();
72 
76  bool reserveID(uint16 id);
77 
79  void clearID(uint16 id);
80 
82  bool isIDUsed(uint16 id) const {
83  return id >= _begin && id <= _end && _ids[id] == 0 && id != _last;
84  }
85 
91  void setNewMax(uint16 maxEnd) {
92  _maxEnd = maxEnd;
93  }
94 
95  void save(Common::WriteStream *ws) const;
96  bool load(Common::ReadStream *rs, uint32 version);
97 
98 private:
101  void expand();
102 };
103 
104 } // End of namespace Ultima8
105 } // End of namespace Ultima
106 
107 #endif
bool isIDUsed(uint16 id) const
check if an ID is in use
Definition: id_man.h:82
bool reserveID(uint16 id)
Definition: stream.h:77
Definition: detection.h:27
idMan(uint16 begin, uint16 max_end, uint16 startcount=0)
void setNewMax(uint16 maxEnd)
Definition: id_man.h:91
Definition: id_man.h:43
Definition: stream.h:385
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:62
void clearID(uint16 id)
release an id