Ocean
Path.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_PATH_H
9 #define META_OCEAN_IO_PATH_H
10 
11 #include "ocean/io/IO.h"
12 
13 namespace Ocean
14 {
15 
16 namespace IO
17 {
18 
19 /**
20  * This class holds a path.
21  * A path is the base for a file or a directory.<br>
22  * Each path can be absolute or relative.<br>
23  * Files do not end with a separator, directories must end with a separator.<br>
24  * @ingroup io
25  */
26 class OCEAN_IO_EXPORT Path
27 {
28  public:
29 
30  /**
31  * Definition of different path types.
32  */
33  enum Type
34  {
35  /// Invalid path type.
36  TYPE_INVALID = 0,
37  /// File path.
38  TYPE_FILE = 1,
39  /// Directory path.
40  TYPE_DIRECTORY = 2,
41  /// Absolute path.
42  TYPE_ABSOLUTE = 4,
43  /// Relative path.
44  TYPE_RELATIVE = 8,
45  /// Network path.
46  TYPE_NETWORK = 16
47  };
48 
49  /**
50  * Definition of path separators.
51  */
52  enum Separator
53  {
54  /// Undefined separator.
55  SEPARATOR_UNDEFINED = 0,
56  /// Slash as separator.
57  SEPARATOR_SLASH = '/',
58  /// Backslash as separator.
59  SEPARATOR_BACKSLASH = '\\'
60  };
61 
62  public:
63 
64  /**
65  * Destructs a path.
66  */
67  virtual ~Path() = default;
68 
69  /**
70  * Returns the type of this path.
71  * @return Path type.
72  */
73  inline Type type() const;
74 
75  /**
76  * Returns whether this path is absolute.
77  * If the path is invalid the result is False.
78  * @return True, if so
79  */
80  inline bool isAbsolute() const;
81 
82  /**
83  * Returns whether this path is relative.
84  * If the path is invalid the result is False.
85  * @return True, if so
86  */
87  inline bool isRelative() const;
88 
89  /**
90  * Returns whether the path is valid.
91  * @return True, if so
92  */
93  virtual bool isValid() const = 0;
94 
95  /**
96  * Returns whether this path holds no valid.
97  * @return True, if so
98  */
99  inline bool isNull() const;
100 
101  /**
102  * Returns whether the path exists already.
103  * @return True, if so
104  */
105  virtual bool exists() const = 0;
106 
107  /**
108  * Returns whether two paths are equal.
109  * @param right Right path to compare
110  * @return True, if so
111  */
112  bool operator==(const Path& right) const;
113 
114  /**
115  * Returns whether two paths are not equal.
116  * @param path Right path to compare
117  * @return True, if so
118  */
119  inline bool operator!=(const Path& path) const;
120 
121  /**
122  * Returns the value of this path.
123  * @return Path value
124  */
125  inline const std::string& operator()() const;
126 
127  /**
128  * Less operator comparing the actual strings of two paths.
129  * @param path The second path to compare
130  */
131  inline bool operator<(const Path& path) const;
132 
133  /**
134  * Returns whether a given character is a separator.
135  * @param character Character to check for a separator
136  * @return True, if so
137  */
138  static bool isSeparator(const char character);
139 
140  /**
141  * Returns the default separator of a path.
142  * @return The default separator for the current platform
143  */
144  static char defaultSeparator();
145 
146  protected:
147 
148  /**
149  * Creates a new undefined path.
150  */
151  Path() = default;
152 
153  /**
154  * Copy constructor.
155  * @param path The path to copy
156  */
157  Path(const Path& path) = default;
158 
159  /**
160  * Creates a new path.
161  * @param value Path value
162  */
163  explicit Path(const std::string& value);
164 
165  /**
166  * Checks the type of this path and trims it.
167  * @param expectType Expected path type
168  */
169  void checkPath(const Type expectType);
170 
171  /**
172  * Trims this path.
173  */
174  void trim();
175 
176  /**
177  * Default assign operator.
178  * @param path The path to assign
179  */
180  Path& operator=(const Path& path) = default;
181 
182  protected:
183 
184  /// Path type.
185  Type pathType_ = TYPE_INVALID;
186 
187  /// Path value.
188  std::string pathValue_;
189 };
190 
191 inline Path::Type Path::type() const
192 {
193  return pathType_;
194 }
195 
196 inline bool Path::isAbsolute() const
197 {
198  return (type() & TYPE_ABSOLUTE) != 0;
199 }
200 
201 inline bool Path::isRelative() const
202 {
203  return (type() & TYPE_RELATIVE) != 0;
204 }
205 
206 inline bool Path::isNull() const
207 {
208  return pathValue_.empty();
209 }
210 
211 inline bool Path::operator!=(const Path& path) const
212 {
213  return !(*this == path);
214 }
215 
216 inline const std::string& Path::operator()() const
217 {
218  return pathValue_;
219 }
220 
221 inline bool Path::operator<(const Path& path) const
222 {
223  return (*this)() < path();
224 }
225 
226 }
227 
228 }
229 
230 #endif // META_OCEAN_IO_PATH_H
This class holds a path.
Definition: Path.h:27
bool operator==(const Path &right) const
Returns whether two paths are equal.
void trim()
Trims this path.
bool isRelative() const
Returns whether this path is relative.
Definition: Path.h:201
virtual bool isValid() const =0
Returns whether the path is valid.
void checkPath(const Type expectType)
Checks the type of this path and trims it.
Type
Definition of different path types.
Definition: Path.h:34
@ TYPE_RELATIVE
Relative path.
Definition: Path.h:44
@ TYPE_ABSOLUTE
Absolute path.
Definition: Path.h:42
Path & operator=(const Path &path)=default
Default assign operator.
std::string pathValue_
Path value.
Definition: Path.h:188
bool isNull() const
Returns whether this path holds no valid.
Definition: Path.h:206
bool operator!=(const Path &path) const
Returns whether two paths are not equal.
Definition: Path.h:211
Type pathType_
Path type.
Definition: Path.h:185
static char defaultSeparator()
Returns the default separator of a path.
bool operator<(const Path &path) const
Less operator comparing the actual strings of two paths.
Definition: Path.h:221
virtual ~Path()=default
Destructs a path.
Path(const Path &path)=default
Copy constructor.
const std::string & operator()() const
Returns the value of this path.
Definition: Path.h:216
Separator
Definition of path separators.
Definition: Path.h:53
virtual bool exists() const =0
Returns whether the path exists already.
static bool isSeparator(const char character)
Returns whether a given character is a separator.
Path(const std::string &value)
Creates a new path.
Path()=default
Creates a new undefined path.
Type type() const
Returns the type of this path.
Definition: Path.h:191
bool isAbsolute() const
Returns whether this path is absolute.
Definition: Path.h:196
The namespace covering the entire Ocean framework.
Definition: Accessor.h:15