Pyrogenesis  13997
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
vfs_tree.h
Go to the documentation of this file.
1 /* Copyright (c) 2010 Wildfire Games
2  *
3  * Permission is hereby granted, free of charge, to any person obtaining
4  * a copy of this software and associated documentation files (the
5  * "Software"), to deal in the Software without restriction, including
6  * without limitation the rights to use, copy, modify, merge, publish,
7  * distribute, sublicense, and/or sell copies of the Software, and to
8  * permit persons to whom the Software is furnished to do so, subject to
9  * the following conditions:
10  *
11  * The above copyright notice and this permission notice shall be included
12  * in all copies or substantial portions of the Software.
13  *
14  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
15  * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
16  * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
17  * IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
18  * CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
19  * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
20  * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
21  */
22 
23 /*
24  * 'tree' of VFS directories and files
25  */
26 
27 #ifndef INCLUDED_VFS_TREE
28 #define INCLUDED_VFS_TREE
29 
30 #include <map>
31 
32 #include "lib/file/file_system.h" // CFileInfo
33 #include "lib/file/common/file_loader.h" // PIFileLoader
34 #include "lib/file/common/real_directory.h" // PRealDirectory
35 #include "lib/file/vfs/vfs_path.h"
36 
37 class VfsFile
38 {
39 public:
40  VfsFile(const VfsPath& name, size_t size, time_t mtime, size_t priority, const PIFileLoader& provider);
41 
42  const VfsPath& Name() const
43  {
44  return m_name;
45  }
46 
47  size_t Size() const
48  {
49  return m_size;
50  }
51 
52  time_t MTime() const
53  {
54  return m_mtime;
55  }
56 
57  size_t Priority() const
58  {
59  return m_priority;
60  }
61 
62  const PIFileLoader& Loader() const
63  {
64  return m_loader;
65  }
66 
67 private:
69  size_t m_size;
70  time_t m_mtime;
71 
72  size_t m_priority;
73 
75 };
76 
77 
79 {
80 public:
81  typedef std::map<VfsPath, VfsFile> VfsFiles;
82  typedef std::map<VfsPath, VfsDirectory> VfsSubdirectories;
83 
84  VfsDirectory();
85 
86  /**
87  * @return address of existing or newly inserted file.
88  **/
89  VfsFile* AddFile(const VfsFile& file);
90 
91  /**
92  * @return address of existing or newly inserted subdirectory.
93  **/
94  VfsDirectory* AddSubdirectory(const VfsPath& name);
95 
96  /**
97  * remove the given file from the virtual directory (no effect on
98  * the physical file). no effect if the file does not exist.
99  **/
100  void RemoveFile(const VfsPath& name);
101 
102  /**
103  * @return file with the given name.
104  * (note: non-const to allow changes to the file)
105  **/
106  VfsFile* GetFile(const VfsPath& name);
107 
108  /**
109  * @return subdirectory with the given name.
110  * (note: non-const to allow changes to the subdirectory)
111  **/
112  VfsDirectory* GetSubdirectory(const VfsPath& name);
113 
114  // note: exposing only iterators wouldn't enable callers to reserve space.
115 
116  const VfsFiles& Files() const
117  {
118  return m_files;
119  }
120 
122  {
123  return m_subdirectories;
124  }
125 
126  /**
127  * side effect: the next ShouldPopulate() will return true.
128  **/
129  void SetAssociatedDirectory(const PRealDirectory& realDirectory);
130 
132  {
133  return m_realDirectory;
134  }
135 
136  /**
137  * @return whether this directory should be populated from its
138  * AssociatedDirectory(). note that calling this is a promise to
139  * do so if true is returned -- the flag is reset immediately.
140  **/
141  bool ShouldPopulate();
142 
143  /**
144  * ensure the next ShouldPopulate returns true.
145  **/
146  void RequestRepopulate();
147 
148  /**
149  * empty file and subdirectory lists (e.g. when rebuilding VFS).
150  * CAUTION: this invalidates all previously returned pointers.
151  **/
152  void Clear();
153 
154 private:
157 
159  volatile intptr_t m_shouldPopulate; // (cpu_CAS can't be used on bool)
160 };
161 
162 
163 /**
164  * @return a string containing file attributes (location, size, timestamp) and name.
165  **/
166 extern std::wstring FileDescription(const VfsFile& file);
167 
168 /**
169  * @return a string holding each files' description (one per line).
170  **/
171 extern std::wstring FileDescriptions(const VfsDirectory& directory, size_t indentLevel);
172 
173 /**
174  * append each directory's files' description to the given string.
175  **/
176 void DirectoryDescriptionR(std::wstring& descriptions, const VfsDirectory& directory, size_t indentLevel);
177 
178 #endif // #ifndef INCLUDED_VFS_TREE
bool ShouldPopulate()
Definition: vfs_tree.cpp:143
VfsFile * AddFile(const VfsFile &file)
Definition: vfs_tree.cpp:81
size_t m_size
Definition: vfs_tree.h:69
const VfsPath & Name() const
Definition: vfs_tree.h:42
void RemoveFile(const VfsPath &name)
remove the given file from the virtual directory (no effect on the physical file).
Definition: vfs_tree.cpp:111
void DirectoryDescriptionR(std::wstring &descriptions, const VfsDirectory &directory, size_t indentLevel)
append each directory&#39;s files&#39; description to the given string.
Definition: vfs_tree.cpp:198
volatile intptr_t m_shouldPopulate
Definition: vfs_tree.h:159
PRealDirectory m_realDirectory
Definition: vfs_tree.h:158
VfsPath m_name
Definition: vfs_tree.h:68
const VfsFiles & Files() const
Definition: vfs_tree.h:116
const PRealDirectory & AssociatedDirectory() const
Definition: vfs_tree.h:131
std::wstring FileDescriptions(const VfsDirectory &directory, size_t indentLevel)
Definition: vfs_tree.cpp:178
size_t Size() const
Definition: vfs_tree.h:47
void SetAssociatedDirectory(const PRealDirectory &realDirectory)
side effect: the next ShouldPopulate() will return true.
Definition: vfs_tree.cpp:135
VfsFiles m_files
Definition: vfs_tree.h:155
VfsFile * GetFile(const VfsPath &name)
Definition: vfs_tree.cpp:117
const VfsSubdirectories & Subdirectories() const
Definition: vfs_tree.h:121
std::wstring FileDescription(const VfsFile &file)
Definition: vfs_tree.cpp:166
Definition: path.h:75
shared_ptr< RealDirectory > PRealDirectory
VfsSubdirectories m_subdirectories
Definition: vfs_tree.h:156
size_t Priority() const
Definition: vfs_tree.h:57
time_t MTime() const
Definition: vfs_tree.h:52
shared_ptr< IFileLoader > PIFileLoader
Definition: file_loader.h:39
const PIFileLoader & Loader() const
Definition: vfs_tree.h:62
size_t m_priority
Definition: vfs_tree.h:72
void Clear()
empty file and subdirectory lists (e.g.
Definition: vfs_tree.cpp:155
std::map< VfsPath, VfsFile > VfsFiles
Definition: vfs_tree.h:81
time_t m_mtime
Definition: vfs_tree.h:70
void RequestRepopulate()
ensure the next ShouldPopulate returns true.
Definition: vfs_tree.cpp:149
std::map< VfsPath, VfsDirectory > VfsSubdirectories
Definition: vfs_tree.h:82
PIFileLoader m_loader
Definition: vfs_tree.h:74
VfsDirectory * GetSubdirectory(const VfsPath &name)
Definition: vfs_tree.cpp:126
VfsFile(const VfsPath &name, size_t size, time_t mtime, size_t priority, const PIFileLoader &provider)
Definition: vfs_tree.cpp:38
VfsDirectory * AddSubdirectory(const VfsPath &name)
Definition: vfs_tree.cpp:103