c_api.h
Go to the documentation of this file.
1 #pragma once
2 
3 // SPDX-License-Identifier: BSD-3-Clause
4 // SPDX-FileCopyrightText: Czech Technical University in Prague
5 
12 #include <string>
13 #include <vector>
14 
15 #include <ros/message_traits.h>
16 #include <ros/serialization.h>
17 
19 
20 namespace cras
21 {
22 
36 typedef void* (*allocator_t)(size_t);
37 
46 char* outputString(allocator_t allocator, const char* string, size_t length);
47 
55 char* outputString(allocator_t allocator, const std::string& string);
56 
64 uint8_t* outputByteBuffer(allocator_t allocator, const uint8_t* bytes, size_t length);
65 
72 uint8_t* outputByteBuffer(allocator_t allocator, const std::vector<uint8_t>& bytes);
73 
80 template<typename Message, typename Enable = ::std::enable_if_t<::ros::message_traits::IsMessage<Message>::value>>
81 uint8_t* outputRosMessage(allocator_t allocator, const Message& msg)
82 {
83  // This code does almost the same as ros::serialization::serializeMessage(), but it directly uses the allocated
84  // buffer instead of creating its own one and then copying.
85 
86  const auto len = static_cast<uint32_t>(::ros::serialization::serializationLength(msg));
87  const auto buf = static_cast<uint8_t*>(allocator(len));
88 
90  ::ros::serialization::serialize(s, msg);
91 
92  return buf;
93 }
94 
95 }
cras::outputRosMessage
uint8_t * outputRosMessage(allocator_t allocator, const Message &msg)
Allocate enough bytes using the given allocator and serialize the given message into it.
Definition: c_api.h:81
ros::serialization::OStream
msg
msg
message_utils.hpp
Utilities for working with ROS message files.
cras
Definition: any.hpp:15
s
XmlRpcServer s
cras::outputByteBuffer
uint8_t * outputByteBuffer(allocator_t allocator, const uint8_t *bytes, size_t length)
Allocate enough bytes using the given allocator and copy the given bytes into the buffer.
cras::outputString
char * outputString(allocator_t allocator, const char *string, size_t length)
Allocate enough bytes using the given allocator and copy the given string into the buffer (including ...
message_traits.h
serialization.h
cras::allocator_t
void *(* allocator_t)(size_t)
Allocator function that should allocate a buffer of the given size on the caller side and return a po...
Definition: c_api.h:36


cras_cpp_common
Author(s): Martin Pecka
autogenerated on Sun Jan 14 2024 03:48:14