Add action_clone() 24/325124/2
authorSangyoon Jang <jeremy.jang@samsung.com>
Wed, 4 Jun 2025 00:33:48 +0000 (09:33 +0900)
committerSangyoon Jang <jeremy.jang@samsung.com>
Thu, 5 Jun 2025 01:46:06 +0000 (10:46 +0900)
action_clone() should be called to use action handle outside of foreach
callback.

Change-Id: I07f6158824017b8df3de282676885d0e3bda5ec7
Signed-off-by: Sangyoon Jang <jeremy.jang@samsung.com>
src/api/action.h
src/api/api_stub.cc

index 30b55edf585062ed84f46d82519e39ef6e5840fa..ec7ebefad45313ebb9ffb0fc40e5a21347fc7666 100644 (file)
@@ -86,11 +86,13 @@ int action_client_get_action(action_client_h client, const char *name, action_h
 
 /**
  * @brief Retrieves all actions.
+ * @remarks You must clone the action handle using action_clone() if you want to use it after returning from the callback.
  * @param[in] client The action client handle.
  * @param[in] cb The callback function to get each action.
  * @param[in] user_data The user data to be passed to the callback function.
  * @retval #ACTION_ERROR_NONE Successful
  * @retval #ACTION_ERROR_INVALID_PARAMETER Invalid parameter
+ * @see action_clone()
  */
 int action_client_foreach_action(action_client_h client, action_foreach_action_cb cb, void *user_data);
 
@@ -107,6 +109,17 @@ int action_client_foreach_action(action_client_h client, action_foreach_action_c
  */
 int action_client_execute(action_client_h client, const char *param, action_result_cb cb, void *user_data, int *execution_id);
 
+/**
+ * @brief Clones the action handle.
+ * @param[in] action The action handle.
+ * @param[out] clone The cloned action handle.
+ * @retval #ACTION_ERROR_NONE Successful
+ * @retval #ACTION_ERROR_OUT_OF_MEMORY Out of memory
+ * @retval #ACTION_ERROR_INVALID_PARAMETER Invalid parameter
+ * @see action_destroy()
+ */
+int action_clone(action_h action, action_h *clone);
+
 /**
  * @brief Gets the name of the action.
  * @remarks If you no longer use @name, you must release it using free().
index 72cf2d409b685d7acaa2e7efa22daf688008de38..0ca7547f7efe2e493d0c4c33433287051144cb29 100644 (file)
@@ -141,6 +141,22 @@ API int action_client_execute(action_client_h client, const char* param,
   return ACTION_ERROR_NONE;
 }
 
+API int action_clone(action_h action, action_h* clone) {
+  if (action == nullptr || clone == nullptr) {
+    LOG(ERROR) << "Invalid Parameter";
+    return ACTION_ERROR_INVALID_PARAMETER;
+  }
+
+  auto src = static_cast<common::ActionSchema*>(action);
+  *clone = new (std::nothrow) common::ActionSchema(src->GetJsonString());
+  if (*clone == nullptr) {
+    LOG(ERROR) << "Out-of-memory";
+    return ACTION_ERROR_OUT_OF_MEMORY;
+  }
+
+  return ACTION_ERROR_NONE;
+}
+
 API int action_get_name(action_h action, const char** name) {
   if (action == nullptr || name == nullptr) {
     LOG(WARNING) << "Invalid Parameter";