File: CustomPayloadPool.hpp

package info (click to toggle)
fastdds 3.3.0%2Bds-3
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 60,540 kB
  • sloc: cpp: 793,735; xml: 15,283; python: 5,902; sh: 219; makefile: 95; ansic: 12
file content (100 lines) | stat: -rw-r--r-- 3,090 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
// Copyright 2023 Proyectos y Sistemas de Mantenimiento SL (eProsima).
//
// 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.

/**
 * @file CustomPayloadPool.hpp
 */

#ifndef FASTDDS_EXAMPLES_CPP_CUSTOM_PAYLOAD_POOL__CUSTOMPAYLOADPOOL_HPP
#define FASTDDS_EXAMPLES_CPP_CUSTOM_PAYLOAD_POOL__CUSTOMPAYLOADPOOL_HPP

#include <assert.h>
#include <stdio.h>
#include <string.h>

#include <fastdds/rtps/history/IPayloadPool.hpp>
#include <fastdds/rtps/common/SerializedPayload.hpp>

class CustomPayloadPool : public eprosima::fastdds::rtps::IPayloadPool
{
public:

    ~CustomPayloadPool() = default;

    bool get_payload(
            unsigned int size,
            eprosima::fastdds::rtps::SerializedPayload_t& payload)
    {
        // Reserve new memory for the payload buffer
        unsigned char* payload_buff = new unsigned char[size];

        // Assign the payload buffer to the SerializedPayload and update sizes
        payload.data = payload_buff;
        payload.length = size;
        payload.max_size = size;

        // Tell the SerializedPayload who needs to release its payload
        payload.payload_owner = this;

        return true;
    }

    bool get_payload(
            const eprosima::fastdds::rtps::SerializedPayload_t& data,
            eprosima::fastdds::rtps::SerializedPayload_t& payload)
    {
        // Reserve new memory for the payload buffer
        unsigned char* payload_buff = new unsigned char[data.length];

        // Copy the data
        memcpy(payload_buff, data.data, data.length);

        // Tell the SerializedPayload who needs to release its payload
        payload.payload_owner = this;

        // Assign the payload buffer to the SerializedPayload and update sizes
        payload.data = payload_buff;
        payload.length = data.length;
        payload.max_size = data.length;

        return true;
    }

    bool release_payload(
            eprosima::fastdds::rtps::SerializedPayload_t& payload)
    {
        // Ensure precondition
        if (this != payload.payload_owner)
        {
            std::cerr << "Trying to release a payload buffer allocated by a different PayloadPool." << std::endl;
            return false;
        }

        // Dealloc the buffer of the payload
        delete[] payload.data;

        // Reset sizes and pointers
        payload.data = nullptr;
        payload.length = 0;
        payload.max_size = 0;

        // Reset the owner of the payload
        payload.payload_owner = nullptr;

        return true;
    }

};

#endif  // FASTDDS_EXAMPLES_CPP_CUSTOM_PAYLOAD_POOL__CUSTOMPAYLOADPOOL_HPP