Add function that checks if an element exists in a container
authorJi-hoon Lee <dalton.lee@samsung.com>
Mon, 8 Apr 2019 01:06:21 +0000 (10:06 +0900)
committerJi-hoon Lee <dalton.lee@samsung.com>
Mon, 8 Apr 2019 02:14:39 +0000 (11:14 +0900)
Change-Id: Ia43bc567646915443f77d7a60c1a1edbbe1940a3

plugins/wakeup-manager/src/wakeup_engine_manager.cpp

index 63879aa..eb8dacb 100644 (file)
@@ -10,6 +10,13 @@ namespace multiassistant
 namespace wakeup
 {
 
+/* Utility function for checking if an element exists in a container */
+template<class C, class T>
+static auto contains(const C& v, const T& x) -> decltype(end(v), true)
+{
+    return end(v) != find(begin(v), end(v), x);
+}
+
 CWakeupEngineManager::CWakeupEngineManager()
 {
 }
@@ -112,11 +119,10 @@ bool CWakeupEngineManager::get_audio_data_required()
 void CWakeupEngineManager::set_selected_wakeup_info(wakeup_event_info wakeup_info)
 {
        for (const auto& info : mEngineInfo) {
-               auto iter = find(
-                       info.assistant_list.begin(),
-                       info.assistant_list.end(),
-                       string{wakeup_info.wakeup_appid});
-               if (iter != info.assistant_list.end()) {
+               string appid = string{wakeup_info.wakeup_appid};
+               bool found = contains(info.assistant_list, appid);
+
+               if (found) {
                        mSelectedEngine = &info;
                        MWR_LOGD("Selected : %s", info.engine_name.c_str());
                }
@@ -266,11 +272,8 @@ void CWakeupEngineManager::engine_add_target_assistant(string engine_name, strin
 void CWakeupEngineManager::engine_add_wakeup_word(string appid, string wakeup_word, string language)
 {
        for (const auto& info : mEngineInfo) {
-               auto iter = find(
-                       info.assistant_list.begin(),
-                       info.assistant_list.end(),
-                       appid);
-               if (iter != info.assistant_list.end()) {
+               bool found = contains(info.assistant_list, appid);
+               if (found) {
                        if (info.interface.add_wakeup_word) {
                                info.interface.add_wakeup_word(appid.c_str(), wakeup_word.c_str(), language.c_str());
                        }
@@ -281,11 +284,8 @@ void CWakeupEngineManager::engine_add_wakeup_word(string appid, string wakeup_wo
 void CWakeupEngineManager::engine_set_assistant_specific_command(string appid, string command)
 {
        for (const auto& info : mEngineInfo) {
-               auto iter = find(
-                       info.assistant_list.begin(),
-                       info.assistant_list.end(),
-                       appid);
-               if (iter != info.assistant_list.end()) {
+               bool found = contains(info.assistant_list, appid);
+               if (found) {
                        if (info.interface.set_assistant_specific_command) {
                                info.interface.set_assistant_specific_command(appid.c_str(), command.c_str());
                        }