Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 9 additions & 0 deletions include/pulsar/c/message.h
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
5 changes: 5 additions & 0 deletions lib/c/c_Message.cc
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
34 changes: 34 additions & 0 deletions tests/c/c_MessageTest.cc
Original file line number Diff line number Diff line change
@@ -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);
}