Skip to content

Commit

Permalink
CBufferPayloadWriter moved to internal API (#1173)
Browse files Browse the repository at this point in the history
  • Loading branch information
rex-schilasky committed Jul 31, 2023
1 parent 942f01d commit c4da94b
Show file tree
Hide file tree
Showing 5 changed files with 93 additions and 58 deletions.
1 change: 1 addition & 0 deletions ecal/core/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -273,6 +273,7 @@ set(ecal_service_header_src

set(ecal_cmn_header_src
src/convert_utf.h
src/ecal_buffer_payload_writer.h
src/ecal_config_reader.h
src/ecal_config_reader_hlp.h
src/ecal_def.h
Expand Down
58 changes: 0 additions & 58 deletions ecal/core/include/ecal/ecal_payload_writer.h
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,6 @@
#pragma once

#include <cstddef>
#include <cstring>

namespace eCAL
{
Expand Down Expand Up @@ -108,61 +107,4 @@ namespace eCAL
virtual size_t GetSize() = 0;
};

/**
* @brief Payload writer class that wraps a classic (void*, size_t) interface.
*
* This class is a payload writer that wraps a classic interface using `void*` and `size_t`
* arguments. It inherits from the base class CPayloadWriter, allowing zero-copy memory
* operations.
*/
class CBufferPayloadWriter : public CPayloadWriter
{
public:
/**
* @brief Constructor for CBufferPayloadWriter.
*
* @param buffer_ Pointer to the buffer containing the data to be written.
* @param size_ Size of the data to be written.
*/
CBufferPayloadWriter(const void* const buffer_, size_t size_) : m_buffer(buffer_), m_size(size_) {};

/**
* @brief Make a dump memory copy of the stored buffer.
*
* This function performs a dump memory copy of the stored buffer to the provided
* memory location (buffer_) with the specified size (size_). The size of the provided
* memory buffer should be equal to or greater than the stored buffer size to avoid
* memory corruption.
*
* @param buffer_ Pointer to the target buffer where the data will be copied.
* @param size_ Size of the target buffer.
*
* @return True if the copy operation is successful, false otherwise.
*/
bool Write(void* buffer_, size_t size_) override
{
if (buffer_ == nullptr) return false;
if (size_ < m_size) return false;
if (m_buffer == nullptr) return false;
if (m_size == 0) return false;
memcpy(buffer_, m_buffer, m_size);
return true;
}

/**
* @brief Get the size of the memory that needs to be copied.
*
* This function returns the size of the memory buffer that needs to be copied during
* the write operation. It is used by the base class CPayloadWriter to allocate the
* required memory for eCAL.
*
* @return The size of the memory that needs to be copied.
*/
size_t GetSize() override { return m_size; };

private:
const void* m_buffer = nullptr; ///< Pointer to the buffer containing the data to be written.
size_t m_size = 0; ///< Size of the data to be written.
};

} // namespace eCAL
90 changes: 90 additions & 0 deletions ecal/core/src/ecal_buffer_payload_writer.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,90 @@
/* ========================= eCAL LICENSE =================================
*
* Copyright (C) 2016 - 2019 Continental Corporation
*
* Licensed 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.
*
* ========================= eCAL LICENSE =================================
*/

/**
* @file ecal_payload_writer.h
* @brief eCAL payload writer base class
**/

#pragma once

#include <ecal/ecal_payload_writer.h>

#include <cstring>

namespace eCAL
{
/**
* @brief Payload writer class that wraps a classic (void*, size_t) interface.
*
* This class is a payload writer that wraps a classic interface using `void*` and `size_t`
* arguments. It inherits from the base class CPayloadWriter, allowing zero-copy memory
* operations.
*/
class CBufferPayloadWriter : public CPayloadWriter
{
public:
/**
* @brief Constructor for CBufferPayloadWriter.
*
* @param buffer_ Pointer to the buffer containing the data to be written.
* @param size_ Size of the data to be written.
*/
CBufferPayloadWriter(const void* const buffer_, size_t size_) : m_buffer(buffer_), m_size(size_) {};

/**
* @brief Make a dump memory copy of the stored buffer.
*
* This function performs a dump memory copy of the stored buffer to the provided
* memory location (buffer_) with the specified size (size_). The size of the provided
* memory buffer should be equal to or greater than the stored buffer size to avoid
* memory corruption.
*
* @param buffer_ Pointer to the target buffer where the data will be copied.
* @param size_ Size of the target buffer.
*
* @return True if the copy operation is successful, false otherwise.
*/
bool Write(void* buffer_, size_t size_) override
{
if (buffer_ == nullptr) return false;
if (size_ < m_size) return false;
if (m_buffer == nullptr) return false;
if (m_size == 0) return false;
memcpy(buffer_, m_buffer, m_size);
return true;
}

/**
* @brief Get the size of the memory that needs to be copied.
*
* This function returns the size of the memory buffer that needs to be copied during
* the write operation. It is used by the base class CPayloadWriter to allocate the
* required memory for eCAL.
*
* @return The size of the memory that needs to be copied.
*/
size_t GetSize() override { return m_size; };

private:
const void* m_buffer = nullptr; ///< Pointer to the buffer containing the data to be written.
size_t m_size = 0; ///< Size of the data to be written.
};

} // namespace eCAL
1 change: 1 addition & 0 deletions ecal/core/src/pubsub/ecal_publisher.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
#include <ecal/ecal_config.h>

#include "ecal_config_reader_hlp.h"
#include "ecal_buffer_payload_writer.h"
#include "ecal_globals.h"

#include "readwrite/ecal_writer.h"
Expand Down
1 change: 1 addition & 0 deletions ecal/core/src/readwrite/ecal_writer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
#include <ecal/ecal_payload_writer.h>

#include "ecal_def.h"
#include "ecal_buffer_payload_writer.h"
#include "ecal_config_reader_hlp.h"

#include "ecal_registration_provider.h"
Expand Down

0 comments on commit c4da94b

Please sign in to comment.