plugins icon indicating copy to clipboard operation
plugins copied to clipboard

[video_player_avplay] Remove duplicate code for checking player

Open xiaowei-guan opened this issue 1 year ago • 1 comments

  1. Extract a new Macro Definition to check if the player exists.
#define CHECK_PLAYER(statement)                                    \
  {                                                                \
    VideoPlayer *player = (statement);                             \
    if (!player) {                                                 \
      return FlutterError("Invalid argument", "Player not found"); \
    }                                                              \
  }

  1. Extract a new macro definition to check the result of player
#define CHECK_PLAYER_RESULT(statement, return_value)                           \
  {                                                                            \
    const auto result = (statement);                                           \
    if (result != PLAYER_ERROR_NONE) {                                         \
      LOG_ERROR("[MediaPlayer] Player error : %s", get_error_message(result)); \
      return return_value;                                                     \
    }                                                                          \
  }

#define CHECK_PLAYER_RESULT_NO_RETURN(statement)                               \
  {                                                                            \
    const auto result = (statement);                                           \
    if (result != PLAYER_ERROR_NONE) {                                         \
      LOG_ERROR("[MediaPlayer] Player error : %s", get_error_message(result)); \
    }                                                                          \
  }

xiaowei-guan avatar Mar 04 '24 09:03 xiaowei-guan

Hi I agree with your intention to reduce duplicate code. However, we don't generally use the macro method.(we follow Google c++ style guide.) https://google.github.io/styleguide/cppguide.html#Preprocessor_Macros If there is no suitable way, you can rename the macro (add project name + avoid conflict) and add #undef. But I recommend changing it to an appropriate function.

Ok, I'll find a batter solution.

xiaowei-guan avatar Mar 07 '24 02:03 xiaowei-guan