RTI Connext Modern C++ API Version 7.3.0
dds::core::xtypes::DynamicData Class Reference

<<value-type>> A data sample of any complex data type, which can be inspected and manipulated reflectively. More...

#include <dds/core/xtypes/DynamicData.hpp>

Public Member Functions

 DynamicData (const dds::core::xtypes::DynamicType &type)
 Creates a DynamicData instance for a type. More...
 
 DynamicData (const dds::core::xtypes::DynamicType &type, const DynamicDataProperty &property)
 Creates a DynamicData instance for a type with specific memory-management. More...
 
template<typename T >
void value (const std::string &name, const T &v)
 Sets the value of a member by member name. More...
 
template<typename T >
void value (uint32_t index, const T &v)
 Sets the value of a member by member index. More...
 
template<typename T >
value (const std::string &name) const
 Gets the value of a member by member name. More...
 
template<typename T >
value (uint32_t index) const
 Gets the value of a member by member index. More...
 
template<typename T >
void get_values (const std::string &name, std::vector< T > &out_array) const
 Obtains the values of an array or sequence member by member name. More...
 
template<typename T >
void get_values (uint32_t index, std::vector< T > &out_array) const
 Obtains the values of an array or sequence member by member index. More...
 
template<typename T >
std::vector< T > get_values (uint32_t index) const
 Obtains the values of an array or sequence member by member index. More...
 
template<typename T >
std::vector< T > get_values (const std::string &name) const
 Obtains the values of an array or sequence member by member name. More...
 
template<typename T >
void set_values (uint32_t index, const std::vector< T > &v)
 Sets the values of an array or sequence member by member index. More...
 
template<typename T >
void set_values (const std::string &name, const std::vector< T > &v)
 Sets the values of an array or sequence member by member name. More...
 
LoanedDynamicData loan_value (const std::string &name)
 Gets a view of a complex member. More...
 
LoanedDynamicData loan_value (uint32_t index)
 Gets a view of a complex member. More...
 
LoanedDynamicData & loan_value (LoanedDynamicData &data, const std::string &name)
 Gets a view of a complex member. More...
 
LoanedDynamicData & loan_value (LoanedDynamicData &data, uint32_t mid)
 Gets a view of a complex member. More...
 
uint32_t discriminator_value () const
 Obtains the value of the union discriminator (UnionType only) More...
 
void clear_all_members ()
 Clear the contents of all data members of this object, including key members. More...
 
void clear_optional_member (const std::string &name)
 Clear the contents of a single optional data member of this object. More...
 
void clear_optional_member (uint32_t index)
 Clear the contents of a single optional data member of this object. More...
 
void clear_member (const std::string &name)
 Clear the contents of a single data member of this object. More...
 
void clear_member (uint32_t index)
 Clear the contents of a single data member of this object. More...
 
bool is_cdr () const
 Determines if this DynamicData sample is represented as CDR. More...
 
std::pair< const char *, uint32_t > get_cdr_buffer () const
 Gets the CDR buffer of this DynamicData sample. More...
 
void set_cdr_buffer (const char *buffer, uint32_t length)
 Sets the CDR buffer of this DynamicData sample. More...
 
const dds::core::xtypes::DynamicTypetype () const
 Gets the data type of this DynamicData. More...
 
dds::core::xtypes::TypeKind type_kind () const
 Gets the data type kind of this DynamicData. More...
 
uint32_t member_count () const
 Get the number of members in this sample. More...
 
bool member_exists (const std::string &name) const
 Indicates whether a member exists in this sample. More...
 
bool member_exists (uint32_t index) const
 Indicates whether a member exists in this sample. More...
 
bool member_exists_in_type (const std::string &name) const
 Indicates whether a member with a particular name exists in type() More...
 
bool member_exists_in_type (uint32_t index) const
 Indicates whether a member of a particular index exists in type() More...
 
DynamicDataInfo info () const
 Returns information about this sample. More...
 
rti::core::xtypes::DynamicDataMemberInfo member_info (const std::string &name) const
 Returns information about a member. More...
 
rti::core::xtypes::DynamicDataMemberInfo member_info (uint32_t index) const
 Returns information about a member. More...
 
uint32_t member_index (const std::string &name) const
 Translates from member name to member index. More...
 
bool is_member_key (const std::string &name) const
 Returns whether a member is a key. More...
 
bool is_member_key (uint32_t index) const
 Returns whether a member is a key. More...
 
dds::core::xtypes::DynamicType member_type (const std::string &name) const
 Returns the type of a member. More...
 
dds::core::xtypes::DynamicType member_type (uint32_t index) const
 Returns the type of a member. More...
 
void from_string (RTI_STRING_IN str, rti::topic::PrintFormatKind format_kind=rti::topic::PrintFormatKind::JSON)
 Populates this DynamicData object from a JSON string representation. More...
 

Related Functions

(Note that these are not member functions.)

std::vector< char > & to_cdr_buffer (std::vector< char > &buffer, const DynamicData &sample, dds::core::policy::DataRepresentationId representation_id=dds::core::policy::DataRepresentation::auto_id())
 <<extension>> Serializes a DynamicData sample into CDR format More...
 
DynamicDatafrom_cdr_buffer (DynamicData &sample, const std::vector< char > &buffer)
 <<extension>> Creates a DynamicData sample by deserializing a CDR buffer' More...
 
template<typename TopicType >
TopicType convert (const DynamicData &sample)
 <<extension>> Creates a typed sample from a DynamicData sample. More...
 
template<typename TopicType >
DynamicData convert (const TopicType &sample)
 <<extension>> Creates a DynamicData sample from a typed sample More...
 
template<typename TopicType >
bool can_convert (const DynamicData &sample)
 <<extension>> Determines if the DynamicType of this sample is equal to the DynamicType of TopicType More...
 
template<typename... Types>
std::tuple< Types... > get_tuple (const dds::core::xtypes::DynamicData &data)
 <<C++11>> <<experimental>> <<extension>> Retrieves all the values of a DynamicData object into an std::tuple at once More...
 
template<typename... Types>
void set_tuple (dds::core::xtypes::DynamicData &data, const std::tuple< Types... > &value)
 <<C++11>> <<experimental>> <<extension>> Assigns the values of a std::tuple to a DynamicData object at once More...
 

Detailed Description

<<value-type>> A data sample of any complex data type, which can be inspected and manipulated reflectively.

DynamicData allows manipulating a data sample whose type is unknown at compilation time. A DynamicData object is created by passing its DynamicType, which can be a StructType, a UnionType, ArrayType, SequenceType or AliasType. DynamicData is a valid topic-type, so you can instantiate a Topic<DynamicData> (using the constructor that receives a DynamicType), and then a DataReader<DynamicData> or a DataWriter<DynamicData>.

Several overloaded versions of the function value() allow reading or writing the value of a member by name or index. Another set of functions, loan_value(), can access a non-primitive member (for reading and writing) without making a copy. For primitive arrays and sequences DynamicData also provides a way to get or set all the values at once, using set_values() and get_values().

A number of other functions allow querying information about the sample and its type.

Applications that use both statically typed data and DynamicData can benefit from the conversion between them, using rti::core::xtypes::convert().

For examples on how to use DynamicData, see Using DynamicData.

Member Names and Indexes

As mentioned before, to access a member you need to specify its name or its index. Depending on the DynamicType of the DynamicData sample, the name may apply or not and the index can mean different things. This table summarizes that:

DynamicType of the sample Can access by name? What does index mean?
StructType Yes The position of the member in the type (starting at 1)
UnionType Yes The value of the label that selects a member, see discriminator_value()
ArrayType No The position of the element in the array (1 to ArrayType::total_element_count())
SequenceType No The position of the element in the sequence (1 to member_count()) or past member_count() to increase the length of the sequence.
Any other type N/A N/A

For example, given the following IDL description of a type:

struct Foo {
long x;
long y;
};
An example topic-type.
Definition: types.dxx:34

If we have a DynamicData sample representing data of that type, we can access the member y in two different ways:

sample.value("y", 33); // Set value to 33
sample.value(2, 33); // Same effect

Accessing a member by index can be more efficient, so when accessing a member repeatedly it may be beneficial to obtain its index first:

const uint32_t y_index = sample.member_index("y");
while (...) {
int32_t y_value = sample.value<int32_t>(y_index);
// ...
}

For an example on how to access a union member, see discriminator_value(), and for more examples, including how to access elements in a collection see Using DynamicData.

Hierarchical Member Names

It is possible to refer to a nested member in a type without first having to use the loan_value() API. You can do this by using a hierarchical name. A hierarchical member name is a concatenation of member names separated by the '.' character. The hierarchical name describes the complete path from a top-level type to the nested member. For example, in the following type:

struct MyNestedType {
char theChar;
octet theOctetArray[10];
long long theMultidimensionalArray[4][6][12];
sequence<long> myArrayOfSeq[8];
};
struct MyType {
MyNestedType theNestedType;
};

any DynamicData API that receives a member name will accept "theNestedType.theChar" to refer to the char member in MyNestedType:

char my_char = myDynamicData.value<char>("theNestedType.theChar");

In order to access the value of theChar without using a hierarchical name, you would have to first bind to theNestedType and then get the value:

auto member = myDynamicData.loan_value("theNestedType");
char my_char = member.get().value<char>("theChar");

As you can see, using a hierarchical member name removes the need to call the loan_value() API, and allows for access to nested members at any depth directly from the top-level type.

The member name can also contain indexes to address members in arrays and sequences. For example, to set the third member in the array theOctetArray, you can pass in "theNestedType.theOctetArray[2]" as the member name. The index values when used as part of the member name are 0-based.

For multi-dimensional arrays, the indexes for each dimension should be listed comma-separated in between brackets. For example, to address a member of theMultidimensionalArray, the member name should be something like "theNestedType.theMultidimensionalArray[3,2,5]".

In complex types with arrays and sequences that contain other arrays and sequences, the hierarchical name may include multiple index values, one right after another. For example, in MyNestedType, myArrayOfSeq is an array of sequences. In order to set the third member of the sequence in the fourth member of the array, the member name would be "myNestedType.myArrayOfSeq[3][2]".

Exceptions accessing a member

The different DynamicData functions that access members may throw the following exceptions:

  • dds::core::InvalidArgumentError when attempting to get the value a member that doesn't exist in the type, is optional and is not set, or is part of a union and not currently selected (see discriminator_value). Attempting to set an unset optional member or an unselected union member is legal and will set the optional member or change the active union member.
  • dds::core::IllegalOperationError when attempting to get or set a member of the wrong type–that is, when the DynamicType of that member doesn't match the template type T of the function. For example assumming that my_string_member is a string, the two following statements are illegal:
    data.value("my_string_member", 3);
    int v = data.value<int>("my_string_member");

Performance Considerations

By default, a DynamicData object stores its content in an implementation-specific representation that allows getting and setting its fields by name or ID.

When a sample is received, the CDR-formatted data is deserialized into this buffer.

When a sample is published, the data is serialized into CDR from the buffer and sent to the network.

However, there are scenarios where DynamicData is used for purposes that don't involve manipulating the object's content directly. Two common examples are:

  • Recording/Replaying: Storing samples in persistent storage for later analysis or replay.
  • Bridging: Transferring samples from one domain to another by transmitting samples received with a DynamicDataReader to another domain using a DynamicDataWriter.

RTI provides dedicated services, namely RTI Recording Service and RTI Routing Service, to address these use cases. However, in certain situations, you might be unable to utilize these services.

For scenarios where inspecting or modifying the content of DynamicData fields is unnecessary, DynamicData offers a high-performance method to configure a DataReader to avoid deserializing incoming samples into the internal representation. Additionally, the DynamicData API provides a method to link a CDR-formatted serialization buffer directly with a DynamicData object, facilitating direct transmission without converting the data from the internal representation to CDR.

To skip the deserialization of a DynamicData object on the DataReader side, you can set the field DynamicDataTypeSerializationProperty_t::skip_deserialization to true.

Once a sample is received, you can access the serialized buffer directly by using the dds::core::xtypes::DynamicData::get_cdr_buffer. This buffer can be stored in persistent storage in a Recording/Replaying use case.

To link a serialization buffer directly with a DynamicData object, you can use the dds::core::xtypes::DynamicData::set_cdr_buffer method. This method allows you to set the buffer that will be used to serialize the DynamicData object when it is published.

See also
Using DynamicData

Constructor & Destructor Documentation

◆ DynamicData() [1/2]

dds::core::xtypes::DynamicData::DynamicData ( const dds::core::xtypes::DynamicType type)

Creates a DynamicData instance for a type.

Uses default DynamicDataProperty.

Precondition
!is_primitive(type) – a DynamicData object cannot encapsulate a primitive type.
Parameters
typeThe type of this data sample

◆ DynamicData() [2/2]

dds::core::xtypes::DynamicData::DynamicData ( const dds::core::xtypes::DynamicType type,
const DynamicDataProperty &  property 
)

Creates a DynamicData instance for a type with specific memory-management.

Precondition
!is_primitive(type) – a DynamicData object cannot encapsulate a primitive type.
Parameters
typeThe type of this data sample
propertyMemory-management configuration

Member Function Documentation

◆ value() [1/4]

template<typename T >
void dds::core::xtypes::DynamicData::value ( const std::string &  name,
const T &  v 
)

Sets the value of a member by member name.

Template Parameters
TThe type of the member to set. The possible types are:
Note
If the member type is a collection type, use loan_value for collections of complex types or set_values for collections of primitive types.

For examples on how to set member values, see Publishing data using DynamicData.

If the member is optional and unset, this function will set it. To unset it again, see clear_optional_member().

It is not always possible to rely on inferring the type T based on the argument v. For example, if the member being set is a short, this statement will throw an exception, because T is deduced as int:

sample.value("my_short", 10);

There are two ways to specify that the member is a short:

sample.value<short>("my_short", 10);
sample.value("my_short", (short) 10);
Parameters
nameThe member name, see Member Names and Indexes. This function also allows to access nested members
vThe value to set for that member
Exceptions
SeeExceptions accessing a member
See also
member_exists(const std::string&) const
Publishing data using DynamicData

◆ value() [2/4]

template<typename T >
void dds::core::xtypes::DynamicData::value ( uint32_t  index,
const T &  v 
)

Sets the value of a member by member index.

Parameters
indexThe position of the member in the type, see Member Names and Indexes.
vThe value to set for that member
Exceptions
SeeExceptions accessing a member
See also
value(const std::string&, const T&)
Member Names and Indexes

◆ value() [3/4]

template<typename T >
T dds::core::xtypes::DynamicData::value ( const std::string &  name) const

Gets the value of a member by member name.

Template Parameters
TThe type of the member. See value(const std::string&, const T&)

For examples on how to get member values, see Subscribing to data using DynamicData.

Parameters
nameThe member name, see Member Names and Indexes.
Returns
The value of that member
Exceptions
SeeExceptions accessing a member
See also
value(const std::string&, const T&)
Subscribing to data using DynamicData

◆ value() [4/4]

template<typename T >
T dds::core::xtypes::DynamicData::value ( uint32_t  index) const

Gets the value of a member by member index.

Template Parameters
TThe type of the member. See value(const std::string&, const T&)

For examples on how to get member values, see Subscribing to data using DynamicData.

Parameters
indexThe position of the member in the type, see Member Names and Indexes.
Returns
The value of that member
Exceptions
SeeExceptions accessing a member
See also
value(const std::string&, const T&)
Member Names and Indexes

◆ get_values() [1/4]

template<typename T >
void dds::core::xtypes::DynamicData::get_values ( const std::string &  name,
std::vector< T > &  out_array 
) const

Obtains the values of an array or sequence member by member name.

This function reuses an existing vector

Template Parameters
TThe element type of the array or sequence. These are the possible types:
  • The primitive types char, (u)int8_t, (u)int16_t, DDS_Long, DDS_UnsignedLong, DDS_LongLong, DDS_UnsignedLongLong, float, double. The types int and unsigned int may be used instead of DDS_Long and DDS_UnsignedLong except in some platforms (VxWorks).
  • If the element type is an enumeration, use int32_t.
  • If the element type is bool, use uint8_t.
Parameters
nameThe member name, see Member Names and Indexes.
out_arrayA vector containing the values of that array or sequence. The vector will be resized accordingly.

◆ get_values() [2/4]

template<typename T >
void dds::core::xtypes::DynamicData::get_values ( uint32_t  index,
std::vector< T > &  out_array 
) const

Obtains the values of an array or sequence member by member index.

This function reuses an existing vector

Template Parameters
TThe element type of the array or sequence. (See get_values(const std::string&, std::vector<T>&) const)
Parameters
indexThe position of the array or sequence member in the type, see Member Names and Indexes.
out_arrayA vector containing the values of that array or sequence. The vector will be resized accordingly.
See also
Member Names and Indexes
get_values(const std::string& name, std::vector<T>& out_array) const

◆ get_values() [3/4]

template<typename T >
std::vector< T > dds::core::xtypes::DynamicData::get_values ( uint32_t  index) const

Obtains the values of an array or sequence member by member index.

This function returns a new vector.

Template Parameters
TThe element type of the array or sequence. (See get_values(const std::string&, std::vector<T>&) const)
Parameters
indexThe position of the array or sequence member in the type
Returns
A vector containing the values of that array or sequence.
See also
Member Names and Indexes
get_values(const std::string& name, std::vector<T>& out_array) const

◆ get_values() [4/4]

template<typename T >
std::vector< T > dds::core::xtypes::DynamicData::get_values ( const std::string &  name) const

Obtains the values of an array or sequence member by member name.

This function returns a new vector.

Template Parameters
TThe element type of the array or sequence. (See get_values(const std::string&, std::vector<T>&) const)
Parameters
nameThe member name, see Member Names and Indexes.
Returns
A vector containing the values of that array or sequence.
See also
get_values(const std::string& name, std::vector<T>& out_array) const

◆ set_values() [1/2]

template<typename T >
void dds::core::xtypes::DynamicData::set_values ( uint32_t  index,
const std::vector< T > &  v 
)

Sets the values of an array or sequence member by member index.

Template Parameters
TThe element type of the array or sequence. (See get_values(const std::string&, std::vector<T>&) const)
Parameters
indexThe position of the array or sequence member in the type
vA vector containing the values to be set for that array or sequence.
See also
Member Names and Indexes
get_values(const std::string& name, std::vector<T>& out_array) const

◆ set_values() [2/2]

template<typename T >
void dds::core::xtypes::DynamicData::set_values ( const std::string &  name,
const std::vector< T > &  v 
)

Sets the values of an array or sequence member by member name.

Template Parameters
TThe element type of the array or sequence. (See get_values(const std::string&, std::vector<T>&) const)
Parameters
nameThe member name, see Member Names and Indexes.
vA vector containing the values to be set for that array or sequence.
See also
get_values(const std::string& name, std::vector<T>& out_array) const

◆ loan_value() [1/4]

LoanedDynamicData dds::core::xtypes::DynamicData::loan_value ( const std::string &  name)

Gets a view of a complex member.

This function returns a new LoanedDynamicData instance that provides a view of a complex member (a struct or a union).

Only a single member of a given DynamicData object may be loaned at a time. However, members of members may be recursively loaned to any depth. Furthermore, while the outer object has a bound member, it may only be modified through that bound member. That is, after loaning this member, the outer object disables all calls to set value() until the loan is returned.

This method, as opposed to value<DynamicData>() doesn't make a copy; it provides a view of the member so changes to the loaned member change the outer object.

Parameters
nameThe member name, see Member Names and Indexes.
Returns
The object to access the complex member
Exceptions
Oneof the Standard Exceptions
See also
rti::core::xtypes::LoanedDynamicData
Using DynamicData.

◆ loan_value() [2/4]

LoanedDynamicData dds::core::xtypes::DynamicData::loan_value ( uint32_t  index)

Gets a view of a complex member.

This function returns a new LoanedDynamicData instance that provides a view of a complex member (a struct or a union).

Parameters
indexThe position of the member in the type, see Member Names and Indexes.
Returns
The object to access the complex member
Exceptions
Oneof the Standard Exceptions
See also
loan_value(const std::string&)
rti::core::xtypes::LoanedDynamicData
Member Names and Indexes

◆ loan_value() [3/4]

LoanedDynamicData & dds::core::xtypes::DynamicData::loan_value ( LoanedDynamicData &  data,
const std::string &  name 
)

Gets a view of a complex member.

This function reuses a LoanedDynamicData instance, returning its previous loan.

Parameters
dataThe object to access the complex member
nameThe member name, see Member Names and Indexes.
See also
loan_value(const std::string&)
rti::core::xtypes::LoanedDynamicData

◆ loan_value() [4/4]

LoanedDynamicData & dds::core::xtypes::DynamicData::loan_value ( LoanedDynamicData &  data,
uint32_t  mid 
)

Gets a view of a complex member.

This function reuses a LoanedDynamicData instance, returning its previous loan.

Parameters
dataThe object to access the complex member
midThe position of the member in the type, see Member Names and Indexes.
See also
loan_value(const std::string&)
rti::core::xtypes::LoanedDynamicData

◆ discriminator_value()

uint32_t dds::core::xtypes::DynamicData::discriminator_value ( ) const

Obtains the value of the union discriminator (UnionType only)

Use the value that this function returns in any function expecting a member index to access the active member in this union.

For example, given the following IDL Union:

union Foo switch (short) {
case 1:
long x;
case 2:
case 3:
string y;
};

If we have a DynamicData sample of that type whose active member is "y", we can access it as follows:

std::string y_value = sample.value<std::string>(sample.discriminator_value());
y_value = sample.value<std::string>("y"); // Same effect
y_value = sample.value<std::string>(3); // Same effect
basic_string< char, rti::core::memory::OsapiAllocator< char > > string
A string convertible to std::string and with similar functionality.
Definition: String.hpp:266

If you set a different member, the discriminator value automatically changes:

sample.value("x", 10);
assert (sample.discriminator_value() == 1);
Precondition
type().kind() == TypeKind::UNION_TYPE – Only applies to samples of a UnionType.
Returns
The value of this sample's discriminator.

◆ clear_all_members()

void dds::core::xtypes::DynamicData::clear_all_members ( )

Clear the contents of all data members of this object, including key members.

MT Safety:
UNSAFE.
Exceptions
Oneof the Standard Exceptions

This API is not supported when the DynamicData object is in CDR format and will fail with dds::core::PreconditionNotMetError. See dds::core::xtypes::DynamicData::is_cdr for more information.

See also
dds::core::xtypes::DynamicData::clear_optional_member

◆ clear_optional_member() [1/2]

void dds::core::xtypes::DynamicData::clear_optional_member ( const std::string &  name)

Clear the contents of a single optional data member of this object.

Parameters
nameThe member name, see Member Names and Indexes.

◆ clear_optional_member() [2/2]

void dds::core::xtypes::DynamicData::clear_optional_member ( uint32_t  index)

Clear the contents of a single optional data member of this object.

Parameters
indexThe position of the member in the type, see Member Names and Indexes.

◆ clear_member() [1/2]

void dds::core::xtypes::DynamicData::clear_member ( const std::string &  name)

Clear the contents of a single data member of this object.

Parameters
nameThe member name, see Member Names and Indexes.

◆ clear_member() [2/2]

void dds::core::xtypes::DynamicData::clear_member ( uint32_t  index)

Clear the contents of a single data member of this object.

Parameters
indexThe position of the member in the type, see Member Names and Indexes.

◆ is_cdr()

bool dds::core::xtypes::DynamicData::is_cdr ( ) const

Determines if this DynamicData sample is represented as CDR.

This method returns true if the DynamicData object is using CDR as its data representation, and false otherwise.

When the DynamicData object is using CDR as its data representation, many of the DynamicData APIs cannot be used. The DynamicData object will use CDR as its data representation when DynamicDataTypeSerializationProperty_t::skip_deserialization has been set to true or dds::core::xtypes::DynamicData::set_cdr_buffer has been used to associate a CDR buffer with the DynamicData object. See each functions's documentation for whether or not the function is supported for DynamicData objects in CDR format.

See also
dds::core::xtypes::DynamicData::get_cdr_buffer
dds::core::xtypes::DynamicData::set_cdr_buffer
DynamicDataTypeSerializationProperty_t::skip_deserialization

◆ get_cdr_buffer()

std::pair< const char *, uint32_t > dds::core::xtypes::DynamicData::get_cdr_buffer ( ) const

Gets the CDR buffer of this DynamicData sample.

This method returns the CDR buffer associated with a DynamicData object.

If the DynamicData object is not using CDR as its data representation, this method will throw an exception.

To configure a DynamicData DataReader to not deserialize the incoming samples and keep the data in CDR format, you must set the configuration option DynamicDataTypeSerializationProperty_t::skip_deserialization to true.

MT Safety:
UNSAFE.
Parameters
length<<out>> CDR buffer length.
Exceptions
Oneof the Standard Exceptions
Returns
The buffer

◆ set_cdr_buffer()

void dds::core::xtypes::DynamicData::set_cdr_buffer ( const char *  buffer,
uint32_t  length 
)

Sets the CDR buffer of this DynamicData sample.

Parameters
bufferThe buffer to set
lengthThe size of the buffer

◆ type()

const dds::core::xtypes::DynamicType & dds::core::xtypes::DynamicData::type ( ) const

Gets the data type of this DynamicData.

◆ type_kind()

dds::core::xtypes::TypeKind dds::core::xtypes::DynamicData::type_kind ( ) const

Gets the data type kind of this DynamicData.

Returns
this->type().kind().

◆ member_count()

uint32_t dds::core::xtypes::DynamicData::member_count ( ) const

Get the number of members in this sample.

For objects of type ArrayType or SequenceType, this method returns the number of elements in the collection.

For objects of type kind StructType, it returns the number of fields in the sample, which will always be the same as the number of fields in the type.

◆ member_exists() [1/2]

bool dds::core::xtypes::DynamicData::member_exists ( const std::string &  name) const

Indicates whether a member exists in this sample.

If the member doesn't exist in the type, this function returns false. In all other cases, it provides the same result as rti::core::xtypes::DynamicDataMemberInfo::member_exists(), which is retrieved with member_info()).

Parameters
nameThe member name, see Member Names and Indexes.

◆ member_exists() [2/2]

bool dds::core::xtypes::DynamicData::member_exists ( uint32_t  index) const

Indicates whether a member exists in this sample.

If the member doesn't exist in the type, this function returns false. In all other cases, it provides the same result as rti::core::xtypes::DynamicDataMemberInfo::member_exists(), which is retrieved with member_info()).

Parameters
indexThe member index, see Member Names and Indexes.

◆ member_exists_in_type() [1/2]

bool dds::core::xtypes::DynamicData::member_exists_in_type ( const std::string &  name) const

Indicates whether a member with a particular name exists in type()

◆ member_exists_in_type() [2/2]

bool dds::core::xtypes::DynamicData::member_exists_in_type ( uint32_t  index) const

Indicates whether a member of a particular index exists in type()

◆ info()

DynamicDataInfo dds::core::xtypes::DynamicData::info ( ) const

Returns information about this sample.

◆ member_info() [1/2]

rti::core::xtypes::DynamicDataMemberInfo dds::core::xtypes::DynamicData::member_info ( const std::string &  name) const

Returns information about a member.

Precondition
this->type_kind() must be dds::core::xtypes::TypeKind_def::ARRAY_TYPE, dds::core::xtypes::TypeKind_def::SEQUENCE_TYPE, dds::core::xtypes::TypeKind_def::STRUCTURE_TYPE, or dds::core::xtypes::TypeKind_def::UNION_TYPE

When name identifies a field in a struct or union:

When name identifies a sequence element, such as "my_sequence[i]":

When name identifies an array element, such as "my_array[i], this function either throws when the i is out of bounds or else returns an object with rti::core::xtypes::DynamicDataMemberInfo::member_exists() set to true.

Parameters
nameThe member name, see Member Names and Indexes.
Returns
Information about the member.
Exceptions
dds::core::InvalidArgumentErrorif the member doesn't exist in the type, or Standard Exceptions.

◆ member_info() [2/2]

rti::core::xtypes::DynamicDataMemberInfo dds::core::xtypes::DynamicData::member_info ( uint32_t  index) const

Returns information about a member.

Precondition
this->type_kind() must be dds::core::xtypes::TypeKind_def::ARRAY_TYPE, dds::core::xtypes::TypeKind_def::SEQUENCE_TYPE, or dds::core::xtypes::TypeKind_def::STRUCTURE_TYPE, dds::core::xtypes::TypeKind_def::UNION_TYPE

When this sample represents a struct and index is the position of the member in the type definition, or when the sample represents a union and index is the discriminator value:

When this sample represents a sequence and index is the 1-based element index:

When this sample represents an array this function either throws dds::core::InvalidArgumentError when the index is out of bounds or else returns an object with rti::core::xtypes::DynamicDataMemberInfo::member_exists() set to true.

Parameters
indexThe member index, see Member Names and Indexes.
Returns
DynamicDataMemberInfo Information about the member.
Exceptions
dds::core::InvalidArgumentErrorif the member doesn't exist in the type or the element with this index doesn't exist in this sequence. See also Standard Exceptions.

◆ member_index()

uint32_t dds::core::xtypes::DynamicData::member_index ( const std::string &  name) const

Translates from member name to member index.

Parameters
nameThe member name, see Member Names and Indexes.
Returns
The member index for the member with that name
Exceptions
SeeExceptions accessing a member

◆ is_member_key() [1/2]

bool dds::core::xtypes::DynamicData::is_member_key ( const std::string &  name) const

Returns whether a member is a key.

Parameters
nameThe member name, see Member Names and Indexes.
Exceptions
SeeExceptions accessing a member

◆ is_member_key() [2/2]

bool dds::core::xtypes::DynamicData::is_member_key ( uint32_t  index) const

Returns whether a member is a key.

Parameters
indexThe member index, see Member Names and Indexes.
Exceptions
SeeExceptions accessing a member

◆ member_type() [1/2]

dds::core::xtypes::DynamicType dds::core::xtypes::DynamicData::member_type ( const std::string &  name) const

Returns the type of a member.

◆ member_type() [2/2]

dds::core::xtypes::DynamicType dds::core::xtypes::DynamicData::member_type ( uint32_t  index) const

Returns the type of a member.

◆ from_string()

void dds::core::xtypes::DynamicData::from_string ( RTI_STRING_IN  str,
rti::topic::PrintFormatKind  format_kind = rti::topic::PrintFormatKind::JSON 
)
inline

Populates this DynamicData object from a JSON string representation.

This method takes a JSON string representing a data sample of this type, and uses it to populate this DynamicData.

Parameters
str<<in>>. The JSON string representation
format_kind<<in>>. Must be set to PrintFormatKind::JSON_PRINT_FORMAT, the only currently supported format.
Exceptions
Oneof the Standard Exceptions, dds::core::OutOfResourcesError

Referenced by rti::topic::from_string().

Friends And Related Function Documentation

◆ to_cdr_buffer()

std::vector< char > & to_cdr_buffer ( std::vector< char > &  buffer,
const DynamicData sample,
dds::core::policy::DataRepresentationId  representation_id = dds::core::policy::DataRepresentation::auto_id() 
)
related

<<extension>> Serializes a DynamicData sample into CDR format

Note
This is a standalone function in the namespace rti::core::xtypes
Parameters
bufferThe output buffer, which will be resized to the correct size
sampleThe sample to serialize
representation_idThe data representation used to serialize the data
Returns
A reference to buffer
Examples
Foo.hpp.

◆ from_cdr_buffer()

DynamicData & from_cdr_buffer ( DynamicData sample,
const std::vector< char > &  buffer 
)
related

<<extension>> Creates a DynamicData sample by deserializing a CDR buffer'

Note
This is a standalone function in the namespace rti::core::xtypes
Parameters
sampleThe sample to deserialize.
bufferThe buffer containing the CDR data
Returns
A reference to sample
Examples
Foo.hpp.

◆ convert() [1/2]

template<typename TopicType >
TopicType convert ( const DynamicData sample)
related

<<extension>> Creates a typed sample from a DynamicData sample.

Note
This is a standalone function in the namespace rti::core::xtypes

This function is useful when an application uses DynamicData and regular typed Topics at the same time.

For example, given this IDL type:

struct Foo {
long x;
};

We can convert from Foo and DynamicData:

// Receive a DynamicData sample
assert (samples.size() > 0);
// Create a Foo sample
const DynamicData& dynamic_data = samples[0].data();
assert (rti::core::xtypes::can_convert<Foo>(dynamic_data));
Foo typed_data = rti::core::xtypes::convert<Foo>(dynamic_data);
assert (typed_data.x() == dynamic_data.value<int32_t>("x"));
<<value-type>> A data sample of any complex data type, which can be inspected and manipulated reflect...
Definition: DynamicDataImpl.hpp:250
DynamicData(const dds::core::xtypes::DynamicType &type)
Creates a DynamicData instance for a type.
<<reference-type>> Allows the application to: (1) declare the data it wishes to receive (i....
Definition: TDataReader.hpp:74
LoanedSamples< T > take()
Take all samples using the default filter state.
Definition: TDataReader.hpp:1160
<<move-only-type>> Provides temporary access to a collection of samples (data and info) from a DataRe...
Definition: LoanedSamplesImpl.hpp:77
Precondition
can_convert<TopicType>(sample), otherwise the behavior is undefined.
Template Parameters
TopicTypeAn IDL topic-type
Parameters
sampleThe DynamicData sample to convert to type TopicType
Returns
An instance of TopicType containing the data in sample.

◆ convert() [2/2]

template<typename TopicType >
DynamicData convert ( const TopicType &  sample)
related

<<extension>> Creates a DynamicData sample from a typed sample

Note
This is a standalone function in the namespace rti::core::xtypes
Template Parameters
TopicTypeAn IDL topic-type
Parameters
sampleThe typed sample to convert to DynamicData
Returns
A new DynamicData object whose type is rti::topic::dynamic_type<TopicType> and contains the data from sample

◆ can_convert()

template<typename TopicType >
bool can_convert ( const DynamicData sample)
related

<<extension>> Determines if the DynamicType of this sample is equal to the DynamicType of TopicType

Note
This is a standalone function in the namespace rti::core::xtypes
Template Parameters
TopicTypeAn IDL topic-type
Returns
True if the DynamicType of TopicType is the same as this sample's and therefore it is possible to create an instance of TopicType from this DynamicData object.
See also
convert()

◆ get_tuple()

template<typename... Types>
std::tuple< Types... > get_tuple ( const dds::core::xtypes::DynamicData data)
related

<<C++11>> <<experimental>> <<extension>> Retrieves all the values of a DynamicData object into an std::tuple at once

Note
This is a standalone function in the namespace rti::core::xtypes
See also
Manipulating data reflectively using C++ tuples

◆ set_tuple()

template<typename... Types>
void set_tuple ( dds::core::xtypes::DynamicData data,
const std::tuple< Types... > &  value 
)
related

<<C++11>> <<experimental>> <<extension>> Assigns the values of a std::tuple to a DynamicData object at once

Note
This is a standalone function in the namespace rti::core::xtypes
See also
Manipulating data reflectively using C++ tuples