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);
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 
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);
120 
121  // Validate the class end footer
122  if (file->isClassStart())
123  error("Unexpected class start");
124  }
125  }
126 
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 
139  }
140 
144  T *add() {
145  T *item = new T();
147  return item;
148  }
149 
150  bool contains(const T *item) const {
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 */
void load(SimpleFile *file) override
Definition: list.h:99
T * add()
Definition: list.h:144
Definition: list.h:44
void load(SimpleFile *file) override
Definition: list.h:35
Definition: simple_file.h:49
void readBuffer(char *buffer=nullptr, size_t count=0)
void save(SimpleFile *file, int indent) override
Definition: list.h:78
iterator end()
Definition: list.h:240
virtual void saveHeader(SimpleFile *file, int indent)
Definition: list.h:71
Definition: arm.h:30
Definition: string.h:40
void destroyContents()
Definition: list.h:130
void clear()
Definition: list.h:206
void save(SimpleFile *file, int indent) override
void NORETURN_PRE error(MSVC_PRINTF const char *s,...) GCC_PRINTF(1
void writeNumberLine(int val, int indent) const
Definition: list_intern.h:48
void writeQuotedLine(const CString &str, int indent) const
Definition: list_intern.h:51
Definition: saveable_object.h:58
static CSaveableObject * createInstance(const Common::String &name)
void push_back(const t_T &element)
Definition: list.h:140
Definition: list.h:61
virtual void saveFooter(SimpleFile *file, int indent)