/
folder.h
258 lines (224 loc) · 8.46 KB
/
folder.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
/*
* The Doomsday Engine Project -- libdeng2
*
* Copyright (c) 2009, 2011 Jaakko Keränen <jaakko.keranen@iki.fi>
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, see <http://www.gnu.org/licenses/>.
*/
#ifndef LIBDENG2_FOLDER_H
#define LIBDENG2_FOLDER_H
#include "../libdeng2.h"
#include "../File"
#include <map>
#include <list>
namespace de
{
class Feed;
/**
* A folder contains a set of files. It is used for building a tree of files
* in the file system (de::FS). This is the base class for all types of folders.
*
* @see Feed
*
* @ingroup fs
*/
class DENG2_PUBLIC Folder : public File
{
public:
/// A folder cannot contain two or more files with the same name. @ingroup errors
DENG2_ERROR(DuplicateNameError)
/// File path did not point to a file. @ingroup errors
DENG2_ERROR(NotFoundError)
/// Creating a new file was unsuccessful. @ingroup errors
DENG2_ERROR(NewFileError)
/**
* Accesses the properties of a Folder. Allows using properties of a
* folder (like how many items it contains) as a Value, for instance in
* script expressions.
*
* @ingroup fs
*/
class Accessor : public AccessorValue
{
public:
/// Property of the file to access.
enum Property {
CONTENT_SIZE
};
public:
Accessor(Folder& owner, Property prop);
void update() const;
Value* duplicateContent() const;
private:
Folder& _owner;
Property _prop;
};
typedef std::list<Feed*> Feeds;
typedef std::map<String, File*> Contents;
public:
Folder(const String& name = "");
virtual ~Folder();
/**
* Populates the folder with a set of File instances. Each feed attached to
* the folder will contribute. Every populated file will also be
* added to the file system's main index.
*
* Repopulation is nondestructive as long as the source data has not changed.
* Population may be performed more than once during the lifetime of the folder,
* for example when it's necessary to synchronize it with the contents of a
* native hard drive directory.
*/
virtual void populate();
/**
* Provides direct read-only access to the content of the folder.
*/
const Contents& contents() const;
/**
* Destroys the contents of the folder. All contained file objects are deleted.
*/
void clear();
/**
* Creates a new file in the folder. The feeds attached to the folder will
* decide what kind of file is actually created. The new file is added to
* the file system's index.
*
* @param name Name or path of the new file, relative to this folder.
* @param replaceExisting Replacing existing file with the same name.
*
* @return The created file (write mode enabled).
*/
File& newFile(const String& name, bool replaceExisting = false);
/**
* Creates a new file in the folder, replacing an existing file with the
* same name. Same as calling <code>newFile(name, true)</code>.
*
* @param name Name or path of the new file, relative to this folder.
*/
File& replaceFile(const String& name);
/**
* Removes a file from a folder. If it has an origin feed, the feed will be
* asked to remove the file as well.
*
* @param name Name or path of file to remove, relative to this folder.
*/
void removeFile(const String& name);
/**
* Checks whether the folder contains a file.
*
* @param name File to check for. The name is not case sensitive.
*/
bool has(const String& name) const;
/**
* Adds an object to the folder. The object must be an instance of a class
* derived from File.
*
* @param fileObject Object to add to the folder. The folder takes
* ownership of this object. Cannot be NULL.
*
* @return Reference to @a fileObject, for convenience.
*/
template <typename Type>
Type& add(Type* fileObject) {
DENG2_ASSERT(fileObject != 0);
add(static_cast<File*>(fileObject));
return *fileObject;
}
/**
* Adds a file instance to the contents of the folder.
*
* @param file File to add. The folder takes ownership of this instance.
*
* @return Reference to the file, for convenience.
*/
virtual File& add(File* file);
/**
* Removes a file from the folder, by name. The file is not deleted. The
* ownership of the file is given to the caller.
*
* @return The removed file object. Ownership of the object is given to
* the caller.
*/
File* remove(const String& name);
template <typename Type>
Type* remove(Type* fileObject) {
DENG2_ASSERT(fileObject != 0);
remove(*static_cast<File*>(fileObject));
return fileObject;
}
/**
* Removes a file from the folder. The file is not deleted. The ownership
* of the file is given to the caller.
*
* @return The removed file object. Ownership of the object is given to
* the caller.
*/
virtual File* remove(File& file);
/**
* Locates a file in this folder or in one of its subfolders. Looks recursively
* through subfolders.
*
* @param path Path to look for. Relative to this folder.
*
* @return The located file, or @c NULL if the path was not found.
*/
virtual File* tryLocateFile(const String& path) const;
template <typename Type>
Type* tryLocate(const String& path) const {
return dynamic_cast<Type*>(tryLocateFile(path));
}
/**
* Locates a file in this folder or in one of its subfolders. Looks recusively
* through subfolders.
*
* @param path Path to look for. Relative to this folder.
*
* @return The found file.
*/
template <typename Type>
Type& locate(const String& path) const {
Type* found = tryLocate<Type>(path);
if(!found) {
/// @throw NotFoundError Path didn't exist, or the located file had
/// an incompatible type.
throw NotFoundError("Folder::locate", path +": path was not found or had incompatible type");
}
return *found;
}
/**
* Attach a feed to the folder. The feed will provide content for the folder.
*
* @param feed Feed to attach to the folder. The folder gets ownership of the feed.
*/
void attach(Feed* feed);
/**
* Detaches a feed from the folder. The feed object is not deleted.
*
* @param feed Feed to detach from the folder.
*
* @return The Feed object. Ownership is returned to the caller.
*/
Feed* detach(Feed& feed);
/**
* Provides access to the list of Feeds for this folder. The feeds are responsible
* for creating File and Folder instances in the folder.
*/
const Feeds& feeds() const { return _feeds; }
private:
/// A map of file names to file instances.
Contents _contents;
/// Feeds provide content for the folder.
Feeds _feeds;
};
}
#endif /* LIBDENG2_FOLDER_H */