14#include <fastdds/dds/topic/IContentFilter.hpp>
15#include <fastdds/dds/topic/TopicDataType.hpp>
16#include <fastrtps/types/DynamicData.h>
17#include <fastrtps/types/DynamicDataFactory.h>
18#include <fastrtps/types/DynamicPubSubType.h>
19#include <fastrtps/types/DynamicTypePtr.h>
20#include <xtypes/xtypes.hpp>
22#include "Converter.hpp"
39 const eprosima::fastdds::dds::TopicDataType* data_type,
const std::string&
type_name)
46 namespace etypes = eprosima::fastrtps::types;
47 const auto* dynpubsub =
dynamic_cast<const etypes::DynamicPubSubType*
>(data_type);
48 if (dynpubsub ==
nullptr) {
49 throw std::runtime_error(
"Custom filter only works with dynamic types, was unable to cast "
50 "TopicDataType* to DynamicPubSubType* ");
53 eprosima::fastrtps::types::DynamicType_ptr dyn_type = dynpubsub->GetDynamicType();
54 pubsub_type =
new eprosima::fastrtps::types::DynamicPubSubType(dyn_type);
55 dyn_data = eprosima::fastrtps::types::DynamicDataFactory::get_instance()->create_data(dyn_type);
58 eprosima::fastrtps::types::DynamicData*
dyn_data;
67 sample_data.for_each([&](
const eprosima::xtypes::DynamicData::ReadableNode& a_node) {
68 bool a_is_leaf = (a_node.type().is_primitive_type() || a_node.type().is_enumerated_type());
69 bool a_is_string = a_node.type().kind() == eprosima::xtypes::TypeKind::STRING_TYPE;
70 if ((a_is_leaf || a_is_string) && a_node.from_member() && a_node.from_member()->is_key()) {
72 key_data.for_each([&](
const eprosima::xtypes::DynamicData::ReadableNode& b_node) {
74 (b_node.type().is_primitive_type() || b_node.type().is_enumerated_type());
75 bool b_is_string = b_node.type().kind() == eprosima::xtypes::TypeKind::STRING_TYPE;
77 (b_is_leaf || b_is_string) && b_node.from_member() && b_node.from_member()->is_key()) {
78 if (key_a == key_b) { is_equal &= (a_node.data() == b_node.data()); }
101 std::map<std::string, std::unique_ptr<FilterMemberType>> member_types;
112 const eprosima::fastdds::dds::TopicDataType* data_type,
const std::string& type_name,
113 const eprosima::fastdds::dds::LoanableTypedCollection<const char*>& parameters) {
114 if (
add_type(data_type, type_name, parameters)) {
125 return static_cast<bool>(member_types.count(guid));
136 const eprosima::fastdds::dds::TopicDataType* data_type,
const std::string& type_name,
137 const eprosima::fastdds::dds::LoanableTypedCollection<const char*>& parameters);
151 const SerializedPayload& payload,
const FilterSampleInfo& sample_info,
152 const GUID_t& reader_guid)
const override;
Custom key filter for dynamic data topics.
Definition: CustomKeyFilter.hpp:99
bool has_reader_GUID(const std::string &guid)
Check if the filter has registered reader with given GUID.
Definition: CustomKeyFilter.hpp:124
virtual ~CustomKeyFilter()=default
bool evaluate(const SerializedPayload &payload, const FilterSampleInfo &sample_info, const GUID_t &reader_guid) const override
Evaluate filter discriminating whether the sample is relevant or not, i.e. whether it meets the filte...
Definition: CustomKeyFilter.cpp:107
bool add_type(const eprosima::fastdds::dds::TopicDataType *data_type, const std::string &type_name, const eprosima::fastdds::dds::LoanableTypedCollection< const char * > ¶meters)
Registers a new data type with associated dynamic data type pointer.
Definition: CustomKeyFilter.cpp:13
CustomKeyFilter(const eprosima::fastdds::dds::TopicDataType *data_type, const std::string &type_name, const eprosima::fastdds::dds::LoanableTypedCollection< const char * > ¶meters)
Construct a new CustomKeyFilter object.
Definition: CustomKeyFilter.hpp:111
Definition: auxiliaries.cpp:26
Class with functions to convert between xtypes::DynamicData and fast-dds DynamicData.
Definition: Converter.hpp:31
A helper to hold dynamic data type information.
Definition: CustomKeyFilter.hpp:36
eprosima::xtypes::DynamicData sample_data
Definition: CustomKeyFilter.hpp:59
~FilterMemberType()
Definition: CustomKeyFilter.hpp:91
FilterMemberType(const eprosima::fastdds::dds::TopicDataType *data_type, const std::string &type_name)
Definition: CustomKeyFilter.hpp:38
std::string type_name
Definition: CustomKeyFilter.hpp:62
eprosima::fastrtps::types::DynamicPubSubType * pubsub_type
Definition: CustomKeyFilter.hpp:57
eprosima::fastrtps::types::DynamicData * dyn_data
Definition: CustomKeyFilter.hpp:58
eprosima::xtypes::DynamicData key_data
Definition: CustomKeyFilter.hpp:59
FilterMemberType()=delete
bool compare_keys()
Definition: CustomKeyFilter.hpp:64
size_t key_count
Definition: CustomKeyFilter.hpp:61