RTI Connext Modern C++ API  Version 6.1.0
Qos Provider Use Cases

How to use dds::core::QosProvider to access XML QoS profiles. More...

How to use dds::core::QosProvider to access XML QoS profiles.

Managing Qos Profiles

This section provides several examples on how to use a dds::core::QosProvider to load QoS profiles from XML. The class documentation provides general usage information.

These examples use the following file, ExampleQos.xml, which defines only the EntityName QoS policy for the DomainParticipant and DataWriter to illustrate how the profiles are loaded:

<?xml version="1.0"?>
<dds xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:noNamespaceSchemaLocation="https://community.rti.com/schema/current/rti_dds_profiles.xsd">
<qos_library name="MyLibrary">
<qos_profile name="MyProfile" is_default_qos="true">
<domain_participant_qos>
<participant_name>
<name>ExampleParticipantName</name>
</participant_name>
</domain_participant_qos>
<datawriter_qos>
<publication_name>
<name>ExamplePublicationName</name>
</publication_name>
</datawriter_qos>
</qos_profile>
</qos_library>
<qos_library name="MySecondLibrary">
<qos_profile name="MySecondProfile">
<domain_participant_qos>
<participant_name>
<name>SecondExampleParticipantName</name>
</participant_name>
</domain_participant_qos>
<datawriter_qos>
<publication_name>
<name>SecondExamplePublicationName</name>
</publication_name>
</datawriter_qos>
</qos_profile>
</qos_library>
</dds>

The example code shows how to create a QosProvider, load QoS profiles from the provider, and create entities with those QoS values:

// Load the contents of an XML file:
dds::core::QosProvider my_provider("ExampleQos.xml");
// Create a participant with the default profile:
auto participant_qos = my_provider.participant_qos();
dds::domain::DomainParticipant participant(0, participant_qos);
// This prints ExampleParticipantName because MyLibrary::MyProfile is marked
// with is_default_qos="true"
std::cout << participant.qos().policy<EntityName>().name().value()
<< std::endl;
// Load a specific profile. This time it prints SecondExampleParticipantName
participant_qos =
my_provider.participant_qos("MySecondLibrary::MySecondProfile");
std::cout << participant_qos.policy<EntityName>().name().value()
<< std::endl;
// Change the default profile:
my_provider.extensions().default_profile("MySecondLibrary::MySecondProfile");
// Now when the profile is not specified, participant_qos() looks at
// "MySecondLibrary::MySecondProfile", printing SecondExampleParticipantName
participant_qos = my_provider.participant_qos();
std::cout << participant_qos.policy<EntityName>().name().value()
<< std::endl;
// Create a topic, publisher and writer:
dds::topic::Topic<Foo> topic(participant, "Example Foo");
dds::pub::Publisher publisher(participant);
publisher,
topic,
my_provider.datawriter_qos());
// Prints SecondExamplePublicationName
std::cout << writer.qos().policy<EntityName>().name().value()
<< std::endl;
// If you don't want to specify the QoS argument for each writer you create
// you can set the default value:
my_provider.datawriter_qos("MyLibrary::MyProfile"));
dds::pub::DataWriter<Foo> other_writer(publisher, topic);
// Prints ExamplePublicationName
std::cout << writer.qos().policy<EntityName>().name().value() << std::endl;
// You can also load the QoS profiles from a string:
const char * my_xml_str =
"str://\"<dds>"
"<qos_library name=\"MyThirdLibrary\">"
"<qos_profile name=\"MyThirdProfile\">"
"<domain_participant_qos>"
"<participant_name>"
"<name>ThirdExampleParticipantName</name>"
"</participant_name>"
"</domain_participant_qos>"
"</qos_profile>"
"</qos_library>"
"</dds>\"";
// In this case we're also specifying which profile we want to load by
// default in the constructor
dds::core::QosProvider my_provider2(
my_xml_str,
"MyThirdLibrary::MyThirdProfile");
// This prints ThirdExampleParticipantName
participant_qos = my_provider2.participant_qos();
std::cout << participant_qos.policy<EntityName>().name().value()
<< std::endl;
// You can also examine which libraries and profiles that have been
// loaded:
auto libraries = my_provider.extensions().qos_profile_libraries();
for (const std::string& library : libraries) {
std::cout << library << std::endl;
auto profiles = my_provider.extensions().qos_profiles(library);
for (const std::string& profile : profiles) {
std::cout << " -" << profile << std::endl;
}
}
See also
Create a DynamicType from an XML description
XML Application Creation

The Default Qos Provider

This example shows how to configure the default QosProvider:

// We're going to configure the default QoS Provider to load ExampleQos.xml
// and to ignore the NDDS_QOS_PROFILES environment variable and the file
// USER_QOS_PROFILES.xml
params.url_profile({ "ExampleQos.xml" });
params.ignore_user_profile(true);
// To ensure that the new configuration takes effect before any other
// profiles are loaded, set the new parameters before accessing
// QosProvider::Default().
dds::domain::DomainParticipant participant1(0, default_provider.participant_qos());
// Prints "ExampleParticipantName"
std::cout << participant1.qos().policy<EntityName>().name().value()
<< std::endl;
// Change the default profile
default_provider.extensions().default_profile("MySecondLibrary::MySecondProfile");
dds::domain::DomainParticipant participant2(1, default_provider.participant_qos());
// Prints "SecondExampleParticipantName"
std::cout << participant2.qos().policy<EntityName>().name().value()
<< std::endl;
// Reset the QosProviderParams for the default QosProvider
// This participant will not have a participant name (the default
// RTI Connext value) because we reset the default QosProviderParams,
// meaning that the value for string_profile is empty and the above example
// XML Qos profile is not loaded.
dds::domain::DomainParticipant participant3(2, default_provider.participant_qos());
// Prints false
std::cout << std::boolalpha
<< participant2.qos().policy<EntityName>().name().has_value()
<< std::endl;