-
Notifications
You must be signed in to change notification settings - Fork 8
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Utility to convert QoS ids and masks to string (#120)
* Auxiliar methods to convert to string QoS ids and policies Signed-off-by: tempate <[email protected]> * Documentation Signed-off-by: tempate <[email protected]> * Uncrustify Signed-off-by: tempate <[email protected]> * Tests Signed-off-by: tempate <[email protected]> * Print invalid policy masks as invalid Signed-off-by: tempate <[email protected]> * Apply self-suggestions Signed-off-by: tempate <[email protected]> * Apply suggestions Signed-off-by: tempate <[email protected]> --------- Signed-off-by: tempate <[email protected]>
- Loading branch information
Showing
6 changed files
with
386 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
// Copyright 2024 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 qos_utils.hpp | ||
*/ | ||
|
||
#pragma once | ||
|
||
#include <string> | ||
|
||
#include <fastdds/dds/core/policy/QosPolicies.hpp> | ||
|
||
#include <cpp_utils/library/library_dll.h> | ||
|
||
namespace eprosima { | ||
namespace utils { | ||
|
||
/** | ||
* @brief Converts a \c QosPolicyId_t to a string. | ||
* | ||
* @param qos_policy_id The \c QosPolicyId_t to convert. | ||
* @return The string representation of the \c QosPolicyId_t. | ||
*/ | ||
CPP_UTILS_DllAPI std::string qos_policy_id_to_string( | ||
const fastdds::dds::QosPolicyId_t& qos_policy_id); | ||
|
||
/** | ||
* @brief Converts a \c PolicyMask to a string. | ||
* | ||
* @param qos_policy_mask The \c PolicyMask to convert. | ||
* @return The string representation of the \c PolicyMask. | ||
*/ | ||
CPP_UTILS_DllAPI std::string qos_policy_mask_to_string( | ||
const fastdds::dds::PolicyMask& qos_policy_mask); | ||
|
||
} /* namespace utils */ | ||
} /* namespace eprosima */ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,148 @@ | ||
// Copyright 2024 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 qos_utils.cpp | ||
*/ | ||
|
||
#include <string> | ||
|
||
#include <fastdds/dds/core/policy/QosPolicies.hpp> | ||
|
||
#include <cpp_utils/qos/qos_utils.hpp> | ||
|
||
using namespace eprosima::fastdds::dds; | ||
|
||
namespace eprosima { | ||
namespace utils { | ||
|
||
std::string qos_policy_id_to_string( | ||
const QosPolicyId_t& qos_policy_id) | ||
{ | ||
switch (qos_policy_id) | ||
{ | ||
case QosPolicyId_t::USERDATA_QOS_POLICY_ID: | ||
return "User data"; | ||
case QosPolicyId_t::DURABILITY_QOS_POLICY_ID: | ||
return "Durability"; | ||
case QosPolicyId_t::PRESENTATION_QOS_POLICY_ID: | ||
return "Presentation"; | ||
case QosPolicyId_t::DEADLINE_QOS_POLICY_ID: | ||
return "Deadline"; | ||
case QosPolicyId_t::LATENCYBUDGET_QOS_POLICY_ID: | ||
return "Latency budget"; | ||
case QosPolicyId_t::OWNERSHIP_QOS_POLICY_ID: | ||
return "Ownership"; | ||
case QosPolicyId_t::OWNERSHIPSTRENGTH_QOS_POLICY_ID: | ||
return "Ownership strength"; | ||
case QosPolicyId_t::LIVELINESS_QOS_POLICY_ID: | ||
return "Liveliness"; | ||
case QosPolicyId_t::TIMEBASEDFILTER_QOS_POLICY_ID: | ||
return "Time-based filter"; | ||
case QosPolicyId_t::PARTITION_QOS_POLICY_ID: | ||
return "Partition"; | ||
case QosPolicyId_t::RELIABILITY_QOS_POLICY_ID: | ||
return "Reliability"; | ||
case QosPolicyId_t::DESTINATIONORDER_QOS_POLICY_ID: | ||
return "Destination order"; | ||
case QosPolicyId_t::HISTORY_QOS_POLICY_ID: | ||
return "History"; | ||
case QosPolicyId_t::RESOURCELIMITS_QOS_POLICY_ID: | ||
return "Resource limits"; | ||
case QosPolicyId_t::ENTITYFACTORY_QOS_POLICY_ID: | ||
return "Entity factory"; | ||
case QosPolicyId_t::WRITERDATALIFECYCLE_QOS_POLICY_ID: | ||
return "Writer data lifecycle"; | ||
case QosPolicyId_t::READERDATALIFECYCLE_QOS_POLICY_ID: | ||
return "Reader data lifecycle"; | ||
case QosPolicyId_t::TOPICDATA_QOS_POLICY_ID: | ||
return "Topic data"; | ||
case QosPolicyId_t::GROUPDATA_QOS_POLICY_ID: | ||
return "Group data"; | ||
case QosPolicyId_t::TRANSPORTPRIORITY_QOS_POLICY_ID: | ||
return "Transport priority"; | ||
case QosPolicyId_t::LIFESPAN_QOS_POLICY_ID: | ||
return "Lifespan"; | ||
case QosPolicyId_t::DURABILITYSERVICE_QOS_POLICY_ID: | ||
return "Durability service"; | ||
case QosPolicyId_t::DATAREPRESENTATION_QOS_POLICY_ID: | ||
return "Data representation"; | ||
case QosPolicyId_t::TYPECONSISTENCYENFORCEMENT_QOS_POLICY_ID: | ||
return "Type consistency enforcement"; | ||
case QosPolicyId_t::DISABLEPOSITIVEACKS_QOS_POLICY_ID: | ||
return "Disable positive acks"; | ||
case QosPolicyId_t::PARTICIPANTRESOURCELIMITS_QOS_POLICY_ID: | ||
return "Participant resource limits"; | ||
case QosPolicyId_t::PROPERTYPOLICY_QOS_POLICY_ID: | ||
return "Property policy"; | ||
case QosPolicyId_t::PUBLISHMODE_QOS_POLICY_ID: | ||
return "Publish mode"; | ||
case QosPolicyId_t::READERRESOURCELIMITS_QOS_POLICY_ID: | ||
return "Reader resource limits"; | ||
case QosPolicyId_t::RTPSENDPOINT_QOS_POLICY_ID: | ||
return "RTPS endpoint"; | ||
case QosPolicyId_t::RTPSRELIABLEREADER_QOS_POLICY_ID: | ||
return "RTPS reliable reader"; | ||
case QosPolicyId_t::RTPSRELIABLEWRITER_QOS_POLICY_ID: | ||
return "RTPS reliable writer"; | ||
case QosPolicyId_t::TRANSPORTCONFIG_QOS_POLICY_ID: | ||
return "Transport config"; | ||
case QosPolicyId_t::TYPECONSISTENCY_QOS_POLICY_ID: | ||
return "Type consistency"; | ||
case QosPolicyId_t::WIREPROTOCOLCONFIG_QOS_POLICY_ID: | ||
return "Wire protocol config"; | ||
case QosPolicyId_t::WRITERRESOURCELIMITS_QOS_POLICY_ID: | ||
return "Writer resource limits"; | ||
case QosPolicyId_t::INVALID_QOS_POLICY_ID: | ||
default: | ||
return "Invalid"; | ||
} | ||
} | ||
|
||
std::string qos_policy_mask_to_string( | ||
const PolicyMask& qos_policy_mask) | ||
{ | ||
if (qos_policy_mask.none()) | ||
{ | ||
return qos_policy_id_to_string(QosPolicyId_t::INVALID_QOS_POLICY_ID); | ||
} | ||
|
||
std::string policy_mask_str; | ||
bool first_id_in_mask = true; | ||
|
||
for (std::uint32_t id_int = QosPolicyId_t::INVALID_QOS_POLICY_ID; id_int < QosPolicyId_t::NEXT_QOS_POLICY_ID; | ||
id_int++) | ||
{ | ||
const auto qos_policy_id = static_cast<QosPolicyId_t>(id_int); | ||
|
||
if (qos_policy_mask.test(qos_policy_id)) | ||
{ | ||
if (first_id_in_mask) | ||
{ | ||
first_id_in_mask = false; | ||
} | ||
else | ||
{ | ||
policy_mask_str += ", "; | ||
} | ||
|
||
policy_mask_str += qos_policy_id_to_string(qos_policy_id); | ||
} | ||
} | ||
|
||
return policy_mask_str; | ||
} | ||
|
||
} /* namespace utils */ | ||
} /* namespace eprosima */ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
# Copyright 2024 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. | ||
|
||
############################ | ||
# QOS UTILS TEST | ||
############################ | ||
|
||
set(TEST_NAME qos_utils_test) | ||
|
||
set(TEST_SOURCES | ||
qos_utils_test.cpp | ||
${PROJECT_SOURCE_DIR}/src/cpp/qos/qos_utils.cpp | ||
) | ||
|
||
set(TEST_LIST | ||
qos_policy_id_to_string, | ||
qos_policy_mask_to_string | ||
) | ||
|
||
set(TEST_EXTRA_LIBRARIES | ||
fastcdr | ||
fastdds | ||
$<$<BOOL:${WIN32}>:iphlpapi$<SEMICOLON>Shlwapi> | ||
) | ||
|
||
add_unittest_executable( | ||
"${TEST_NAME}" | ||
"${TEST_SOURCES}" | ||
"${TEST_LIST}" | ||
"${TEST_EXTRA_LIBRARIES}" | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,143 @@ | ||
// Copyright 2024 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 <map> | ||
#include <string> | ||
|
||
#include <cpp_utils/testing/gtest_aux.hpp> | ||
#include <gtest/gtest.h> | ||
|
||
#include <fastdds/dds/core/policy/QosPolicies.hpp> | ||
|
||
#include <cpp_utils/qos/qos_utils.hpp> | ||
|
||
using namespace eprosima; | ||
using namespace eprosima::fastdds::dds; | ||
|
||
std::map<QosPolicyId_t, std::string> ids_to_str{ | ||
{QosPolicyId_t::USERDATA_QOS_POLICY_ID, "User data"}, | ||
{QosPolicyId_t::DURABILITY_QOS_POLICY_ID, "Durability"}, | ||
{QosPolicyId_t::PRESENTATION_QOS_POLICY_ID, "Presentation"}, | ||
{QosPolicyId_t::DEADLINE_QOS_POLICY_ID, "Deadline"}, | ||
{QosPolicyId_t::LATENCYBUDGET_QOS_POLICY_ID, "Latency budget"}, | ||
{QosPolicyId_t::OWNERSHIP_QOS_POLICY_ID, "Ownership"}, | ||
{QosPolicyId_t::OWNERSHIPSTRENGTH_QOS_POLICY_ID, "Ownership strength"}, | ||
{QosPolicyId_t::LIVELINESS_QOS_POLICY_ID, "Liveliness"}, | ||
{QosPolicyId_t::TIMEBASEDFILTER_QOS_POLICY_ID, "Time-based filter"}, | ||
{QosPolicyId_t::PARTITION_QOS_POLICY_ID, "Partition"}, | ||
{QosPolicyId_t::RELIABILITY_QOS_POLICY_ID, "Reliability"}, | ||
{QosPolicyId_t::DESTINATIONORDER_QOS_POLICY_ID, "Destination order"}, | ||
{QosPolicyId_t::HISTORY_QOS_POLICY_ID, "History"}, | ||
{QosPolicyId_t::RESOURCELIMITS_QOS_POLICY_ID, "Resource limits"}, | ||
{QosPolicyId_t::ENTITYFACTORY_QOS_POLICY_ID, "Entity factory"}, | ||
{QosPolicyId_t::WRITERDATALIFECYCLE_QOS_POLICY_ID, "Writer data lifecycle"}, | ||
{QosPolicyId_t::READERDATALIFECYCLE_QOS_POLICY_ID, "Reader data lifecycle"}, | ||
{QosPolicyId_t::TOPICDATA_QOS_POLICY_ID, "Topic data"}, | ||
{QosPolicyId_t::GROUPDATA_QOS_POLICY_ID, "Group data"}, | ||
{QosPolicyId_t::TRANSPORTPRIORITY_QOS_POLICY_ID, "Transport priority"}, | ||
{QosPolicyId_t::LIFESPAN_QOS_POLICY_ID, "Lifespan"}, | ||
{QosPolicyId_t::DURABILITYSERVICE_QOS_POLICY_ID, "Durability service"}, | ||
{QosPolicyId_t::DATAREPRESENTATION_QOS_POLICY_ID, "Data representation"}, | ||
{QosPolicyId_t::TYPECONSISTENCYENFORCEMENT_QOS_POLICY_ID, "Type consistency enforcement"}, | ||
{QosPolicyId_t::DISABLEPOSITIVEACKS_QOS_POLICY_ID, "Disable positive acks"}, | ||
{QosPolicyId_t::PARTICIPANTRESOURCELIMITS_QOS_POLICY_ID, "Participant resource limits"}, | ||
{QosPolicyId_t::PROPERTYPOLICY_QOS_POLICY_ID, "Property policy"}, | ||
{QosPolicyId_t::PUBLISHMODE_QOS_POLICY_ID, "Publish mode"}, | ||
{QosPolicyId_t::READERRESOURCELIMITS_QOS_POLICY_ID, "Reader resource limits"}, | ||
{QosPolicyId_t::RTPSENDPOINT_QOS_POLICY_ID, "RTPS endpoint"}, | ||
{QosPolicyId_t::RTPSRELIABLEREADER_QOS_POLICY_ID, "RTPS reliable reader"}, | ||
{QosPolicyId_t::RTPSRELIABLEWRITER_QOS_POLICY_ID, "RTPS reliable writer"}, | ||
{QosPolicyId_t::TRANSPORTCONFIG_QOS_POLICY_ID, "Transport config"}, | ||
{QosPolicyId_t::TYPECONSISTENCY_QOS_POLICY_ID, "Type consistency"}, | ||
{QosPolicyId_t::WIREPROTOCOLCONFIG_QOS_POLICY_ID, "Wire protocol config"}, | ||
{QosPolicyId_t::WRITERRESOURCELIMITS_QOS_POLICY_ID, "Writer resource limits"}, | ||
{QosPolicyId_t::INVALID_QOS_POLICY_ID, "Invalid"}}; | ||
|
||
/** | ||
* Test \c qos_policy_id_to_string call | ||
*/ | ||
TEST(qos_utils_test, qos_policy_id_to_string) | ||
{ | ||
for (const auto& id_to_str : ids_to_str) | ||
{ | ||
const auto id_str = utils::qos_policy_id_to_string(id_to_str.first); | ||
ASSERT_EQ(id_str, id_to_str.second); | ||
} | ||
} | ||
|
||
/** | ||
* Test \c qos_policy_mask_to_string call | ||
*/ | ||
TEST(qos_utils_test, qos_policy_mask_to_string) | ||
{ | ||
// Empty mask | ||
{ | ||
const PolicyMask mask; | ||
const auto mask_str = utils::qos_policy_mask_to_string(mask); | ||
const auto mask_str_expected = ids_to_str.at(QosPolicyId_t::INVALID_QOS_POLICY_ID); | ||
ASSERT_EQ(mask_str, mask_str_expected); | ||
} | ||
|
||
// One policy | ||
{ | ||
for (const auto& id_to_str : ids_to_str) | ||
{ | ||
PolicyMask mask; | ||
mask.set(id_to_str.first); | ||
|
||
const auto mask_str = utils::qos_policy_mask_to_string(mask); | ||
const auto mask_str_expected = id_to_str.second; | ||
ASSERT_EQ(mask_str, mask_str_expected); | ||
} | ||
} | ||
|
||
// All policies | ||
{ | ||
PolicyMask mask; | ||
|
||
for (const auto& id_to_str : ids_to_str) | ||
{ | ||
mask.set(id_to_str.first); | ||
} | ||
|
||
const auto mask_str = utils::qos_policy_mask_to_string(mask); | ||
const auto mask_str_expected = | ||
"Invalid, User data, Durability, Presentation, Deadline, Latency budget, Ownership, " | ||
"Ownership strength, Liveliness, Time-based filter, Partition, Reliability, Destination order, " | ||
"History, Resource limits, Entity factory, Writer data lifecycle, Reader data lifecycle, Topic data, " | ||
"Group data, Transport priority, Lifespan, Durability service, Data representation, " | ||
"Type consistency enforcement, Disable positive acks, Participant resource limits, Property policy, " | ||
"Publish mode, Reader resource limits, RTPS endpoint, RTPS reliable reader, RTPS reliable writer, " | ||
"Transport config, Type consistency, Wire protocol config, Writer resource limits"; | ||
ASSERT_EQ(mask_str, mask_str_expected); | ||
} | ||
|
||
// Invalid policy | ||
{ | ||
PolicyMask mask; | ||
mask.set(QosPolicyId_t::INVALID_QOS_POLICY_ID); | ||
|
||
const auto mask_str = utils::qos_policy_mask_to_string(mask); | ||
const auto mask_str_expected = ids_to_str.at(QosPolicyId_t::INVALID_QOS_POLICY_ID); | ||
ASSERT_EQ(mask_str, mask_str_expected); | ||
} | ||
} | ||
|
||
int main( | ||
int argc, | ||
char** argv) | ||
{ | ||
::testing::InitGoogleTest(&argc, argv); | ||
return RUN_ALL_TESTS(); | ||
} |
Oops, something went wrong.