Skip to content
Open
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
11 changes: 7 additions & 4 deletions .github/workflows/reusable-ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -68,10 +68,13 @@ jobs:
matrix:
cmake-build-type:
- 'RelWithDebInfo'
strict-bool:
- false
- true

steps:
- name: Add ci-pending label if PR
if: ${{ github.event_name == 'pull_request' && inputs.add-label == true}}
if: ${{ github.event_name == 'pull_request' && inputs.add-label == true && matrix.strict-bool == false }}
uses: eProsima/eProsima-CI/external/add_labels@v0
with:
labels: ci-pending
Expand Down Expand Up @@ -122,7 +125,7 @@ jobs:
colcon_meta_file: ${{ github.workspace }}/src/fastcdr/.github/workflows/config/build.meta
colcon_build_args: ${{ inputs.colcon-args }}
colcon_build_args_default: --event-handlers=console_direct+
cmake_args: ${{ inputs.cmake-args }}
cmake_args: ${{ inputs.cmake-args }} ${{ matrix.strict-bool == true && '-DSTRICT_BOOL=ON' || '' }}
cmake_args_default: ${{ env.colcon-build-default-cmake-args }} ${{ env.toolset }}
cmake_build_type: ${{ matrix.cmake-build-type }}
workspace: ${{ github.workspace }}
Expand All @@ -137,7 +140,7 @@ jobs:
ctest_args: ${{ inputs.ctest-args }}
packages_names: fastcdr
workspace: ${{ github.workspace }}
test_report_artifact: ${{ inputs.label }}
test_report_artifact: ${{ inputs.label }}${{ matrix.strict-bool == true && '-strict-bool' || '' }}

- name: Fast CDR Test summary
uses: eProsima/eProsima-CI/multiplatform/junit_summary@v0
Expand All @@ -153,5 +156,5 @@ jobs:
if: always()
uses: eProsima/eProsima-CI/external/upload-artifact@v0
with:
name: test-results-${{ inputs.label }}
name: test-results-${{ inputs.label }}${{ matrix.strict-bool == true && '-strict-bool' || '' }}
path: log/latest_test/fastcdr
5 changes: 5 additions & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,11 @@ endif()
# unless the library was explicitly added as a static library.
option(BUILD_SHARED_LIBS "Create shared libraries by default" ON)

###############################################################################
# Strict bool
###############################################################################
option(STRICT_BOOL "Enforce bool values to 0/1 during serialization" OFF)

###############################################################################
# Test system configuration
###############################################################################
Expand Down
10 changes: 10 additions & 0 deletions include/fastcdr/Cdr.h
Original file line number Diff line number Diff line change
Expand Up @@ -3007,6 +3007,16 @@ class Cdr
std::wstring*& sequence_t,
size_t& num_elements);

/*!
* @brief Serializes the canonical @c uint8_t representation of @p bool_t.
*
* When @c FASTCDR_STRICT_BOOL is defined the serialized value is guaranteed to be exactly @c 0 or @c 1.
*
* @param[in] bool_t The boolean value to serialize.
*/
Cdr_DllAPI void serialize_bool(
bool bool_t);

/*!
* @brief This function template detects the content type of the STD container array and serializes the array.
* @param array_t The array that will be serialized in the buffer.
Expand Down
10 changes: 10 additions & 0 deletions include/fastcdr/FastCdr.h
Original file line number Diff line number Diff line change
Expand Up @@ -2041,6 +2041,16 @@ class Cdr_DllAPI FastCdr
FastCdr& deserialize_bool_sequence(
std::vector<bool>& vector_t);

/*!
* @brief Serializes the canonical @c uint8_t representation of @p bool_t.
*
* When @c FASTCDR_STRICT_BOOL is defined the serialized value is guaranteed to be exactly @c 0 or @c 1.
*
* @param[in] bool_t The boolean value to serialize.
*/
void serialize_bool(
bool bool_t);

FastCdr& deserialize_string_sequence(
std::string*& sequence_t,
size_t& num_elements);
Expand Down
2 changes: 2 additions & 0 deletions src/cpp/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ set(${PROJECT_NAME}_source_files
exceptions/Exception.cpp
exceptions/LockedExternalAccessException.cpp
exceptions/NotEnoughMemoryException.cpp
helpers/memory_helpers.cpp
FastCdr.rc
)

Expand All @@ -57,6 +58,7 @@ set_target_properties(${PROJECT_NAME} PROPERTIES
target_compile_definitions(${PROJECT_NAME}
PRIVATE
${PROJECT_NAME_UPPER}_SOURCE
$<$<BOOL:${STRICT_BOOL}>:${PROJECT_NAME_UPPER}_STRICT_BOOL>
INTERFACE
$<$<BOOL:${WIN32}>:${PROJECT_NAME_UPPER}_NO_LIB>
PUBLIC
Expand Down
46 changes: 15 additions & 31 deletions src/cpp/Cdr.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
#include <limits>

#include <fastcdr/Cdr.h>
#include "helpers/memory_helpers.hpp"

namespace eprosima {
namespace fastcdr {
Expand Down Expand Up @@ -821,14 +822,7 @@ Cdr& Cdr::serialize(
// Save last datasize.
last_data_size_ = sizeof(uint8_t);

if (bool_t)
{
offset_++ << static_cast<uint8_t>(1);
}
else
{
offset_++ << static_cast<uint8_t>(0);
}
serialize_bool(bool_t);

return *this;
}
Expand Down Expand Up @@ -929,13 +923,7 @@ Cdr& Cdr::serialize_array(

for (size_t count = 0; count < num_elements; ++count)
{
uint8_t value = 0;

if (bool_t[count])
{
value = 1;
}
offset_++ << value;
serialize_bool(bool_t[count]);
}

return *this;
Expand Down Expand Up @@ -2179,6 +2167,16 @@ Cdr& Cdr::operator <<(
return *this;
}

inline void Cdr::serialize_bool(
bool bool_t)
{
#if FASTCDR_STRICT_BOOL
offset_++ << static_cast<uint8_t>(normalize_bool(bool_t));
#else
offset_++ << (bool_t ? static_cast<uint8_t>(1) : static_cast<uint8_t>(0));
#endif // if FASTCDR_STRICT_BOOL
}
Comment thread
MiguelCompany marked this conversation as resolved.

Cdr& Cdr::serialize_bool_array(
const std::vector<bool>& vector_t)
{
Expand All @@ -2193,14 +2191,7 @@ Cdr& Cdr::serialize_bool_array(

for (size_t count = 0; count < vector_t.size(); ++count)
{
uint8_t value = 0;
std::vector<bool>::const_reference ref = vector_t[count];

if (ref)
{
value = 1;
}
offset_++ << value;
serialize_bool(vector_t[count]);
}
}
else
Expand Down Expand Up @@ -2233,14 +2224,7 @@ Cdr& Cdr::serialize_bool_sequence(

for (size_t count = 0; count < vector_t.size(); ++count)
{
uint8_t value = 0;
std::vector<bool>::const_reference ref = vector_t[count];

if (ref)
{
value = 1;
}
offset_++ << value;
serialize_bool(vector_t[count]);
}
}
else
Expand Down
36 changes: 14 additions & 22 deletions src/cpp/FastCdr.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
#include <fastcdr/FastCdr.h>
#include <fastcdr/exceptions/BadParamException.h>
#include <string.h>
#include "helpers/memory_helpers.hpp"

using namespace eprosima::fastcdr;
using namespace ::exception;
Expand Down Expand Up @@ -90,15 +91,9 @@ bool FastCdr::resize(
FastCdr& FastCdr::serialize(
const bool bool_t)
{
uint8_t value = 0;

if (((last_position_ - current_position_) >= sizeof(uint8_t)) || resize(sizeof(uint8_t)))
{
if (bool_t)
{
value = 1;
}
current_position_++ << value;
serialize_bool(bool_t);

return *this;
}
Expand Down Expand Up @@ -190,13 +185,7 @@ FastCdr& FastCdr::serialize_array(
{
for (size_t count = 0; count < num_elements; ++count)
{
uint8_t value = 0;

if (bool_t[count])
{
value = 1;
}
current_position_++ << value;
serialize_bool(bool_t[count]);
}

return *this;
Expand Down Expand Up @@ -691,6 +680,16 @@ FastCdr& FastCdr::deserialize_array(
throw NotEnoughMemoryException(NotEnoughMemoryException::NOT_ENOUGH_MEMORY_MESSAGE_DEFAULT);
}

inline void FastCdr::serialize_bool(
bool bool_t)
{
#if FASTCDR_STRICT_BOOL
current_position_++ << static_cast<uint8_t>(normalize_bool(bool_t));
#else
current_position_++ << (bool_t ? static_cast<uint8_t>(1) : static_cast<uint8_t>(0));
#endif // if FASTCDR_STRICT_BOOL
}

FastCdr& FastCdr::serialize_bool_sequence(
const std::vector<bool>& vector_t)
{
Expand All @@ -704,14 +703,7 @@ FastCdr& FastCdr::serialize_bool_sequence(
{
for (size_t count = 0; count < vector_t.size(); ++count)
{
uint8_t value = 0;
std::vector<bool>::const_reference ref = vector_t[count];

if (ref)
{
value = 1;
}
current_position_++ << value;
serialize_bool(vector_t[count]);
}
}
else
Expand Down
25 changes: 25 additions & 0 deletions src/cpp/helpers/memory_helpers.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
// Copyright 2026 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.

#include "memory_helpers.hpp"
Comment thread
MiguelCompany marked this conversation as resolved.

namespace {
void const volatile* volatile global_force_escape_pointer;
} // namespace

void eprosima::fastcdr::internal::use_char_pointer(
char const volatile *const v)
{
global_force_escape_pointer = reinterpret_cast<void const volatile*>(v);
}
Loading
Loading