Program Listing for File Path.hpp

Return to documentation for file (include/depthai/utility/Path.hpp)

#pragma once
#if(__cplusplus >= 201703L) || (_MSVC_LANG >= 201703L)
    #define DEPTHAI_NODISCARD [[nodiscard]]
    #if(defined(_MSC_VER)) || (defined(__has_include) && __has_include(<filesystem>)) || (__cplusplus >= 202002L)
        #include <filesystem>
    #endif
#else
    #define DEPTHAI_NODISCARD
#endif
#include <string>

namespace dai {

// TODO C++20 char8_t
// TODO test if caller works when replace "dai::Path" -> "std::filesystem::path"
// TODO test if can `using dai::Path = std::filesystem::path` on C++17 to completely use STL

class Path {
   public:
#if defined(_WIN32) && defined(_MSC_VER)
    using value_type = wchar_t;
#else
    using value_type = char;
#endif
    using string_type = std::basic_string<value_type>;

    Path() = default;
    ~Path() = default;
    Path(const Path&) = default;
    Path(Path&&) = default;
    Path& operator=(const Path&) = default;
    Path& operator=(Path&&) = default;

    Path(string_type&& source) noexcept : _nativePath(std::move(source)) {}

    Path(const string_type& source) : _nativePath(source) {}

    Path(const value_type* source) : _nativePath(string_type(source)) {}

#if defined(__cpp_lib_filesystem)
    Path(const std::filesystem::path& source) : _nativePath(source) {}

    #if defined(__cpp_lib_char8_t)
    Path(const std::u8string& source) : Path(std::filesystem::path(source)) {}

    Path(const char8_t* source) : Path(std::filesystem::path(source)) {}
    #endif
#endif

#if defined(_WIN32) && defined(_MSC_VER)
   private:
    static std::wstring convert_utf8_to_wide(const std::string& utf8string);

   public:
    Path(const std::string& source) : _nativePath(convert_utf8_to_wide(source)) {}

    Path(const char* source) : _nativePath(convert_utf8_to_wide(std::string(source))) {}

    std::string string() const;

    #if defined(__cpp_lib_char8_t)
    std::u8string u8string() const {
        return std::filesystem::path(_nativePath).u8string();
    }
    #else
    std::string u8string() const;
    #endif
#else
    std::string string() const {
        return _nativePath;
    }

    #if defined(__cpp_lib_char8_t)
    std::u8string u8string() const {
        return std::filesystem::path(_nativePath).u8string();
    }
    #else
    std::string u8string() const {
        return _nativePath;
    }
    #endif
#endif

    operator string_type() const noexcept {
        return _nativePath;
    }

    const string_type& native() const noexcept {
        return _nativePath;
    }

    // TODO add back DEPTHAI_NODISCARD once sphinx fixes are in place
    bool empty() const noexcept {
        return _nativePath.empty();
    }

   private:
    string_type _nativePath;
};

}  // namespace dai