Setting an array of strings

3 posts / 0 new
Last post
Offline
Last seen: 11 years 6 months ago
Joined: 05/06/2013
Posts: 2
Setting an array of strings

Given next field in a topic.

<member name="exerciseNickname"          type="string" arrayDimensions="4" stringMaxLength="20"/>

With the usual dynamicdata you have set some array types to set as double, byte, float, etc but there's no set_string_array.

Notice in my case i don't use idl file i use a file .qos to describe every topic instead.

How can i set the array of string?

Keywords:
Gerardo Pardo's picture
Offline
Last seen: 3 weeks 5 hours ago
Joined: 06/02/2010
Posts: 602

Hello,

You need to use the DynamicData::bind_complex_member operation to first bind your member. I will elaborate below.

A structure can contain members of different types (longs, strings, arrays, sequences, other structures). If a member is of a simple type, e.g. a primitive like a long, float, or even sequences of primitive types, then you can use the "set_xxx" operations directly on the structure to set the member.  However more complex members, like members that themselves have a structured type or arrays/sequences of non primitive types require the use of the  DynamicData::bind_complex_member() to bind another DynamicData object to the inner member. This can be done recursively until you get to a member of a type where you can use the  aforementioned "set_xxx" operations.

Assuming the data-type described by this XML:

<types>
    <struct name="EmbeddedStringArrayType">
        <member name="array_of_string_member" stringMaxLength="20" type="string" arrayDimensions="4"/>   
    </struct> 
</types>

You can use the following C++ code to set the elements of array of strings inside:

        // This code assumes embeddedStringArray points to a DDS_DynamicData containing
        // a data of type EmbeddedStringArrayType
        DDS_DynamicData *embeddedStringArray = ... 
   
        // Define a DynamicData to hold a reference to the member inside embeddedStringArray
        DDS_DynamicDataProperty_t dd_property;
        DDS_DynamicData array_of_string(NULL, dd_property);

        /* Bind array_of_string to the member called "array_of_string_member" */
        retcode = embeddedStringArray->bind_complex_member(array_of_string, "array_of_string_member",
                                                 DDS_DYNAMIC_DATA_MEMBER_ID_UNSPECIFIED);
        if (retcode != DDS_RETCODE_OK) {
              printf("bind_complex_member (array_of_string) error %d\n", retcode);
              return;
        }

        retcode = array_of_string.set_string(NULL, 1, "Hello 1");
        retcode = array_of_string.set_string(NULL, 2, "Hello 2");
        retcode = array_of_string.set_string(NULL, 3, "Hello 3");
        retcode = array_of_string.set_string(NULL, 4, "Hello 4");
        if (retcode != DDS_RETCODE_OK) {
            printf("set_string error %d\n", retcode);
            return;
        }
        embeddedStringArray->unbind_complex_member(array_of_string);

I also uploaded an example that uses this API to the File Exchange it can be found at this link dyndata_string_seq.zip 

Gerardo

Offline
Last seen: 11 years 6 months ago
Joined: 05/06/2013
Posts: 2

Thanks!!

That works fine!!