From 66bec49862142f36eda4227a0b0e586a1b04c3c3 Mon Sep 17 00:00:00 2001 From: Dave Reisner Date: Tue, 7 Jun 2016 08:14:47 -0400 Subject: declare enums as enum classes --- notify.h | 28 +++++++++++++-------------- ponymix.cc | 64 +++++++++++++++++++++++++++++++------------------------------- pulse.cc | 64 +++++++++++++++++++++++++++++++------------------------------- pulse.h | 38 ++++++++++++++++++------------------- 4 files changed, 97 insertions(+), 97 deletions(-) diff --git a/notify.h b/notify.h index a2ddbc0..6016df0 100644 --- a/notify.h +++ b/notify.h @@ -6,18 +6,18 @@ #include #endif -enum NotificationType { - NOTIFY_VOLUME, - NOTIFY_BALANCE, - NOTIFY_UNMUTE, - NOTIFY_MUTE, +enum class NotificationType { + VOLUME, + BALANCE, + UNMUTE, + MUTE, }; class Notifier { public: virtual ~Notifier() {} - virtual void Notify(enum NotificationType type, long value, bool mute) const = 0; + virtual void Notify(NotificationType type, long value, bool mute) const = 0; protected: bool initialized_; @@ -35,10 +35,10 @@ class CommandLineNotifier : public Notifier { virtual void Notify(enum NotificationType type, long value, bool) const { switch (type) { - case NOTIFY_VOLUME: - case NOTIFY_BALANCE: - case NOTIFY_UNMUTE: - case NOTIFY_MUTE: + case NotificationType::VOLUME: + case NotificationType::BALANCE: + case NotificationType::UNMUTE: + case NotificationType::MUTE: printf("%ld\n", value); break; } @@ -58,11 +58,11 @@ class LibnotifyNotifier : public Notifier { virtual void Notify(enum NotificationType type, long value, bool mute) const { switch (type) { - case NOTIFY_BALANCE: + case NotificationType::BALANCE: break; - case NOTIFY_VOLUME: - case NOTIFY_UNMUTE: - case NOTIFY_MUTE: + case NotificationType::VOLUME: + case NotificationType::UNMUTE: + case NotificationType::MUTE: volchange(value, mute); break; } diff --git a/ponymix.cc b/ponymix.cc index 40e7286..fce4611 100644 --- a/ponymix.cc +++ b/ponymix.cc @@ -50,7 +50,7 @@ struct Color { const char* mute; }; -static enum DeviceType opt_devtype; +static DeviceType opt_devtype; static bool opt_listrestrict; static bool opt_short; static const char* opt_action; @@ -72,27 +72,27 @@ static int xstrtol(const char *str, long *out) { return 0; } -static const char* type_to_string(enum DeviceType t) { +static const char* type_to_string(DeviceType t) { switch (t) { - case DEVTYPE_SINK: + case DeviceType::SINK: return "sink"; - case DEVTYPE_SOURCE: + case DeviceType::SOURCE: return "source"; - case DEVTYPE_SINK_INPUT: + case DeviceType::SINK_INPUT: return "sink-input"; - case DEVTYPE_SOURCE_OUTPUT: + case DeviceType::SOURCE_OUTPUT: return "source-output"; } throw unreachable(); } -static enum DeviceType string_to_devtype_or_die(const char* str) { - static std::map typemap{ - { "sink", DEVTYPE_SINK }, - { "source", DEVTYPE_SOURCE }, - { "sink-input", DEVTYPE_SINK_INPUT }, - { "source-output", DEVTYPE_SOURCE_OUTPUT }, +static DeviceType string_to_devtype_or_die(const char* str) { + static std::map typemap{ + { "sink", DeviceType::SINK }, + { "source", DeviceType::SOURCE }, + { "sink-input", DeviceType::SINK_INPUT }, + { "source-output", DeviceType::SOURCE_OUTPUT }, }; try { return typemap.at(str); @@ -103,7 +103,7 @@ static enum DeviceType string_to_devtype_or_die(const char* str) { static Device* string_to_device_or_die(PulseClient& ponymix, std::string arg, - enum DeviceType type) { + DeviceType type) { Device* device = ponymix.GetDevice(arg, type); if (device == nullptr) errx(1, "no match found for device: %s", arg.c_str()); return device; @@ -357,17 +357,17 @@ static int SetProfile(PulseClient& ponymix, int, char* argv[]) { static int Move(PulseClient& ponymix, int, char* argv[]) { // this assignment is a lie. stfu g++ - enum DeviceType target_devtype = opt_devtype; + DeviceType target_devtype = opt_devtype; switch (opt_devtype) { - case DEVTYPE_SOURCE: - opt_devtype = DEVTYPE_SOURCE_OUTPUT; - case DEVTYPE_SOURCE_OUTPUT: - target_devtype = DEVTYPE_SOURCE; + case DeviceType::SOURCE: + opt_devtype = DeviceType::SOURCE_OUTPUT; + case DeviceType::SOURCE_OUTPUT: + target_devtype = DeviceType::SOURCE; break; - case DEVTYPE_SINK: - opt_devtype = DEVTYPE_SINK_INPUT; - case DEVTYPE_SINK_INPUT: - target_devtype = DEVTYPE_SINK; + case DeviceType::SINK: + opt_devtype = DeviceType::SINK_INPUT; + case DeviceType::SINK_INPUT: + target_devtype = DeviceType::SINK; break; } @@ -380,11 +380,11 @@ static int Move(PulseClient& ponymix, int, char* argv[]) { static int Kill(PulseClient& ponymix, int, char*[]) { switch (opt_devtype) { - case DEVTYPE_SOURCE: - opt_devtype = DEVTYPE_SOURCE_OUTPUT; + case DeviceType::SOURCE: + opt_devtype = DeviceType::SOURCE_OUTPUT; break; - case DEVTYPE_SINK: - opt_devtype = DEVTYPE_SINK_INPUT; + case DeviceType::SINK: + opt_devtype = DeviceType::SINK_INPUT; break; default: break; @@ -397,7 +397,7 @@ static int Kill(PulseClient& ponymix, int, char*[]) { static int IsAvailable(PulseClient& ponymix, int, char*[]) { auto device = string_to_device_or_die(ponymix, opt_device, opt_devtype); - return ponymix.Availability(*device) == Device::AVAILABLE_YES; + return ponymix.Availability(*device) == Device::Availability::YES; } static bool endswith(const std::string& subject, const std::string& predicate) { @@ -611,20 +611,20 @@ bool parse_options(int argc, char** argv) { break; case 0x100: case 0x101: - opt_devtype = DEVTYPE_SINK; + opt_devtype = DeviceType::SINK; opt_listrestrict = true; break; case 0x102: case 0x103: - opt_devtype = DEVTYPE_SOURCE; + opt_devtype = DeviceType::SOURCE; opt_listrestrict = true; break; case 0x104: - opt_devtype = DEVTYPE_SINK_INPUT; + opt_devtype = DeviceType::SINK_INPUT; opt_listrestrict = true; break; case 0x105: - opt_devtype = DEVTYPE_SOURCE_OUTPUT; + opt_devtype = DeviceType::SOURCE_OUTPUT; opt_listrestrict = true; break; case 0x106: @@ -653,7 +653,7 @@ int main(int argc, char* argv[]) { // that on demand if a function needs it. ServerInfo defaults = ponymix.GetDefaults(); opt_action = "defaults"; - opt_devtype = DEVTYPE_SINK; + opt_devtype = DeviceType::SINK; opt_maxvolume = 100; if (!parse_options(argc, argv)) return 1; diff --git a/pulse.cc b/pulse.cc index 8d55a19..adfadcd 100644 --- a/pulse.cc +++ b/pulse.cc @@ -177,45 +177,45 @@ Device* PulseClient::get_device(std::vector& devices, const std::string& } } -Device* PulseClient::GetDevice(const uint32_t index, enum DeviceType type) { +Device* PulseClient::GetDevice(const uint32_t index, DeviceType type) { switch (type) { - case DEVTYPE_SINK: + case DeviceType::SINK: return GetSink(index); - case DEVTYPE_SOURCE: + case DeviceType::SOURCE: return GetSource(index); - case DEVTYPE_SINK_INPUT: + case DeviceType::SINK_INPUT: return GetSinkInput(index); - case DEVTYPE_SOURCE_OUTPUT: + case DeviceType::SOURCE_OUTPUT: return GetSourceOutput(index); } throw unreachable(); } -Device* PulseClient::GetDevice(const std::string& name, enum DeviceType type) { +Device* PulseClient::GetDevice(const std::string& name, DeviceType type) { switch (type) { - case DEVTYPE_SINK: + case DeviceType::SINK: return GetSink(name); - case DEVTYPE_SOURCE: + case DeviceType::SOURCE: return GetSource(name); - case DEVTYPE_SINK_INPUT: + case DeviceType::SINK_INPUT: return GetSinkInput(name); - case DEVTYPE_SOURCE_OUTPUT: + case DeviceType::SOURCE_OUTPUT: return GetSourceOutput(name); } throw unreachable(); } -const std::vector& PulseClient::GetDevices(enum DeviceType type) const { +const std::vector& PulseClient::GetDevices(DeviceType type) const { switch (type) { - case DEVTYPE_SINK: + case DeviceType::SINK: return GetSinks(); - case DEVTYPE_SOURCE: + case DeviceType::SOURCE: return GetSources(); - case DEVTYPE_SINK_INPUT: + case DeviceType::SINK_INPUT: return GetSinkInputs(); - case DEVTYPE_SOURCE_OUTPUT: + case DeviceType::SOURCE_OUTPUT: return GetSourceOutputs(); } @@ -355,7 +355,7 @@ bool PulseClient::SetMute(Device& device, bool mute) { if (success) { device.mute_ = mute; - notifier_->Notify(mute ? NOTIFY_MUTE : NOTIFY_UNMUTE, + notifier_->Notify(mute ? NotificationType::MUTE : NotificationType::UNMUTE, device.volume_percent_, mute); } @@ -382,7 +382,7 @@ bool PulseClient::SetVolume(Device& device, long volume) { if (success) { device.update_volume(*cvol); - notifier_->Notify(NOTIFY_VOLUME, device.volume_percent_, device.mute_); + notifier_->Notify(NotificationType::VOLUME, device.volume_percent_, device.mute_); } return success; @@ -418,7 +418,7 @@ bool PulseClient::SetBalance(Device& device, long balance) { if (success) { device.update_volume(*cvol); - notifier_->Notify(NOTIFY_BALANCE, device.balance_, false); + notifier_->Notify(NotificationType::BALANCE, device.balance_, false); } return success; @@ -520,15 +520,15 @@ bool PulseClient::SetDefault(Device& device) { if (success) { switch (device.type_) { - case DEVTYPE_SINK: + case DeviceType::SINK: defaults_.sink = device.name_; break; - case DEVTYPE_SOURCE: + case DeviceType::SOURCE: defaults_.source = device.name_; break; default: errx(1, "impossible to set a default for device type %d", - device.type_); + static_cast(device.type_)); } } @@ -539,16 +539,16 @@ void PulseClient::remove_device(Device& device) { std::vector* devlist; switch (device.type_) { - case DEVTYPE_SINK: + case DeviceType::SINK: devlist = &sinks_; break; - case DEVTYPE_SINK_INPUT: + case DeviceType::SINK_INPUT: devlist = &sink_inputs_; break; - case DEVTYPE_SOURCE: + case DeviceType::SOURCE: devlist = &sources_; break; - case DEVTYPE_SOURCE_OUTPUT: + case DeviceType::SOURCE_OUTPUT: devlist = &source_outputs_; break; } @@ -581,7 +581,7 @@ Card::Card(const pa_card_info* info) : // Devices // Device::Device(const pa_sink_info* info) : - type_(DEVTYPE_SINK), + type_(DeviceType::SINK), index_(info->index), name_(info->name ? info->name : ""), desc_(info->description), @@ -600,20 +600,20 @@ Device::Device(const pa_sink_info* info) : if (info->active_port) { switch (info->active_port->available) { case PA_PORT_AVAILABLE_YES: - available_ = Device::AVAILABLE_YES; + available_ = Device::Availability::YES; break; case PA_PORT_AVAILABLE_NO: - available_ = Device::AVAILABLE_NO; + available_ = Device::Availability::NO; break; case PA_PORT_AVAILABLE_UNKNOWN: - available_ = Device::AVAILABLE_UNKNOWN; + available_ = Device::Availability::UNKNOWN; break; } } } Device::Device(const pa_source_info* info) : - type_(DEVTYPE_SOURCE), + type_(DeviceType::SOURCE), index_(info->index), name_(info->name ? info->name : ""), desc_(info->description), @@ -631,7 +631,7 @@ Device::Device(const pa_source_info* info) : } Device::Device(const pa_sink_input_info* info) : - type_(DEVTYPE_SINK_INPUT), + type_(DeviceType::SINK_INPUT), index_(info->index), name_(info->name ? info->name : ""), mute_(info->mute), @@ -652,7 +652,7 @@ Device::Device(const pa_sink_input_info* info) : } Device::Device(const pa_source_output_info* info) : - type_(DEVTYPE_SOURCE_OUTPUT), + type_(DeviceType::SOURCE_OUTPUT), index_(info->index), name_(info->name ? info->name : ""), mute_(info->mute), diff --git a/pulse.h b/pulse.h index 9f2d447..3bfd4e6 100644 --- a/pulse.h +++ b/pulse.h @@ -14,11 +14,11 @@ // external #include -enum DeviceType { - DEVTYPE_SINK, - DEVTYPE_SOURCE, - DEVTYPE_SINK_INPUT, - DEVTYPE_SOURCE_OUTPUT, +enum class DeviceType { + SINK, + SOURCE, + SINK_INPUT, + SOURCE_OUTPUT, }; struct Profile { @@ -45,11 +45,11 @@ struct Operations { class Device { public: - typedef enum { - AVAILABLE_UNKNOWN = 0, - AVAILABLE_NO, - AVAILABLE_YES, - } Availability; + enum class Availability { + UNKNOWN = 0, + NO, + YES, + }; Device(const pa_source_info* info); Device(const pa_sink_info* info); @@ -62,14 +62,14 @@ class Device { int Volume() const { return volume_percent_; } int Balance() const { return balance_; } bool Muted() const { return mute_; } - enum DeviceType Type() const { return type_; } + DeviceType Type() const { return type_; } private: friend class PulseClient; void update_volume(const pa_cvolume& newvol); - enum DeviceType type_; + DeviceType type_; uint32_t index_; std::string name_; std::string desc_; @@ -80,7 +80,7 @@ class Device { int balance_; uint32_t card_idx_; Operations ops_; - Device::Availability available_ = Device::AVAILABLE_UNKNOWN; + Device::Availability available_ = Availability::UNKNOWN; }; class Card { @@ -110,11 +110,11 @@ struct ServerInfo { std::string source; std::string empty = ""; - const std::string& GetDefault(enum DeviceType type) { + const std::string& GetDefault(DeviceType type) { switch (type) { - case DEVTYPE_SINK: + case DeviceType::SINK: return sink; - case DEVTYPE_SOURCE: + case DeviceType::SOURCE: return source; default: return empty; @@ -150,9 +150,9 @@ class PulseClient { void Populate(); // Get a device by index or name and type, or all devices by type. - Device* GetDevice(const uint32_t index, enum DeviceType type); - Device* GetDevice(const std::string& name, enum DeviceType type); - const std::vector& GetDevices(enum DeviceType type) const; + Device* GetDevice(const uint32_t index, DeviceType type); + Device* GetDevice(const std::string& name, DeviceType type); + const std::vector& GetDevices(DeviceType type) const; // Get a sink by index or name, or all sinks. Device* GetSink(const uint32_t index); -- cgit v1.2.3