-
Notifications
You must be signed in to change notification settings - Fork 352
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge branch 'protocol_features' of https://github.com/jcorporation/MPD
- Loading branch information
Showing
10 changed files
with
322 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,74 @@ | ||
// SPDX-License-Identifier: GPL-2.0-or-later | ||
// Copyright The Music Player Daemon Project | ||
|
||
#include "ProtocolFeature.hxx" | ||
#include "Client.hxx" | ||
#include "Response.hxx" | ||
#include "util/StringAPI.hxx" | ||
|
||
#include <cassert> | ||
#include <fmt/format.h> | ||
|
||
|
||
struct feature_type_table { | ||
const char *name; | ||
|
||
ProtocolFeatureType type; | ||
}; | ||
|
||
static constexpr struct feature_type_table protocol_feature_names_init[] = { | ||
{"hide_playlists_in_root", PF_HIDE_PLAYLISTS_IN_ROOT}, | ||
}; | ||
|
||
/** | ||
* This function converts the #tag_item_names_init array to an | ||
* associative array at compile time. This is a kludge because C++20 | ||
* doesn't support designated initializers for arrays, unlike C99. | ||
*/ | ||
static constexpr auto | ||
MakeProtocolFeatureNames() noexcept | ||
{ | ||
std::array<const char *, PF_NUM_OF_ITEM_TYPES> result{}; | ||
|
||
static_assert(std::size(protocol_feature_names_init) == result.size()); | ||
|
||
for (const auto &i : protocol_feature_names_init) { | ||
/* no duplicates allowed */ | ||
assert(result[i.type] == nullptr); | ||
|
||
result[i.type] = i.name; | ||
} | ||
|
||
return result; | ||
} | ||
|
||
constinit const std::array<const char *, PF_NUM_OF_ITEM_TYPES> protocol_feature_names = MakeProtocolFeatureNames(); | ||
|
||
void | ||
protocol_features_print(Client &client, Response &r) noexcept | ||
{ | ||
const auto protocol_feature = client.GetProtocolFeatures(); | ||
for (unsigned i = 0; i < PF_NUM_OF_ITEM_TYPES; i++) | ||
if (protocol_feature.Test(ProtocolFeatureType(i))) | ||
r.Fmt(FMT_STRING("feature: {}\n"), protocol_feature_names[i]); | ||
} | ||
|
||
void | ||
protocol_features_print_all(Response &r) noexcept | ||
{ | ||
for (unsigned i = 0; i < PF_NUM_OF_ITEM_TYPES; i++) | ||
r.Fmt(FMT_STRING("feature: {}\n"), protocol_feature_names[i]); | ||
} | ||
|
||
ProtocolFeatureType | ||
protocol_feature_parse_i(const char *name) noexcept | ||
{ | ||
for (unsigned i = 0; i < PF_NUM_OF_ITEM_TYPES; ++i) { | ||
assert(protocol_feature_names[i] != nullptr); | ||
|
||
if (StringIsEqualIgnoreCase(name, protocol_feature_names[i])) | ||
return (ProtocolFeatureType)i; | ||
} | ||
|
||
return PF_NUM_OF_ITEM_TYPES; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,110 @@ | ||
// SPDX-License-Identifier: GPL-2.0-or-later | ||
// Copyright The Music Player Daemon Project | ||
|
||
#pragma once | ||
|
||
#include <string_view> | ||
#include <cstdint> | ||
|
||
class Client; | ||
class Response; | ||
|
||
/** | ||
* Codes for the type of a protocol feature. | ||
*/ | ||
enum ProtocolFeatureType : uint8_t { | ||
PF_HIDE_PLAYLISTS_IN_ROOT, | ||
|
||
PF_NUM_OF_ITEM_TYPES | ||
}; | ||
|
||
class ProtocolFeature { | ||
using protocol_feature_t = uint_least8_t; | ||
|
||
/* must have enough bits to represent all protocol features | ||
supported by MPD */ | ||
static_assert(PF_NUM_OF_ITEM_TYPES <= sizeof(protocol_feature_t) * 8); | ||
|
||
protocol_feature_t value; | ||
|
||
explicit constexpr ProtocolFeature(protocol_feature_t _value) noexcept | ||
:value(_value) {} | ||
|
||
public: | ||
constexpr ProtocolFeature() noexcept = default; | ||
|
||
constexpr ProtocolFeature(ProtocolFeatureType _value) noexcept | ||
:value(protocol_feature_t(1) << protocol_feature_t(_value)) {} | ||
|
||
static constexpr ProtocolFeature None() noexcept { | ||
return ProtocolFeature(protocol_feature_t(0)); | ||
} | ||
|
||
static constexpr ProtocolFeature All() noexcept { | ||
return ~None(); | ||
} | ||
|
||
constexpr ProtocolFeature operator~() const noexcept { | ||
return ProtocolFeature(~value); | ||
} | ||
|
||
constexpr ProtocolFeature operator&(ProtocolFeature other) const noexcept { | ||
return ProtocolFeature(value & other.value); | ||
} | ||
|
||
constexpr ProtocolFeature operator|(ProtocolFeature other) const noexcept { | ||
return ProtocolFeature(value | other.value); | ||
} | ||
|
||
constexpr ProtocolFeature operator^(ProtocolFeature other) const noexcept { | ||
return ProtocolFeature(value ^ other.value); | ||
} | ||
|
||
constexpr ProtocolFeature &operator&=(ProtocolFeature other) noexcept { | ||
value &= other.value; | ||
return *this; | ||
} | ||
|
||
constexpr ProtocolFeature &operator|=(ProtocolFeature other) noexcept { | ||
value |= other.value; | ||
return *this; | ||
} | ||
|
||
constexpr ProtocolFeature &operator^=(ProtocolFeature other) noexcept { | ||
value ^= other.value; | ||
return *this; | ||
} | ||
|
||
constexpr bool TestAny() const noexcept { | ||
return value != 0; | ||
} | ||
|
||
constexpr bool Test(ProtocolFeatureType feature) const noexcept { | ||
return (*this & feature).TestAny(); | ||
} | ||
|
||
constexpr void Set(ProtocolFeature features) noexcept { | ||
*this |= features; | ||
} | ||
|
||
constexpr void Unset(ProtocolFeature features) noexcept { | ||
*this &= ~ProtocolFeature(features); | ||
} | ||
|
||
constexpr void SetAll() noexcept { | ||
*this = ProtocolFeature::All(); | ||
} | ||
|
||
constexpr void Clear() noexcept { | ||
*this = ProtocolFeature::None(); | ||
} | ||
}; | ||
|
||
void | ||
protocol_features_print(Client &client, Response &r) noexcept; | ||
|
||
void | ||
protocol_features_print_all(Response &r) noexcept; | ||
|
||
ProtocolFeatureType | ||
protocol_feature_parse_i(const char *name) noexcept; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters