Ocean
File.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_FILE_H
9 #define META_OCEAN_IO_FILE_H
10 
11 #include "ocean/io/IO.h"
12 #include "ocean/io/Path.h"
13 
14 #include <vector>
15 
16 namespace Ocean
17 {
18 
19 namespace IO
20 {
21 
22 // Forward declaration.
23 class File;
24 
25 /**
26  * Definition of a vector holding files.
27  * @ingroup io
28  */
29 typedef std::vector<File> Files;
30 
31 /**
32  * This class holds a file.
33  * @ingroup io
34  */
35 class OCEAN_IO_EXPORT File : public Path
36 {
37  public:
38 
39  /**
40  * Creates an empty file.
41  */
42  File();
43 
44  /**
45  * Create a new file.
46  * @param filename File name
47  */
48  explicit File(const std::string& filename);
49 
50  /**
51  * Returns whether the path is valid.
52  * @see Path::isValid();
53  */
54  bool isValid() const override;
55 
56  /**
57  * Returns whether the file exists already.
58  * @see Path::exists(), remove().
59  */
60  bool exists() const override;
61 
62  /**
63  * Removes this file from the filesystem.
64  * @return True, if succeeded
65  * @see exists().
66  */
67  bool remove() const;
68 
69  /**
70  * Returns the base of this file.
71  * The file's base is the entire file path without file extension (and the character in front of the extension).<br>
72  * The base of e.g. "example.bmp" is "example"; "/first/second/example.txt" is "/first/second/example"
73  * @return File base
74  */
75  std::string base() const;
76 
77  /**
78  * Returns the extension of this file.
79  * The file's extension of e.g. "example.bmp" is "bmp".
80  * @return File extension
81  */
82  std::string extension() const;
83 
84  /**
85  * Returns the name of this file.
86  * The file's name is the local filename including the file extension without the prefix path.<br>
87  * The name of e.g. "example.bmp" is "example.bmp"; "/first/second/example.txt" is "example.txt"
88  * @return File name
89  */
90  std::string name() const;
91 
92  /**
93  * Returns the base name of this file.
94  * The base name is the local filename without extension.<br>
95  * The base name of e.g. "example.bmp" is "example"; "/first/second/example.txt" is "example"
96  * @return File base name
97  */
98  std::string baseName() const;
99 
100 #if defined(__APPLE__)
101 
102  /**
103  * Returns whether a file exists (specialization for Apple platforms).
104  * @param file The file to check
105  * @return True, if so
106  */
107  static bool existsApple(const std::string& file);
108 
109 #endif // defined(__APPLE__)
110 };
111 
112 /**
113  * This class implements a scoped file object which will delete the underlying file from the filesystem when the scope ends.
114  * @see File
115  * @ingroup io
116  */
117 class ScopedFile final : public File
118 {
119  public:
120 
121  /**
122  * Move constructor.
123  * @param scopedFile The scoped file to be moved
124  */
125  inline ScopedFile(ScopedFile&& scopedFile);
126 
127  /**
128  * Create a new scoped file.
129  * @param file The file for which the new scoped object will be created
130  */
131  explicit inline ScopedFile(const File& file);
132 
133  /**
134  * Create a new scoped file.
135  * @param filename The name of the file for which a new scoped object will be created
136  */
137  explicit inline ScopedFile(const std::string& filename);
138 
139  /**
140  * Destructs this object and deletes the actual file.
141  */
143 
144  /**
145  * Move operator.
146  * @param scopedFile The scoped file to be moved
147  * @return Reference to this object
148  */
150 
151  protected:
152 
153  /**
154  * Disabled copy constructor.
155  * @param scopedFile File which would be copied
156  */
157  ScopedFile(const ScopedFile& scopedFile) = delete;
158 
159  /**
160  * Disabled copy constructor.
161  * @param scopedFile File which would be copied
162  * @return Reference to this object
163  */
164  ScopedFile& operator=(const ScopedFile& scopedFile) = delete;
165 };
166 
167 inline ScopedFile::ScopedFile(ScopedFile&& scopedFile) :
168  File()
169 {
170  *this = std::move(scopedFile);
171 }
172 
173 inline ScopedFile::ScopedFile(const File& file) :
174  File(file)
175 {
176  // nothing to do here
177 }
178 
179 inline ScopedFile::ScopedFile(const std::string& filename) :
180  File(filename)
181 {
182  // nothing to do here
183 }
184 
185 }
186 
187 }
188 
189 #endif // META_OCEAN_IO_FILE_H
This class holds a file.
Definition: File.h:36
std::string baseName() const
Returns the base name of this file.
std::string name() const
Returns the name of this file.
std::string extension() const
Returns the extension of this file.
std::string base() const
Returns the base of this file.
static bool existsApple(const std::string &file)
Returns whether a file exists (specialization for Apple platforms).
bool isValid() const override
Returns whether the path is valid.
bool remove() const
Removes this file from the filesystem.
bool exists() const override
Returns whether the file exists already.
File(const std::string &filename)
Create a new file.
File()
Creates an empty file.
This class holds a path.
Definition: Path.h:27
This class implements a scoped file object which will delete the underlying file from the filesystem ...
Definition: File.h:118
~ScopedFile()
Destructs this object and deletes the actual file.
ScopedFile & operator=(ScopedFile &&scopedFile)
Move operator.
ScopedFile(ScopedFile &&scopedFile)
Move constructor.
Definition: File.h:167
ScopedFile(const ScopedFile &scopedFile)=delete
Disabled copy constructor.
ScopedFile & operator=(const ScopedFile &scopedFile)=delete
Disabled copy constructor.
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