Remove unnecessary exception handling codes 56/206356/5
authorhyunho <hhstark.kang@samsung.com>
Fri, 17 May 2019 01:49:34 +0000 (10:49 +0900)
committerHyunho Kang <hhstark.kang@samsung.com>
Wed, 22 May 2019 00:32:54 +0000 (00:32 +0000)
Change-Id: Ia78cd72be09d5baf8fe7f056701261ff61c90059
Signed-off-by: hyunho <hhstark.kang@samsung.com>
watchface-complication-provider/complication-provider.cc
watchface-complication-provider/watchface-complication-provider.cc
watchface-complication/complication.cc
watchface-complication/design-element.cc
watchface-complication/editables-manager.cc
watchface-complication/received-editable.cc
watchface-complication/watchface-complication.cc
watchface-complication/watchface-editable.cc
watchface-editor/editables-editor.cc
watchface-editor/watchface-editor.cc

index 57b63ab..90de87b 100644 (file)
@@ -177,39 +177,36 @@ ComplicationProvider::Impl::SenderInfo* ComplicationProvider::Impl::GetSenderInf
       std::string sender_name, GDBusConnection* connection, GVariant* parameters) {
 
   auto iter = sender_info_.find(sender_name);
-  SenderInfo* si;
-  if (iter == sender_info_.end()) {
-    char* sender_app_id = NULL;
-    g_variant_get_child(parameters, 0, "&s", &sender_app_id);
-    if (sender_app_id == NULL  ||
-        !util::CheckSender(sender_app_id, sender_name, connection)) {
-      LOGE("invalid sender_app_id %s", sender_app_id);
-      return NULL;
-    }
+  if (iter != sender_info_.end())
+    return iter->second;
+
+  char* sender_app_id = nullptr;
+  g_variant_get_child(parameters, 0, "&s", &sender_app_id);
+  if (sender_app_id == nullptr  ||
+      !util::CheckSender(sender_app_id, sender_name, connection)) {
+    LOGE("invalid sender_app_id %s", sender_app_id);
+    return nullptr;
+  }
 
-    int watcher_id = 0;
-    watcher_id = gdbus_.get()->Watch(std::string(sender_app_id), this);
-    try {
-      if (trusted_ && !util::CheckCertificate(sender_app_id)) {
-        LOGE("Permission denied");
-        return NULL;
-      }
-      std::string watch_name = util::EncodeStr(util::EncodeType::Name, sender_app_id);
-      if (watch_name.empty()) {
-        LOGE("fail to get watch name");
-        return NULL;
-      }
-      si = new SenderInfo(sender_name, sender_app_id, watcher_id, watch_name);
-      sender_info_[sender_name] = si;
-      LOGI("sender_info added (%s) ", watch_name.c_str());
-    } catch (const std::bad_alloc &ba) {
-      LOGE("SenderInfo::Exception bad_alloc");
-      return NULL;
-    }
-    LOGI("create new sender_info_ ");
-  } else {
-    si = iter->second;
+  int watcher_id = gdbus_.get()->Watch(std::string(sender_app_id), this);
+  if (trusted_ && !util::CheckCertificate(sender_app_id)) {
+    LOGE("Permission denied");
+    return nullptr;
+  }
+  std::string watch_name = util::EncodeStr(
+      util::EncodeType::Name, sender_app_id);
+  if (watch_name.empty()) {
+    LOGE("fail to get watch name");
+    return nullptr;
+  }
+  SenderInfo* si = new (std::nothrow) SenderInfo(
+      sender_name, sender_app_id, watcher_id, watch_name);
+  if (si == nullptr) {
+    LOGE("Out of memory");
+    return nullptr;
   }
+  sender_info_[sender_name] = si;
+  LOGI("sender_info added (%s) ", watch_name.c_str());
   return si;
 }
 
index 74f56f8..a456983 100644 (file)
@@ -45,6 +45,7 @@
 static int _add_bundle_data(bundle* shared_data, const char* key,
     const char* value);
 
+using namespace std;
 using namespace tizen_base;
 using namespace watchface_complication;
 class CallbackInfo {
@@ -53,6 +54,23 @@ class CallbackInfo {
     void* user_data) : cb_(cb), user_data_(user_data) {
   }
 
+  CallbackInfo(CallbackInfo&& c) noexcept {
+    cb_ = c.cb_;
+    user_data_ = c.user_data_;
+    c.cb_ = nullptr;
+    c.user_data_ = nullptr;
+  }
+
+  CallbackInfo& operator = (CallbackInfo&& c) noexcept {
+    if (this != &c) {
+      cb_ = c.cb_;
+      user_data_ = c.user_data_;
+      c.cb_ = nullptr;
+      c.user_data_ = nullptr;
+    }
+    return *this;
+  }
+
   void Invoke(const std::string& provider_id, const std::string& sender_appid,
               ComplicationType type, const Bundle& context,
               Bundle* shared_data) {
@@ -87,14 +105,14 @@ class WatchComplicationProviderStub : public ComplicationProvider {
         GetProviderId().c_str(), sender_appid.c_str());
   }
 
-  int AddCallbackInfo(CallbackInfo* ci) {
+  int AddCallbackInfo(unique_ptr<CallbackInfo> ci) {
     for (auto& i : cb_list_) {
       if (i.get()->GetCallback() == ci->GetCallback()) {
         LOGI("already registered callback");
         return WATCHFACE_COMPLICATION_ERROR_INVALID_PARAMETER;
       }
     }
-    cb_list_.emplace_back(ci);
+    cb_list_.emplace_back(move(ci));
     return WATCHFACE_COMPLICATION_ERROR_NONE;
   }
 
@@ -130,7 +148,6 @@ extern "C" EXPORT_API int watchface_complication_provider_add_update_requested_c
     return WATCHFACE_COMPLICATION_ERROR_INVALID_PARAMETER;
   }
 
-  int ret = WATCHFACE_COMPLICATION_ERROR_NONE;
   auto cp = __providers.find(provider_id);
   auto ws = cp->second;
 
@@ -148,12 +165,14 @@ extern "C" EXPORT_API int watchface_complication_provider_add_update_requested_c
     LOGI("create new provider : %s", provider_id);
   }
 
-  auto ci = new CallbackInfo(cb, user_data);
-  ret = ws->AddCallbackInfo(ci);
-  if (ret != WATCHFACE_COMPLICATION_ERROR_NONE)
-    delete ci;
+  auto ci = unique_ptr<CallbackInfo>(
+      new (std::nothrow) CallbackInfo(cb, user_data));
+  if (ci.get() == nullptr) {
+    LOGE("Out of memeory");
+    return WATCHFACE_COMPLICATION_ERROR_OUT_OF_MEMORY;
+  }
 
-  return ret;
+  return ws->AddCallbackInfo(move(ci));
 }
 
 extern "C" EXPORT_API int watchface_complication_provider_remove_update_requested_cb(
@@ -169,7 +188,8 @@ extern "C" EXPORT_API int watchface_complication_provider_remove_update_requeste
   auto ws = cp->second;
 
   if (cp == __providers.end()) {
-     return WATCHFACE_COMPLICATION_ERROR_INVALID_PARAMETER;
+    LOGE("Cannot find callback");
+    return WATCHFACE_COMPLICATION_ERROR_INVALID_PARAMETER;
   }
 
   return ws->RemoveCallbackInfo(cb);
index 9bfcfad..b1c4b14 100644 (file)
@@ -203,17 +203,19 @@ void Complication::Impl::UpdatedProcess(GVariant* parameters) {
     return;
   }
 
-  if (raw != NULL) {
-    try {
-        last_data_.reset(new Bundle(std::string(reinterpret_cast<char*>(raw))));
-        LOGI("data: %s, %d, cur_type :%d",
-            provider_id, complication_id, cur_type_);
-        parent_->OnDataUpdated(std::string(provider_id), cur_type_, last_data_);
-    } catch (Exception &ex) {
-      LOGE("%s %d", ex.what(), ex.GetErrorCode());
-      return;
-    }
+  if (raw == nullptr) {
+    LOGW("Empty data !!");
+    return;
+  }
+
+  last_data_.reset(new (std::nothrow) Bundle(
+      string(reinterpret_cast<char*>(raw))));
+  if (last_data_.get() == nullptr) {
+    LOGE("Out of memmory, create bundle fail");
+    return;
   }
+  LOGI("data: %s, %d, cur_type :%d", provider_id, complication_id, cur_type_);
+  parent_->OnDataUpdated(std::string(provider_id), cur_type_, last_data_);
 }
 
 void Complication::Impl::NotifyDataUpdateProcess(GVariant* parameters) {
@@ -851,40 +853,27 @@ int Complication::Impl::GetNotSupportedEvents(
 }
 
 int Complication::Impl::AddCandidate(std::string provider_id, int type) {
-  bundle* data = bundle_create();
-  if (data == NULL)
+  unique_ptr<Bundle> data = unique_ptr<Bundle>(new (std::nothrow) Bundle());
+  if (data.get() == nullptr) {
+    LOGE("Out of memory");
     return WATCHFACE_COMPLICATION_ERROR_OUT_OF_MEMORY;
-  bundle_add_str(data, provider_id_key_.c_str(), provider_id.c_str());
-  bundle_add_str(data, provider_type_key_.c_str(),
-      std::to_string(type).c_str());
+  }
+  data->Add(provider_id_key_, provider_id);
+  data->Add(provider_type_key_, std::to_string(type));
 
   std::string error_msg = GetNotSupportedPrivileges(provider_id);
   if (!error_msg.empty())
-    bundle_add_str(data, privilege_error_key_.c_str(), error_msg.c_str());
+    data->Add(privilege_error_key_, error_msg);
 
   int not_supported_events = GetNotSupportedEvents(provider_id);
   if (not_supported_events > 0) {
-      bundle_add_str(data, supported_events_error_key_.c_str(),
-          std::to_string(not_supported_events).c_str());
+      data->Add(supported_events_error_key_,
+          std::to_string(not_supported_events));
   } else if (not_supported_events < 0) {
     LOGE("fail to get required events");
-    bundle_free(data);
     return WATCHFACE_COMPLICATION_ERROR_DB;
   }
-
-  try {
-    candidates_list_.emplace_back(new Bundle(data));
-  } catch (const std::bad_alloc &ba) {
-    LOGE("Bundle::Exception bad_alloc");
-    bundle_free(data);
-    return WATCHFACE_COMPLICATION_ERROR_OUT_OF_MEMORY;
-  } catch (Exception &ex) {
-    LOGE("%s %d", ex.what(), ex.GetErrorCode());
-    bundle_free(data);
-    return ex.GetErrorCode();
-  }
-  bundle_free(data);
-
+  candidates_list_.emplace_back(move(data));
   return WATCHFACE_COMPLICATION_ERROR_NONE;
 }
 
index a1d30f9..6a63c30 100644 (file)
@@ -172,18 +172,17 @@ std::unique_ptr<Bundle>& DesignElement::GetContext() const {
 }
 
 int DesignElement::UpdateLastContext() {
-  if (impl_->context_data_.get() == NULL) {
+  if (impl_->context_data_.get() == nullptr) {
     LOGI("Empty context");
     return WATCHFACE_COMPLICATION_ERROR_NONE;
   }
-
-  try {
-    impl_->last_context_data_.reset(
-            new Bundle((impl_->context_data_.get())->GetHandle()));
-  } catch (Exception &ex) {
-    LOGE("%s %d", ex.what(), ex.GetErrorCode());
-    return ex.GetErrorCode();
+  Bundle* ctx = new (std::nothrow) Bundle(
+      (impl_->context_data_.get())->GetHandle());
+  if (ctx == nullptr) {
+    LOGE("Out of memory");
+    return WATCHFACE_COMPLICATION_ERROR_OUT_OF_MEMORY;
   }
+  impl_->last_context_data_.reset(ctx);
   return WATCHFACE_COMPLICATION_ERROR_NONE;
 }
 
index 2d7703e..3f9e31f 100644 (file)
@@ -54,11 +54,9 @@ EditablesManager::Impl::Impl() = default;
 int EditablesManager::Init(bool mock) {
   int ret;
   int open_flags = (SQLITE_OPEN_READWRITE | SQLITE_OPEN_CREATE);
-  std::unique_ptr<Impl> tmp_impl;
-
-  try {
-    tmp_impl = std::unique_ptr<EditablesManager::Impl>(new Impl());
-  } catch (const std::bad_alloc &ba) {
+  std::unique_ptr<Impl> tmp_impl
+      = std::unique_ptr<EditablesManager::Impl>(new (std::nothrow) Impl());
+  if (tmp_impl.get() == nullptr) {
     LOGE("Out of memory");
     return WATCHFACE_COMPLICATION_ERROR_OUT_OF_MEMORY;
   }
index 1dccd84..7c45fa5 100644 (file)
@@ -217,21 +217,17 @@ std::unique_ptr<Bundle>& ReceivedEditable::GetContext() const {
 }
 
 int ReceivedEditable::UpdateLastContext() {
-  if (impl_->context_data_.get() == NULL) {
+  if (impl_->context_data_.get() == nullptr) {
     LOGI("Empty context");
     return WATCHFACE_COMPLICATION_ERROR_NONE;
   }
 
-  try {
-    impl_->last_context_data_.reset(
-        new Bundle((impl_->context_data_.get())->GetHandle()));
-  } catch (const std::bad_alloc &ba) {
-    LOGE("Bundle::Exception bad_alloc");
+  Bundle* ctx = new Bundle((impl_->context_data_.get())->GetHandle());
+  if (ctx == nullptr) {
+    LOGE("Out of memory");
     return WATCHFACE_COMPLICATION_ERROR_OUT_OF_MEMORY;
-  } catch (Exception &ex) {
-    LOGE("%s %d", ex.what(), ex.GetErrorCode());
-    return ex.GetErrorCode();
   }
+  impl_->last_context_data_.reset(ctx);
   return WATCHFACE_COMPLICATION_ERROR_NONE;
 }
 
index 574b1bf..ca26ae6 100644 (file)
@@ -42,6 +42,7 @@ struct complication_allowed_list_ {
   GList* allowed_list;
 };
 
+using namespace std;
 using namespace tizen_base;
 using namespace watchface_complication;
 class CallbackInfo {
@@ -51,6 +52,27 @@ class CallbackInfo {
     : cb_(cb), error_cb_(error_cb), user_data_(user_data) {
   }
 
+  CallbackInfo(CallbackInfo&& c) noexcept {
+    cb_ = c.cb_;
+    error_cb_ = c.error_cb_;
+    user_data_ = c.user_data_;
+    c.cb_ = nullptr;
+    c.error_cb_ = nullptr;
+    c.user_data_ = nullptr;
+  }
+
+  CallbackInfo& operator = (CallbackInfo&& c) noexcept {
+    if (this != &c) {
+      cb_ = c.cb_;
+      error_cb_ = c.error_cb_;
+      user_data_ = c.user_data_;
+      c.cb_ = nullptr;
+      c.error_cb_ = nullptr;
+      c.user_data_ = nullptr;
+    }
+    return *this;
+  }
+
   void Invoke(int complication_id,
               const std::string& provider_id,
               ComplicationType type, const std::unique_ptr<Bundle>& data) {
@@ -130,14 +152,14 @@ class WatchComplicationStub : public Complication {
     LOGI("update call!! done");
   }
 
-  int AddCallbackInfo(CallbackInfo* ci) {
+  int AddCallbackInfo(unique_ptr<CallbackInfo> ci) {
     for (auto& i : cb_list_) {
       if (i.get()->GetCallback() == ci->GetCallback()) {
         LOGI("already registered callback");
         return WATCHFACE_COMPLICATION_ERROR_INVALID_PARAMETER;
       }
     }
-    cb_list_.emplace_back(ci);
+    cb_list_.emplace_back(move(ci));
     return WATCHFACE_COMPLICATION_ERROR_NONE;
   }
 
@@ -184,25 +206,15 @@ extern "C" EXPORT_API int watchface_complication_add_updated_cb(
 
   auto sh = static_cast<SharedHandle<WatchComplicationStub>*>(handle);
   auto ptr = SharedHandle<WatchComplicationStub>::Share(sh);
-
-  CallbackInfo* ci = nullptr;
-  try {
-    ci = new CallbackInfo(cb, error_cb, user_data);
-    ret = ptr.get()->AddCallbackInfo(ci);
-    if (ret != WATCHFACE_COMPLICATION_ERROR_NONE) {
-      delete ci;
-      return ret;
-    }
-  } catch (const std::bad_alloc &ba) {
-    LOGE("WatchComplicationProviderStub::Exception bad_alloc");
-    if (ci != nullptr)
-      delete ci;
+  unique_ptr<CallbackInfo> ci = unique_ptr<CallbackInfo>(
+      new (std::nothrow) CallbackInfo(cb, error_cb, user_data));
+  if (ci.get() == nullptr) {
+    LOGE("Out of memory");
     return WATCHFACE_COMPLICATION_ERROR_OUT_OF_MEMORY;
-  } catch (Exception &ex) {
-    LOGE("%s %d", ex.what(), ex.GetErrorCode());
-    delete ci;
-    return ex.GetErrorCode();
   }
+  ret = ptr.get()->AddCallbackInfo(move(ci));
+  if (ret != WATCHFACE_COMPLICATION_ERROR_NONE)
+    return ret;
   return ret;
 }
 
index 248daa8..d9addbc 100644 (file)
@@ -60,6 +60,13 @@ class ReadyCallbackInfo {
     : cb_(cb), user_data_(user_data) {
   }
 
+  ReadyCallbackInfo(ReadyCallbackInfo&& b) noexcept {
+    cb_ = b.cb_;
+    user_data_ = b.user_data_;
+    b.cb_ = nullptr;
+    b.user_data_ = nullptr;
+  }
+
   void Invoke(void* stub, const std::string& editor_appid) {
     cb_(stub, editor_appid.c_str(), user_data_);
   }
@@ -79,6 +86,13 @@ class UpdateCallbackInfo {
     : cb_(cb), user_data_(user_data) {
   }
 
+  UpdateCallbackInfo(UpdateCallbackInfo&& b) noexcept {
+    cb_ = b.cb_;
+    user_data_ = b.user_data_;
+    b.cb_ = nullptr;
+    b.user_data_ = nullptr;
+  }
+
   void Invoke(const void* handle, int selected_idx,
       const watchface_editable_edit_state_e state) {
     LOGI("call update");
@@ -111,7 +125,7 @@ class EditablesContainerStub : public EditablesContainer {
     }
   }
 
-  int AddReadyCallbackInfo(ReadyCallbackInfo* ci) {
+  int AddReadyCallbackInfo(unique_ptr<ReadyCallbackInfo> ci) {
     for (auto& i : ready_cb_list_) {
       if (i.get()->GetCallback() == ci->GetCallback()) {
         LOGI("already registered callback");
@@ -119,7 +133,7 @@ class EditablesContainerStub : public EditablesContainer {
       }
     }
 
-    ready_cb_list_.emplace_back(ci);
+    ready_cb_list_.emplace_back(move(ci));
     return WATCHFACE_COMPLICATION_ERROR_NONE;
   }
 
@@ -137,8 +151,8 @@ class EditablesContainerStub : public EditablesContainer {
     update_cb_list_.clear();
   }
 
-  void AddUpdateCallbackInfo(UpdateCallbackInfo* ci) {
-    update_cb_list_.emplace_back(ci);
+  void AddUpdateCallbackInfo(unique_ptr<UpdateCallbackInfo> ci) {
+    update_cb_list_.emplace_back(move(ci));
   }
 
  private:
@@ -184,27 +198,22 @@ extern "C" EXPORT_API int watchface_editable_add_design_element(
     cur_data_idx >= static_cast<int>(
       g_list_length(list_handle->candidates_list)))
     return WATCHFACE_COMPLICATION_ERROR_INVALID_PARAMETER;
+
   EditablesContainerStub* ec = static_cast<EditablesContainerStub*>(handle);
-  bundle* data;
-  int str_len = 0;
-  int ret;
-  bundle_raw* str_raw = NULL;
   GList* iter = list_handle->candidates_list;
   std::list<std::shared_ptr<Bundle>> new_list;
 
   for (; iter; iter = iter->next) {
-    data = reinterpret_cast<bundle*>(iter->data);
-    if (data != NULL) {
-      ret = bundle_encode(data, &str_raw, &str_len);
-      if (ret == BUNDLE_ERROR_NONE && str_raw) {
-        new_list.emplace_back(
-          new Bundle(std::string(reinterpret_cast<char*>(str_raw))));
-        free(str_raw);
-        str_raw = NULL;
-      } else {
-        return WATCHFACE_COMPLICATION_ERROR_OUT_OF_MEMORY;
-      }
+    bundle* data = reinterpret_cast<bundle*>(iter->data);
+    if (data == nullptr)
+      continue;
+
+    Bundle* new_data = new Bundle(data);
+    if (new_data == nullptr) {
+      LOGE("Out of memory");
+      return WATCHFACE_COMPLICATION_ERROR_OUT_OF_MEMORY;
     }
+    new_list.emplace_back(move(new_data));
   }
 
   if (ec->IsExist(edit_id)) {
@@ -223,7 +232,7 @@ extern "C" EXPORT_API int watchface_editable_add_design_element(
       de.get()->SetHighlight(std::move(hi_ptr));
     }
     de.get()->SetLabel(std::string(editable_name));
-    ret = de.get()->SetCandidates(new_list);
+    int ret = de.get()->SetCandidates(new_list);
     if (ret != WATCHFACE_COMPLICATION_ERROR_NONE)
       return ret;
 
@@ -305,21 +314,14 @@ extern "C" EXPORT_API int watchface_editable_request_edit(
   }
 
   EditablesContainerStub* ec = static_cast<EditablesContainerStub*>(handle);
-  UpdateCallbackInfo* ci = nullptr;
-  try {
-    ci = new UpdateCallbackInfo(cb, user_data);
-    ec->ClearUpdateCallbackInfo();
-    ec->AddUpdateCallbackInfo(ci);
-  } catch (const std::bad_alloc &ba) {
-    LOGE("UpdateCallbackInfo::Exception bad_alloc");
-    if (ci != nullptr)
-      delete ci;
+  unique_ptr<UpdateCallbackInfo> ci = unique_ptr<UpdateCallbackInfo>(
+      new (std::nothrow) UpdateCallbackInfo(cb, user_data));
+  if (ci.get() == nullptr) {
+    LOGE("Out of memory");
     return WATCHFACE_COMPLICATION_ERROR_OUT_OF_MEMORY;
-  } catch (watchface_complication::Exception &ex) {
-    LOGE("%s %d", ex.what(), ex.GetErrorCode());;
-    delete ci;
-    return ex.GetErrorCode();
   }
+  ec->ClearUpdateCallbackInfo();
+  ec->AddUpdateCallbackInfo(move(ci));
   ret = ec->RequestEdit();
 
   return ret;
@@ -330,38 +332,25 @@ extern "C" EXPORT_API int watchface_editable_add_edit_ready_cb(
   if (!watchface_complication::util::CheckWatchFeatureEnabled())
     return WATCHFACE_COMPLICATION_ERROR_NOT_SUPPORTED;
 
-  if (cb == NULL)
+  if (cb == nullptr)
     return WATCHFACE_COMPLICATION_ERROR_INVALID_PARAMETER;
 
-  try {
-    if (__container == NULL)
-      __container = new EditablesContainerStub();
-  } catch  (const std::bad_alloc &ba) {
-    LOGE("EditablesContainerStub::Exception bad_alloc");
+  if (__container == nullptr)
+      __container = new (std::nothrow) EditablesContainerStub();
+  if (__container == nullptr) {
+    LOGE("Out of memory");
     return WATCHFACE_COMPLICATION_ERROR_OUT_OF_MEMORY;
-  } catch (watchface_complication::Exception &ex) {
-    LOGE("%s %d", ex.what(), ex.GetErrorCode());;
-    return ex.GetErrorCode();
   }
 
-  ReadyCallbackInfo* ci = nullptr;
-  try {
-    ci = new ReadyCallbackInfo(cb, user_data);
-    int ret = __container->AddReadyCallbackInfo(ci);
-    if (ret != WATCHFACE_COMPLICATION_ERROR_NONE) {
-      delete ci;
-      return ret;
-    }
-  } catch (const std::bad_alloc &ba) {
-    LOGE("UpdateCallbackInfo::Exception bad_alloc");
-    if (ci != nullptr)
-      delete ci;
+  unique_ptr<ReadyCallbackInfo> ci = unique_ptr<ReadyCallbackInfo>(
+      new (std::nothrow) ReadyCallbackInfo(cb, user_data));
+  if (ci.get() == nullptr) {
+    LOGE("Out of memory");
     return WATCHFACE_COMPLICATION_ERROR_OUT_OF_MEMORY;
-  } catch (watchface_complication::Exception &ex) {
-    LOGE("%s %d", ex.what(), ex.GetErrorCode());
-    delete ci;
-    return ex.GetErrorCode();
   }
+  int ret = __container->AddReadyCallbackInfo(move(ci));
+  if (ret != WATCHFACE_COMPLICATION_ERROR_NONE)
+    return ret;
 
   return WATCHFACE_COMPLICATION_ERROR_NONE;
 }
@@ -491,17 +480,18 @@ extern "C" EXPORT_API int watchface_editable_load_current_data(
     return WATCHFACE_COMPLICATION_ERROR_INVALID_PARAMETER;
 
   std::unique_ptr<Bundle> setting_data;
-
   try {
     setting_data = EditablesManager::GetInst().LoadSetting(editable_id);
   } catch (watchface_complication::Exception &ex) {
     LOGE("%s %d", ex.what(), ex.GetErrorCode());
     return ex.GetErrorCode();
   }
+
   if (setting_data != nullptr) {
-    *selected_data = bundle_dup(setting_data.get()->GetHandle());
-    if (*selected_data == NULL)
+    bundle* dup_bundle = bundle_dup(setting_data.get()->GetHandle());
+    if (dup_bundle == nullptr)
       return WATCHFACE_COMPLICATION_ERROR_OUT_OF_MEMORY;
+    *selected_data = dup_bundle;
   } else {
     return WATCHFACE_COMPLICATION_ERROR_NO_DATA;
   }
index 76f8372..1fd9ce9 100644 (file)
@@ -32,6 +32,7 @@
 
 #define LOG_TAG "WATCHFACE_COMPLICATION"
 
+using namespace std;
 using namespace tizen_base;
 namespace watchface_complication {
 
@@ -106,46 +107,30 @@ void EditablesEditor::Impl::OnSignal(GDBusConnection* connection,
       return;
     }
 
-    bundle* data = bundle_decode(raw, strlen(reinterpret_cast<char*>(raw)));
-    const char** str_arr = NULL;
-    int len = 0;
-
-    if (data == NULL) {
-      LOGE("bundle decode failed %d", get_last_result());
-      return;
-    }
-
-    str_arr = bundle_get_str_array(data, "EDITABLE_LIST", &len);
-    for (int i = 0; i < len; i++) {
-      try {
-        e_list.emplace_back(std::unique_ptr<IEditable>(
-             new ReceivedEditable(std::string(str_arr[i]))));
-      } catch (const std::bad_alloc &ba) {
-        LOGE("ReceivedEditable::Exception bad_alloc");
-        return;
-      } catch (Exception &ex) {
-        LOGE("%s %d", ex.what(), ex.GetErrorCode());
+    Bundle data(reinterpret_cast<char*>(raw));
+    vector<string> raw_arr = data.GetStringArray("EDITABLE_LIST");
+    for (auto& i : raw_arr) {
+      std::unique_ptr<IEditable> received = std::unique_ptr<IEditable>(
+          new ReceivedEditable(i));
+      if (received.get() == nullptr) {
+        LOGE("Out of memory");
         return;
       }
+      e_list.emplace_back(move(received));
     }
     parent_->OnRequestEdit(std::string(appid), std::move(e_list));
-    bundle_free(data);
   } else if (signal_name.compare(
       util::GetCmdStr(util::CmdType::SetupReply)) == 0) {
     int edit_id;
     char* raw_str;
-
     g_variant_get(parameters, "(i&s)", &edit_id, &raw_str);
-    try {
-      parent_->OnSetupReply(sender_appid, edit_id,
-              std::unique_ptr<Bundle>(new Bundle(std::string(raw_str))));
-    } catch (const std::bad_alloc &ba) {
-      LOGE("Bundle::Exception bad_alloc");
-      return;
-    } catch (Exception &ex) {
-      LOGE("%s %d", ex.what(), ex.GetErrorCode());
+    unique_ptr<Bundle> reply_data =
+        unique_ptr<Bundle>(new (std::nothrow) Bundle(std::string(raw_str)));
+    if (reply_data.get() == nullptr) {
+      LOGE("Out of memory");
       return;
     }
+    parent_->OnSetupReply(sender_appid, edit_id, move(reply_data));
   }
 }
 
@@ -197,19 +182,16 @@ int EditablesEditor::EditPreview(IEditable& ed, int cur_data_idx) {
 }
 
 int EditablesEditor::EditComplete() {
-  bool emit_result;
-  int ret;
-
   if (impl_->edit_appid_.empty()) {
     LOGE("Editing is not ready");
     return WATCHFACE_COMPLICATION_ERROR_EDIT_NOT_READY;
   }
 
-  ret = impl_->CheckPrivilege();
+  int ret = impl_->CheckPrivilege();
   if (ret != WATCHFACE_COMPLICATION_ERROR_NONE)
     return ret;
 
-  emit_result = impl_->gdbus_.get()->EmitSignal(
+  bool emit_result = impl_->gdbus_.get()->EmitSignal(
     IGDBus::Editable,
     impl_->edit_appid_.c_str(),
     impl_->edit_appid_.c_str(),
index a612bb3..a784ba7 100644 (file)
@@ -33,6 +33,7 @@
 
 #define LOG_TAG "WATCHFACE_COMPLICATION"
 
+using namespace std;
 using namespace tizen_base;
 using namespace watchface_complication;
 
@@ -42,6 +43,13 @@ class CallbackInfo {
     : cb_(cb), user_data_(user_data) {
   }
 
+  CallbackInfo(CallbackInfo&& b) noexcept {
+    cb_ = b.cb_;
+    user_data_ = b.user_data_;
+    b.cb_ = nullptr;
+    b.user_data_ = nullptr;
+  }
+
   void Invoke(const std::string& appid, editable_list_h list_h) {
     cb_(appid.c_str(), list_h, user_data_);
   }
@@ -63,6 +71,15 @@ class SetupCallbackInfo {
                            cb_(cb), user_data_(user_data) {
   }
 
+  SetupCallbackInfo(SetupCallbackInfo&& b) noexcept {
+    setup_appid_ = b.setup_appid_;
+    editable_id_ = b.editable_id_;
+    cb_ = b.cb_;
+    user_data_ = b.user_data_;
+    b.cb_ = nullptr;
+    b.user_data_ = nullptr;
+  }
+
   void Invoke(bundle* new_context) {
     cb_(editable_id_, new_context, user_data_);
   }
@@ -117,20 +134,20 @@ class EditablesEditorStub : public EditablesEditor {
     LOGE("Not exist callback info");
   }
 
-  void AddSetupCallbackInfo(SetupCallbackInfo* ci) {
-    setup_cb_list_.emplace_back(ci);
+  void AddSetupCallbackInfo(unique_ptr<SetupCallbackInfo> ci) {
+    setup_cb_list_.emplace_back(move(ci));
   }
 
-  void RemoveSetupCallbackInfo(SetupCallbackInfo* ci) {
+  void RemoveSetupCallbackInfo(unique_ptr<SetupCallbackInfo> ci) {
     for (auto& i : setup_cb_list_) {
-      if (i.get() == ci) {
+      if (i.get() == ci.get()) {
         setup_cb_list_.remove(i);
         break;
       }
     }
   }
 
-  int AddCallbackInfo(CallbackInfo* ci) {
+  int AddCallbackInfo(unique_ptr<CallbackInfo> ci) {
     for (auto& i : cb_list_) {
       if (i.get()->GetCallback() == ci->GetCallback()) {
         LOGI("already registered callback");
@@ -138,7 +155,7 @@ class EditablesEditorStub : public EditablesEditor {
       }
     }
 
-    cb_list_.emplace_back(ci);
+    cb_list_.emplace_back(move(ci));
     return WATCHFACE_COMPLICATION_ERROR_NONE;
   }
 
@@ -165,8 +182,6 @@ class EditablesEditorStub : public EditablesEditor {
 static std::unique_ptr<EditablesEditorStub> __stub = nullptr;
 extern "C" EXPORT_API int watchface_editor_add_request_edit_cb(
     watchface_editor_request_edit_cb cb, void* user_data) {
-  int ret;
-
   if (!watchface_complication::util::CheckWatchFeatureEnabled())
     return WATCHFACE_COMPLICATION_ERROR_NOT_SUPPORTED;
 
@@ -176,35 +191,21 @@ extern "C" EXPORT_API int watchface_editor_add_request_edit_cb(
   }
 
   if (__stub == nullptr) {
-    try {
-      __stub = std::unique_ptr<EditablesEditorStub>(new EditablesEditorStub());
-    } catch (const std::bad_alloc &ba) {
-       LOGE("EditablesEditorStub::Exception bad_alloc");
-       return WATCHFACE_COMPLICATION_ERROR_OUT_OF_MEMORY;
-    } catch (Exception &ex) {
-      LOGE("%s %d", ex.what(), ex.GetErrorCode());
-      return ex.GetErrorCode();
+    __stub = std::unique_ptr<EditablesEditorStub>(
+        new (std::nothrow) EditablesEditorStub());
+    if (__stub == nullptr) {
+      LOGE("Out of memory");
+      return WATCHFACE_COMPLICATION_ERROR_OUT_OF_MEMORY;
     }
   }
 
-  CallbackInfo* ci = nullptr;
-  try {
-    ci = new CallbackInfo(cb, user_data);
-    ret = __stub->AddCallbackInfo(ci);
-    if (ret != WATCHFACE_COMPLICATION_ERROR_NONE)
-      delete ci;
-  } catch (const std::bad_alloc &ba) {
-    LOGE("CallbackInfo::Exception bad_alloc");
-    if (ci != nullptr)
-      delete ci;
+  unique_ptr<CallbackInfo> ci = unique_ptr<CallbackInfo>(
+      new (std::nothrow) CallbackInfo(cb, user_data));
+  if (ci.get() == nullptr) {
+    LOGE("Out of memory");
     return WATCHFACE_COMPLICATION_ERROR_OUT_OF_MEMORY;
-  } catch (Exception &ex) {
-    LOGE("%s %d", ex.what(), ex.GetErrorCode());
-    delete ci;
-    return ex.GetErrorCode();
   }
-
-  return ret;
+  return __stub->AddCallbackInfo(move(ci));
 }
 
 extern "C" EXPORT_API int watchface_editor_remove_request_edit_cb(
@@ -241,40 +242,36 @@ extern "C" EXPORT_API int watchface_editor_edit_preview(
   return __stub->EditPreview(*ed, cur_data_idx);
 }
 
+static int __init_stub() {
+  if (__stub != nullptr)
+    return WATCHFACE_COMPLICATION_ERROR_NONE;
+  try {
+    __stub = std::unique_ptr<EditablesEditorStub>(new EditablesEditorStub());
+  } catch (const std::bad_alloc &ba) {
+    LOGE("EditablesEditorStub::Exception bad_alloc");
+    return WATCHFACE_COMPLICATION_ERROR_OUT_OF_MEMORY;
+  } catch (Exception &ex) {
+    LOGE("%s %d", ex.what(), ex.GetErrorCode());
+    return ex.GetErrorCode();
+  }
+  return WATCHFACE_COMPLICATION_ERROR_NONE;
+}
+
 extern "C" EXPORT_API int watchface_editor_edit_complete(void) {
   if (!watchface_complication::util::CheckWatchFeatureEnabled())
     return WATCHFACE_COMPLICATION_ERROR_NOT_SUPPORTED;
-
-  if (__stub == nullptr) {
-    try {
-      __stub = std::unique_ptr<EditablesEditorStub>(new EditablesEditorStub());
-    } catch (const std::bad_alloc &ba) {
-      LOGE("EditablesEditorStub::Exception bad_alloc");
-      return WATCHFACE_COMPLICATION_ERROR_OUT_OF_MEMORY;
-    } catch (Exception &ex) {
-      LOGE("%s %d", ex.what(), ex.GetErrorCode());
-      return ex.GetErrorCode();
-    }
-  }
-
+  int ret = __init_stub();
+  if (ret != WATCHFACE_COMPLICATION_ERROR_NONE)
+    return ret;
   return __stub->EditComplete();
 }
 
 extern "C" EXPORT_API int watchface_editor_edit_cancel(void) {
   if (!watchface_complication::util::CheckWatchFeatureEnabled())
     return WATCHFACE_COMPLICATION_ERROR_NOT_SUPPORTED;
-
-  if (__stub == nullptr) {
-    try {
-      __stub = std::unique_ptr<EditablesEditorStub>(new EditablesEditorStub());
-    } catch (const std::bad_alloc &ba) {
-      LOGE("EditablesEditorStub::Exception bad_alloc");
-      return WATCHFACE_COMPLICATION_ERROR_OUT_OF_MEMORY;
-    } catch (Exception &ex) {
-      LOGE("%s %d", ex.what(), ex.GetErrorCode());
-      return ex.GetErrorCode();
-    }
-  }
+  int ret = __init_stub();
+  if (ret != WATCHFACE_COMPLICATION_ERROR_NONE)
+    return ret;
 
   return __stub->EditCancel();
 }
@@ -289,20 +286,10 @@ extern "C" EXPORT_API int watchface_editor_notify_edit_ready(
     return WATCHFACE_COMPLICATION_ERROR_INVALID_PARAMETER;
   }
 
-  if (__stub == nullptr) {
-    try {
-      __stub = std::unique_ptr<EditablesEditorStub>(new EditablesEditorStub());
-    } catch (const std::bad_alloc &ba) {
-      LOGE("EditablesEditorStub::Exception bad_alloc");
-      return WATCHFACE_COMPLICATION_ERROR_OUT_OF_MEMORY;
-    } catch (Exception &ex) {
-      LOGE("%s %d", ex.what(), ex.GetErrorCode());
-      return ex.GetErrorCode();
-    }
-  } else {
-    __stub->ClearEditableList();
-  }
-
+  int ret = __init_stub();
+  if (ret != WATCHFACE_COMPLICATION_ERROR_NONE)
+    return ret;
+  __stub->ClearEditableList();
   return __stub->NotifyEditReady(std::string(appid));
 }
 
@@ -452,16 +439,12 @@ extern "C" EXPORT_API int watchface_editor_set_context(
   int re = WATCHFACE_COMPLICATION_ERROR_NONE;
   IEditable* ed = static_cast<IEditable*>(handle);
   if (new_context) {
-    try {
-      std::unique_ptr<Bundle> b(new Bundle(new_context));
-      re = ed->SetContext(std::move(b));
-    } catch (const std::bad_alloc &ba) {
+    std::unique_ptr<Bundle> b(new (std::nothrow) Bundle(new_context));
+    if (b.get() == nullptr) {
       LOGE("Out of memory");
       return WATCHFACE_COMPLICATION_ERROR_OUT_OF_MEMORY;
-    } catch (Exception &ex) {
-      LOGE("%s %d", ex.what(), ex.GetErrorCode());
-      return ex.GetErrorCode();
     }
+    re = ed->SetContext(std::move(b));
   } else {
     re = ed->SetContext(std::unique_ptr<Bundle>{});
   }
@@ -564,23 +547,14 @@ extern "C" EXPORT_API int watchface_editor_launch_setup_app(
   }
   app_control_destroy(service);
 
-  SetupCallbackInfo* ci = nullptr;
-  try {
-    if (__stub == nullptr)
-      __stub = std::unique_ptr<EditablesEditorStub>(new EditablesEditorStub());
+  unique_ptr<SetupCallbackInfo> ci = unique_ptr<SetupCallbackInfo>(
+      new (std::nothrow) SetupCallbackInfo(
+        appid, ed->GetEditableId(), cb, user_data));
 
-    ci = new SetupCallbackInfo(appid, ed->GetEditableId(), cb, user_data);
-    __stub->AddSetupCallbackInfo(ci);
-  } catch (const std::bad_alloc &ba) {
-    LOGE("Out of memory");
-    if (ci != nullptr)
-      delete ci;
-    return WATCHFACE_COMPLICATION_ERROR_OUT_OF_MEMORY;
-  } catch (Exception &ex) {
-    LOGE("%s %d", ex.what(), ex.GetErrorCode());
-    delete ci;
-    return ex.GetErrorCode();
-  }
+  ret = __init_stub();
+  if (ret != WATCHFACE_COMPLICATION_ERROR_NONE)
+    return ret;
+  __stub->AddSetupCallbackInfo(move(ci));
 
   return WATCHFACE_COMPLICATION_ERROR_NONE;
 }