Path.hpp
Go to the documentation of this file.
1 #pragma once
2 #if(__cplusplus >= 201703L) || (_MSVC_LANG >= 201703L)
3  #define DEPTHAI_NODISCARD [[nodiscard]]
4  #if(defined(_MSC_VER)) || (defined(__has_include) && __has_include(<filesystem>)) || (__cplusplus >= 202002L)
5  #include <filesystem>
6  #endif
7 #else
8  #define DEPTHAI_NODISCARD
9 #endif
10 #include <string>
11 
12 namespace dai {
13 
14 // TODO C++20 char8_t
15 // TODO test if caller works when replace "dai::Path" -> "std::filesystem::path"
16 // TODO test if can `using dai::Path = std::filesystem::path` on C++17 to completely use STL
17 
27 class Path {
28  public:
29 #if defined(_WIN32) && defined(_MSC_VER)
30  using value_type = wchar_t;
31 #else
32  using value_type = char;
33 #endif
34  using string_type = std::basic_string<value_type>;
35 
36  Path() = default;
37  ~Path() = default;
38  Path(const Path&) = default;
39  Path(Path&&) = default;
40  Path& operator=(const Path&) = default;
41  Path& operator=(Path&&) = default;
42 
48  Path(string_type&& source) noexcept : _nativePath(std::move(source)) {}
49 
55  Path(const string_type& source) : _nativePath(source) {}
56 
62  Path(const value_type* source) : _nativePath(string_type(source)) {}
63 
64 #if defined(__cpp_lib_filesystem)
65 
70  Path(const std::filesystem::path& source) : _nativePath(source) {}
71 
72  #if defined(__cpp_lib_char8_t)
73 
78  Path(const std::u8string& source) : Path(std::filesystem::path(source)) {}
79 
85  Path(const char8_t* source) : Path(std::filesystem::path(source)) {}
86  #endif
87 #endif
88 
89 #if defined(_WIN32) && defined(_MSC_VER)
90  private:
91  static std::wstring convert_utf8_to_wide(const std::string& utf8string);
92 
93  public:
99  Path(const std::string& source) : _nativePath(convert_utf8_to_wide(source)) {}
100 
106  Path(const char* source) : _nativePath(convert_utf8_to_wide(std::string(source))) {}
107 
116  std::string string() const;
117 
118  #if defined(__cpp_lib_char8_t)
119 
126  std::u8string u8string() const {
127  return std::filesystem::path(_nativePath).u8string();
128  }
129  #else
130 
137  std::string u8string() const;
138  #endif
139 #else
140 
145  std::string string() const {
146  return _nativePath;
147  }
148 
149  #if defined(__cpp_lib_char8_t)
150 
155  std::u8string u8string() const {
156  return std::filesystem::path(_nativePath).u8string();
157  }
158  #else
159 
164  std::string u8string() const {
165  return _nativePath;
166  }
167  #endif
168 #endif
169 
175  operator string_type() const noexcept {
176  return _nativePath;
177  }
178 
184  const string_type& native() const noexcept {
185  return _nativePath;
186  }
187 
193  // TODO add back DEPTHAI_NODISCARD once sphinx fixes are in place
194  bool empty() const noexcept {
195  return _nativePath.empty();
196  }
197 
198  private:
200 };
201 
202 } // namespace dai
dai::Path::u8string
std::string u8string() const
Get path in utf-8.
Definition: Path.hpp:164
dai::Path::empty
bool empty() const noexcept
Observes if path is empty (contains no string/folders/filename)
Definition: Path.hpp:194
dai::Path::Path
Path(string_type &&source) noexcept
Construct Path object from source.
Definition: Path.hpp:48
dai::Path::value_type
char value_type
character used by native-encoding of filesystem
Definition: Path.hpp:32
dai::Path::operator=
Path & operator=(const Path &)=default
dai::Path::string
std::string string() const
Get path in native-encoding string; no conversion.
Definition: Path.hpp:145
dai::Path::Path
Path(const value_type *source)
Construct Path object from source.
Definition: Path.hpp:62
dai::Path::~Path
~Path()=default
dai::Path::string_type
std::basic_string< value_type > string_type
Definition: Path.hpp:34
dai::Path::Path
Path()=default
std
Definition: Node.hpp:366
dai::Path::Path
Path(const string_type &source)
Construct Path object from source.
Definition: Path.hpp:55
dai::Path::_nativePath
string_type _nativePath
Definition: Path.hpp:199
dai::Path
Represents paths on a filesystem; accepts utf-8, Windows utf-16 wchar_t, or std::filesystem::path.
Definition: Path.hpp:27
dai::Path::native
const string_type & native() const noexcept
Returns native-encoding string by const reference, suitable for use with OS APIs.
Definition: Path.hpp:184
dai
Definition: CameraExposureOffset.hpp:6


depthai
Author(s): Martin Peterlin
autogenerated on Sat Mar 22 2025 02:58:19