libweave: Cleanup enum to string conversion Conversion should be done using weave::EnumToString and weave::StringToEnum. enum_to_string header moved public interface. BUG=brillo:1242 TEST='FEATURES=test emerge-gizmo buffet' Change-Id: I96af61ef0b263d4ec149b14a7c9055bd51bf2e4c Reviewed-on: https://chromium-review.googlesource.com/285775 Tested-by: Vitaly Buka <vitalybuka@chromium.org> Trybot-Ready: Vitaly Buka <vitalybuka@chromium.org> Reviewed-by: Alex Vakulenko <avakulenko@chromium.org> Commit-Queue: Vitaly Buka <vitalybuka@chromium.org>
diff --git a/buffet/buffet.gyp b/buffet/buffet.gyp index 1716756..33e22e8 100644 --- a/buffet/buffet.gyp +++ b/buffet/buffet.gyp
@@ -62,6 +62,7 @@ '../libweave/src/privet/peerd_client.cc', '../libweave/src/privet/privet_handler.cc', '../libweave/src/privet/privet_manager.cc', + '../libweave/src/privet/privet_types.cc', '../libweave/src/privet/security_manager.cc', '../libweave/src/privet/shill_client.cc', '../libweave/src/privet/wifi_bootstrap_manager.cc',
diff --git a/buffet/manager.cc b/buffet/manager.cc index a8f6112..9fa54cd 100644 --- a/buffet/manager.cc +++ b/buffet/manager.cc
@@ -23,6 +23,8 @@ #include <dbus/object_path.h> #include <dbus/values_util.h> +#include "weave/enum_to_string.h" + using chromeos::dbus_utils::AsyncEventSequencer; using chromeos::dbus_utils::ExportedObjectManager; @@ -37,21 +39,6 @@ const char kErrorDomain[] = "buffet"; const char kNotImplemented[] = "notImplemented"; -std::string StatusToString(weave::RegistrationStatus status) { - switch (status) { - case weave::RegistrationStatus::kUnconfigured: - return "unconfigured"; - case weave::RegistrationStatus::kConnecting: - return "connecting"; - case weave::RegistrationStatus::kConnected: - return "connected"; - case weave::RegistrationStatus::kInvalidCredentials: - return "invalid_credentials"; - } - CHECK(0) << "Unknown status"; - return "unknown"; -} - } // anonymous namespace Manager::Manager(const base::WeakPtr<ExportedObjectManager>& object_manager) @@ -185,8 +172,12 @@ chromeos::ErrorPtr error; weave::UserRole role; - if (!FromString(in_user_role, &role, &error)) + if (!StringToEnum(in_user_role, &role)) { + chromeos::Error::AddToPrintf(&error, FROM_HERE, kErrorDomain, + "invalid_user_role", "Invalid role: '%s'", + in_user_role.c_str()); return response->ReplyWithError(error.get()); + } std::string id; if (!device_->GetCommands()->AddCommand(*command, role, &id, &error)) @@ -305,7 +296,7 @@ } void Manager::OnRegistrationChanged(weave::RegistrationStatus status) { - dbus_adaptor_.SetStatus(StatusToString(status)); + dbus_adaptor_.SetStatus(weave::EnumToString(status)); } void Manager::OnConfigChanged(const weave::BuffetConfig& config) { @@ -319,22 +310,8 @@ dbus_adaptor_.SetAnonymousAccessRole(config.local_anonymous_access_role()); } -void Manager::UpdateWiFiBootstrapState( - weave::privet::WifiBootstrapManager::State state) { - switch (state) { - case weave::WifiSetupState::kDisabled: - dbus_adaptor_.SetWiFiBootstrapState("disabled"); - break; - case weave::WifiSetupState::kBootstrapping: - dbus_adaptor_.SetWiFiBootstrapState("waiting"); - break; - case weave::WifiSetupState::kMonitoring: - dbus_adaptor_.SetWiFiBootstrapState("monitoring"); - break; - case weave::WifiSetupState::kConnecting: - dbus_adaptor_.SetWiFiBootstrapState("connecting"); - break; - } +void Manager::UpdateWiFiBootstrapState(weave::WifiSetupState state) { + dbus_adaptor_.SetWiFiBootstrapState(weave::EnumToString(state)); } void Manager::OnPairingStart(const std::string& session_id, @@ -344,7 +321,7 @@ // the most recent pairing attempt. dbus_adaptor_.SetPairingInfo(chromeos::VariantDictionary{ {kPairingSessionIdKey, session_id}, - {kPairingModeKey, weave::privet::PairingTypeToString(pairing_type)}, + {kPairingModeKey, weave::EnumToString(pairing_type)}, {kPairingCodeKey, code}, }); }
diff --git a/libweave/include/weave/enum_to_string.h b/libweave/include/weave/enum_to_string.h new file mode 100644 index 0000000..647f990 --- /dev/null +++ b/libweave/include/weave/enum_to_string.h
@@ -0,0 +1,72 @@ +// Copyright 2015 The Chromium OS Authors. All rights reserved. +// Use of this source code is governed by a BSD-style license that can be +// found in the LICENSE file. + +#ifndef LIBWEAVE_INCLUDE_WEAVE_ENUM_TO_STRING_H_ +#define LIBWEAVE_INCLUDE_WEAVE_ENUM_TO_STRING_H_ + +#include <base/logging.h> + +#include <string> + +namespace weave { + +// Helps to map enumeration to stings and back. +// +// Usage example: +// .h file: +// enum class MyEnum { kV1, kV2 }; +// +// .cc file: +// template <> +// EnumToStringMap<MyEnum>::EnumToStringMap() : EnumToStringMap(kMap) { }; +template <typename T> +class EnumToStringMap final { + static_assert(std::is_enum<T>::value, "The type must be an enumeration"); + + public: + struct Map { + const T id; + const char* const name; + }; + + EnumToStringMap(); + + const Map* begin() const { return begin_; } + const Map* end() const { return end_; } + + private: + template <size_t size> + explicit EnumToStringMap(const Map(&map)[size]) + : begin_(map), end_(map + size) {} + + const Map* begin_; + const Map* end_; +}; + +template <typename T> +std::string EnumToString(T id) { + for (const auto& m : EnumToStringMap<T>()) { + if (m.id == id) { + CHECK(m.name); + return m.name; + } + } + NOTREACHED(); + return std::string(); +} + +template <typename T> +bool StringToEnum(const std::string& name, T* id) { + for (const auto& m : EnumToStringMap<T>()) { + if (m.name && m.name == name) { + *id = m.id; + return true; + } + } + return false; +} + +} // namespace weave + +#endif // LIBWEAVE_INCLUDE_WEAVE_ENUM_TO_STRING_H_
diff --git a/libweave/src/buffet_config.cc b/libweave/src/buffet_config.cc index 2b62c27..5355566 100644 --- a/libweave/src/buffet_config.cc +++ b/libweave/src/buffet_config.cc
@@ -13,6 +13,7 @@ #include "libweave/src/storage_impls.h" #include "libweave/src/storage_interface.h" +#include "weave/enum_to_string.h" namespace { @@ -159,7 +160,7 @@ for (const std::string& mode : chromeos::string_utils::Split(modes_str, ",", true, true)) { PairingType pairing_mode; - CHECK(privet::StringToPairingType(mode, &pairing_mode)); + CHECK(StringToEnum(mode, &pairing_mode)); pairing_modes.insert(pairing_mode); } pairing_modes_ = std::move(pairing_modes);
diff --git a/libweave/src/commands/command_dictionary.cc b/libweave/src/commands/command_dictionary.cc index 5ad1939..536c7fc 100644 --- a/libweave/src/commands/command_dictionary.cc +++ b/libweave/src/commands/command_dictionary.cc
@@ -9,6 +9,7 @@ #include "libweave/src/commands/command_definition.h" #include "libweave/src/commands/schema_constants.h" +#include "weave/enum_to_string.h" namespace weave { @@ -131,7 +132,11 @@ if (command_def_json->GetString(commands::attributes::kCommand_Role, &value)) { - if (!FromString(value, &minimal_role, error)) { + if (!StringToEnum(value, &minimal_role)) { + chromeos::Error::AddToPrintf(error, FROM_HERE, + errors::commands::kDomain, + errors::commands::kInvalidPropValue, + "Invalid role: '%s'", value.c_str()); chromeos::Error::AddToPrintf( error, FROM_HERE, errors::commands::kDomain, errors::commands::kInvalidMinimalRole, @@ -236,7 +241,7 @@ command_def->Set(commands::attributes::kCommand_Parameters, parameters.release()); command_def->SetString(commands::attributes::kCommand_Role, - ToString(pair.second->GetMinimalRole())); + EnumToString(pair.second->GetMinimalRole())); package->SetWithoutPathExpansion(command_name, command_def); } return dict;
diff --git a/libweave/src/commands/command_manager.cc b/libweave/src/commands/command_manager.cc index 3deb36a..1b59da6 100644 --- a/libweave/src/commands/command_manager.cc +++ b/libweave/src/commands/command_manager.cc
@@ -11,6 +11,7 @@ #include "libweave/src/commands/schema_constants.h" #include "libweave/src/utils.h" +#include "weave/enum_to_string.h" using chromeos::dbus_utils::ExportedObjectManager; @@ -113,8 +114,8 @@ if (role < minimal_role) { chromeos::Error::AddToPrintf( error, FROM_HERE, errors::commands::kDomain, "access_denied", - "User role '%s' less than minimal: '%s'", ToString(role).c_str(), - ToString(minimal_role).c_str()); + "User role '%s' less than minimal: '%s'", EnumToString(role).c_str(), + EnumToString(minimal_role).c_str()); return false; }
diff --git a/libweave/src/commands/enum_to_string.h b/libweave/src/commands/enum_to_string.h deleted file mode 100644 index f1de19b..0000000 --- a/libweave/src/commands/enum_to_string.h +++ /dev/null
@@ -1,69 +0,0 @@ -// Copyright 2015 The Chromium OS Authors. All rights reserved. -// Use of this source code is governed by a BSD-style license that can be -// found in the LICENSE file. - -#ifndef LIBWEAVE_SRC_COMMANDS_ENUM_TO_STRING_H_ -#define LIBWEAVE_SRC_COMMANDS_ENUM_TO_STRING_H_ - -#include <string> - -namespace weave { - -// Helps to map enumeration to stings and back. -// -// Usage example: -// .h file: -// enum class MyEnum { kV1, kV2 }; -// std::string ToString(MyEnum id); -// bool FromString(const std::string& str, MyEnum* id); -// -// .cc file: -// template <> -// const EnumToString<MyEnum>::Map EnumToString<MyEnum>::kMap[] = { -// {MyEnum::kV1, "v1"}, -// {MyEnum::kV2, "v2"}, -// }; -// -// std::string ToString(MyEnum id) { -// return EnumToString<MyEnum>::FindNameById(id); -// } -// -// bool FromString(const std::string& str, MyEnum* id) { -// return EnumToString<MyEnum>::FindIdByName(str, id)); -// } -template <typename T> -class EnumToString final { - public: - static std::string FindNameById(T id) { - for (const Map& m : kMap) { - if (m.id == id) { - CHECK(m.name); - return m.name; - } - } - NOTREACHED(); - return std::string(); - } - - static bool FindIdByName(const std::string& name, T* id) { - for (const Map& m : kMap) { - if (m.name && m.name == name) { - *id = m.id; - return true; - } - } - return false; - } - - private: - struct Map { - const T id; - const char* const name; - }; - - static const Map kMap[]; -}; - -} // namespace weave - -#endif // LIBWEAVE_SRC_COMMANDS_ENUM_TO_STRING_H_
diff --git a/libweave/src/commands/user_role.cc b/libweave/src/commands/user_role.cc index 2edd620..87edad1 100644 --- a/libweave/src/commands/user_role.cc +++ b/libweave/src/commands/user_role.cc
@@ -6,32 +6,24 @@ #include <chromeos/errors/error.h> -#include "libweave/src/commands/enum_to_string.h" #include "libweave/src/commands/schema_constants.h" +#include "weave/enum_to_string.h" namespace weave { -template <> -const EnumToString<UserRole>::Map EnumToString<UserRole>::kMap[] = { +namespace { + +const EnumToStringMap<UserRole>::Map kMap[] = { {UserRole::kViewer, commands::attributes::kCommand_Role_Viewer}, {UserRole::kUser, commands::attributes::kCommand_Role_User}, {UserRole::kOwner, commands::attributes::kCommand_Role_Owner}, {UserRole::kManager, commands::attributes::kCommand_Role_Manager}, }; -std::string ToString(UserRole role) { - return EnumToString<UserRole>::FindNameById(role); -} +} // namespace -bool FromString(const std::string& str, - UserRole* role, - chromeos::ErrorPtr* error) { - if (EnumToString<UserRole>::FindIdByName(str, role)) - return true; - chromeos::Error::AddToPrintf(error, FROM_HERE, errors::commands::kDomain, - errors::commands::kInvalidPropValue, - "Invalid role: '%s'", str.c_str()); - return false; -} +template <> +EnumToStringMap<UserRole>::EnumToStringMap() + : EnumToStringMap(kMap) {} } // namespace weave
diff --git a/libweave/src/commands/user_role.h b/libweave/src/commands/user_role.h index c422dfa..5f5b3d7 100644 --- a/libweave/src/commands/user_role.h +++ b/libweave/src/commands/user_role.h
@@ -17,12 +17,6 @@ kOwner, }; -std::string ToString(UserRole role); - -bool FromString(const std::string& str, - UserRole* role, - chromeos::ErrorPtr* error); - } // namespace weave #endif // LIBWEAVE_SRC_COMMANDS_USER_ROLE_H_
diff --git a/libweave/src/privet/cloud_delegate.cc b/libweave/src/privet/cloud_delegate.cc index 8f2aefc..c5efb4d 100644 --- a/libweave/src/privet/cloud_delegate.cc +++ b/libweave/src/privet/cloud_delegate.cc
@@ -21,8 +21,8 @@ #include "libweave/src/commands/command_manager.h" #include "libweave/src/device_registration_info.h" #include "libweave/src/privet/constants.h" -#include "libweave/src/registration_status.h" #include "libweave/src/states/state_manager.h" +#include "weave/enum_to_string.h" using chromeos::ErrorPtr; using chromeos::VariantDictionary; @@ -125,8 +125,8 @@ AuthScope GetAnonymousMaxScope() const override { AuthScope scope; - if (StringToAuthScope(device_->GetConfig().local_anonymous_access_role(), - &scope)) { + if (StringToEnum(device_->GetConfig().local_anonymous_access_role(), + &scope)) { return scope; } return AuthScope::kNone; @@ -177,8 +177,13 @@ chromeos::ErrorPtr error; UserRole role; - if (!FromString(AuthScopeToString(user_info.scope()), &role, &error)) + std::string str_scope = EnumToString(user_info.scope()); + if (!StringToEnum(str_scope, &role)) { + chromeos::Error::AddToPrintf(&error, FROM_HERE, errors::kDomain, + errors::kInvalidParams, "Invalid role: '%s'", + str_scope.c_str()); return error_callback.Run(error.get()); + } std::string id; if (!command_manager_->AddCommand(command, role, &id, &error)) @@ -258,7 +263,7 @@ chromeos::ErrorPtr error; chromeos::Error::AddToPrintf( &error, FROM_HERE, errors::kDomain, errors::kInvalidState, - "Unexpected buffet status: %s", StatusToString(status).c_str()); + "Unexpected buffet status: %s", EnumToString(status).c_str()); connection_state_ = ConnectionState{std::move(error)}; } NotifyOnDeviceInfoChanged();
diff --git a/libweave/src/privet/privet_handler.cc b/libweave/src/privet/privet_handler.cc index 22f10a0..914922e 100644 --- a/libweave/src/privet/privet_handler.cc +++ b/libweave/src/privet/privet_handler.cc
@@ -24,6 +24,7 @@ #include "libweave/src/privet/identity_delegate.h" #include "libweave/src/privet/security_delegate.h" #include "libweave/src/privet/wifi_delegate.h" +#include "weave/enum_to_string.h" namespace weave { namespace privet { @@ -121,86 +122,6 @@ return value_list; } -template <typename T> -class EnumToStringMap final { - public: - static std::string FindNameById(T id) { - for (const Map& m : kMap) { - if (m.id == id) { - CHECK(m.name); - return m.name; - } - } - NOTREACHED() << static_cast<int>(id) << " is not part of " - << typeid(T).name(); - return std::string(); - } - - static bool FindIdByName(const std::string& name, T* id) { - for (const Map& m : kMap) { - if (m.name && m.name == name) { - *id = m.id; - return true; - } - } - return false; - } - - private: - struct Map { - const T id; - const char* const name; - }; - static const Map kMap[]; -}; - -template <> -const EnumToStringMap<ConnectionState::Status>::Map - EnumToStringMap<ConnectionState::Status>::kMap[] = { - {ConnectionState::kDisabled, "disabled"}, - {ConnectionState::kUnconfigured, "unconfigured"}, - {ConnectionState::kConnecting, "connecting"}, - {ConnectionState::kOnline, "online"}, - {ConnectionState::kOffline, "offline"}, -}; - -template <> -const EnumToStringMap<SetupState::Status>::Map - EnumToStringMap<SetupState::Status>::kMap[] = { - {SetupState::kNone, nullptr}, - {SetupState::kInProgress, "inProgress"}, - {SetupState::kSuccess, "success"}, -}; - -template <> -const EnumToStringMap<WifiType>::Map EnumToStringMap<WifiType>::kMap[] = { - {WifiType::kWifi24, "2.4GHz"}, - {WifiType::kWifi50, "5.0GHz"}, -}; - -template <> -const EnumToStringMap<PairingType>::Map EnumToStringMap<PairingType>::kMap[] = { - {PairingType::kPinCode, "pinCode"}, - {PairingType::kEmbeddedCode, "embeddedCode"}, - {PairingType::kUltrasound32, "ultrasound32"}, - {PairingType::kAudible32, "audible32"}, -}; - -template <> -const EnumToStringMap<CryptoType>::Map EnumToStringMap<CryptoType>::kMap[] = { - {CryptoType::kNone, "none"}, - {CryptoType::kSpake_p224, "p224_spake2"}, - {CryptoType::kSpake_p256, "p256_spake2"}, -}; - -template <> -const EnumToStringMap<AuthScope>::Map EnumToStringMap<AuthScope>::kMap[] = { - {AuthScope::kNone, "none"}, - {AuthScope::kViewer, "viewer"}, - {AuthScope::kUser, "user"}, - {AuthScope::kOwner, "owner"}, -}; - struct { const char* const reason; int code; @@ -226,16 +147,6 @@ {errors::kNotImplemented, chromeos::http::status_code::NotSupported}, }; -template <typename T> -std::string EnumToString(T id) { - return EnumToStringMap<T>::FindNameById(id); -} - -template <typename T> -bool StringToEnum(const std::string& name, T* id) { - return EnumToStringMap<T>::FindIdByName(name, id); -} - AuthScope AuthScopeFromString(const std::string& scope, AuthScope auto_scope) { if (scope == kAuthScopeAutoValue) return auto_scope; @@ -931,21 +842,5 @@ base::Bind(&OnCommandRequestFailed, callback)); } -bool StringToPairingType(const std::string& mode, PairingType* id) { - return StringToEnum(mode, id); -} - -std::string PairingTypeToString(PairingType id) { - return EnumToString(id); -} - -bool StringToAuthScope(const std::string& scope, AuthScope* id) { - return StringToEnum(scope, id); -} - -std::string AuthScopeToString(AuthScope id) { - return EnumToString(id); -} - } // namespace privet } // namespace weave
diff --git a/libweave/src/privet/privet_types.cc b/libweave/src/privet/privet_types.cc new file mode 100644 index 0000000..7c68929 --- /dev/null +++ b/libweave/src/privet/privet_types.cc
@@ -0,0 +1,98 @@ +// Copyright 2014 The Chromium OS Authors. All rights reserved. +// Use of this source code is governed by a BSD-style license that can be +// found in the LICENSE file. + +#include "libweave/src/privet/privet_types.h" + +#include <string> + +#include "weave/enum_to_string.h" +#include "weave/types.h" + +namespace weave { + +namespace { + +using privet::AuthScope; +using privet::ConnectionState; +using privet::CryptoType; +using privet::SetupState; +using privet::WifiType; + +const EnumToStringMap<PairingType>::Map kPairingTypeMap[] = { + {PairingType::kPinCode, "pinCode"}, + {PairingType::kEmbeddedCode, "embeddedCode"}, + {PairingType::kUltrasound32, "ultrasound32"}, + {PairingType::kAudible32, "audible32"}, +}; + +const EnumToStringMap<ConnectionState::Status>::Map kConnectionStateMap[] = { + {ConnectionState::kDisabled, "disabled"}, + {ConnectionState::kUnconfigured, "unconfigured"}, + {ConnectionState::kConnecting, "connecting"}, + {ConnectionState::kOnline, "online"}, + {ConnectionState::kOffline, "offline"}, +}; + +const EnumToStringMap<SetupState::Status>::Map kSetupStateMap[] = { + {SetupState::kNone, nullptr}, + {SetupState::kInProgress, "inProgress"}, + {SetupState::kSuccess, "success"}, +}; + +const EnumToStringMap<WifiType>::Map kWifiTypeMap[] = { + {WifiType::kWifi24, "2.4GHz"}, + {WifiType::kWifi50, "5.0GHz"}, +}; + +const EnumToStringMap<CryptoType>::Map kCryptoTypeMap[] = { + {CryptoType::kNone, "none"}, + {CryptoType::kSpake_p224, "p224_spake2"}, + {CryptoType::kSpake_p256, "p256_spake2"}, +}; + +const EnumToStringMap<AuthScope>::Map kAuthScopeMap[] = { + {AuthScope::kNone, "none"}, + {AuthScope::kViewer, "viewer"}, + {AuthScope::kUser, "user"}, + {AuthScope::kOwner, "owner"}, +}; + +const EnumToStringMap<WifiSetupState>::Map kWifiSetupStateMap[] = { + {WifiSetupState::kDisabled, "disabled"}, + {WifiSetupState::kBootstrapping, "waiting"}, + {WifiSetupState::kMonitoring, "monitoring"}, + {WifiSetupState::kConnecting, "connecting"}, +}; + +} // namespace + +template <> +EnumToStringMap<PairingType>::EnumToStringMap() + : EnumToStringMap(kPairingTypeMap) {} + +template <> +EnumToStringMap<ConnectionState::Status>::EnumToStringMap() + : EnumToStringMap(kConnectionStateMap) {} + +template <> +EnumToStringMap<SetupState::Status>::EnumToStringMap() + : EnumToStringMap(kSetupStateMap) {} + +template <> +EnumToStringMap<WifiType>::EnumToStringMap() + : EnumToStringMap(kWifiTypeMap) {} + +template <> +EnumToStringMap<CryptoType>::EnumToStringMap() + : EnumToStringMap(kCryptoTypeMap) {} + +template <> +EnumToStringMap<AuthScope>::EnumToStringMap() + : EnumToStringMap(kAuthScopeMap) {} + +template <> +EnumToStringMap<WifiSetupState>::EnumToStringMap() + : EnumToStringMap(kWifiSetupStateMap) {} + +} // namespace weave
diff --git a/libweave/src/privet/privet_types.h b/libweave/src/privet/privet_types.h index 6247f6d..7626400 100644 --- a/libweave/src/privet/privet_types.h +++ b/libweave/src/privet/privet_types.h
@@ -12,6 +12,17 @@ namespace weave { namespace privet { +enum class CryptoType { + kNone, + kSpake_p224, + kSpake_p256, +}; + +enum class WifiType { + kWifi24, + kWifi50, +}; + // Scopes in order of increasing privileges. enum class AuthScope { kNone,
diff --git a/libweave/src/privet/security_delegate.h b/libweave/src/privet/security_delegate.h index 57098f6..616cc3d 100644 --- a/libweave/src/privet/security_delegate.h +++ b/libweave/src/privet/security_delegate.h
@@ -18,12 +18,6 @@ namespace weave { namespace privet { -enum class CryptoType { - kNone, - kSpake_p224, - kSpake_p256, -}; - // Interface to provide Security related logic for |PrivetHandler|. class SecurityDelegate { public: @@ -63,12 +57,6 @@ chromeos::ErrorPtr* error) = 0; }; -bool StringToPairingType(const std::string& mode, PairingType* id); -std::string PairingTypeToString(PairingType id); - -bool StringToAuthScope(const std::string& scope, AuthScope* id); -std::string AuthScopeToString(AuthScope id); - } // namespace privet } // namespace weave
diff --git a/libweave/src/privet/wifi_delegate.h b/libweave/src/privet/wifi_delegate.h index 7cab3d0..ae71849 100644 --- a/libweave/src/privet/wifi_delegate.h +++ b/libweave/src/privet/wifi_delegate.h
@@ -14,11 +14,6 @@ namespace weave { namespace privet { -enum class WifiType { - kWifi24, - kWifi50, -}; - // Interface to provide WiFi functionality for PrivetHandler. class WifiDelegate { public:
diff --git a/libweave/src/registration_status.cc b/libweave/src/registration_status.cc index 1a56f75..8392d57 100644 --- a/libweave/src/registration_status.cc +++ b/libweave/src/registration_status.cc
@@ -2,25 +2,24 @@ // Use of this source code is governed by a BSD-style license that can be // found in the LICENSE file. -#include "libweave/src/registration_status.h" - -#include <base/logging.h> +#include "weave/enum_to_string.h" +#include "weave/types.h" namespace weave { -std::string StatusToString(RegistrationStatus status) { - switch (status) { - case RegistrationStatus::kUnconfigured: - return "unconfigured"; - case RegistrationStatus::kConnecting: - return "connecting"; - case RegistrationStatus::kConnected: - return "connected"; - case RegistrationStatus::kInvalidCredentials: - return "invalid_credentials"; - } - CHECK(0) << "Unknown status"; - return "unknown"; -} +namespace { + +const EnumToStringMap<RegistrationStatus>::Map kMap[] = { + {RegistrationStatus::kUnconfigured, "unconfigured"}, + {RegistrationStatus::kConnecting, "connecting"}, + {RegistrationStatus::kConnected, "connected"}, + {RegistrationStatus::kInvalidCredentials, "invalid_credentials"}, +}; + +} // namespace + +template <> +EnumToStringMap<RegistrationStatus>::EnumToStringMap() + : EnumToStringMap(kMap) {} } // namespace weave
diff --git a/libweave/src/registration_status.h b/libweave/src/registration_status.h deleted file mode 100644 index 6511de1..0000000 --- a/libweave/src/registration_status.h +++ /dev/null
@@ -1,19 +0,0 @@ -// Copyright 2015 The Chromium OS Authors. All rights reserved. -// Use of this source code is governed by a BSD-style license that can be -// found in the LICENSE file. - -#ifndef LIBWEAVE_SRC_REGISTRATION_STATUS_H_ -#define LIBWEAVE_SRC_REGISTRATION_STATUS_H_ - -#include <string> - -#include "weave/types.h" - -namespace weave { - -// TODO(vitalybuka): Use EnumToString. -std::string StatusToString(RegistrationStatus status); - -} // namespace weave - -#endif // LIBWEAVE_SRC_REGISTRATION_STATUS_H_