Casting dds::core::vector<dds::core::string> to std::vector<std::string>

5 posts / 0 new
Last post
Offline
Last seen: 1 year 3 weeks ago
Joined: 08/13/2014
Posts: 55
Casting dds::core::vector<dds::core::string> to std::vector<std::string>

Hi

I have an IDL file like below:

module myIDL {  struct payload { sequence name; sequence value; string moduleName; //@key }; // end of payload}; // end of module 

In my code, I want to cast the received data from my data writer like below:

std::vector names = data.name();

But I will get the following error; " error: conversion from 'rti::core::vector<dds::core::basic_string<char, rti::core::memory::OsapiAllocator > >' to non-scalar type 'std::vector<std::basic_string >' requested"

and I have to do the casting with a for loop as follows:

std::vector<std::string> names; for (int i = 0; i < data.names().size(); i++) { names.at(i) = data.name().at(i); }

Is there any way to do the casting?

Best Regards,

Bonjefir

 

Offline
Last seen: 1 year 3 weeks ago
Joined: 08/13/2014
Posts: 55

 

Any Answer?

Offline
Last seen: 2 weeks 5 days ago
Joined: 04/02/2013
Posts: 148

Hi,

Automatic conversion works for strings and vectors of primitive types, but it doesn't work for vectors of strings. However you can easily achieve the same result with std::copy:

std::copy(rti_vector.begin(), rti_vector.end(), std::back_inserter(std_vector));

Offline
Last seen: 1 year 3 weeks ago
Joined: 08/13/2014
Posts: 55

Hi Alex,

Thanks for your email. But your implementation of std::vector is a little strange. Assume you want to insert a string in your vector ( suppose the vector "name" in "myIDL" (First post) ). How would you do that? You do not have "insert", "push_back", or "assign". This situation is also true for getting a value of your vector. You don't have "pop_back", "erase" . 

So How should I fill my vector?!! I think the only way is to creating a std::vector<std::string> and fill it, then cast it to dds::core::vector<dds::core::string>. Am I right? I am not a professional programmer so excuse my question if it is a bit dummy. :)

thanks in advance for your help.

Bonjefir

Offline
Last seen: 2 weeks 5 days ago
Joined: 04/02/2013
Posts: 148

You you may have to play with resize() to fill up the vector or shrink it. If you need to do insertions in the middle or something more complicated I recommend using a std::vector and copy into the RTI vector.