Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

C++20 Support #1235

Merged
merged 1 commit into from
Feb 15, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion clients/sfizz_render.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
#include "sfizz/MathHelpers.h"
#include "sfizz/SfzHelpers.h"
#include "sfizz/SIMDHelpers.h"
#include "sfizz/utility/U8Strings.h"
#include "MidiHelpers.h"
#include <st_audiofile_libs.h>
#include <cxxopts.hpp>
Expand Down Expand Up @@ -165,7 +166,7 @@ int main(int argc, char** argv)
ERROR_IF(!synth.loadSfzFile(sfzPath), "There was an error loading the SFZ file.");
LOG_INFO(synth.getNumRegions() << " regions in the SFZ.");

fmidi_smf_u midiFile { fmidi_smf_file_read(midiPath.u8string().c_str()) };
fmidi_smf_u midiFile { fmidi_smf_file_read(u8EncodedString(midiPath).c_str()) };
paulfd marked this conversation as resolved.
Show resolved Hide resolved
ERROR_IF(!midiFile, "Can't read " << midiPath);

const auto* midiInfo = fmidi_smf_get_info(midiFile.get());
Expand Down
12 changes: 12 additions & 0 deletions external/threadpool/ThreadPool.h
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,11 @@ class ThreadPool {
ThreadPool(size_t);
template<class F, class... Args>
auto enqueue(F&& f, Args&&... args)
#if __cplusplus >= 201703L
-> std::future<typename std::invoke_result<F, Args...>::type>;
#else
-> std::future<typename std::result_of<F(Args...)>::type>;
#endif
~ThreadPool();
private:
// need to keep track of threads so we can join them
Expand Down Expand Up @@ -63,9 +67,17 @@ inline ThreadPool::ThreadPool(size_t threads)
// add new work item to the pool
template<class F, class... Args>
auto ThreadPool::enqueue(F&& f, Args&&... args)
#if __cplusplus >= 201703L
-> std::future<typename std::invoke_result<F, Args...>::type>
#else
-> std::future<typename std::result_of<F(Args...)>::type>
#endif
{
#if __cplusplus >= 201703L
using return_type = typename std::invoke_result<F, Args...>::type;
#else
using return_type = typename std::result_of<F(Args...)>::type;
#endif

auto task = std::make_shared< std::packaged_task<return_type()> >(
std::bind(std::forward<F>(f), std::forward<Args>(args)...)
Expand Down
13 changes: 10 additions & 3 deletions src/sfizz/Opcode.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@

#include "Opcode.h"
#include "LFODescription.h"
#include "absl/strings/string_view.h"
#include "utility/StringViewHelpers.h"
#include "utility/Debug.h"
#include <absl/strings/ascii.h>
Expand Down Expand Up @@ -271,11 +272,10 @@ absl::optional<uint8_t> readNoteValue(absl::string_view value)
///
std::pair<absl::string_view, int> flatSharpPrefixes[] = {
{ "#", +1 },
{ u8"♯", +1 },
{ (const char*)u8"♯", +1 },
{ "b", -1 },
{ u8"♭", -1 },
{ (const char*)u8"♭", -1 },
};

for (const auto& prefix : flatSharpPrefixes) {
if (absl::StartsWith(value, prefix.first)) {
if (prefix.second == +1) {
Expand Down Expand Up @@ -304,6 +304,13 @@ absl::optional<uint8_t> readNoteValue(absl::string_view value)
return static_cast<uint8_t>(noteNumber);
}

#if defined(__cpp_lib_char8_t)
absl::optional<uint8_t> readNoteValue(std::u8string_view value)
{
return readNoteValue(absl::string_view { reinterpret_cast<const char*>(value.data()), value.size() });
}
#endif

absl::optional<bool> readBoolean(absl::string_view value)
{
// Cakewalk-style booleans, case-insensitive
Expand Down
10 changes: 10 additions & 0 deletions src/sfizz/Opcode.h
Original file line number Diff line number Diff line change
Expand Up @@ -143,6 +143,16 @@ struct Opcode {
*/
absl::optional<uint8_t> readNoteValue(absl::string_view value);

#if defined(__cpp_lib_char8_t)
/**
* @brief Convert a note in string to its equivalent midi note number
*
* @param value
* @return absl::optional<uint8_t>
*/
absl::optional<uint8_t> readNoteValue(std::u8string_view value);
#endif

/**
* @brief Read a boolean value from the sfz file and cast it to the destination parameter.
*/
Expand Down
3 changes: 2 additions & 1 deletion src/sfizz/Synth.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
#include "Config.h"
#include "utility/Debug.h"
#include "utility/Macros.h"
#include "utility/U8Strings.h"
#include "modulations/ModId.h"
#include "modulations/ModKey.h"
#include "modulations/ModMatrix.h"
Expand Down Expand Up @@ -702,7 +703,7 @@ void Synth::Impl::finalizeSfzLoad()
filePool.setRootDirectory(rootDirectory);

// a string representation used for OSC purposes
rootPath_ = rootDirectory.u8string();
rootPath_ = u8EncodedString(rootDirectory);

paulfd marked this conversation as resolved.
Show resolved Hide resolved
size_t currentRegionIndex = 0;
size_t currentRegionCount = layers_.size();
Expand Down
6 changes: 3 additions & 3 deletions src/sfizz/import/foreign_instruments/AudioFile.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
// If not, contact the sfizz maintainers at https://github.com/sfztools/sfizz

#include "AudioFile.h"
#include "utility/U8Strings.h"
#include <absl/strings/match.h>
#include <absl/strings/string_view.h>
#include <absl/memory/memory.h>
Expand All @@ -31,8 +32,7 @@ const char* AudioFileInstrumentFormat::name() const noexcept

bool AudioFileInstrumentFormat::matchesFilePath(const fs::path& path) const
{
const std::string ext = path.extension().u8string();

const std::string ext = u8EncodedString(path.extension());
for (absl::string_view knownExt : kRecognizedAudioExtensions) {
if (absl::EqualsIgnoreCase(ext, knownExt))
return true;
Expand All @@ -51,7 +51,7 @@ std::string AudioFileInstrumentImporter::convertToSfz(const fs::path& path) cons
{
std::ostringstream os;
os.imbue(std::locale::classic());
os << "<region>sample=" << path.filename().u8string();
os << "<region>sample=" << u8EncodedString(path.filename());
return os.str();
}

Expand Down
3 changes: 2 additions & 1 deletion src/sfizz/import/foreign_instruments/DecentSampler.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@

#include "DecentSampler.h"
#include "sfizz/Opcode.h"
#include "utility/U8Strings.h"
#include <absl/strings/match.h>
#include <absl/strings/string_view.h>
#include <absl/memory/memory.h>
Expand All @@ -29,7 +30,7 @@ const char* DecentSamplerInstrumentFormat::name() const noexcept

bool DecentSamplerInstrumentFormat::matchesFilePath(const fs::path& path) const
{
const std::string ext = path.extension().u8string();
const std::string ext = u8EncodedString(path.extension());
return absl::EqualsIgnoreCase(ext, ".dspreset");
}

Expand Down
2 changes: 2 additions & 0 deletions src/sfizz/utility/Size.h
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@

#include <type_traits>

#if !defined(__cpp_lib_ssize)
template<class C>
constexpr auto ssize(const C& c)
-> std::common_type_t<std::ptrdiff_t,
Expand All @@ -23,3 +24,4 @@ constexpr std::ptrdiff_t ssize(const T (&)[N]) noexcept
{
return N;
}
#endif
28 changes: 28 additions & 0 deletions src/sfizz/utility/U8Strings.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
// SPDX-License-Identifier: BSD-2-Clause

// This code is part of the sfizz library and is licensed under a BSD 2-clause
// license. You should have receive a LICENSE.md file along with the code.
// If not, contact the sfizz maintainers at https://github.com/sfztools/sfizz

#pragma once

#include "ghc/fs_std.hpp"
#include <string>

inline std::string from_u8string(const std::string &s) {
return s;
}

inline std::string from_u8string(std::string &&s) {
return std::move(s);
}

#if defined(__cpp_lib_char8_t)
inline std::string from_u8string(const std::u8string &s) {
return std::string(s.begin(), s.end());
}
#endif

inline std::string u8EncodedString(const fs::path& path) {
return from_u8string(path.u8string());
}
Loading