ScummVM API documentation
list.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 TITANIC_LIST_H
23
#define TITANIC_LIST_H
24
25
#include "common/scummsys.h"
26
#include "common/list.h"
27
#include "titanic/support/simple_file.h"
28
#include "titanic/core/saveable_object.h"
29
30
namespace
Titanic
{
31
35
class
ListItem
:
public
CSaveableObject
{
36
public
:
37
CLASSDEF;
38
42
void
save
(
SimpleFile
*file,
int
indent)
override
;
43
47
void
load
(
SimpleFile
*file)
override
;
48
};
49
53
#define PTR_LIST_ITEM(T) class T##ListItem : public ListItem { \
54
public: T *_item; \
55
T##ListItem() : _item(nullptr) {} \
56
T##ListItem(T *item) : _item(item) {} \
57
virtual ~T##ListItem() { delete _item; } \
58
}
59
60
template
<
typename
T>
61
class
PtrListItem
:
public
ListItem
{
62
public
:
63
T *_item;
64
public
:
65
PtrListItem
() : _item(
nullptr
) {}
66
PtrListItem
(T *item) : _item(item) {}
67
~
PtrListItem
()
override
{
delete
_item; }
68
};
69
70
template
<
typename
T>
71
class
List
:
public
CSaveableObject
,
public
Common::List
<T *> {
72
public
:
73
~
List
()
override
{ destroyContents(); }
74
78
void
save
(
SimpleFile
*file,
int
indent)
override
{
79
file->
writeNumberLine
(0, indent);
80
81
// Write out number of items
82
file->
writeQuotedLine
(
"L"
, indent);
83
file->
writeNumberLine
(
Common::List<T *>::size
(), indent);
84
85
// Iterate through writing entries
86
typename
Common::List<T *>::iterator
i;
87
for
(i =
Common::List<T *>::begin
(); i !=
Common::List<T *>::end
(); ++i) {
88
ListItem
*item = *i;
89
item->
saveHeader
(file, indent);
90
item->
save
(file, indent + 1);
91
item->
saveFooter
(file, indent);
92
}
93
94
}
95
99
void
load
(
SimpleFile
*file)
override
{
100
file->
readNumber
();
101
file->
readBuffer
();
102
103
Common::List<T *>::clear
();
104
uint count = file->
readNumber
();
105
106
for
(uint idx = 0; idx < count; ++idx) {
107
// Validate the class start header
108
if
(!file->
isClassStart
())
109
error
(
"Unexpected class end"
);
110
111
// Get item's class name and use it to instantiate an item
112
CString
className = file->
readString
();
113
T *newItem =
dynamic_cast<
T *
>
(
CSaveableObject::createInstance
(className));
114
if
(!newItem)
115
error
(
"Could not create instance of %s"
, className.c_str());
116
117
// Load the item's data and add it to the list
118
newItem->load(file);
119
Common::List<T *>::push_back
(newItem);
120
121
// Validate the class end footer
122
if
(file->
isClassStart
())
123
error
(
"Unexpected class start"
);
124
}
125
}
126
130
void
destroyContents
() {
131
typename
Common::List<T *>::iterator
i;
132
for
(i =
Common::List<T *>::begin
();
133
i !=
Common::List<T *>::end
(); ++i) {
134
CSaveableObject
*obj = *i;
135
delete
obj;
136
}
137
138
Common::List<T *>::clear
();
139
}
140
144
T *
add
() {
145
T *item =
new
T();
146
Common::List<T *>::push_back
(item);
147
return
item;
148
}
149
150
bool
contains(
const
T *item)
const
{
151
for
(
typename
Common::List<T *>::const_iterator
i =
Common::List<T *>::begin
();
152
i !=
Common::List<T *>::end
(); ++i) {
153
if
(*i == item)
154
return
true
;
155
}
156
157
return
false
;
158
}
159
};
160
161
}
// End of namespace Titanic
162
163
#endif
/* TITANIC_LIST_H */
Titanic::List::load
void load(SimpleFile *file) override
Definition:
list.h:99
Titanic::SimpleFile::readString
CString readString()
Titanic::List::add
T * add()
Definition:
list.h:144
Titanic::SimpleFile::isClassStart
bool isClassStart()
Common::List
Definition:
list.h:44
Titanic::ListItem::load
void load(SimpleFile *file) override
Titanic::ListItem
Definition:
list.h:35
Titanic::SimpleFile
Definition:
simple_file.h:49
Titanic::SimpleFile::readBuffer
void readBuffer(char *buffer=nullptr, size_t count=0)
Titanic::List::save
void save(SimpleFile *file, int indent) override
Definition:
list.h:78
Common::List::end
iterator end()
Definition:
list.h:240
Titanic::SimpleFile::readNumber
int readNumber()
Titanic::CSaveableObject::saveHeader
virtual void saveHeader(SimpleFile *file, int indent)
Titanic::List
Definition:
list.h:71
Titanic
Definition:
arm.h:30
Titanic::CString
Definition:
string.h:40
Titanic::List::destroyContents
void destroyContents()
Definition:
list.h:130
Common::List::clear
void clear()
Definition:
list.h:206
Titanic::ListItem::save
void save(SimpleFile *file, int indent) override
error
void NORETURN_PRE error(MSVC_PRINTF const char *s,...) GCC_PRINTF(1
Titanic::SimpleFile::writeNumberLine
void writeNumberLine(int val, int indent) const
Common::ListInternal::ConstIterator
Definition:
list_intern.h:48
Titanic::SimpleFile::writeQuotedLine
void writeQuotedLine(const CString &str, int indent) const
Common::ListInternal::Iterator
Definition:
list_intern.h:51
Titanic::CSaveableObject
Definition:
saveable_object.h:58
Titanic::CSaveableObject::createInstance
static CSaveableObject * createInstance(const Common::String &name)
Common::List::push_back
void push_back(const t_T &element)
Definition:
list.h:140
Titanic::PtrListItem
Definition:
list.h:61
Titanic::CSaveableObject::saveFooter
virtual void saveFooter(SimpleFile *file, int indent)
engines
titanic
core
list.h
Generated on Sun Dec 22 2024 09:05:55 for ScummVM API documentation by
1.8.13