Skip to content

Commit

Permalink
C++20 Support
Browse files Browse the repository at this point in the history
  • Loading branch information
KKQ-KKQ authored and paulfd committed Feb 4, 2024
1 parent f8fc628 commit d917a5c
Show file tree
Hide file tree
Showing 9 changed files with 66 additions and 9 deletions.
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(from_u8string(midiPath.u8string()).c_str()) };
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_ = from_u8string(rootDirectory.u8string());

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 = from_u8string(path.extension().u8string());
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=" << from_u8string(path.filename().u8string());
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 = from_u8string(path.extension().u8string());
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
23 changes: 23 additions & 0 deletions src/sfizz/utility/U8Strings.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
// 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 <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

0 comments on commit d917a5c

Please sign in to comment.