fs/FileInfo: add constexpr and noexcept

This commit is contained in:
Max Kellermann 2023-10-07 10:19:56 +02:00
parent 19c73680cf
commit 51c0a03e94

View File

@ -1,8 +1,7 @@
// SPDX-License-Identifier: GPL-2.0-or-later // SPDX-License-Identifier: GPL-2.0-or-later
// Copyright The Music Player Daemon Project // Copyright The Music Player Daemon Project
#ifndef MPD_FS_FILE_INFO_HXX #pragma once
#define MPD_FS_FILE_INFO_HXX
#include "Path.hxx" #include "Path.hxx"
#include "lib/fmt/PathFormatter.hxx" #include "lib/fmt/PathFormatter.hxx"
@ -19,7 +18,7 @@
class FileInfo { class FileInfo {
friend bool GetFileInfo(Path path, FileInfo &info, friend bool GetFileInfo(Path path, FileInfo &info,
bool follow_symlinks); bool follow_symlinks) noexcept;
friend class FileReader; friend class FileReader;
#ifdef _WIN32 #ifdef _WIN32
@ -29,7 +28,7 @@ class FileInfo {
#endif #endif
public: public:
FileInfo() = default; constexpr FileInfo() noexcept = default;
FileInfo(Path path, bool follow_symlinks=true) { FileInfo(Path path, bool follow_symlinks=true) {
if (!GetFileInfo(path, *this, follow_symlinks)) { if (!GetFileInfo(path, *this, follow_symlinks)) {
@ -41,7 +40,7 @@ public:
} }
} }
bool IsRegular() const { constexpr bool IsRegular() const noexcept {
#ifdef _WIN32 #ifdef _WIN32
return (data.dwFileAttributes & return (data.dwFileAttributes &
(FILE_ATTRIBUTE_DIRECTORY|FILE_ATTRIBUTE_DEVICE)) == 0; (FILE_ATTRIBUTE_DIRECTORY|FILE_ATTRIBUTE_DEVICE)) == 0;
@ -50,7 +49,7 @@ public:
#endif #endif
} }
bool IsDirectory() const { constexpr bool IsDirectory() const noexcept {
#ifdef _WIN32 #ifdef _WIN32
return data.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY; return data.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY;
#else #else
@ -58,7 +57,7 @@ public:
#endif #endif
} }
uint64_t GetSize() const { constexpr uint64_t GetSize() const noexcept {
#ifdef _WIN32 #ifdef _WIN32
return ConstructUint64(data.nFileSizeLow, data.nFileSizeHigh); return ConstructUint64(data.nFileSizeLow, data.nFileSizeHigh);
#else #else
@ -66,7 +65,8 @@ public:
#endif #endif
} }
std::chrono::system_clock::time_point GetModificationTime() const { [[gnu::pure]]
std::chrono::system_clock::time_point GetModificationTime() const noexcept {
#ifdef _WIN32 #ifdef _WIN32
return FileTimeToChrono(data.ftLastWriteTime); return FileTimeToChrono(data.ftLastWriteTime);
#else #else
@ -75,26 +75,26 @@ public:
} }
#ifndef _WIN32 #ifndef _WIN32
uid_t GetUid() const { constexpr uid_t GetUid() const noexcept {
return st.st_uid; return st.st_uid;
} }
mode_t GetMode() const { constexpr mode_t GetMode() const noexcept {
return st.st_mode; return st.st_mode;
} }
dev_t GetDevice() const { constexpr dev_t GetDevice() const noexcept {
return st.st_dev; return st.st_dev;
} }
ino_t GetInode() const { constexpr ino_t GetInode() const noexcept {
return st.st_ino; return st.st_ino;
} }
#endif #endif
}; };
inline bool inline bool
GetFileInfo(Path path, FileInfo &info, bool follow_symlinks=true) GetFileInfo(Path path, FileInfo &info, bool follow_symlinks=true) noexcept
{ {
#ifdef _WIN32 #ifdef _WIN32
(void)follow_symlinks; (void)follow_symlinks;
@ -107,5 +107,3 @@ GetFileInfo(Path path, FileInfo &info, bool follow_symlinks=true)
return ret == 0; return ret == 0;
#endif #endif
} }
#endif