[mediacontroller][common] Use PlatformEnum in MediaController. 52/205552/9
authorMichal Michalski <m.michalski2@partner.samsung.com>
Tue, 7 May 2019 08:59:32 +0000 (10:59 +0200)
committerMichal Michalski <m.michalski2@partner.samsung.com>
Wed, 8 May 2019 09:13:47 +0000 (11:13 +0200)
Refactor enums in MediaController module using PlatformEnum
template class.

[Verification] Code compiles + MC TCT passed.

Signed-off-by: Michal Michalski <m.michalski2@partner.samsung.com>
Change-Id: Ie861d1c0aca7651a2f9ce45b966773082abf6045

src/mediacontroller/mediacontroller_client.cc
src/mediacontroller/mediacontroller_server.cc
src/mediacontroller/mediacontroller_utils.cc
src/mediacontroller/mediacontroller_utils.h

index 0899a3f734f30498eaacf7b817c96dbd206b98d4..2db0e8dfef4d41a131d0141ca8f4e968582e5fd3 100644 (file)
@@ -157,10 +157,9 @@ PlatformResult MediaControllerClient::GetLatestServerInfo(picojson::value* serve
   }
 
   std::string state_str;
-  PlatformResult result = Types::PlatformEnumToString(Types::kMediaControllerServerState,
-                                                      static_cast<int>(state), &state_str);
+  PlatformResult result = types::MediaControllerServerStateEnum.getName(state, &state_str);
   if (!result) {
-    LoggerE("PlatformEnumToString failed, error: %s", result.message().c_str());
+    LoggerE("MediaControllerServerStateEnum.getName() failed, error: %s", result.message().c_str());
     return result;
   }
 
@@ -195,7 +194,7 @@ PlatformResult MediaControllerClient::GetPlaybackInfo(const std::string& server_
 
   // playback state
   std::string state;
-  PlatformResult result = Types::ConvertPlaybackState(playback_h, &state);
+  PlatformResult result = types::ConvertPlaybackState(playback_h, &state);
   if (!result) {
     LoggerE("ConvertPlaybackState failed, error: %s", result.message().c_str());
     return result;
@@ -203,7 +202,7 @@ PlatformResult MediaControllerClient::GetPlaybackInfo(const std::string& server_
 
   // playback position
   double position;
-  result = Types::ConvertPlaybackPosition(playback_h, &position);
+  result = types::ConvertPlaybackPosition(playback_h, &position);
   if (!result) {
     LoggerE("ConvertPlaybackPosition failed, error: %s", result.message().c_str());
     return result;
@@ -211,7 +210,7 @@ PlatformResult MediaControllerClient::GetPlaybackInfo(const std::string& server_
 
   // content age rating
   std::string rating;
-  result = Types::ConvertContentAgeRating(playback_h, &rating);
+  result = types::ConvertContentAgeRating(playback_h, &rating);
   if (!result) {
     LoggerE("ConvertContentAgeRating failed, error: %s", result.message().c_str());
     return result;
@@ -259,7 +258,6 @@ PlatformResult MediaControllerClient::GetPlaybackInfo(const std::string& server_
   (*playback_info)["index"] = picojson::value(std::string(index ? index : ""));
   (*playback_info)["playlistName"] =
       picojson::value(std::string(playlist_name ? playlist_name : ""));
-  ;
 
   return PlatformResult(ErrorCode::NO_ERROR);
 }
@@ -281,7 +279,7 @@ PlatformResult MediaControllerClient::GetMetadata(const std::string& server_name
     mc_metadata_destroy(metadata_h);
   };
 
-  PlatformResult result = Types::ConvertMetadata(metadata_h, metadata);
+  PlatformResult result = types::ConvertMetadata(metadata_h, metadata);
   if (!result) {
     return result;
   }
@@ -323,10 +321,9 @@ void MediaControllerClient::OnServerStatusUpdate(const char* server_name, mc_ser
 
   // server state
   std::string state_str;
-  PlatformResult result = Types::PlatformEnumToString(Types::kMediaControllerServerState,
-                                                      static_cast<int>(state), &state_str);
+  PlatformResult result = types::MediaControllerServerStateEnum.getName(state, &state_str);
   if (!result) {
-    LoggerE("PlatformEnumToString failed, error: %s", result.message().c_str());
+    LoggerE("MediaControllerServerStateEnum.getName() failed, error: %s", result.message().c_str());
     return;
   }
 
@@ -420,7 +417,7 @@ void MediaControllerClient::OnPlaybackUpdate(const char* server_name, mc_playbac
 
   // playback state
   std::string state;
-  PlatformResult result = Types::ConvertPlaybackState(playback, &state);
+  PlatformResult result = types::ConvertPlaybackState(playback, &state);
   if (!result) {
     LoggerE("ConvertPlaybackState failed, error: %s", result.message().c_str());
     return;
@@ -428,7 +425,7 @@ void MediaControllerClient::OnPlaybackUpdate(const char* server_name, mc_playbac
 
   // playback position
   double position;
-  result = Types::ConvertPlaybackPosition(playback, &position);
+  result = types::ConvertPlaybackPosition(playback, &position);
   if (!result) {
     LoggerE("ConvertPlaybackPosition failed, error: %s", result.message().c_str());
     return;
@@ -477,11 +474,10 @@ void MediaControllerClient::OnRepeatModeUpdate(const char* server_name, mc_repea
     data_o["mode"] = picojson::value(mode == MC_REPEAT_MODE_ON);
     client->playback_info_listener_(&data);
   }
-  std::string state{};
-  PlatformResult result = Types::PlatformEnumToString(Types::kMediaControllerRepeatState,
-                                                      static_cast<int>(mode), &state);
+  std::string state;
+  PlatformResult result = types::MediaControllerRepeatModeEnum.getName(mode, &state);
   if (!result) {
-    LoggerE("PlatformEnumToString failed, error: %s", result.message().c_str());
+    LoggerE("MediaControllerRepeatModeEnum.getName() failed, error: %s", result.message().c_str());
     return;
   }
   data_o["action"] = picojson::value(std::string("onrepeatstatechanged"));
@@ -499,7 +495,7 @@ void MediaControllerClient::OnMetadataUpdate(const char* server_name, mc_metadat
   picojson::object& data_o = data.get<picojson::object>();
 
   picojson::value metadata = picojson::value(picojson::object());
-  PlatformResult result = Types::ConvertMetadata(metadata_h, &metadata.get<picojson::object>());
+  PlatformResult result = types::ConvertMetadata(metadata_h, &metadata.get<picojson::object>());
   if (!result) {
     LoggerE("ConvertMetadata failed, error: %s", result.message().c_str());
     return;
@@ -594,11 +590,10 @@ void MediaControllerClient::OnCommandReply(const char* server_name, const char*
 PlatformResult MediaControllerClient::SendPlaybackState(const std::string& server_name,
                                                         const std::string& state) {
   ScopeLogger();
-  int state_e;
   // In Native API, since Tizen 5.0 an action instead of a state is sent to change the state of a
   // server. In Web API the names were not refactored.
-  PlatformResult result =
-      Types::StringToPlatformEnum(Types::kMediaControllerPlaybackAction, state, &state_e);
+  mc_playback_action_e action_e;
+  PlatformResult result = types::MediaControllerPlaybackActionEnum.getValue(state, &action_e);
   if (!result) {
     return result;
   }
@@ -608,10 +603,7 @@ PlatformResult MediaControllerClient::SendPlaybackState(const std::string& serve
   SCOPE_EXIT {
     free(request_id);
   };*/
-  int ret;
-  ret = mc_client_send_playback_action_cmd(handle_, server_name.c_str(),
-                                           static_cast<mc_playback_action_e>(state_e),
-                                           /*&request_id*/ nullptr);
+  int ret = mc_client_send_playback_action_cmd(handle_, server_name.c_str(), action_e, nullptr);
   if (MEDIA_CONTROLLER_ERROR_NONE != ret) {
     return LogAndCreateResult(ErrorCode::UNKNOWN_ERR, "Error sending playback state",
                               ("mc_client_send_playback_action_cmd() error: %d, message: %s", ret,
@@ -687,15 +679,13 @@ PlatformResult MediaControllerClient::SendRepeatState(const std::string& server_
   SCOPE_EXIT {
     free(request_id);
   };*/
-  int state_e;
-  PlatformResult result =
-      Types::StringToPlatformEnum(Types::kMediaControllerRepeatState, state, &state_e);
+  mc_repeat_mode_e state_e;
+  PlatformResult result = types::MediaControllerRepeatModeEnum.getValue(state, &state_e);
   if (!result) {
     return result;
   }
-  int ret = mc_client_send_repeat_mode_cmd(handle_, server_name.c_str(),
-                                           static_cast<mc_repeat_mode_e>(state_e),
-                                           /*&request_id*/ nullptr);
+
+  int ret = mc_client_send_repeat_mode_cmd(handle_, server_name.c_str(), state_e, nullptr);
   if (MEDIA_CONTROLLER_ERROR_NONE != ret) {
     return LogAndCreateResult(
         ErrorCode::UNKNOWN_ERR, "Error sending repeat state",
@@ -711,19 +701,17 @@ PlatformResult MediaControllerClient::SendPlaybackItem(const std::string& name,
                                                        const std::string& state, double position) {
   ScopeLogger();
 
-  int state_e;
   // In Native API, since Tizen 5.0 an action instead of a state is sent to change the state of a
   // server. In Web API the names were not refactored.
-  PlatformResult result =
-      Types::StringToPlatformEnum(Types::kMediaControllerPlaybackAction, state, &state_e);
-
+  mc_playback_action_e action_e;
+  PlatformResult result = types::MediaControllerPlaybackActionEnum.getValue(state, &action_e);
   if (!result) {
     return result;
   }
 
+  auto position_ull = static_cast<unsigned long long>(position);
   int ret = mc_client_send_playlist_cmd(handle_, name.c_str(), playlist_name.c_str(), index.c_str(),
-                                        static_cast<mc_playback_action_e>(state_e),
-                                        static_cast<unsigned long long>(position), nullptr);
+                                        action_e, position_ull, nullptr);
 
   if (MEDIA_CONTROLLER_ERROR_NONE != ret) {
     return LogAndCreateResult(
index b8de484286779c304185866f36a28b8b7f3ce42d..6eafb3929fbe28996a8cbd6d573e2b89ac5fe8be 100644 (file)
@@ -80,27 +80,25 @@ PlatformResult MediaControllerServer::Init() {
 PlatformResult MediaControllerServer::SetPlaybackState(const std::string& state) {
   ScopeLogger();
 
-  int state_int;
-  PlatformResult result =
-      Types::StringToPlatformEnum(Types::kMediaControllerPlaybackState, state, &state_int);
-
+  mc_playback_states_e state_e;
+  PlatformResult result = types::MediaControllerPlaybackStateEnum.getValue(state, &state_e);
   if (!result) {
     return result;
   }
 
-  if (static_cast<mc_playback_states_e>(state_int) == playback_state_) {
+  if (state_e == playback_state_) {
     LoggerD("No change in playback state requested, skipping");
     return PlatformResult(ErrorCode::NO_ERROR);
   }
 
-  int ret = mc_server_set_playback_state(handle_, static_cast<mc_playback_states_e>(state_int));
+  int ret = mc_server_set_playback_state(handle_, state_e);
   if (MEDIA_CONTROLLER_ERROR_NONE != ret) {
     return LogAndCreateResult(
         ErrorCode::UNKNOWN_ERR, "Error setting playback state",
         ("mc_server_set_playback_state() error: %d, message: %s", ret, get_error_message(ret)));
   }
 
-  playback_state_ = static_cast<mc_playback_states_e>(state_int);
+  playback_state_ = state_e;
 
   ret = mc_server_update_playback_info(handle_);
   if (MEDIA_CONTROLLER_ERROR_NONE != ret) {
@@ -142,23 +140,20 @@ PlatformResult MediaControllerServer::SetPlaybackPosition(double position) {
 PlatformResult MediaControllerServer::SetContentAgeRating(const std::string& rating) {
   ScopeLogger();
 
-  int rating_int = static_cast<int>(MC_CONTENT_RATING_ALL);
-  PlatformResult result =
-      Types::StringToPlatformEnum(Types::kMediaControllerContentAgeRating, rating, &rating_int);
-
+  mc_content_age_rating_e rating_e;
+  PlatformResult result = types::MediaControllerContentAgeRatingEnum.getValue(rating, &rating_e);
   if (!result) {
     return result;
   }
 
-  int ret =
-      mc_server_set_content_age_rating(handle_, static_cast<mc_content_age_rating_e>(rating_int));
+  int ret = mc_server_set_content_age_rating(handle_, rating_e);
   if (MEDIA_CONTROLLER_ERROR_NONE != ret) {
     return LogAndCreateResult(
         ErrorCode::UNKNOWN_ERR, "Error setting content age rating",
         ("mc_server_set_content_age_rating() error: %d, message: %s", ret, get_error_message(ret)));
   }
 
-  age_rating_ = static_cast<mc_content_age_rating_e>(rating_int);
+  age_rating_ = rating_e;
 
   ret = mc_server_update_playback_info(handle_);
   if (MEDIA_CONTROLLER_ERROR_NONE != ret) {
@@ -217,26 +212,25 @@ PlatformResult MediaControllerServer::SetRepeatMode(bool mode) {
 PlatformResult MediaControllerServer::SetRepeatState(const std::string& state) {
   ScopeLogger();
 
-  int repeat_mode = MC_REPEAT_MODE_OFF;
-  PlatformResult result =
-      Types::StringToPlatformEnum(Types::kMediaControllerRepeatState, state, &repeat_mode);
+  mc_repeat_mode_e mode_e;
+  PlatformResult result = types::MediaControllerRepeatModeEnum.getValue(state, &mode_e);
   if (!result) {
     return result;
   }
 
-  if ((repeat_mode == repeat_mode_) && (is_repeat_mode_set_)) {
+  if ((mode_e == repeat_mode_) && (is_repeat_mode_set_)) {
     LoggerD("No change in repeat mode requested, skipping");
     return PlatformResult(ErrorCode::NO_ERROR);
   }
 
-  int ret = mc_server_update_repeat_mode(handle_, static_cast<mc_repeat_mode_e>(repeat_mode));
+  int ret = mc_server_update_repeat_mode(handle_, mode_e);
   if (MEDIA_CONTROLLER_ERROR_NONE != ret) {
     return LogAndCreateResult(
         ErrorCode::UNKNOWN_ERR, "Error updating repeat mode",
         ("mc_server_update_repeat_mode() error: %d, message: %s", ret, get_error_message(ret)));
   }
 
-  repeat_mode_ = static_cast<mc_repeat_mode_e>(repeat_mode);
+  repeat_mode_ = mode_e;
   is_repeat_mode_set_ = true;
 
   return PlatformResult(ErrorCode::NO_ERROR);
@@ -245,16 +239,14 @@ PlatformResult MediaControllerServer::SetRepeatState(const std::string& state) {
 PlatformResult MediaControllerServer::SetMetadata(const picojson::object& metadata) {
   ScopeLogger();
 
-  int attribute_int, ret;
   for (picojson::object::const_iterator i = metadata.begin(); i != metadata.end(); ++i) {
-    PlatformResult result = Types::StringToPlatformEnum(Types::kMediaControllerMetadataAttribute,
-                                                        i->first, &attribute_int);
+    mc_meta_e attr_e;
+    PlatformResult result = types::MediaControllerMetadataAttributeEnum.getValue(i->first, &attr_e);
     if (!result) {
       return result;
     }
 
-    ret = mc_server_set_metadata(handle_, static_cast<mc_meta_e>(attribute_int),
-                                 i->second.to_str().c_str());
+    int ret = mc_server_set_metadata(handle_, attr_e, i->second.to_str().c_str());
     if (MEDIA_CONTROLLER_ERROR_NONE != ret) {
       return LogAndCreateResult(ErrorCode::UNKNOWN_ERR, "Error setting metadata",
                                 ("mc_server_set_metadata(%s) error: %d, message: %s",
@@ -262,7 +254,7 @@ PlatformResult MediaControllerServer::SetMetadata(const picojson::object& metada
     }
   }
 
-  ret = mc_server_update_metadata(handle_);
+  int ret = mc_server_update_metadata(handle_);
   if (MEDIA_CONTROLLER_ERROR_NONE != ret) {
     return LogAndCreateResult(
         ErrorCode::UNKNOWN_ERR, "Error updating metadata",
@@ -475,19 +467,17 @@ PlatformResult MediaControllerServer::MediaControllerPlaylistAddItem(
     return LogAndCreateResult(ErrorCode::UNKNOWN_ERR, "Playlist with given name doesn't exist");
   }
 
-  int attribute_int = MC_META_MEDIA_TITLE, ret = MEDIA_CONTROLLER_ERROR_NONE;
   for (const auto& v : metadata) {
     LoggerD("Key: %s - Value: %s ", v.first.c_str(), v.second.to_str().c_str());
 
-    PlatformResult result = Types::StringToPlatformEnum(Types::kMediaControllerMetadataAttribute,
-                                                        v.first, &attribute_int);
+    mc_meta_e attr_e;
+    PlatformResult result = types::MediaControllerMetadataAttributeEnum.getValue(v.first, &attr_e);
     if (!result) {
       return result;
     }
 
-    ret = mc_server_add_item_to_playlist(handle_, playlist_handle_map_[name], index.c_str(),
-                                         static_cast<mc_meta_e>(attribute_int),
-                                         v.second.to_str().c_str());
+    int ret = mc_server_add_item_to_playlist(handle_, playlist_handle_map_[name], index.c_str(),
+                                         attr_e, v.second.to_str().c_str());
     if (MEDIA_CONTROLLER_ERROR_NONE != ret) {
       return LogAndCreateResult(
           ErrorCode::UNKNOWN_ERR, "Error adding playlist item",
@@ -515,7 +505,7 @@ PlatformResult MediaControllerServer::MediaControllerPlaylistGetItems(const std:
     picojson::value metadata_v = picojson::value(picojson::object());
     picojson::object& metadata_obj = metadata_v.get<picojson::object>();
 
-    auto result = Types::ConvertMetadata(metadata, &metadata_obj);
+    auto result = types::ConvertMetadata(metadata, &metadata_obj);
     if (!result) {
       return false;
     }
@@ -671,17 +661,17 @@ void MediaControllerServer::OnPlaybackActionCommand(const char* client_name, con
   MediaControllerServer* server = static_cast<MediaControllerServer*>(user_data);
 
   // Here, we need to convert mc_playback_action_e enum to mc_playback_states_e enum.
-  std::string state;
-  PlatformResult result = Types::PlatformEnumToString(Types::kMediaControllerPlaybackAction,
-                                                      static_cast<int>(action), &state);
+  std::string action_str;
+  PlatformResult result = types::MediaControllerPlaybackActionEnum.getName(action, &action_str);
   if (!result) {
-    LoggerW("PlatformEnumToString failed, error: %s", result.message().c_str());
+    LoggerW("MediaControllerPlaybackActionEnum.getName() failed, error: %s", result.message().c_str());
     return;
   }
-  int state_e = 0;
-  result = Types::StringToPlatformEnum(Types::kMediaControllerPlaybackState, state, &state_e);
+
+  mc_playback_states_e state_e;
+  result = types::MediaControllerPlaybackStateEnum.getValue(action_str, &state_e);
   if (!result) {
-    LoggerE("PlatformEnumToString failed, error: %s", result.message().c_str());
+    LoggerE("MediaControllerPlaybackStateEnum.getValue() failed, error: %s", result.message().c_str());
     return;
   }
 
@@ -694,7 +684,7 @@ void MediaControllerServer::OnPlaybackActionCommand(const char* client_name, con
   picojson::object& data_o = data.get<picojson::object>();
 
   data_o["action"] = picojson::value(std::string("onplaybackstaterequest"));
-  data_o["state"] = picojson::value(state);
+  data_o["state"] = picojson::value(action_str);
   data_o["clientName"] = picojson::value(client_name);
 
   server->change_request_playback_info_listener_(&data);
@@ -768,11 +758,10 @@ void MediaControllerServer::OnRepeatModeCommand(const char* client_name, const c
     server->change_request_playback_info_listener_(&data);
   }
 
-  std::string state{};
-  PlatformResult result = Types::PlatformEnumToString(Types::kMediaControllerRepeatState,
-                                                      static_cast<int>(mode), &state);
+  std::string state;
+  PlatformResult result = types::MediaControllerRepeatModeEnum.getName(mode, &state);
   if (!result) {
-    LoggerE("PlatformEnumToString failed, error: %s", result.message().c_str());
+    LoggerE("MediaControllerRepeatModeEnum.getName() failed, error: %s", result.message().c_str());
     return;
   }
   data_o["action"] = picojson::value(std::string("onrepeatstaterequest"));
@@ -789,12 +778,10 @@ void MediaControllerServer::OnPlaybackItemCommand(const char* client_name, const
 
   MediaControllerServer* server = static_cast<MediaControllerServer*>(user_data);
 
-  std::string state;
-  PlatformResult result = Types::PlatformEnumToString(Types::kMediaControllerPlaybackAction,
-                                                      static_cast<int>(action), &state);
-
+  std::string action_str;
+  PlatformResult result = types::MediaControllerPlaybackActionEnum.getName(action, &action_str);
   if (!result) {
-    LoggerE("PlatformEnumToString failed, error: %s", result.message().c_str());
+    LoggerE("MediaControllerPlaybackActionEnum.getName() failed, error: %s", result.message().c_str());
     return;
   }
 
@@ -804,7 +791,7 @@ void MediaControllerServer::OnPlaybackItemCommand(const char* client_name, const
   data_o["action"] = picojson::value(std::string("onplaybackitemrequest"));
   data_o["playlistName"] = picojson::value(playlist_name);
   data_o["index"] = picojson::value(index);
-  data_o["state"] = picojson::value(state);
+  data_o["state"] = picojson::value(action_str);
   data_o["position"] = picojson::value(static_cast<double>(position));
   data_o["clientName"] = picojson::value(client_name);
 
index e643c85805248414b0c2110a054e4b3a78635a9b..be6cb2979b65f7978653f7b6091c6397b9c01bb8 100644 (file)
@@ -28,133 +28,69 @@ namespace mediacontroller {
 using common::PlatformResult;
 using common::ErrorCode;
 
-const std::string Types::kMediaControllerServerState = "MediaControllerServerState";
-const std::string Types::kMediaControllerPlaybackState = "MediaControllerPlaybackState";
-const std::string Types::kMediaControllerPlaybackAction = "MediaControllerPlaybackAction";
-const std::string Types::kMediaControllerMetadataAttribute = "MediaControllerMetadataAttribute";
-const std::string Types::kMediaControllerRepeatState = "MediaControllerRepeatState";
-const std::string Types::kMediaControllerContentAgeRating = "MediaControllerContentAgeRationg";
-
-const PlatformEnumMap Types::platform_enum_map_ = {
-    {kMediaControllerServerState,
-     {{"NONE", MC_SERVER_STATE_NONE},
-      {"ACTIVE", MC_SERVER_STATE_ACTIVATE},
-      {"INACTIVE", MC_SERVER_STATE_DEACTIVATE}}},
-    {kMediaControllerPlaybackState,
-     {{"PLAY", MC_PLAYBACK_STATE_PLAYING},
-      {"PAUSE", MC_PLAYBACK_STATE_PAUSED},
-      {"STOP", MC_PLAYBACK_STATE_STOPPED},
-      {"NEXT", MC_PLAYBACK_STATE_MOVING_TO_NEXT},
-      {"PREV", MC_PLAYBACK_STATE_MOVING_TO_PREVIOUS},
-      {"FORWARD", MC_PLAYBACK_STATE_FAST_FORWARDING},
-      {"REWIND", MC_PLAYBACK_STATE_REWINDING}}},
-    {kMediaControllerPlaybackAction,
-     {{"PLAY", MC_PLAYBACK_ACTION_PLAY},
-      {"PAUSE", MC_PLAYBACK_ACTION_PAUSE},
-      {"STOP", MC_PLAYBACK_ACTION_STOP},
-      {"NEXT", MC_PLAYBACK_ACTION_NEXT},
-      {"PREV", MC_PLAYBACK_ACTION_PREV},
-      {"FORWARD", MC_PLAYBACK_ACTION_FAST_FORWARD},
-      {"REWIND", MC_PLAYBACK_ACTION_REWIND}}},
-    {kMediaControllerMetadataAttribute,
-     {{"title", MC_META_MEDIA_TITLE},
-      {"artist", MC_META_MEDIA_ARTIST},
-      {"album", MC_META_MEDIA_ALBUM},
-      {"author", MC_META_MEDIA_AUTHOR},
-      {"genre", MC_META_MEDIA_GENRE},
-      {"duration", MC_META_MEDIA_DURATION},
-      {"date", MC_META_MEDIA_DATE},
-      {"copyright", MC_META_MEDIA_COPYRIGHT},
-      {"description", MC_META_MEDIA_DESCRIPTION},
-      {"trackNum", MC_META_MEDIA_TRACK_NUM},
-      {"picture", MC_META_MEDIA_PICTURE}}},
-    {kMediaControllerRepeatState,
-     {{"REPEAT_OFF", MC_REPEAT_MODE_OFF},
-      {"REPEAT_ONE", MC_REPEAT_MODE_ONE_MEDIA},
-      {"REPEAT_ALL", MC_REPEAT_MODE_ON}}},
-    {kMediaControllerContentAgeRating,
-     {
-         {"ALL", MC_CONTENT_RATING_ALL},    {"1", MC_CONTENT_RATING_1_PLUS},
-         {"2", MC_CONTENT_RATING_2_PLUS},   {"3", MC_CONTENT_RATING_3_PLUS},
-         {"4", MC_CONTENT_RATING_4_PLUS},   {"5", MC_CONTENT_RATING_5_PLUS},
-         {"6", MC_CONTENT_RATING_6_PLUS},   {"7", MC_CONTENT_RATING_7_PLUS},
-         {"8", MC_CONTENT_RATING_8_PLUS},   {"9", MC_CONTENT_RATING_9_PLUS},
-         {"10", MC_CONTENT_RATING_10_PLUS}, {"11", MC_CONTENT_RATING_11_PLUS},
-         {"12", MC_CONTENT_RATING_12_PLUS}, {"13", MC_CONTENT_RATING_13_PLUS},
-         {"14", MC_CONTENT_RATING_14_PLUS}, {"15", MC_CONTENT_RATING_15_PLUS},
-         {"16", MC_CONTENT_RATING_16_PLUS}, {"17", MC_CONTENT_RATING_17_PLUS},
-         {"18", MC_CONTENT_RATING_18_PLUS}, {"19", MC_CONTENT_RATING_19_PLUS},
-     }}};
-
-PlatformEnumReverseMap Types::platform_enum_reverse_map_ = {};
-
-PlatformResult Types::GetPlatformEnumMap(const std::string& type,
-                                         std::map<std::string, int> const** enum_map) {
-  ScopeLogger();
-
-  auto iter = platform_enum_map_.find(type);
-  if (iter == platform_enum_map_.end()) {
-    return LogAndCreateResult(ErrorCode::UNKNOWN_ERR,
-                              std::string("Undefined platform enum type ") + type);
-  }
-
-  *enum_map = &iter->second;
-
-  return PlatformResult(ErrorCode::NO_ERROR);
-}
-
-PlatformResult Types::StringToPlatformEnum(const std::string& type, const std::string& value,
-                                           int* platform_enum) {
-  ScopeLogger();
-
-  std::map<std::string, int> const* def = nullptr;
-  PlatformResult result = GetPlatformEnumMap(type, &def);
-  if (!result) {
-    return result;
-  }
-
-  auto def_iter = def->find(value);
-  if (def_iter != def->end()) {
-    *platform_enum = def_iter->second;
-    return PlatformResult(ErrorCode::NO_ERROR);
-  }
-
-  std::string message = "Platform enum value " + value + " not found for " + type;
-  return LogAndCreateResult(ErrorCode::INVALID_VALUES_ERR, message);
-}
-
-PlatformResult Types::PlatformEnumToString(const std::string& type, int value,
-                                           std::string* platform_str) {
-  ScopeLogger();
-
-  if (platform_enum_reverse_map_.empty()) {
-    for (auto& def : platform_enum_map_) {
-      platform_enum_reverse_map_[def.first] = {};
-
-      for (auto& key : def.second) {
-        platform_enum_reverse_map_[def.first][key.second] = key.first;
-      }
-    }
-  }
-
-  auto it = platform_enum_reverse_map_.find(type);
-  if (it == platform_enum_reverse_map_.end()) {
-    return LogAndCreateResult(ErrorCode::UNKNOWN_ERR,
-                              std::string("Undefined platform enum type ") + type);
-  }
-
-  auto def = platform_enum_reverse_map_.at(type);
-  auto def_it = def.find(value);
-  if (def_it != def.end()) {
-    *platform_str = def_it->second;
-    return PlatformResult(ErrorCode::NO_ERROR);
-  }
-
-  std::string message = "Platform enum value " + std::to_string(value) + " not found for " + type;
-  return LogAndCreateResult(ErrorCode::INVALID_VALUES_ERR, message);
-}
-
-PlatformResult Types::ConvertPlaybackState(mc_playback_h playback_h, std::string* state) {
+namespace types {
+
+const common::PlatformEnum<mc_server_state_e> MediaControllerServerStateEnum {
+    {"NONE", MC_SERVER_STATE_NONE},
+    {"ACTIVE", MC_SERVER_STATE_ACTIVATE},
+    {"INACTIVE", MC_SERVER_STATE_DEACTIVATE}
+};
+
+const common::PlatformEnum<mc_playback_states_e> MediaControllerPlaybackStateEnum {
+    {"PLAY", MC_PLAYBACK_STATE_PLAYING},
+    {"PAUSE", MC_PLAYBACK_STATE_PAUSED},
+    {"STOP", MC_PLAYBACK_STATE_STOPPED},
+    {"NEXT", MC_PLAYBACK_STATE_MOVING_TO_NEXT},
+    {"PREV", MC_PLAYBACK_STATE_MOVING_TO_PREVIOUS},
+    {"FORWARD", MC_PLAYBACK_STATE_FAST_FORWARDING},
+    {"REWIND", MC_PLAYBACK_STATE_REWINDING}
+};
+
+const common::PlatformEnum<mc_playback_action_e> MediaControllerPlaybackActionEnum {
+    {"PLAY", MC_PLAYBACK_ACTION_PLAY},
+    {"PAUSE", MC_PLAYBACK_ACTION_PAUSE},
+    {"STOP", MC_PLAYBACK_ACTION_STOP},
+    {"NEXT", MC_PLAYBACK_ACTION_NEXT},
+    {"PREV", MC_PLAYBACK_ACTION_PREV},
+    {"FORWARD", MC_PLAYBACK_ACTION_FAST_FORWARD},
+    {"REWIND", MC_PLAYBACK_ACTION_REWIND}
+};
+
+const common::PlatformEnum<mc_meta_e> MediaControllerMetadataAttributeEnum {
+    {"title", MC_META_MEDIA_TITLE},
+    {"artist", MC_META_MEDIA_ARTIST},
+    {"album", MC_META_MEDIA_ALBUM},
+    {"author", MC_META_MEDIA_AUTHOR},
+    {"genre", MC_META_MEDIA_GENRE},
+    {"duration", MC_META_MEDIA_DURATION},
+    {"date", MC_META_MEDIA_DATE},
+    {"copyright", MC_META_MEDIA_COPYRIGHT},
+    {"description", MC_META_MEDIA_DESCRIPTION},
+    {"trackNum", MC_META_MEDIA_TRACK_NUM},
+    {"picture", MC_META_MEDIA_PICTURE}
+};
+
+const common::PlatformEnum<mc_repeat_mode_e> MediaControllerRepeatModeEnum {
+    {"REPEAT_OFF", MC_REPEAT_MODE_OFF},
+    {"REPEAT_ONE", MC_REPEAT_MODE_ONE_MEDIA},
+    {"REPEAT_ALL", MC_REPEAT_MODE_ON}
+};
+
+const common::PlatformEnum<mc_content_age_rating_e> MediaControllerContentAgeRatingEnum {
+     {"ALL", MC_CONTENT_RATING_ALL},    {"1", MC_CONTENT_RATING_1_PLUS},
+     {"2", MC_CONTENT_RATING_2_PLUS},   {"3", MC_CONTENT_RATING_3_PLUS},
+     {"4", MC_CONTENT_RATING_4_PLUS},   {"5", MC_CONTENT_RATING_5_PLUS},
+     {"6", MC_CONTENT_RATING_6_PLUS},   {"7", MC_CONTENT_RATING_7_PLUS},
+     {"8", MC_CONTENT_RATING_8_PLUS},   {"9", MC_CONTENT_RATING_9_PLUS},
+     {"10", MC_CONTENT_RATING_10_PLUS}, {"11", MC_CONTENT_RATING_11_PLUS},
+     {"12", MC_CONTENT_RATING_12_PLUS}, {"13", MC_CONTENT_RATING_13_PLUS},
+     {"14", MC_CONTENT_RATING_14_PLUS}, {"15", MC_CONTENT_RATING_15_PLUS},
+     {"16", MC_CONTENT_RATING_16_PLUS}, {"17", MC_CONTENT_RATING_17_PLUS},
+     {"18", MC_CONTENT_RATING_18_PLUS}, {"19", MC_CONTENT_RATING_19_PLUS}
+};
+
+
+PlatformResult ConvertPlaybackState(mc_playback_h playback_h, std::string* state) {
   ScopeLogger();
 
   int ret;
@@ -169,17 +105,16 @@ PlatformResult Types::ConvertPlaybackState(mc_playback_h playback_h, std::string
     state_e = MC_PLAYBACK_STATE_STOPPED;
   }
 
-  PlatformResult result = Types::PlatformEnumToString(Types::kMediaControllerPlaybackState,
-                                                      static_cast<int>(state_e), state);
+  PlatformResult result = MediaControllerPlaybackStateEnum.getName(state_e, state);
   if (!result) {
-    LoggerE("PlatformEnumToString failed, error: %s", result.message().c_str());
+    LoggerE("MediaControllerPlaybackStateEnum.getName() failed, error: %s", result.message().c_str());
     return result;
   }
 
   return PlatformResult(ErrorCode::NO_ERROR);
 }
 
-PlatformResult Types::ConvertContentAgeRating(mc_playback_h playback_h, std::string* rating) {
+PlatformResult ConvertContentAgeRating(mc_playback_h playback_h, std::string* rating) {
   ScopeLogger();
 
   mc_content_age_rating_e rating_e = MC_CONTENT_RATING_ALL;
@@ -190,18 +125,16 @@ PlatformResult Types::ConvertContentAgeRating(mc_playback_h playback_h, std::str
         ("mc_client_get_age_rating() error: %d, message: %s", ret, get_error_message(ret)));
   }
 
-  PlatformResult result = Types::PlatformEnumToString(Types::kMediaControllerContentAgeRating,
-                                                      static_cast<int>(rating_e), rating);
-
+  PlatformResult result = MediaControllerContentAgeRatingEnum.getName(rating_e, rating);
   if (!result) {
-    LoggerE("PlatformEnumToString failed, error: %s", result.message().c_str());
+    LoggerE("MediaControllerContentAgeRatingEnum.getName() failed, error: %s", result.message().c_str());
     return result;
   }
 
   return PlatformResult(ErrorCode::NO_ERROR);
 }
 
-PlatformResult Types::ConvertPlaybackPosition(mc_playback_h playback_h, double* position) {
+PlatformResult ConvertPlaybackPosition(mc_playback_h playback_h, double* position) {
   ScopeLogger();
 
   int ret;
@@ -219,36 +152,31 @@ PlatformResult Types::ConvertPlaybackPosition(mc_playback_h playback_h, double*
   return PlatformResult(ErrorCode::NO_ERROR);
 }
 
-PlatformResult Types::ConvertMetadata(mc_metadata_h metadata_h, picojson::object* metadata) {
+PlatformResult ConvertMetadata(mc_metadata_h metadata_h, picojson::object* metadata) {
   ScopeLogger();
 
-  std::map<std::string, int> const* metadata_fields = nullptr;
-  PlatformResult result =
-      GetPlatformEnumMap(Types::kMediaControllerMetadataAttribute, &metadata_fields);
-  if (!result) {
-    LoggerE("GetPlatformEnumMap failed, error: %s", result.message().c_str());
-    return result;
-  }
-
   char* value = nullptr;
   SCOPE_EXIT {
     free(value);
   };
 
-  for (auto& field : *metadata_fields) {
-    int ret = mc_metadata_get(metadata_h, static_cast<mc_meta_e>(field.second), &value);
+  for (auto entry: MediaControllerMetadataAttributeEnum) {
+    int ret = mc_metadata_get(metadata_h, entry.second, &value);
     if (ret != MEDIA_CONTROLLER_ERROR_NONE) {
       return LogAndCreateResult(ErrorCode::UNKNOWN_ERR, "Error getting metadata",
-                                ("mc_metadata_get(%s) error: %d, message: %s", field.first.c_str(),
+                                ("mc_metadata_get(%s) error: %d, message: %s", entry.first.c_str(),
                                  ret, get_error_message(ret)));
     }
 
-    (*metadata)[field.first] = picojson::value(std::string(value ? value : ""));
+    (*metadata)[entry.first] = picojson::value(std::string(value ? value : ""));
   }
 
   return PlatformResult(ErrorCode::NO_ERROR);
 }
 
+} // types
+
+
 PlatformResult utils::GetAllPlaylists(const std::string& app_id, picojson::array* playlists) {
   ScopeLogger();
 
index 97dff6a12caa1567a32abcd0e67999c2750470c2..7766dc8172a3d6593690b59dae78558aebf38cc4 100644 (file)
 #include <string>
 
 #include "common/platform_result.h"
+#include "common/platform_enum.h"
+
 
 namespace extension {
 namespace mediacontroller {
 
-typedef std::map<std::string, std::map<std::string, int>> PlatformEnumMap;
-typedef std::map<std::string, std::map<int, std::string>> PlatformEnumReverseMap;
 typedef std::function<void(picojson::value*)> JsonCallback;
 
-class Types {
- public:
-  static const std::string kMediaControllerServerState;
-  static const std::string kMediaControllerPlaybackState;
-  static const std::string kMediaControllerPlaybackAction;
-  static const std::string kMediaControllerMetadataAttribute;
-  static const std::string kMediaControllerRepeatState;
-  static const std::string kMediaControllerContentAgeRating;
-
-  static common::PlatformResult GetPlatformEnumMap(const std::string& type,
-                                                   std::map<std::string, int> const** platform_str);
 
-  static common::PlatformResult StringToPlatformEnum(const std::string& type,
-                                                     const std::string& value, int* platform_enum);
+namespace types {
 
-  static common::PlatformResult PlatformEnumToString(const std::string& type, int value,
-                                                     std::string* platform_str);
+common::PlatformResult ConvertPlaybackState(mc_playback_h playback_h, std::string* state);
+common::PlatformResult ConvertContentAgeRating(mc_playback_h playback_h, std::string* state);
+common::PlatformResult ConvertPlaybackPosition(mc_playback_h playback_h, double* position);
+common::PlatformResult ConvertMetadata(mc_metadata_h metadata_h, picojson::object* metadata);
 
-  static common::PlatformResult ConvertPlaybackState(mc_playback_h playback_h, std::string* state);
-  static common::PlatformResult ConvertContentAgeRating(mc_playback_h playback_h,
-                                                        std::string* state);
+extern const common::PlatformEnum<mc_server_state_e> MediaControllerServerStateEnum;
+extern const common::PlatformEnum<mc_playback_states_e> MediaControllerPlaybackStateEnum;
+extern const common::PlatformEnum<mc_playback_action_e> MediaControllerPlaybackActionEnum;
+extern const common::PlatformEnum<mc_meta_e> MediaControllerMetadataAttributeEnum;
+extern const common::PlatformEnum<mc_repeat_mode_e> MediaControllerRepeatModeEnum;
+extern const common::PlatformEnum<mc_content_age_rating_e> MediaControllerContentAgeRatingEnum;
 
-  static common::PlatformResult ConvertPlaybackPosition(mc_playback_h playback_h, double* position);
-  static common::PlatformResult ConvertMetadata(mc_metadata_h metadata_h,
-                                                picojson::object* metadata);
+} // namespace types
 
- private:
-  static const PlatformEnumMap platform_enum_map_;
-  static PlatformEnumReverseMap platform_enum_reverse_map_;
-};
 
 namespace utils {
 common::PlatformResult GetAllPlaylists(const std::string& app_id, picojson::array* playlists);