Two topics with the same name

7 posts / 0 new
Last post
Offline
Last seen: 2 years 10 months ago
Joined: 09/17/2015
Posts: 53
Two topics with the same name

Hello all,

I try to create two topics with the same name, same publisher, etc. but in different C++ classes. This leads to the following exception:

 

DDS_Topic_createI:!create presentation topic
terminate called after throwing an instance of 'dds::core::Error'
what(): Failed to create Topic

I don't understand why I cannot subscribe to the same topic in two different classes in the same application.

Best regards,

Andreas

Gerardo Pardo's picture
Offline
Last seen: 2 weeks 3 days ago
Joined: 06/02/2010
Posts: 594

Hi Andreas,

The creation of the Topic fails because you cannot create two Topics with the same name within the same DomainParticipant. You can create multiple DataWriters and DataReaders to that same Topic. But you have to use the same Topic object, not create a new object each time.

You can either save a reference to the Topic somewhere in your application so that you can reuse it when you need to create the additional DataWriter or DataReaders or alternatively you can use the lookup_topicdescription operation on the DomainParticipant to retrive a previously-created Topic given its name. This operation can also be used to check if the Topic has already been created.

Gerardo

Offline
Last seen: 2 years 10 months ago
Joined: 09/17/2015
Posts: 53

Thank you for the quick answer! Is such a function also available in the modern C++11 API?

Offline
Last seen: 2 years 10 months ago
Joined: 09/17/2015
Posts: 53

I found it: dds::topic::find. But I cannot get it to work. I give it a domain participant and a topic name as std::string but I always get:

error: no matching function for call to 'find(const DomainParticipant&, const string&)'   auto Topic = dds::topic::find(participant, str);

 

Gerardo Pardo's picture
Offline
Last seen: 2 weeks 3 days ago
Joined: 06/02/2010
Posts: 594

Looking at the documentation of Topic::find() in the modern C++ API it seems like it only supports the underlying find() operation and not the lookup_topidescription() operation which is the one you need. I am checking with the developers to see if there is a workaround or different API you can use...

Gerardo

Offline
Last seen: 2 years 10 months ago
Joined: 09/17/2015
Posts: 53

Fixed it, the syntax is:

dds::topic::find<dds::topic::Topic<TopicType> >(dds::domain::find(0), "TopicName");

Gerardo Pardo's picture
Offline
Last seen: 2 weeks 3 days ago
Joined: 06/02/2010
Posts: 594

Ah OK, that makes sense!  I had mis-interpreted the warning that appears in the function documentation to mean that it was not doing a lookup_topicdescription, when in fact what is says is that the find() operation will only find Topic(s) but not other TopicDescription objects like ContentFilteredTopic...

Of course you only need to call  dds::domain::find(0) if you want to also find the DomainParticipant from the domain_id. If you have the DomainParticipant you could use it... 

I stand corrected and I am glad you found the solution!

Gerardo