3.4 Ranges

The annotations @range, @min, and @max can be used to restrict the possible values for a primitive member. For example:

struct Position {
    @range(min = 0, max = 200) int32 x;
    @min(50) @default(70) int32 y;
    @max(200) @default(80) int32 z;
};

The annotations are enforced at serialization/deserialization time, not when the value of an object is set. For example, assume the following Position: {x= -3, y= 60, z = 150}. If you try to publish a Position sample with this value, the DataWriter::write operation will fail with a DDS_RETCODE_ERROR. If a DataReader receives this sample, the sample will be lost with the reason DDS_LOST_BY_DESERIALIZATION_FAILURE and it will not be provided to the application. In both cases, you will see a log message indicating that x was outside its valid range.

The range annotations can be applied to the following types: octet, int8, uint8, int16, uint16, int32, uint32, int64, uint64, float, double. These annotations are not supported in long double.

If you specify a @default value for a member that is outside the valid range, the code generation will fail. For example:

struct Position {
    @default(300) @range(min = 0, max = 200) int32 x; // Failure. Default outside valid range
    @min(50) @default(70) int32 y;
    @max(200) @default(80) int32 z;
};

3.4.1 Restrictions

  • For performance reasons, the range annotations are not currently applied to samples of types marked with @language_binding(FLAT_DATA). The annotations can be used for the type members, but they are only informational.
  • The range annotations are not currently supported for sequences or arrays. Using a typedef with the range annotations to work around this restriction is also not supported.
  • The TypeCode API (DynamicType API in Modern C++) does not provide a public API to obtain the value of the @range, @min, and @max annotations.