Skip to content

Commit

Permalink
pick util/stringformat.h from mixxxdj#13236
Browse files Browse the repository at this point in the history
`always_false_v<T>` already exists in midimessage.h, move to new /util/always_false_v.h
  • Loading branch information
ronso0 committed May 16, 2024
1 parent 1382e94 commit 944d379
Show file tree
Hide file tree
Showing 3 changed files with 46 additions and 3 deletions.
4 changes: 1 addition & 3 deletions src/controllers/midi/midimessage.h
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
#include <variant>

#include "preferences/usersettings.h"
#include "util/always_false_v.h"
#include "util/compatibility/qhash.h"

// The second value of each OpCode will be the channel number the message
Expand Down Expand Up @@ -179,9 +180,6 @@ struct MidiKey {
};
};

template<class>
inline constexpr bool always_false_v = false;

struct MidiInputMapping {
MidiInputMapping() {
}
Expand Down
6 changes: 6 additions & 0 deletions src/util/always_false_v.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
#pragma once

// always false, used for static_assert and workaround for compilers without
// https://cplusplus.github.io/CWG/issues/2518.html
template<typename T>
static constexpr bool always_false_v = false;
39 changes: 39 additions & 0 deletions src/util/stringformat.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
#pragma once

#include <QString>
#include <type_traits>
#include <utility>

#include "util/always_false_v.h"

namespace {

// taken from Qt
template<typename T>
static constexpr bool is_convertible_to_view_or_qstring_v =
std::is_convertible_v<T, QString> ||
std::is_convertible_v<T, QStringView> ||
std::is_convertible_v<T, QLatin1String>;

// check if we can call QString::number(T) with T
template<typename T>
static constexpr bool is_number_compatible_v =
std::is_invocable_v<decltype(QString::number(std::declval<T>()))(T), T>;
} // namespace

// Try to convert T to a type that would be accepted by QString::args(Args&&...)
template<typename T>
auto convertToQStringConvertible(T&& arg) {
if constexpr (is_convertible_to_view_or_qstring_v<T>) {
// no need to do anything, just return verbatim
return std::forward<T>(arg);
} else if constexpr (is_number_compatible_v<T>) {
return QString::number(std::forward<T>(arg));
} else {
static_assert(always_false_v<T>, "Unsupported type for QString::arg");
// unreachable, but returning a QString results in a better error message
// because the log won't be spammed with all the QString::arg overloads
// it couldn't match with `void`.
return QString();
}
}

0 comments on commit 944d379

Please sign in to comment.