Skip to content

Commit 698e27f

Browse files
KKQ-KKQpaulfd
authored andcommitted
C++20 Support
1 parent cc33ed1 commit 698e27f

File tree

9 files changed

+71
-9
lines changed

9 files changed

+71
-9
lines changed

clients/sfizz_render.cpp

+2-1
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
#include "sfizz/MathHelpers.h"
44
#include "sfizz/SfzHelpers.h"
55
#include "sfizz/SIMDHelpers.h"
6+
#include "sfizz/utility/U8Strings.h"
67
#include "MidiHelpers.h"
78
#include <st_audiofile_libs.h>
89
#include <cxxopts.hpp>
@@ -165,7 +166,7 @@ int main(int argc, char** argv)
165166
ERROR_IF(!synth.loadSfzFile(sfzPath), "There was an error loading the SFZ file.");
166167
LOG_INFO(synth.getNumRegions() << " regions in the SFZ.");
167168

168-
fmidi_smf_u midiFile { fmidi_smf_file_read(midiPath.u8string().c_str()) };
169+
fmidi_smf_u midiFile { fmidi_smf_file_read(u8EncodedString(midiPath).c_str()) };
169170
ERROR_IF(!midiFile, "Can't read " << midiPath);
170171

171172
const auto* midiInfo = fmidi_smf_get_info(midiFile.get());

external/threadpool/ThreadPool.h

+12
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,11 @@ class ThreadPool {
1818
ThreadPool(size_t);
1919
template<class F, class... Args>
2020
auto enqueue(F&& f, Args&&... args)
21+
#if __cplusplus >= 201703L
22+
-> std::future<typename std::invoke_result<F, Args...>::type>;
23+
#else
2124
-> std::future<typename std::result_of<F(Args...)>::type>;
25+
#endif
2226
~ThreadPool();
2327
private:
2428
// need to keep track of threads so we can join them
@@ -63,9 +67,17 @@ inline ThreadPool::ThreadPool(size_t threads)
6367
// add new work item to the pool
6468
template<class F, class... Args>
6569
auto ThreadPool::enqueue(F&& f, Args&&... args)
70+
#if __cplusplus >= 201703L
71+
-> std::future<typename std::invoke_result<F, Args...>::type>
72+
#else
6673
-> std::future<typename std::result_of<F(Args...)>::type>
74+
#endif
6775
{
76+
#if __cplusplus >= 201703L
77+
using return_type = typename std::invoke_result<F, Args...>::type;
78+
#else
6879
using return_type = typename std::result_of<F(Args...)>::type;
80+
#endif
6981

7082
auto task = std::make_shared< std::packaged_task<return_type()> >(
7183
std::bind(std::forward<F>(f), std::forward<Args>(args)...)

src/sfizz/Opcode.cpp

+10-3
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66

77
#include "Opcode.h"
88
#include "LFODescription.h"
9+
#include "absl/strings/string_view.h"
910
#include "utility/StringViewHelpers.h"
1011
#include "utility/Debug.h"
1112
#include <absl/strings/ascii.h>
@@ -271,11 +272,10 @@ absl::optional<uint8_t> readNoteValue(absl::string_view value)
271272
///
272273
std::pair<absl::string_view, int> flatSharpPrefixes[] = {
273274
{ "#", +1 },
274-
{ u8"", +1 },
275+
{ (const char*)u8"", +1 },
275276
{ "b", -1 },
276-
{ u8"", -1 },
277+
{ (const char*)u8"", -1 },
277278
};
278-
279279
for (const auto& prefix : flatSharpPrefixes) {
280280
if (absl::StartsWith(value, prefix.first)) {
281281
if (prefix.second == +1) {
@@ -304,6 +304,13 @@ absl::optional<uint8_t> readNoteValue(absl::string_view value)
304304
return static_cast<uint8_t>(noteNumber);
305305
}
306306

307+
#if defined(__cpp_lib_char8_t)
308+
absl::optional<uint8_t> readNoteValue(std::u8string_view value)
309+
{
310+
return readNoteValue(absl::string_view { reinterpret_cast<const char*>(value.data()), value.size() });
311+
}
312+
#endif
313+
307314
absl::optional<bool> readBoolean(absl::string_view value)
308315
{
309316
// Cakewalk-style booleans, case-insensitive

src/sfizz/Opcode.h

+10
Original file line numberDiff line numberDiff line change
@@ -143,6 +143,16 @@ struct Opcode {
143143
*/
144144
absl::optional<uint8_t> readNoteValue(absl::string_view value);
145145

146+
#if defined(__cpp_lib_char8_t)
147+
/**
148+
* @brief Convert a note in string to its equivalent midi note number
149+
*
150+
* @param value
151+
* @return absl::optional<uint8_t>
152+
*/
153+
absl::optional<uint8_t> readNoteValue(std::u8string_view value);
154+
#endif
155+
146156
/**
147157
* @brief Read a boolean value from the sfz file and cast it to the destination parameter.
148158
*/

src/sfizz/Synth.cpp

+2-1
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
#include "Config.h"
99
#include "utility/Debug.h"
1010
#include "utility/Macros.h"
11+
#include "utility/U8Strings.h"
1112
#include "modulations/ModId.h"
1213
#include "modulations/ModKey.h"
1314
#include "modulations/ModMatrix.h"
@@ -702,7 +703,7 @@ void Synth::Impl::finalizeSfzLoad()
702703
filePool.setRootDirectory(rootDirectory);
703704

704705
// a string representation used for OSC purposes
705-
rootPath_ = rootDirectory.u8string();
706+
rootPath_ = u8EncodedString(rootDirectory);
706707

707708
size_t currentRegionIndex = 0;
708709
size_t currentRegionCount = layers_.size();

src/sfizz/import/foreign_instruments/AudioFile.cpp

+3-3
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
// If not, contact the sfizz maintainers at https://github.com/sfztools/sfizz
66

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

3233
bool AudioFileInstrumentFormat::matchesFilePath(const fs::path& path) const
3334
{
34-
const std::string ext = path.extension().u8string();
35-
35+
const std::string ext = u8EncodedString(path.extension());
3636
for (absl::string_view knownExt : kRecognizedAudioExtensions) {
3737
if (absl::EqualsIgnoreCase(ext, knownExt))
3838
return true;
@@ -51,7 +51,7 @@ std::string AudioFileInstrumentImporter::convertToSfz(const fs::path& path) cons
5151
{
5252
std::ostringstream os;
5353
os.imbue(std::locale::classic());
54-
os << "<region>sample=" << path.filename().u8string();
54+
os << "<region>sample=" << u8EncodedString(path.filename());
5555
return os.str();
5656
}
5757

src/sfizz/import/foreign_instruments/DecentSampler.cpp

+2-1
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66

77
#include "DecentSampler.h"
88
#include "sfizz/Opcode.h"
9+
#include "utility/U8Strings.h"
910
#include <absl/strings/match.h>
1011
#include <absl/strings/string_view.h>
1112
#include <absl/memory/memory.h>
@@ -29,7 +30,7 @@ const char* DecentSamplerInstrumentFormat::name() const noexcept
2930

3031
bool DecentSamplerInstrumentFormat::matchesFilePath(const fs::path& path) const
3132
{
32-
const std::string ext = path.extension().u8string();
33+
const std::string ext = u8EncodedString(path.extension());
3334
return absl::EqualsIgnoreCase(ext, ".dspreset");
3435
}
3536

src/sfizz/utility/Size.h

+2
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88

99
#include <type_traits>
1010

11+
#if !defined(__cpp_lib_ssize)
1112
template<class C>
1213
constexpr auto ssize(const C& c)
1314
-> std::common_type_t<std::ptrdiff_t,
@@ -23,3 +24,4 @@ constexpr std::ptrdiff_t ssize(const T (&)[N]) noexcept
2324
{
2425
return N;
2526
}
27+
#endif

src/sfizz/utility/U8Strings.h

+28
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
// SPDX-License-Identifier: BSD-2-Clause
2+
3+
// This code is part of the sfizz library and is licensed under a BSD 2-clause
4+
// license. You should have receive a LICENSE.md file along with the code.
5+
// If not, contact the sfizz maintainers at https://github.com/sfztools/sfizz
6+
7+
#pragma once
8+
9+
#include "ghc/fs_std.hpp"
10+
#include <string>
11+
12+
inline std::string from_u8string(const std::string &s) {
13+
return s;
14+
}
15+
16+
inline std::string from_u8string(std::string &&s) {
17+
return std::move(s);
18+
}
19+
20+
#if defined(__cpp_lib_char8_t)
21+
inline std::string from_u8string(const std::u8string &s) {
22+
return std::string(s.begin(), s.end());
23+
}
24+
#endif
25+
26+
inline std::string u8EncodedString(const fs::path& path) {
27+
return from_u8string(path.u8string());
28+
}

0 commit comments

Comments
 (0)