Skip to content

Commit

Permalink
Include filesystem/experimental filesystem conditionally
Browse files Browse the repository at this point in the history
  • Loading branch information
sakertooth committed Dec 3, 2022
1 parent 7f04ed1 commit d2c2c39
Show file tree
Hide file tree
Showing 7 changed files with 49 additions and 26 deletions.
5 changes: 5 additions & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ INCLUDE(PluginList)
INCLUDE(CheckSubmodules)
INCLUDE(AddFileDependencies)
INCLUDE(CheckIncludeFiles)
INCLUDE(CheckIncludeFileCXX)
INCLUDE(FindPkgConfig)
INCLUDE(GenerateExportHeader)

Expand Down Expand Up @@ -154,6 +155,10 @@ CHECK_INCLUDE_FILES(string.h LMMS_HAVE_STRING_H)
CHECK_INCLUDE_FILES(process.h LMMS_HAVE_PROCESS_H)
CHECK_INCLUDE_FILES(locale.h LMMS_HAVE_LOCALE_H)

# Check for filesystem support
CHECK_INCLUDE_FILE_CXX(filesystem LMMS_HAVE_FILESYSTEM)
CHECK_INCLUDE_FILE_CXX(experimental/filesystem LMMS_HAVE_EXPERIMENTAL_FILESYSTEM)

include(CheckLibraryExists)
check_library_exists(rt shm_open "" LMMS_HAVE_LIBRT)

Expand Down
4 changes: 2 additions & 2 deletions include/Sample.h
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ namespace lmms
};

Sample() = default;
Sample(const std::experimental::filesystem::path& sampleFile);
Sample(const fs::path& sampleFile);
Sample(const sampleFrame* data, const int numFrames);
explicit Sample(const SampleBufferV2* buffer);
explicit Sample(const int numFrames);
Expand Down Expand Up @@ -93,7 +93,7 @@ namespace lmms

std::string toBase64() const;

void loadSampleFile(const std::experimental::filesystem::path& sampleFile);
void loadSampleFile(const fs::path& sampleFile);
void loadBase64(const std::string& base64);
void resetMarkers();

Expand Down
31 changes: 19 additions & 12 deletions include/SampleBufferV2.h
Original file line number Diff line number Diff line change
Expand Up @@ -25,23 +25,30 @@
#ifndef SAMPLE_BUFFER_V2_H
#define SAMPLE_BUFFER_V2_H

// TODO: Replace with #include <filesystem> when GHA fully supports it
#include <experimental/filesystem>

#include <memory>
#include <optional>
#include <vector>

#include "AudioEngine.h"
#include "Engine.h"
#include "lmms_basics.h"

// TODO: Replace with just #include <filesystem> when all GHA builds fully support it
#ifdef LMMS_HAVE_FILESYSTEM
#include <filesystem>
namespace fs = std::filesystem;
#elif defined LMMS_HAVE_EXPERIMENTAL_FILESYSTEM
#include <experimental/filesystem>
namespace fs = std::experimental::filesystem;
#else
#error "SampleBufferV2.h: No filesystem support available."
#endif

namespace lmms
{
class SampleBufferV2
{
public:
SampleBufferV2(const std::experimental::filesystem::path& sampleFile);
SampleBufferV2(const fs::path& sampleFile);
SampleBufferV2(const sampleFrame* data, const int numFrames);
explicit SampleBufferV2(const int numFrames);
SampleBufferV2(const SampleBufferV2& other) = delete;
Expand All @@ -51,7 +58,7 @@ namespace lmms
SampleBufferV2& operator=(SampleBufferV2&& other);

const std::vector<sampleFrame>& sampleData() const;
const std::optional<std::experimental::filesystem::path>& filePath() const;
const std::optional<fs::path>& filePath() const;
sample_rate_t sampleRate() const;
int numFrames() const;

Expand All @@ -63,23 +70,23 @@ namespace lmms
* @param filePath
* @return QString
*/
static QString qStringFromFilePath(const std::experimental::filesystem::path& filePath);
static QString qStringFromFilePath(const fs::path& filePath);

/**
* @brief Convert a QString to a STL file path portably.
*
* @param str
* @return std::experimental::filesystem::path
* @return fs::path
*/
static std::experimental::filesystem::path qStringToFilePath(const QString& str);
static fs::path qStringToFilePath(const QString& str);

private:
void loadFromSampleFile(const std::experimental::filesystem::path& sampleFilePath);
void loadFromDrumSynthFile(const std::experimental::filesystem::path& drumSynthFilePath);
void loadFromSampleFile(const fs::path& sampleFilePath);
void loadFromDrumSynthFile(const fs::path& drumSynthFilePath);

private:
std::vector<sampleFrame> m_sampleData;
std::optional<std::experimental::filesystem::path> m_filePath;
std::optional<fs::path> m_filePath;
sample_rate_t m_sampleRate = Engine::audioEngine()->processingSampleRate();
};
}
Expand Down
10 changes: 9 additions & 1 deletion src/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -189,9 +189,17 @@ SET(LMMS_REQUIRED_LIBS ${LMMS_REQUIRED_LIBS}
${FFTW3F_LIBRARIES}
${EXTRA_LIBRARIES}
rpmalloc
stdc++fs
)

# Link filesystem library if std::experimental::filesystem must be used
if(NOT LMMS_HAVE_FILESYSTEM AND LMMS_HAVE_EXPERIMENTAL_FILESYSTEM)
if(CMAKE_CXX_COMPILER_ID MATCHES "GNU")
SET(LMMS_REQUIRED_LIBS ${LMMS_REQUIRED_LIBS} stdc++fs)
elseif(CMAKE_CXX_COMPILER_ID MATCHES "Clang")
SET(LMMS_REQUIRED_LIBS ${LMMS_REQUIRED_LIBS} c++fs)
endif()
endif()

# Expose required libs for tests binary
SET(LMMS_REQUIRED_LIBS ${LMMS_REQUIRED_LIBS} PARENT_SCOPE)

Expand Down
4 changes: 2 additions & 2 deletions src/core/Sample.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ namespace lmms
{
std::array<int, 5> Sample::s_sampleMargin = {64, 64, 64, 4, 4};

Sample::Sample(const std::experimental::filesystem::path& sampleFile)
Sample::Sample(const fs::path& sampleFile)
{
loadSampleFile(sampleFile);
}
Expand Down Expand Up @@ -430,7 +430,7 @@ namespace lmms
return data.toBase64().constData();
}

void Sample::loadSampleFile(const std::experimental::filesystem::path& sampleFile)
void Sample::loadSampleFile(const fs::path& sampleFile)
{
auto cachedSampleBuffer = Engine::sampleBufferCache()->get(sampleFile.generic_string());
m_sampleBuffer = cachedSampleBuffer ? cachedSampleBuffer :
Expand Down
18 changes: 9 additions & 9 deletions src/core/SampleBufferV2.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -30,9 +30,9 @@

namespace lmms
{
SampleBufferV2::SampleBufferV2(const std::experimental::filesystem::path& sampleFile)
SampleBufferV2::SampleBufferV2(const fs::path& sampleFile)
{
if (!std::experimental::filesystem::exists(sampleFile))
if (!fs::exists(sampleFile))
{
throw std::runtime_error{"SampleBufferV2.cpp: Sample file does not exist"};
}
Expand Down Expand Up @@ -79,7 +79,7 @@ namespace lmms
return m_sampleData;
}

const std::optional<std::experimental::filesystem::path>& SampleBufferV2::filePath() const
const std::optional<fs::path>& SampleBufferV2::filePath() const
{
return m_filePath;
}
Expand All @@ -94,7 +94,7 @@ namespace lmms
return m_sampleData.size();
}

QString SampleBufferV2::qStringFromFilePath(const std::experimental::filesystem::path& filePath)
QString SampleBufferV2::qStringFromFilePath(const fs::path& filePath)
{
#ifdef LMMS_BUILD_WIN32
return QString::fromStdWString(filePath);
Expand All @@ -103,16 +103,16 @@ namespace lmms
#endif
}

std::experimental::filesystem::path SampleBufferV2::qStringToFilePath(const QString& str)
fs::path SampleBufferV2::qStringToFilePath(const QString& str)
{
#ifdef LMMS_BUILD_WIN32
return std::experimental::filesystem::path{str.toStdWString()};
return fs::path{str.toStdWString()};
#else
return std::experimental::filesystem::path{str.toStdString()};
return fs::path{str.toStdString()};
#endif
}

void SampleBufferV2::loadFromSampleFile(const std::experimental::filesystem::path& sampleFilePath)
void SampleBufferV2::loadFromSampleFile(const fs::path& sampleFilePath)
{
SF_INFO sfInfo;
sfInfo.format = 0;
Expand Down Expand Up @@ -143,7 +143,7 @@ namespace lmms
}
}

void SampleBufferV2::loadFromDrumSynthFile(const std::experimental::filesystem::path& drumSynthFilePath)
void SampleBufferV2::loadFromDrumSynthFile(const fs::path& drumSynthFilePath)
{
auto ds = DrumSynth();
auto samples = std::make_unique<int16_t>();
Expand Down
3 changes: 3 additions & 0 deletions src/lmmsconfig.h.in
Original file line number Diff line number Diff line change
Expand Up @@ -53,3 +53,6 @@
#cmakedefine LMMS_HAVE_STRING_H
#cmakedefine LMMS_HAVE_PROCESS_H
#cmakedefine LMMS_HAVE_LOCALE_H

#cmakedefine LMMS_HAVE_FILESYSTEM
#cmakedefine LMMS_HAVE_EXPERIMENTAL_FILESYSTEM

0 comments on commit d2c2c39

Please sign in to comment.