Ocean
Directory.h
Go to the documentation of this file.
1 /*
2  * Copyright (c) Meta Platforms, Inc. and affiliates.
3  *
4  * This source code is licensed under the MIT license found in the
5  * LICENSE file in the root directory of this source tree.
6  */
7 
8 #ifndef META_OCEAN_IO_DIRECTORY_H
9 #define META_OCEAN_IO_DIRECTORY_H
10 
11 #include "ocean/io/IO.h"
12 #include "ocean/io/File.h"
13 #include "ocean/io/Path.h"
14 
15 namespace Ocean
16 {
17 
18 namespace IO
19 {
20 
21 // Forward declaration.
22 class Directory;
23 
24 /**
25  * Definition of a vector holding directories.
26  * @ingroup io
27  */
28 typedef std::vector<Directory> Directories;
29 
30 /**
31  * This class holds a directory.
32  * Valid directories will end with a separator.
33  * @ingroup io
34  */
35 class OCEAN_IO_EXPORT Directory : public Path
36 {
37  public:
38 
39  /**
40  * Creates an empty directory.
41  */
43 
44  /**
45  * Creates a new directory.
46  * @param path Directory path
47  */
48  explicit Directory(const std::string& path);
49 
50  /**
51  * Creates a new directory by a given file.
52  * @param file File to get the directory from
53  */
54  explicit Directory(const File& file);
55 
56  /**
57  * Creates a new directory by a given path.
58  * @param path Path to create a directory from
59  */
60  explicit Directory(const Path& path);
61 
62  /**
63  * Returns whether the directory is valid.
64  * @see Path::isValid().
65  */
66  virtual bool isValid() const;
67 
68  /**
69  * Returns whether the directory exists already.
70  * @see Path::exists().
71  */
72  virtual bool exists() const;
73 
74  /**
75  * Removes this directory from the filesystem.
76  * @param recursively True, to remove all sub-directories and their files recursively; False, to remove only this directory if empty
77  * @return True, if succeeded
78  * @see exists().
79  */
80  bool remove(const bool recursively = false) const;
81 
82  /**
83  * Create all necessary sub-directories for the given path, if they do not exist.
84  * @return True, if succeeded
85  */
86  bool create() const;
87 
88  /**
89  * Returns the base of this directory.
90  * The directory's base is the entire directory path without the ending separator.<br>
91  * The base of e.g. "/path/to/foo/bar/" is "/path/to/foo/bar"
92  * @return The directory's base
93  */
94  std::string base() const;
95 
96  /**
97  * Returns the name of a directory.
98  * @return The name of a directory, e.g., for "/path/to/foo/bar/" it returns "bar"; will be empty if the directory doesn't exist or is invalid
99  */
100  std::string name() const;
101 
102  /**
103  * Returns all files in the directory with the given file format.
104  * @param fileType File type to find files for, for example "png", "txt", or "json". Use "*" to list all files.
105  * @param recursive True, to find file in all possible sub-directories; False, to find files in this directory only
106  * @return List of existing files
107  */
108  Files findFiles(const std::string& fileType = std::string("*"), const bool recursive = false) const;
109 
110  /**
111  * Returns all directories of this directory.
112  * @param recursive True, to find sub-directories as well; False, to find direct child directories only
113  * @return List of existing directories
114  */
115  Directories findDirectories(const bool recursive = false) const;
116 
117  /**
118  * Returns a file composed of this directory and a relative file.
119  * @param file Relative file
120  * @return New created file
121  */
122  File operator+(const File& file) const;
123 
124  /**
125  * Returns a directory composed of this and a relative directory.
126  * @param path Relative path to add
127  * @return New created directory
128  */
129  Directory operator+(const Directory& path) const;
130 
131  /**
132  * Adds an relative path to this directory.
133  * @param path Relative path to add
134  * @return Reference to the new path
135  */
137 
138  /**
139  * Removes the last sub-directory from this directory.
140  * @return Reference to the modified path
141  */
143 
144  /**
145  * Removes the last sub-directory from this directory.
146  * @return A copy of this directory before the last sub-directory was removed
147  */
149 
150  /**
151  * Creates a new temporary directory.
152  * The directory will be created as a sub-directory inside of a OS-specific temporary files path.
153  * @return Created temporary directory if the operation succeeded; otherwise, an empty file path is returned.
154  */
156 
157  protected:
158 
159 #if defined(__APPLE__)
160 
161  /**
162  * Creates a new temporary directory (specialization for Apple platforms).
163  * The directory will be created as a sub-directory inside of a OS-specific temporary files path.
164  * @sa createTemporaryDirectory()
165  * @return Created temporary directory if the operation succeeded; otherwise, an empty file path is returned.
166  */
168 
169  /**
170  * Returns whether a directory exists (specialization for Apple platforms).
171  * @param directory Directory to check
172  * @return True, if so
173  */
174  static bool existsApple(const std::string& directory);
175 
176 #endif // defined(__APPLE__)
177 
178 };
179 
180 /**
181  * This class implements a scoped directory object which will delete the underlying directory (and sub-directories including all files) from the filesystem when the scope ends.
182  * @see Directory
183  * @ingroup io
184  */
185 class OCEAN_IO_EXPORT ScopedDirectory final : public Directory
186 {
187  public:
188 
189  /**
190  * Default constructor creating an invalid object not holding any directory.
191  */
192  ScopedDirectory() = default;
193 
194  /**
195  * Move constructor.
196  * @param scopedDirectory The scoped directory to be moved
197  */
198  inline ScopedDirectory(ScopedDirectory&& scopedDirectory);
199 
200  /**
201  * Create a new scoped directory.
202  * @param directory The directory for which the new scoped object will be created
203  */
204  explicit inline ScopedDirectory(const Directory& directory);
205 
206  /**
207  * Create a new scoped directory.
208  * @param directory The name of the directory for which a new scoped object will be created
209  */
210  explicit inline ScopedDirectory(const std::string& directory);
211 
212  /**
213  * Destructs this object and deletes the actual directory.
214  */
216 
217  /**
218  * Move operator.
219  * @param scopedDirectory The scoped directory to be moved
220  * @return Reference to this object
221  */
223 
224  protected:
225 
226  /**
227  * Disabled copy constructor.
228  * @param scopedDirectory Directory which would be copied
229  */
230  ScopedDirectory(const ScopedDirectory& scopedDirectory) = delete;
231 
232  /**
233  * Disabled copy constructor.
234  * @param scopedDirectory Directory which would be copied
235  * @return Reference to this object
236  */
237  ScopedDirectory& operator=(const ScopedDirectory& scopedDirectory) = delete;
238 };
239 
241  Directory()
242 {
243  *this = std::move(scopedDirectory);
244 }
245 
246 inline ScopedDirectory::ScopedDirectory(const Directory& directory) :
247  Directory(directory)
248 {
249  // nothing to do here
250 }
251 
252 inline ScopedDirectory::ScopedDirectory(const std::string& directory) :
253  Directory(directory)
254 {
255  // nothing to do here
256 }
257 
258 }
259 
260 }
261 
262 #endif // META_OCEAN_IO_DIRECTORY_H
This class holds a directory.
Definition: Directory.h:36
static Directory createTemporaryDirectoryApple()
Creates a new temporary directory (specialization for Apple platforms).
static Directory createTemporaryDirectory()
Creates a new temporary directory.
bool remove(const bool recursively=false) const
Removes this directory from the filesystem.
Directory & operator--()
Removes the last sub-directory from this directory.
virtual bool exists() const
Returns whether the directory exists already.
Directory(const std::string &path)
Creates a new directory.
Directory operator--(int)
Removes the last sub-directory from this directory.
Files findFiles(const std::string &fileType=std::string("*"), const bool recursive=false) const
Returns all files in the directory with the given file format.
std::string name() const
Returns the name of a directory.
Directory()
Creates an empty directory.
File operator+(const File &file) const
Returns a file composed of this directory and a relative file.
static bool existsApple(const std::string &directory)
Returns whether a directory exists (specialization for Apple platforms).
Directories findDirectories(const bool recursive=false) const
Returns all directories of this directory.
Directory(const File &file)
Creates a new directory by a given file.
Directory & operator+=(const Directory &path)
Adds an relative path to this directory.
Directory(const Path &path)
Creates a new directory by a given path.
virtual bool isValid() const
Returns whether the directory is valid.
Directory operator+(const Directory &path) const
Returns a directory composed of this and a relative directory.
bool create() const
Create all necessary sub-directories for the given path, if they do not exist.
std::string base() const
Returns the base of this directory.
This class holds a file.
Definition: File.h:36
This class holds a path.
Definition: Path.h:27
This class implements a scoped directory object which will delete the underlying directory (and sub-d...
Definition: Directory.h:186
~ScopedDirectory()
Destructs this object and deletes the actual directory.
ScopedDirectory & operator=(const ScopedDirectory &scopedDirectory)=delete
Disabled copy constructor.
ScopedDirectory(const ScopedDirectory &scopedDirectory)=delete
Disabled copy constructor.
ScopedDirectory()=default
Default constructor creating an invalid object not holding any directory.
ScopedDirectory & operator=(ScopedDirectory &&scopedDirectory)
Move operator.
std::vector< Directory > Directories
Definition of a vector holding directories.
Definition: Directory.h:22
std::vector< File > Files
Definition of a vector holding files.
Definition: File.h:23
The namespace covering the entire Ocean framework.
Definition: Accessor.h:15