This is an automated email from the ASF dual-hosted git repository.
xyz pushed a commit to branch main
in repository https://gitbox.apache.org/repos/asf/pulsar-client-cpp.git
The following commit(s) were added to refs/heads/main by this push:
new 1143a87 feat: Support message copy for C client. (#285)
1143a87 is described below
commit 1143a87f8587ba0122f4d7170dc157c13065b2d6
Author: Baodi Shi <[email protected]>
AuthorDate: Wed Jun 21 16:57:49 2023 +0800
feat: Support message copy for C client. (#285)
---
include/pulsar/c/message.h | 9 +++++++++
lib/c/c_Message.cc | 5 +++++
tests/c/c_MessageTest.cc | 34 ++++++++++++++++++++++++++++++++++
3 files changed, 48 insertions(+)
diff --git a/include/pulsar/c/message.h b/include/pulsar/c/message.h
index 353e609..30f1bad 100644
--- a/include/pulsar/c/message.h
+++ b/include/pulsar/c/message.h
@@ -33,6 +33,15 @@ typedef struct _pulsar_message pulsar_message_t;
typedef struct _pulsar_message_id pulsar_message_id_t;
PULSAR_PUBLIC pulsar_message_t *pulsar_message_create();
+/**
+ * Copy the contents of one pulsar_message_t object to another.
+ *
+ * Note: This method is a shallow copy, which will copy the pulsar::Message.
+ *
+ * @param from A pointer to the pulsar_message_t object that you want to copy
from
+ * @param to A pointer to the pulsar_message_t object that you want to copy to
+ */
+PULSAR_PUBLIC void pulsar_message_copy(const pulsar_message_t *from,
pulsar_message_t *to);
PULSAR_PUBLIC void pulsar_message_free(pulsar_message_t *message);
/// Builder
diff --git a/lib/c/c_Message.cc b/lib/c/c_Message.cc
index 826ebc1..3d30095 100644
--- a/lib/c/c_Message.cc
+++ b/lib/c/c_Message.cc
@@ -23,6 +23,11 @@
pulsar_message_t *pulsar_message_create() { return new pulsar_message_t; }
+void pulsar_message_copy(const pulsar_message_t *from, pulsar_message_t *to) {
+ to->builder = from->builder;
+ to->message = from->message;
+}
+
void pulsar_message_free(pulsar_message_t *message) { delete message; }
void pulsar_message_set_content(pulsar_message_t *message, const void *data,
size_t size) {
diff --git a/tests/c/c_MessageTest.cc b/tests/c/c_MessageTest.cc
new file mode 100644
index 0000000..a64a990
--- /dev/null
+++ b/tests/c/c_MessageTest.cc
@@ -0,0 +1,34 @@
+/**
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied. See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+#include <gtest/gtest.h>
+#include <lib/c/c_structs.h>
+#include <pulsar/c/message.h>
+
+TEST(c_MessageTest, MessageCopy) {
+ pulsar_message_t *from = pulsar_message_create();
+ pulsar_message_set_content(from, "hello", 5);
+ from->message = from->builder.build();
+ pulsar_message_t *to = pulsar_message_create();
+
+ pulsar_message_copy(from, to);
+ ASSERT_STREQ((const char *)pulsar_message_get_data(to), (const char
*)pulsar_message_get_data(from));
+
+ pulsar_message_free(from);
+ pulsar_message_free(to);
+}