Skip to content

Commit

Permalink
Fix all compile issues
Browse files Browse the repository at this point in the history
  • Loading branch information
ChristianFeldmann committed Oct 13, 2024
1 parent d4dee4f commit b250800
Show file tree
Hide file tree
Showing 35 changed files with 503 additions and 449 deletions.
22 changes: 20 additions & 2 deletions YUViewLib/src/common/Formatting.h
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ template <typename T> std::string to_string(const std::vector<T> vec)

static std::ostream &operator<<(std::ostream &stream, const Size &size)
{
stream << "(" << size.width << "x" << size.height << ")";
stream << size.width << "x" << size.height;
return stream;
}

Expand All @@ -89,13 +89,31 @@ inline std::string to_string(const bool b)
return b ? "True" : "False";
}

template <typename T>
static std::ostream &operator<<(std::ostream &stream, const std::optional<T> &opt)
{
if (opt)
stream << opt;
else
stream << "NA";
return stream;
}

template <typename T> inline std::string to_string(const std::optional<T> &opt)
{
std::ostringstream stream;
stream << opt;
return stream.str();
}

inline std::string stringReplaceAll(std::string str, char value, char replacement)
{
std::replace(str.begin(), str.end(), value, replacement);
return str;
}

inline std::string stringReplaceAll(std::string str, std::initializer_list<char> values, char replacement)
inline std::string
stringReplaceAll(std::string str, std::initializer_list<char> values, char replacement)
{
std::replace_if(
str.begin(),
Expand Down
9 changes: 9 additions & 0 deletions YUViewLib/src/common/InfoItemAndData.h
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,15 @@ struct InfoItem
std::string description{};

InfoItem(std::string &&name, std::string &&text) : name(std::move(name)), text(std::move(text)) {}
InfoItem(std::string &&name, std::string &&text, std::string &&description)
: name(std::move(name)), text(std::move(text)), description(std::move(description))
{
}
InfoItem(std::string_view name, std::string_view text) : name(name), text(text) {}
InfoItem(std::string_view name, std::string_view text, std::string_view description)
: name(name), text(text), description(description)
{
}
};

struct InfoData
Expand Down
7 changes: 4 additions & 3 deletions YUViewLib/src/common/Typedef.h
Original file line number Diff line number Diff line change
Expand Up @@ -279,6 +279,7 @@ struct Size
{
return this->width != other.width || this->height != other.height;
}
explicit operator bool() const { return this->isValid(); }
constexpr bool isValid() const { return this->width > 0 && this->height > 0; }
unsigned width{};
unsigned height{};
Expand Down Expand Up @@ -345,7 +346,7 @@ Q_DECLARE_METATYPE(recacheIndicator)
template <typename... Args> struct QNonConstOverload
{
template <typename R, typename T>
Q_DECL_CONSTEXPR auto operator()(R (T::*ptr)(Args...)) const Q_DECL_NOTHROW -> decltype(ptr)
Q_DECL_CONSTEXPR auto operator()(R (T::*ptr)(Args...)) const Q_DECL_NOTHROW->decltype(ptr)
{
return ptr;
}
Expand All @@ -358,7 +359,7 @@ template <typename... Args> struct QNonConstOverload
template <typename... Args> struct QConstOverload
{
template <typename R, typename T>
Q_DECL_CONSTEXPR auto operator()(R (T::*ptr)(Args...) const) const Q_DECL_NOTHROW -> decltype(ptr)
Q_DECL_CONSTEXPR auto operator()(R (T::*ptr)(Args...) const) const Q_DECL_NOTHROW->decltype(ptr)
{
return ptr;
}
Expand All @@ -375,7 +376,7 @@ template <typename... Args> struct QOverload : QConstOverload<Args...>, QNonCons
using QNonConstOverload<Args...>::of;
using QNonConstOverload<Args...>::operator();
template <typename R>
Q_DECL_CONSTEXPR auto operator()(R (*ptr)(Args...)) const Q_DECL_NOTHROW -> decltype(ptr)
Q_DECL_CONSTEXPR auto operator()(R (*ptr)(Args...)) const Q_DECL_NOTHROW->decltype(ptr)
{
return ptr;
}
Expand Down
45 changes: 29 additions & 16 deletions YUViewLib/src/filesource/FileSource.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@

#include "FileSource.h"

#include <common/Formatting.h>
#include <common/Typedef.h>

#include <format>
Expand All @@ -57,7 +58,7 @@ FileSource::FileSource()
&FileSource::fileSystemWatcherFileChanged);
}

bool FileSource::openFile(const std::string &filePath)
bool FileSource::openFile(const std::filesystem::path &filePath)
{
if (!std::filesystem::is_regular_file(filePath))
return false;
Expand Down Expand Up @@ -117,35 +118,47 @@ std::vector<InfoItem> FileSource::getFileInfoList() const
if (!this->isFileOpened)
return {};

std::vector<InfoItem> infoList;
infoList.emplace_back("File Path", this->fullFilePath);
infoList.emplace_back("Time Modified", std::format())
// For now we still use the QFileInfo. There is no easy cross platform formatting
// for the std::filesystem::file_time_type. This is added in C++ 20.
QFileInfo fileInfo(QString::fromStdString(this->fullFilePath.string()));

std::vector<InfoItem> infoList;

infoList.emplace_back("File Path", this->fullFilePath);
#if QT_VERSION < QT_VERSION_CHECK(5, 10, 0)
auto createdtime = this->fileInfo.created().toString("yyyy-MM-dd hh:mm:ss");
const auto createdtime = this->fileInfo.created().toString("yyyy-MM-dd hh:mm:ss");
#else
auto createdtime = this->fileInfo.birthTime().toString("yyyy-MM-dd hh:mm:ss");
const auto createdtime = fileInfo.birthTime().toString("yyyy-MM-dd hh:mm:ss");
#endif
infoList.emplace_back("Time Created", createdtime.toStdString());
infoList.emplace_back("Time Modified",
this->fileInfo.lastModified().toString("yyyy-MM-dd hh:mm:ss"));
infoList.emplace_back("Nr Bytes", QString("%1").arg(this->fileInfo.size()));
fileInfo.lastModified().toString("yyyy-MM-dd hh:mm:ss").toStdString());

if (const auto size = this->getFileSize())
infoList.emplace_back("Nr Bytes", to_string(this->getFileSize()));

return infoList;
}

int64_t FileSource::getFileSize() const
std::optional<int64_t> FileSource::getFileSize() const
{
if (!this->isFileOpened)
return -1;
return {};

return !isFileOpened ? -1 : fileInfo.size();
try
{
const auto size = std::filesystem::file_size(this->fullFilePath);
return static_cast<int64_t>(size);
}
catch (const std::filesystem::filesystem_error &e)
{
return {};
}
}

QString FileSource::getAbsoluteFilePath() const
std::string FileSource::getAbsoluteFilePath() const
{
return this->isFileOpened ? this->fileInfo.absoluteFilePath() : QString();
return this->isFileOpened ? this->fullFilePath.string() : "";
}

// If you are loading a playlist and you have an absolute path and a relative path, this function
Expand Down Expand Up @@ -184,9 +197,9 @@ void FileSource::updateFileWatchSetting()
// The addPath/removePath functions will do nothing if called twice for the same file.
QSettings settings;
if (settings.value("WatchFiles", true).toBool())
fileWatcher.addPath(this->fullFilePath);
fileWatcher.addPath(QString::fromStdString(this->fullFilePath.string()));
else
fileWatcher.removePath(this->fullFilePath);
fileWatcher.removePath(QString::fromStdString(this->fullFilePath.string()));
}

void FileSource::clearFileCache()
Expand All @@ -202,7 +215,7 @@ void FileSource::clearFileCache()
QMutexLocker locker(&this->readMutex);
this->srcFile.close();

LPCWSTR file = (const wchar_t *)this->fullFilePath.utf16();
LPCWSTR file = this->fullFilePath.wstring().c_str();
HANDLE hFile =
CreateFile(file, GENERIC_READ, 0, NULL, OPEN_EXISTING, FILE_FLAG_NO_BUFFERING, NULL);
CloseHandle(hFile);
Expand Down
10 changes: 5 additions & 5 deletions YUViewLib/src/filesource/FileSource.h
Original file line number Diff line number Diff line change
Expand Up @@ -70,10 +70,10 @@ class FileSource : public QObject
public:
FileSource();

virtual bool openFile(const std::string &filePath);
virtual bool openFile(const std::filesystem::path &filePath);

virtual std::vector<InfoItem> getFileInfoList() const;
int64_t getFileSize() const;
std::optional<int64_t> getFileSize() const;
std::string getAbsoluteFilePath() const;
QFile *getQFile() { return &this->srcFile; }
bool getAndResetFileChangedFlag();
Expand Down Expand Up @@ -106,9 +106,9 @@ private slots:
void fileSystemWatcherFileChanged(const QString &) { fileChanged = true; }

protected:
std::string fullFilePath{};
QFile srcFile;
bool isFileOpened{};
std::filesystem::path fullFilePath{};
QFile srcFile;
bool isFileOpened{};

private:
QFileSystemWatcher fileWatcher{};
Expand Down
Loading

0 comments on commit b250800

Please sign in to comment.