src.ctf.lttng-live: use C++ value wrappers for query parameters
[babeltrace.git] / src / plugins / ctf / lttng-live / lttng-live.cpp
index cdf324c21298c2268a9d54753c8e3c424583b2e2..9f62305ef2babfc543dad924fb8cba58796462b2 100644 (file)
@@ -156,38 +156,20 @@ lttng_live_session::~lttng_live_session()
     }
 }
 
-static void lttng_live_msg_iter_destroy(struct lttng_live_msg_iter *lttng_live_msg_iter)
+lttng_live_msg_iter::~lttng_live_msg_iter()
 {
-    if (!lttng_live_msg_iter) {
-        goto end;
-    }
-
-    if (lttng_live_msg_iter->viewer_connection) {
-        delete lttng_live_msg_iter->viewer_connection;
-    }
-    BT_ASSERT(lttng_live_msg_iter->lttng_live_comp);
-    BT_ASSERT(lttng_live_msg_iter->lttng_live_comp->has_msg_iter);
+    BT_ASSERT(this->lttng_live_comp);
+    BT_ASSERT(this->lttng_live_comp->has_msg_iter);
 
     /* All stream iterators must be destroyed at this point. */
-    BT_ASSERT(lttng_live_msg_iter->active_stream_iter == 0);
-    lttng_live_msg_iter->lttng_live_comp->has_msg_iter = false;
-
-    delete lttng_live_msg_iter;
-
-end:
-    return;
+    BT_ASSERT(this->active_stream_iter == 0);
+    this->lttng_live_comp->has_msg_iter = false;
 }
 
 void lttng_live_msg_iter_finalize(bt_self_message_iterator *self_msg_iter)
 {
-    struct lttng_live_msg_iter *lttng_live_msg_iter;
-
-    BT_ASSERT(self_msg_iter);
-
-    lttng_live_msg_iter =
-        (struct lttng_live_msg_iter *) bt_self_message_iterator_get_data(self_msg_iter);
-    BT_ASSERT(lttng_live_msg_iter);
-    lttng_live_msg_iter_destroy(lttng_live_msg_iter);
+    lttng_live_msg_iter::UP {
+        static_cast<lttng_live_msg_iter *>(bt_self_message_iterator_get_data(self_msg_iter))};
 }
 
 static enum lttng_live_iterator_status
@@ -1581,15 +1563,15 @@ end:
     }
 }
 
-static struct lttng_live_msg_iter *
+static lttng_live_msg_iter::UP
 lttng_live_msg_iter_create(struct lttng_live_component *lttng_live_comp,
                            bt_self_message_iterator *self_msg_it)
 {
-    lttng_live_msg_iter *msg_iter = new lttng_live_msg_iter {lttng_live_comp->logger};
+    auto msg_iter = bt2s::make_unique<struct lttng_live_msg_iter>(lttng_live_comp->logger);
+
     msg_iter->self_comp = lttng_live_comp->self_comp;
     msg_iter->lttng_live_comp = lttng_live_comp;
     msg_iter->self_msg_iter = self_msg_it;
-
     msg_iter->active_stream_iter = 0;
     msg_iter->last_msg_ts_ns = INT64_MIN;
     msg_iter->was_interrupted = false;
@@ -1602,9 +1584,7 @@ lttng_live_msg_iter_init(bt_self_message_iterator *self_msg_it,
                          bt_self_message_iterator_configuration *, bt_self_component_port_output *)
 {
     try {
-        bt_message_iterator_class_initialize_method_status status;
         struct lttng_live_component *lttng_live;
-        struct lttng_live_msg_iter *lttng_live_msg_iter;
         enum lttng_live_viewer_status viewer_status;
         bt_self_component *self_comp = bt_self_message_iterator_borrow_component(self_msg_it);
 
@@ -1614,17 +1594,16 @@ lttng_live_msg_iter_init(bt_self_message_iterator *self_msg_it,
         BT_ASSERT(!lttng_live->has_msg_iter);
         lttng_live->has_msg_iter = true;
 
-        lttng_live_msg_iter = lttng_live_msg_iter_create(lttng_live, self_msg_it);
+        auto lttng_live_msg_iter = lttng_live_msg_iter_create(lttng_live, self_msg_it);
         if (!lttng_live_msg_iter) {
             BT_CPPLOGE_APPEND_CAUSE_SPEC(lttng_live->logger,
                                          "Failed to create lttng_live_msg_iter");
-            status = BT_MESSAGE_ITERATOR_CLASS_INITIALIZE_METHOD_STATUS_MEMORY_ERROR;
-            goto error;
+            return BT_MESSAGE_ITERATOR_CLASS_INITIALIZE_METHOD_STATUS_MEMORY_ERROR;
         }
 
         viewer_status = live_viewer_connection_create(
-            lttng_live->params.url.c_str(), false, lttng_live_msg_iter, lttng_live_msg_iter->logger,
-            &lttng_live_msg_iter->viewer_connection);
+            lttng_live->params.url.c_str(), false, lttng_live_msg_iter.get(),
+            lttng_live_msg_iter->logger, lttng_live_msg_iter->viewer_connection);
         if (viewer_status != LTTNG_LIVE_VIEWER_STATUS_OK) {
             if (viewer_status == LTTNG_LIVE_VIEWER_STATUS_ERROR) {
                 BT_CPPLOGE_APPEND_CAUSE_SPEC(lttng_live_msg_iter->logger,
@@ -1638,11 +1617,10 @@ lttng_live_msg_iter_init(bt_self_message_iterator *self_msg_it,
                                              "Interrupted while creating viewer connection");
             }
 
-            status = BT_MESSAGE_ITERATOR_CLASS_INITIALIZE_METHOD_STATUS_ERROR;
-            goto error;
+            return BT_MESSAGE_ITERATOR_CLASS_INITIALIZE_METHOD_STATUS_ERROR;
         }
 
-        viewer_status = lttng_live_create_viewer_session(lttng_live_msg_iter);
+        viewer_status = lttng_live_create_viewer_session(lttng_live_msg_iter.get());
         if (viewer_status != LTTNG_LIVE_VIEWER_STATUS_OK) {
             if (viewer_status == LTTNG_LIVE_VIEWER_STATUS_ERROR) {
                 BT_CPPLOGE_APPEND_CAUSE_SPEC(lttng_live_msg_iter->logger,
@@ -1656,8 +1634,7 @@ lttng_live_msg_iter_init(bt_self_message_iterator *self_msg_it,
                                              "Interrupted when creating viewer session");
             }
 
-            status = BT_MESSAGE_ITERATOR_CLASS_INITIALIZE_METHOD_STATUS_ERROR;
-            goto error;
+            return BT_MESSAGE_ITERATOR_CLASS_INITIALIZE_METHOD_STATUS_ERROR;
         }
 
         if (lttng_live_msg_iter->sessions.empty()) {
@@ -1678,8 +1655,7 @@ lttng_live_msg_iter_init(bt_self_message_iterator *self_msg_it,
                     "component parameter: url =\"{}\"",
                     SESS_NOT_FOUND_ACTION_PARAM, SESS_NOT_FOUND_ACTION_FAIL_STR,
                     lttng_live->params.url);
-                status = BT_MESSAGE_ITERATOR_CLASS_INITIALIZE_METHOD_STATUS_ERROR;
-                goto error;
+                return BT_MESSAGE_ITERATOR_CLASS_INITIALIZE_METHOD_STATUS_ERROR;
             case SESSION_NOT_FOUND_ACTION_END:
                 BT_CPPLOGI_SPEC(lttng_live_msg_iter->logger,
                                 "Unable to connect to the requested live viewer session. "
@@ -1693,14 +1669,8 @@ lttng_live_msg_iter_init(bt_self_message_iterator *self_msg_it,
             }
         }
 
-        bt_self_message_iterator_set_data(self_msg_it, lttng_live_msg_iter);
-        status = BT_MESSAGE_ITERATOR_CLASS_INITIALIZE_METHOD_STATUS_OK;
-        goto end;
-
-error:
-        lttng_live_msg_iter_destroy(lttng_live_msg_iter);
-end:
-        return status;
+        bt_self_message_iterator_set_data(self_msg_it, lttng_live_msg_iter.release());
+        return BT_MESSAGE_ITERATOR_CLASS_INITIALIZE_METHOD_STATUS_OK;
     } catch (const std::bad_alloc&) {
         return BT_MESSAGE_ITERATOR_CLASS_INITIALIZE_METHOD_STATUS_MEMORY_ERROR;
     } catch (const bt2::Error&) {
@@ -1714,18 +1684,18 @@ static struct bt_param_validation_map_value_entry_descr list_sessions_params[] =
     BT_PARAM_VALIDATION_MAP_VALUE_ENTRY_END};
 
 static bt_component_class_query_method_status
-lttng_live_query_list_sessions(const bt_value *params, const bt_value **result,
+lttng_live_query_list_sessions(const bt2::ConstMapValue params, const bt_value **result,
                                const bt2c::Logger& logger)
 {
     bt_component_class_query_method_status status;
-    const bt_value *url_value = NULL;
     const char *url;
-    struct live_viewer_connection *viewer_connection = NULL;
+    live_viewer_connection::UP viewer_connection;
     enum lttng_live_viewer_status viewer_status;
     enum bt_param_validation_status validation_status;
     gchar *validate_error = NULL;
 
-    validation_status = bt_param_validation_validate(params, list_sessions_params, &validate_error);
+    validation_status =
+        bt_param_validation_validate(params.libObjPtr(), list_sessions_params, &validate_error);
     if (validation_status == BT_PARAM_VALIDATION_STATUS_MEMORY_ERROR) {
         status = BT_COMPONENT_CLASS_QUERY_METHOD_STATUS_MEMORY_ERROR;
         goto error;
@@ -1735,10 +1705,9 @@ lttng_live_query_list_sessions(const bt_value *params, const bt_value **result,
         goto error;
     }
 
-    url_value = bt_value_map_borrow_entry_value_const(params, URL_PARAM);
-    url = bt_value_string_get(url_value);
+    url = params[URL_PARAM]->asString().value();
 
-    viewer_status = live_viewer_connection_create(url, true, NULL, logger, &viewer_connection);
+    viewer_status = live_viewer_connection_create(url, true, NULL, logger, viewer_connection);
     if (viewer_status != LTTNG_LIVE_VIEWER_STATUS_OK) {
         if (viewer_status == LTTNG_LIVE_VIEWER_STATUS_ERROR) {
             BT_CPPLOGE_APPEND_CAUSE_SPEC(logger, "Failed to create viewer connection");
@@ -1751,7 +1720,7 @@ lttng_live_query_list_sessions(const bt_value *params, const bt_value **result,
         goto error;
     }
 
-    status = live_viewer_connection_list_sessions(viewer_connection, result);
+    status = live_viewer_connection_list_sessions(viewer_connection.get(), result);
     if (status != BT_COMPONENT_CLASS_QUERY_METHOD_STATUS_OK) {
         BT_CPPLOGE_APPEND_CAUSE_SPEC(logger, "Failed to list viewer sessions");
         goto error;
@@ -1767,55 +1736,53 @@ error:
     }
 
 end:
-    delete viewer_connection;
-
     g_free(validate_error);
 
     return status;
 }
 
 static bt_component_class_query_method_status
-lttng_live_query_support_info(const bt_value *params, const bt_value **result,
+lttng_live_query_support_info(const bt2::ConstMapValue params, const bt_value **result,
                               const bt2c::Logger& logger)
 {
     bt_component_class_query_method_status status = BT_COMPONENT_CLASS_QUERY_METHOD_STATUS_OK;
-    const bt_value *input_type_value;
-    const bt_value *input_value;
+    bt2::OptionalBorrowedObject<bt2::ConstValue> inputValue;
     double weight = 0;
     struct bt_common_lttng_live_url_parts parts = {};
+    bt_common_lttng_live_url_parts_deleter partsDeleter {parts};
 
     /* Used by the logging macros */
     __attribute__((unused)) bt_self_component *self_comp = NULL;
 
     *result = NULL;
-    input_type_value = bt_value_map_borrow_entry_value_const(params, "type");
-    if (!input_type_value) {
+    const auto typeValue = params["type"];
+    if (!typeValue) {
         BT_CPPLOGE_APPEND_CAUSE_SPEC(logger, "Missing expected `type` parameter.");
         goto error;
     }
 
-    if (!bt_value_is_string(input_type_value)) {
+    if (!typeValue->isString()) {
         BT_CPPLOGE_APPEND_CAUSE_SPEC(logger, "`type` parameter is not a string value.");
         goto error;
     }
 
-    if (strcmp(bt_value_string_get(input_type_value), "string") != 0) {
+    if (strcmp(typeValue->asString().value(), "string") != 0) {
         /* We don't handle file system paths */
         goto create_result;
     }
 
-    input_value = bt_value_map_borrow_entry_value_const(params, "input");
-    if (!input_value) {
+    inputValue = params["input"];
+    if (!inputValue) {
         BT_CPPLOGE_APPEND_CAUSE_SPEC(logger, "Missing expected `input` parameter.");
         goto error;
     }
 
-    if (!bt_value_is_string(input_value)) {
+    if (!inputValue->isString()) {
         BT_CPPLOGE_APPEND_CAUSE_SPEC(logger, "`input` parameter is not a string value.");
         goto error;
     }
 
-    parts = bt_common_parse_lttng_live_url(bt_value_string_get(input_value), NULL, 0);
+    parts = bt_common_parse_lttng_live_url(inputValue->asString().value(), NULL, 0);
     if (parts.session_name) {
         /*
          * Looks pretty much like an LTTng live URL: we got the
@@ -1841,7 +1808,6 @@ error:
     BT_ASSERT(!*result);
 
 end:
-    bt_common_destroy_lttng_live_url_parts(&parts);
     return status;
 }
 
@@ -1856,11 +1822,12 @@ bt_component_class_query_method_status lttng_live_query(bt_self_component_class_
         bt2c::Logger logger {bt2::SelfComponentClass {comp_class},
                              bt2::PrivateQueryExecutor {priv_query_exec},
                              "PLUGIN/SRC.CTF.LTTNG-LIVE/QUERY"};
+        const bt2::ConstMapValue paramsObj(params);
 
         if (strcmp(object, "sessions") == 0) {
-            status = lttng_live_query_list_sessions(params, result, logger);
+            status = lttng_live_query_list_sessions(paramsObj, result, logger);
         } else if (strcmp(object, "babeltrace.support-info") == 0) {
-            status = lttng_live_query_support_info(params, result, logger);
+            status = lttng_live_query_support_info(paramsObj, result, logger);
         } else {
             BT_CPPLOGI_SPEC(logger, "Unknown query object `{}`", object);
             status = BT_COMPONENT_CLASS_QUERY_METHOD_STATUS_UNKNOWN_OBJECT;
This page took 0.027452 seconds and 4 git commands to generate.