From 44378b7dbefa906033e18ce9104e892055167b5f Mon Sep 17 00:00:00 2001 From: Rosen Penev Date: Thu, 22 Oct 2020 01:36:13 -0700 Subject: [PATCH 01/15] use structured binding declarations Shorter. Signed-off-by: Rosen Penev --- src/RemoteTagCache.cxx | 14 ++++++-------- src/client/Subscribe.cxx | 3 +-- src/db/Count.cxx | 12 +++++------- src/db/DatabasePrint.cxx | 6 +++--- src/db/plugins/simple/Directory.cxx | 7 ++----- src/db/update/Walk.cxx | 8 +++----- src/input/plugins/CurlInputPlugin.cxx | 4 ++-- src/input/plugins/QobuzClient.cxx | 12 ++++++------ src/lib/curl/Form.cxx | 10 +++++----- src/neighbor/plugins/UdisksNeighborPlugin.cxx | 4 ++-- src/output/Print.cxx | 5 ++--- src/output/plugins/WasapiOutputPlugin.cxx | 8 +++++--- src/sticker/Print.cxx | 4 ++-- src/tag/GenParseName.cxx | 5 +---- 14 files changed, 45 insertions(+), 57 deletions(-) diff --git a/src/RemoteTagCache.cxx b/src/RemoteTagCache.cxx index 78e409660..1e75c3a1f 100644 --- a/src/RemoteTagCache.cxx +++ b/src/RemoteTagCache.cxx @@ -42,9 +42,9 @@ RemoteTagCache::Lookup(const std::string &uri) noexcept std::unique_lock lock(mutex); KeyMap::insert_commit_data hint; - auto result = map.insert_check(uri, Item::Hash(), Item::Equal(), hint); - if (result.second) { - auto *item = new Item(*this, uri); + auto [tag, value] = map.insert_check(uri, Item::Hash(), Item::Equal(), hint); + if (value) { + auto item = new Item(*this, uri); map.insert_commit(*item, hint); waiting_list.push_back(*item); lock.unlock(); @@ -70,15 +70,13 @@ RemoteTagCache::Lookup(const std::string &uri) noexcept ItemResolved(*item); return; } - } else if (result.first->scanner) { + } else if (tag->scanner) { /* already scanning this one - no-op */ } else { /* already finished: re-invoke the handler */ - auto &item = *result.first; - - idle_list.erase(waiting_list.iterator_to(item)); - invoke_list.push_back(item); + idle_list.erase(waiting_list.iterator_to(*tag)); + invoke_list.push_back(*tag); ScheduleInvokeHandlers(); } diff --git a/src/client/Subscribe.cxx b/src/client/Subscribe.cxx index 5091a454d..daf18f3ac 100644 --- a/src/client/Subscribe.cxx +++ b/src/client/Subscribe.cxx @@ -34,8 +34,7 @@ Client::Subscribe(const char *channel) noexcept if (num_subscriptions >= MAX_SUBSCRIPTIONS) return Client::SubscribeResult::FULL; - auto r = subscriptions.insert(channel); - if (!r.second) + if (!subscriptions.insert(channel).second) return Client::SubscribeResult::ALREADY; ++num_subscriptions; diff --git a/src/db/Count.cxx b/src/db/Count.cxx index efcda1169..695991c5b 100644 --- a/src/db/Count.cxx +++ b/src/db/Count.cxx @@ -57,9 +57,9 @@ Print(Response &r, TagType group, const TagCountMap &m) noexcept { assert(unsigned(group) < TAG_NUM_OF_ITEM_TYPES); - for (const auto &i : m) { - tag_print(r, group, i.first.c_str()); - PrintSearchStats(r, i.second); + for (const auto &[tag, stats] : m) { + tag_print(r, group, tag.c_str()); + PrintSearchStats(r, stats); } } @@ -68,8 +68,7 @@ stats_visitor_song(SearchStats &stats, const LightSong &song) noexcept { stats.n_songs++; - const auto duration = song.GetDuration(); - if (!duration.IsNegative()) + if (const auto duration = song.GetDuration(); !duration.IsNegative()) stats.total_duration += duration; } @@ -77,8 +76,7 @@ static void CollectGroupCounts(TagCountMap &map, const Tag &tag, const char *value) noexcept { - auto r = map.insert(std::make_pair(value, SearchStats())); - SearchStats &s = r.first->second; + auto &s = map.insert(std::make_pair(value, SearchStats())).first->second; ++s.n_songs; if (!tag.duration.IsNegative()) s.total_duration += tag.duration; diff --git a/src/db/DatabasePrint.cxx b/src/db/DatabasePrint.cxx index f3f604374..46732b3b6 100644 --- a/src/db/DatabasePrint.cxx +++ b/src/db/DatabasePrint.cxx @@ -195,11 +195,11 @@ PrintUniqueTags(Response &r, ConstBuffer tag_types, const char *const name = tag_item_names[tag_types.front()]; tag_types.pop_front(); - for (const auto &i : map) { - r.Format("%s: %s\n", name, i.first.c_str()); + for (const auto &[key, tag] : map) { + r.Format("%s: %s\n", name, key.c_str()); if (!tag_types.empty()) - PrintUniqueTags(r, tag_types, i.second); + PrintUniqueTags(r, tag_types, tag); } } diff --git a/src/db/plugins/simple/Directory.cxx b/src/db/plugins/simple/Directory.cxx index 1465922eb..4c7bb2873 100644 --- a/src/db/plugins/simple/Directory.cxx +++ b/src/db/plugins/simple/Directory.cxx @@ -138,13 +138,10 @@ Directory::LookupDirectory(std::string_view _uri) noexcept Directory *d = this; do { - auto s = uri.Split(PathTraitsUTF8::SEPARATOR); - if (s.first.empty()) + auto [name, rest] = uri.Split(PathTraitsUTF8::SEPARATOR); + if (name.empty()) break; - const auto name = s.first; - const auto rest = s.second; - Directory *tmp = d->FindChild(name); if (tmp == nullptr) /* not found */ diff --git a/src/db/update/Walk.cxx b/src/db/update/Walk.cxx index 53ae6fec5..6391ed445 100644 --- a/src/db/update/Walk.cxx +++ b/src/db/update/Walk.cxx @@ -427,21 +427,19 @@ UpdateWalk::DirectoryMakeUriParentChecked(Directory &root, StringView uri(_uri); while (true) { - auto s = uri.Split('/'); - const std::string_view name = s.first; - const auto rest = s.second; + auto [name, rest] = uri.Split('/'); if (rest == nullptr) break; if (!name.empty()) { directory = DirectoryMakeChildChecked(*directory, std::string(name).c_str(), - s.first); + name); if (directory == nullptr) break; } - uri = s.second; + uri = rest; } return directory; diff --git a/src/input/plugins/CurlInputPlugin.cxx b/src/input/plugins/CurlInputPlugin.cxx index fb6e7a892..86647f550 100644 --- a/src/input/plugins/CurlInputPlugin.cxx +++ b/src/input/plugins/CurlInputPlugin.cxx @@ -402,8 +402,8 @@ CurlInputStream::CurlInputStream(EventLoop &event_loop, const char *_url, { request_headers.Append("Icy-Metadata: 1"); - for (const auto &i : headers) - request_headers.Append((i.first + ":" + i.second).c_str()); + for (const auto &[key, header] : headers) + request_headers.Append((key + ":" + header).c_str()); } CurlInputStream::~CurlInputStream() noexcept diff --git a/src/input/plugins/QobuzClient.cxx b/src/input/plugins/QobuzClient.cxx index 0e47aee21..92e74c588 100644 --- a/src/input/plugins/QobuzClient.cxx +++ b/src/input/plugins/QobuzClient.cxx @@ -174,8 +174,8 @@ QobuzClient::MakeUrl(const char *object, const char *method, uri += method; QueryStringBuilder q; - for (const auto &i : query) - q(uri, i.first.c_str(), i.second.c_str()); + for (const auto &[key, url] : query) + q(uri, key.c_str(), url.c_str()); q(uri, "app_id", app_id); return uri; @@ -195,11 +195,11 @@ QobuzClient::MakeSignedUrl(const char *object, const char *method, QueryStringBuilder q; std::string concatenated_query(object); concatenated_query += method; - for (const auto &i : query) { - q(uri, i.first.c_str(), i.second.c_str()); + for (const auto &[key, url] : query) { + q(uri, key.c_str(), url.c_str()); - concatenated_query += i.first; - concatenated_query += i.second; + concatenated_query += key; + concatenated_query += url; } q(uri, "app_id", app_id); diff --git a/src/lib/curl/Form.cxx b/src/lib/curl/Form.cxx index 65430641d..5b668ec46 100644 --- a/src/lib/curl/Form.cxx +++ b/src/lib/curl/Form.cxx @@ -36,16 +36,16 @@ EncodeForm(CURL *curl, { std::string result; - for (const auto &i : fields) { + for (const auto &[key, field] : fields) { if (!result.empty()) result.push_back('&'); - result.append(i.first); + result.append(key); result.push_back('='); - if (!i.second.empty()) { - CurlString value(curl_easy_escape(curl, i.second.data(), - i.second.length())); + if (!field.empty()) { + CurlString value( + curl_easy_escape(curl, field.data(), field.length())); if (value) result.append(value); } diff --git a/src/neighbor/plugins/UdisksNeighborPlugin.cxx b/src/neighbor/plugins/UdisksNeighborPlugin.cxx index 5ceacc4e1..605c32a93 100644 --- a/src/neighbor/plugins/UdisksNeighborPlugin.cxx +++ b/src/neighbor/plugins/UdisksNeighborPlugin.cxx @@ -182,8 +182,8 @@ UdisksNeighborExplorer::GetList() const noexcept NeighborExplorer::List result; - for (const auto &i : by_uri) - result.emplace_front(i.second); + for (const auto &[t, r] : by_uri) + result.emplace_front(r); return result; } diff --git a/src/output/Print.cxx b/src/output/Print.cxx index f649bfb52..9f8edf193 100644 --- a/src/output/Print.cxx +++ b/src/output/Print.cxx @@ -40,8 +40,7 @@ printAudioDevices(Response &r, const MultipleOutputs &outputs) ao.GetName(), ao.GetPluginName(), ao.IsEnabled()); - for (const auto &a : ao.GetAttributes()) - r.Format("attribute: %s=%s\n", - a.first.c_str(), a.second.c_str()); + for (const auto &[attribute, value] : ao.GetAttributes()) + r.Format("attribute: %s=%s\n", attribute.c_str(), value.c_str()); } } diff --git a/src/output/plugins/WasapiOutputPlugin.cxx b/src/output/plugins/WasapiOutputPlugin.cxx index 9cdf8e00e..99f7dacf4 100644 --- a/src/output/plugins/WasapiOutputPlugin.cxx +++ b/src/output/plugins/WasapiOutputPlugin.cxx @@ -632,9 +632,11 @@ void WasapiOutput::OpenDevice() { CLSCTX_INPROC_SERVER); if (enumerate_devices && SafeTry([this]() { EnumerateDevices(); })) { - for (const auto &desc : device_desc) { - FormatNotice(wasapi_output_domain, "Device \"%u\" \"%s\"", - desc.first, desc.second.c_str()); + for (const auto &[device, desc] : device_desc) { + FormatNotice(wasapi_output_domain, + "Device \"%u\" \"%s\"", + device, + desc.c_str()); } } diff --git a/src/sticker/Print.cxx b/src/sticker/Print.cxx index fb7a7a85b..b60325b3c 100644 --- a/src/sticker/Print.cxx +++ b/src/sticker/Print.cxx @@ -31,6 +31,6 @@ sticker_print_value(Response &r, void sticker_print(Response &r, const Sticker &sticker) { - for (const auto &i : sticker.table) - sticker_print_value(r, i.first.c_str(), i.second.c_str()); + for (const auto &[name, val] : sticker.table) + sticker_print_value(r, name.c_str(), val.c_str()); } diff --git a/src/tag/GenParseName.cxx b/src/tag/GenParseName.cxx index 88b0c2997..9c493b48f 100644 --- a/src/tag/GenParseName.cxx +++ b/src/tag/GenParseName.cxx @@ -60,10 +60,7 @@ main(int argc, char **argv) char first = 0; - for (const auto &i : names) { - const std::string_view name = i.first; - const TagType tag = i.second; - + for (const auto &[name, tag] : names) { if (name.front() != first) { if (first != 0) fprintf(out, " break;\n\n"); From 4d2d0e7bb89bb9e23411fd77d9b02c779bfdf6ef Mon Sep 17 00:00:00 2001 From: Max Kellermann Date: Fri, 5 Mar 2021 13:14:43 +0100 Subject: [PATCH 02/15] win32/ComWorker: include cleanup --- src/win32/ComWorker.hxx | 2 -- 1 file changed, 2 deletions(-) diff --git a/src/win32/ComWorker.hxx b/src/win32/ComWorker.hxx index cd3bd5db6..0d45f667b 100644 --- a/src/win32/ComWorker.hxx +++ b/src/win32/ComWorker.hxx @@ -21,7 +21,6 @@ #define MPD_WIN32_COM_WORKER_HXX #include -#include #include #include @@ -29,7 +28,6 @@ #include "thread/Mutex.hxx" #include "thread/Thread.hxx" #include "win32/WinEvent.hxx" -#include #include // Worker thread for all COM operation From b14b0e5634bf78ddaafbce356a613b6f8f22992b Mon Sep 17 00:00:00 2001 From: Max Kellermann Date: Fri, 5 Mar 2021 13:13:34 +0100 Subject: [PATCH 03/15] win32/ComWorker: reorder includes --- src/win32/ComWorker.cxx | 5 +++-- src/win32/ComWorker.hxx | 11 ++++++----- 2 files changed, 9 insertions(+), 7 deletions(-) diff --git a/src/win32/ComWorker.cxx b/src/win32/ComWorker.cxx index d708dffbe..cd84c9730 100644 --- a/src/win32/ComWorker.cxx +++ b/src/win32/ComWorker.cxx @@ -16,11 +16,12 @@ * with this program; if not, write to the Free Software Foundation, Inc., * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. */ + #include "ComWorker.hxx" -#include "Log.hxx" +#include "Com.hxx" #include "thread/Name.hxx" #include "util/Domain.hxx" -#include "win32/Com.hxx" +#include "Log.hxx" namespace { static constexpr Domain com_worker_domain("com_worker"); diff --git a/src/win32/ComWorker.hxx b/src/win32/ComWorker.hxx index 0d45f667b..10f2123a4 100644 --- a/src/win32/ComWorker.hxx +++ b/src/win32/ComWorker.hxx @@ -1,5 +1,5 @@ /* - * Copyright 2020 The Music Player Daemon Project + * Copyright 2020-2021 The Music Player Daemon Project * http://www.musicpd.org * * This program is free software; you can redistribute it and/or modify @@ -20,14 +20,15 @@ #ifndef MPD_WIN32_COM_WORKER_HXX #define MPD_WIN32_COM_WORKER_HXX +#include "WinEvent.hxx" +#include "thread/Future.hxx" +#include "thread/Mutex.hxx" +#include "thread/Thread.hxx" + #include #include #include -#include "thread/Future.hxx" -#include "thread/Mutex.hxx" -#include "thread/Thread.hxx" -#include "win32/WinEvent.hxx" #include // Worker thread for all COM operation From 783826548261c52de751c9fa9e77c1e297afbfcc Mon Sep 17 00:00:00 2001 From: Max Kellermann Date: Fri, 5 Mar 2021 13:15:59 +0100 Subject: [PATCH 04/15] win32/ComWorker: remove debug log messages --- src/win32/ComWorker.cxx | 8 -------- 1 file changed, 8 deletions(-) diff --git a/src/win32/ComWorker.cxx b/src/win32/ComWorker.cxx index cd84c9730..702c2c427 100644 --- a/src/win32/ComWorker.cxx +++ b/src/win32/ComWorker.cxx @@ -20,24 +20,16 @@ #include "ComWorker.hxx" #include "Com.hxx" #include "thread/Name.hxx" -#include "util/Domain.hxx" -#include "Log.hxx" - -namespace { -static constexpr Domain com_worker_domain("com_worker"); -} Mutex COMWorker::mutex; unsigned int COMWorker::reference_count = 0; std::optional COMWorker::thread; void COMWorker::COMWorkerThread::Work() noexcept { - FormatDebug(com_worker_domain, "Working thread started"); SetThreadName("COM Worker"); COM com{true}; while (true) { if (!running_flag.test_and_set()) { - FormatDebug(com_worker_domain, "Working thread ended"); return; } while (!spsc_buffer.empty()) { From 932756efce53f5652a610401328c4f68093890e9 Mon Sep 17 00:00:00 2001 From: Max Kellermann Date: Fri, 5 Mar 2021 13:17:22 +0100 Subject: [PATCH 05/15] win32/ComWorker: fix the FormatHResultError() return type Casting to std::runtime_error loses information (and prevents RVO). --- src/win32/HResult.hxx | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/win32/HResult.hxx b/src/win32/HResult.hxx index 2bc851f73..37d37a320 100644 --- a/src/win32/HResult.hxx +++ b/src/win32/HResult.hxx @@ -88,7 +88,7 @@ static inline const std::error_category &hresult_category() noexcept { return hresult_category_instance; } -gcc_printf(2, 3) static inline std::runtime_error +gcc_printf(2, 3) static inline std::system_error FormatHResultError(HRESULT result, const char *fmt, ...) noexcept { std::va_list args1, args2; va_start(args1, fmt); From 922c4bf3f056ff9c665fd06a28832d2126e87361 Mon Sep 17 00:00:00 2001 From: Max Kellermann Date: Fri, 5 Mar 2021 13:22:33 +0100 Subject: [PATCH 06/15] test/TestLookupFile: fix Windows compiler errors --- test/TestLookupFile.cxx | 20 ++++++++++---------- 1 file changed, 10 insertions(+), 10 deletions(-) diff --git a/test/TestLookupFile.cxx b/test/TestLookupFile.cxx index be6d85cfb..aab42ac20 100644 --- a/test/TestLookupFile.cxx +++ b/test/TestLookupFile.cxx @@ -8,23 +8,23 @@ TEST(ArchiveTest, Lookup) { - EXPECT_THROW(LookupFile(Path::FromFS("")), std::system_error); + EXPECT_THROW(LookupFile(Path::FromFS(PATH_LITERAL(""))), std::system_error); - EXPECT_FALSE(LookupFile(Path::FromFS("."))); + EXPECT_FALSE(LookupFile(Path::FromFS(PATH_LITERAL(".")))); - EXPECT_FALSE(LookupFile(Path::FromFS("config.h"))); + EXPECT_FALSE(LookupFile(Path::FromFS(PATH_LITERAL("config.h")))); - EXPECT_THROW(LookupFile(Path::FromFS("src/foo/bar")), std::system_error); + EXPECT_THROW(LookupFile(Path::FromFS(PATH_LITERAL("src/foo/bar"))), std::system_error); fclose(fopen("dummy", "w")); - auto result = LookupFile(Path::FromFS("dummy/foo/bar")); + auto result = LookupFile(Path::FromFS(PATH_LITERAL("dummy/foo/bar"))); EXPECT_TRUE(result); - EXPECT_STREQ(result.archive.c_str(), "dummy"); - EXPECT_STREQ(result.inside.c_str(), "foo/bar"); + EXPECT_STREQ(result.archive.c_str(), PATH_LITERAL("dummy")); + EXPECT_STREQ(result.inside.c_str(), PATH_LITERAL("foo/bar")); - result = LookupFile(Path::FromFS("config.h/foo/bar")); + result = LookupFile(Path::FromFS(PATH_LITERAL("config.h/foo/bar"))); EXPECT_TRUE(result); - EXPECT_STREQ(result.archive.c_str(), "config.h"); - EXPECT_STREQ(result.inside.c_str(), "foo/bar"); + EXPECT_STREQ(result.archive.c_str(), PATH_LITERAL("config.h")); + EXPECT_STREQ(result.inside.c_str(), PATH_LITERAL("foo/bar")); } From c962a6be76edae13d6974ad17ea720aa6846681e Mon Sep 17 00:00:00 2001 From: Max Kellermann Date: Fri, 5 Mar 2021 13:24:23 +0100 Subject: [PATCH 07/15] test/run_convert: fix Windows compiler errors --- test/run_convert.cxx | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/test/run_convert.cxx b/test/run_convert.cxx index d4323fcbb..3cba693fd 100644 --- a/test/run_convert.cxx +++ b/test/run_convert.cxx @@ -28,6 +28,7 @@ #include "pcm/AudioFormat.hxx" #include "pcm/Convert.hxx" #include "fs/Path.hxx" +#include "fs/NarrowPath.hxx" #include "util/ConstBuffer.hxx" #include "util/StaticFifoBuffer.hxx" #include "util/OptionDef.hxx" @@ -47,7 +48,7 @@ struct CommandLine { AudioFormat in_audio_format, out_audio_format; - Path config_path = nullptr; + FromNarrowPath config_path; bool verbose = false; }; @@ -71,7 +72,7 @@ ParseCommandLine(int argc, char **argv) while (auto o = option_parser.Next()) { switch (Option(o.index)) { case OPTION_CONFIG: - c.config_path = Path::FromFS(o.value); + c.config_path = o.value; break; case OPTION_VERBOSE: From 54c1794ceebf27ad66340ab7e8e9194394194d55 Mon Sep 17 00:00:00 2001 From: Max Kellermann Date: Fri, 5 Mar 2021 13:25:24 +0100 Subject: [PATCH 08/15] win32: build static library Fixes linker failure on test/run_output.exe --- meson.build | 2 +- src/output/plugins/meson.build | 1 + src/win32/meson.build | 17 +++++++++++++++++ 3 files changed, 19 insertions(+), 1 deletion(-) create mode 100644 src/win32/meson.build diff --git a/meson.build b/meson.build index d72da267e..321615704 100644 --- a/meson.build +++ b/meson.build @@ -322,7 +322,6 @@ sources = [ if is_windows sources += [ 'src/win32/Win32Main.cxx', - 'src/win32/ComWorker.cxx', ] endif @@ -360,6 +359,7 @@ subdir('src/system') subdir('src/thread') subdir('src/net') subdir('src/event') +subdir('src/win32') subdir('src/apple') diff --git a/src/output/plugins/meson.build b/src/output/plugins/meson.build index 8d48674ae..53c92b9f4 100644 --- a/src/output/plugins/meson.build +++ b/src/output/plugins/meson.build @@ -141,6 +141,7 @@ if is_windows wasapi_dep = [ c_compiler.find_library('ksuser', required: true), c_compiler.find_library('ole32', required: true), + win32_dep, ] else wasapi_dep = dependency('', required: false) diff --git a/src/win32/meson.build b/src/win32/meson.build new file mode 100644 index 000000000..4d70e3861 --- /dev/null +++ b/src/win32/meson.build @@ -0,0 +1,17 @@ +if not is_windows + win32_dep = declare_dependency('', required: false) + subdir_done() +endif + +win32 = static_library( + 'win32', + 'ComWorker.cxx', + include_directories: inc, +) + +win32_dep = declare_dependency( + link_with: win32, + dependencies: [ + thread_dep, + ], +) From 82a61ab3be47baea2311f714eb31d7650103673e Mon Sep 17 00:00:00 2001 From: Max Kellermann Date: Fri, 5 Mar 2021 13:40:33 +0100 Subject: [PATCH 09/15] win32/meson.build: fix syntax error --- src/win32/meson.build | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/win32/meson.build b/src/win32/meson.build index 4d70e3861..786184a6d 100644 --- a/src/win32/meson.build +++ b/src/win32/meson.build @@ -1,5 +1,5 @@ if not is_windows - win32_dep = declare_dependency('', required: false) + win32_dep = dependency('', required: false) subdir_done() endif From 74f9e0715130a7ab8b4851c5d60525f67d55c303 Mon Sep 17 00:00:00 2001 From: Max Kellermann Date: Fri, 5 Mar 2021 13:38:28 +0100 Subject: [PATCH 10/15] win32/HResult: include cleanup --- src/win32/HResult.hxx | 2 -- 1 file changed, 2 deletions(-) diff --git a/src/win32/HResult.hxx b/src/win32/HResult.hxx index 37d37a320..f58cf3cb9 100644 --- a/src/win32/HResult.hxx +++ b/src/win32/HResult.hxx @@ -25,10 +25,8 @@ #include #include #include -#include #include #include -#include #include From 6b83fc6b57f79f34ad0cc078fef6a9d100645392 Mon Sep 17 00:00:00 2001 From: Max Kellermann Date: Fri, 5 Mar 2021 13:18:54 +0100 Subject: [PATCH 11/15] win32/HResult: un-inline FormatHResultError() Reduce header dependencies. --- src/win32/HResult.cxx | 42 ++++++++++++++++++++++++++++++++++++++++++ src/win32/HResult.hxx | 20 ++------------------ src/win32/meson.build | 1 + 3 files changed, 45 insertions(+), 18 deletions(-) create mode 100644 src/win32/HResult.cxx diff --git a/src/win32/HResult.cxx b/src/win32/HResult.cxx new file mode 100644 index 000000000..4397a3650 --- /dev/null +++ b/src/win32/HResult.cxx @@ -0,0 +1,42 @@ +/* + * Copyright 2020-2021 The Music Player Daemon Project + * http://www.musicpd.org + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 2 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License along + * with this program; if not, write to the Free Software Foundation, Inc., + * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. + */ + +#include "HResult.hxx" + +#include +#include + +std::system_error +FormatHResultError(HRESULT result, const char *fmt, ...) noexcept +{ + std::va_list args1, args2; + va_start(args1, fmt); + va_copy(args2, args1); + + const int size = vsnprintf(nullptr, 0, fmt, args1); + va_end(args1); + assert(size >= 0); + + auto buffer = std::make_unique(size + 1); + vsprintf(buffer.get(), fmt, args2); + va_end(args2); + + return std::system_error(std::error_code(result, hresult_category()), + std::string(buffer.get(), size)); +} diff --git a/src/win32/HResult.hxx b/src/win32/HResult.hxx index f58cf3cb9..8ec4b30d4 100644 --- a/src/win32/HResult.hxx +++ b/src/win32/HResult.hxx @@ -23,7 +23,6 @@ #include "util/Compiler.h" #include -#include #include #include #include @@ -86,22 +85,7 @@ static inline const std::error_category &hresult_category() noexcept { return hresult_category_instance; } -gcc_printf(2, 3) static inline std::system_error - FormatHResultError(HRESULT result, const char *fmt, ...) noexcept { - std::va_list args1, args2; - va_start(args1, fmt); - va_copy(args2, args1); - - const int size = vsnprintf(nullptr, 0, fmt, args1); - va_end(args1); - assert(size >= 0); - - auto buffer = std::make_unique(size + 1); - vsprintf(buffer.get(), fmt, args2); - va_end(args2); - - return std::system_error(std::error_code(result, hresult_category()), - std::string(buffer.get(), size)); -} +gcc_printf(2, 3) std::system_error +FormatHResultError(HRESULT result, const char *fmt, ...) noexcept; #endif diff --git a/src/win32/meson.build b/src/win32/meson.build index 786184a6d..30f059669 100644 --- a/src/win32/meson.build +++ b/src/win32/meson.build @@ -6,6 +6,7 @@ endif win32 = static_library( 'win32', 'ComWorker.cxx', + 'HResult.cxx', include_directories: inc, ) From a7e7312ccacc7e9551c2eda2960a62c79bcfeb76 Mon Sep 17 00:00:00 2001 From: Max Kellermann Date: Fri, 5 Mar 2021 13:36:59 +0100 Subject: [PATCH 12/15] win32/HResult: un-inline HResultCategory::message() --- src/win32/HResult.cxx | 15 +++++++++++++++ src/win32/HResult.hxx | 13 +------------ 2 files changed, 16 insertions(+), 12 deletions(-) diff --git a/src/win32/HResult.cxx b/src/win32/HResult.cxx index 4397a3650..a40116a1b 100644 --- a/src/win32/HResult.cxx +++ b/src/win32/HResult.cxx @@ -19,9 +19,24 @@ #include "HResult.hxx" +#include #include +#include #include +std::string +HResultCategory::message(int Errcode) const +{ + const auto msg = HRESULTToString(Errcode); + if (!msg.empty()) + return std::string(msg); + + char buffer[11]; // "0x12345678\0" + int size = snprintf(buffer, sizeof(buffer), "0x%1x", Errcode); + assert(2 <= size && size <= 10); + return std::string(buffer, size); +} + std::system_error FormatHResultError(HRESULT result, const char *fmt, ...) noexcept { diff --git a/src/win32/HResult.hxx b/src/win32/HResult.hxx index 8ec4b30d4..2f48c08f2 100644 --- a/src/win32/HResult.hxx +++ b/src/win32/HResult.hxx @@ -22,8 +22,6 @@ #include "util/Compiler.h" -#include -#include #include #include @@ -66,16 +64,7 @@ static inline const std::error_category &hresult_category() noexcept; class HResultCategory : public std::error_category { public: const char *name() const noexcept override { return "HRESULT"; } - std::string message(int Errcode) const override { - const auto msg = HRESULTToString(Errcode); - if (!msg.empty()) { - return std::string(msg); - } - char buffer[11]; // "0x12345678\0" - int size = snprintf(buffer, sizeof(buffer), "0x%1x", Errcode); - assert(2 <= size && size <= 10); - return std::string(buffer, size); - } + std::string message(int Errcode) const override; std::error_condition default_error_condition(int code) const noexcept override { return std::error_condition(code, hresult_category()); } From d54830de12f6668b9c653cdfa31052d7140a6326 Mon Sep 17 00:00:00 2001 From: Max Kellermann Date: Fri, 5 Mar 2021 13:49:11 +0100 Subject: [PATCH 13/15] thread/WindowsFuture: include cleanup --- src/thread/WindowsFuture.hxx | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/thread/WindowsFuture.hxx b/src/thread/WindowsFuture.hxx index f5d304a1e..e9082a63c 100644 --- a/src/thread/WindowsFuture.hxx +++ b/src/thread/WindowsFuture.hxx @@ -22,7 +22,7 @@ #include "CriticalSection.hxx" #include "WindowsCond.hxx" -#include + #include #include From 2011a6e2ee4f0280a541f276b413f4613359a0f9 Mon Sep 17 00:00:00 2001 From: Max Kellermann Date: Fri, 5 Mar 2021 15:59:52 +0100 Subject: [PATCH 14/15] win32/WinEvent: un-inline the constructor Reduce header dependencies. --- src/win32/WinEvent.cxx | 28 ++++++++++++++++++++++++++++ src/win32/WinEvent.hxx | 11 +++++------ src/win32/meson.build | 1 + 3 files changed, 34 insertions(+), 6 deletions(-) create mode 100644 src/win32/WinEvent.cxx diff --git a/src/win32/WinEvent.cxx b/src/win32/WinEvent.cxx new file mode 100644 index 000000000..b203a6bb3 --- /dev/null +++ b/src/win32/WinEvent.cxx @@ -0,0 +1,28 @@ +/* + * Copyright 2020-2021 The Music Player Daemon Project + * http://www.musicpd.org + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 2 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License along + * with this program; if not, write to the Free Software Foundation, Inc., + * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. + */ + +#include "WinEvent.hxx" +#include "system/Error.hxx" + +WinEvent::WinEvent() + :event(CreateEventW(nullptr, false, false, nullptr)) +{ + if (!event) + throw FormatLastError("Error creating events"); +} diff --git a/src/win32/WinEvent.hxx b/src/win32/WinEvent.hxx index 127855f91..d298daec9 100644 --- a/src/win32/WinEvent.hxx +++ b/src/win32/WinEvent.hxx @@ -20,7 +20,6 @@ #ifndef MPD_WIN32_WINEVENT_HXX #define MPD_WIN32_WINEVENT_HXX -#include "system/Error.hxx" #include // RAII for Windows unnamed event object @@ -28,11 +27,11 @@ class WinEvent { public: - WinEvent() : event(CreateEventW(nullptr, false, false, nullptr)) { - if (!event) { - throw FormatLastError("Error creating events"); - } - } + /** + * Throws on error. + */ + WinEvent(); + ~WinEvent() noexcept { CloseHandle(event); } WinEvent(WinEvent &&) = delete; WinEvent(const WinEvent &) = delete; diff --git a/src/win32/meson.build b/src/win32/meson.build index 30f059669..5f8eaf6b7 100644 --- a/src/win32/meson.build +++ b/src/win32/meson.build @@ -7,6 +7,7 @@ win32 = static_library( 'win32', 'ComWorker.cxx', 'HResult.cxx', + 'WinEvent.cxx', include_directories: inc, ) From 637cf8a0399e1df798c10cfb60b96bd71b6b1d00 Mon Sep 17 00:00:00 2001 From: Max Kellermann Date: Fri, 5 Mar 2021 16:04:56 +0100 Subject: [PATCH 15/15] win32/WinEvent: add default value to Wait() --- src/output/plugins/WasapiOutputPlugin.cxx | 6 +++--- src/win32/WinEvent.hxx | 2 +- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/src/output/plugins/WasapiOutputPlugin.cxx b/src/output/plugins/WasapiOutputPlugin.cxx index 99f7dacf4..84d23dd12 100644 --- a/src/output/plugins/WasapiOutputPlugin.cxx +++ b/src/output/plugins/WasapiOutputPlugin.cxx @@ -155,7 +155,7 @@ public: void Finish() noexcept { return SetStatus(Status::FINISH); } void Play() noexcept { return SetStatus(Status::PLAY); } void Pause() noexcept { return SetStatus(Status::PAUSE); } - void WaitDataPoped() noexcept { data_poped.Wait(INFINITE); } + void WaitDataPoped() noexcept { data_poped.Wait(); } void CheckException() { if (error.occur.load()) { auto err = std::exchange(error.ptr, nullptr); @@ -269,7 +269,7 @@ void WasapiOutputThread::Work() noexcept { COM com{true}; while (true) { try { - event.Wait(INFINITE); + event.Wait(); Status current_state = status.load(); if (current_state == Status::FINISH) { @@ -322,7 +322,7 @@ void WasapiOutputThread::Work() noexcept { } catch (...) { error.ptr = std::current_exception(); error.occur.store(true); - error.thrown.Wait(INFINITE); + error.thrown.Wait(); } } } diff --git a/src/win32/WinEvent.hxx b/src/win32/WinEvent.hxx index d298daec9..7b3305c37 100644 --- a/src/win32/WinEvent.hxx +++ b/src/win32/WinEvent.hxx @@ -40,7 +40,7 @@ public: HANDLE handle() noexcept { return event; } - DWORD Wait(DWORD milliseconds) noexcept { + DWORD Wait(DWORD milliseconds=INFINITE) noexcept { return WaitForSingleObject(event, milliseconds); }