diff --git a/include/pulsar/c/message.h b/include/pulsar/c/message.h index 353e609f..30f1bad0 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 826ebc17..3d300957 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 00000000..a64a9901 --- /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 +#include +#include + +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); +}