diff --git a/src/core/ddsc/include/dds/ddsc/dds_public_impl.h b/src/core/ddsc/include/dds/ddsc/dds_public_impl.h index 730020c..46bfcbe 100644 --- a/src/core/ddsc/include/dds/ddsc/dds_public_impl.h +++ b/src/core/ddsc/include/dds/ddsc/dds_public_impl.h @@ -225,6 +225,13 @@ enum dds_stream_typecode_subtype { #define DDS_OP_FLAG_KEY 0x01 /* key field: applicable to {1,2,4,8}BY, STR, BST, ARR-of-{1,2,4,8}BY */ #define DDS_OP_FLAG_DEF 0x02 /* union has a default case (for DDS_OP_ADR | DDS_OP_TYPE_UNI) */ +/* For a union: (1) the discriminator may be a key field; (2) there may be a default value; + and (3) the discriminator can be an integral type (or enumerated - here treated as equivalent). + What it can't be is a floating-point type. So DEF and FP need never be set at the same time. + There are only a few flag bits, so saving one is not such a bad idea. */ +#define DDS_OP_FLAG_FP 0x02 /* floating-point: applicable to {4,8}BY and arrays, sequences of them */ +#define DDS_OP_FLAG_SGN 0x04 /* signed: applicable to {1,2,4,8}BY and arrays, sequences of them */ + /** * Description : Enable or disable write batching. Overrides default configuration * setting for write batching (Internal/WriteBatch). diff --git a/src/core/ddsi/src/ddsi_cdrstream.c b/src/core/ddsi/src/ddsi_cdrstream.c index 3565d45..6cdfb80 100644 --- a/src/core/ddsi/src/ddsi_cdrstream.c +++ b/src/core/ddsi/src/ddsi_cdrstream.c @@ -1995,14 +1995,42 @@ static size_t isprint_runlen (const unsigned char *s, size_t n) return m; } -static bool prtf_simple (char * __restrict *buf, size_t * __restrict bufsize, dds_istream_t * __restrict is, enum dds_stream_typecode type) +static bool prtf_simple (char * __restrict *buf, size_t * __restrict bufsize, dds_istream_t * __restrict is, enum dds_stream_typecode type, unsigned flags) { switch (type) { - case DDS_OP_VAL_1BY: return prtf (buf, bufsize, "%"PRIu8, dds_is_get1 (is)); - case DDS_OP_VAL_2BY: return prtf (buf, bufsize, "%"PRIu16, dds_is_get2 (is)); - case DDS_OP_VAL_4BY: return prtf (buf, bufsize, "%"PRIu32, dds_is_get4 (is)); - case DDS_OP_VAL_8BY: return prtf (buf, bufsize, "%"PRIu64, dds_is_get8 (is)); + case DDS_OP_VAL_1BY: { + const union { int8_t s; uint8_t u; } x = { .u = dds_is_get1 (is) }; + if (flags & DDS_OP_FLAG_SGN) + return prtf (buf, bufsize, "%"PRId8, x.s); + else + return prtf (buf, bufsize, "%"PRIu8, x.u); + } + case DDS_OP_VAL_2BY: { + const union { int16_t s; uint16_t u; } x = { .u = dds_is_get2 (is) }; + if (flags & DDS_OP_FLAG_SGN) + return prtf (buf, bufsize, "%"PRId16, x.s); + else + return prtf (buf, bufsize, "%"PRIu16, x.u); + } + case DDS_OP_VAL_4BY: { + const union { int32_t s; uint32_t u; float f; } x = { .u = dds_is_get4 (is) }; + if (flags & DDS_OP_FLAG_FP) + return prtf (buf, bufsize, "%g", x.f); + else if (flags & DDS_OP_FLAG_SGN) + return prtf (buf, bufsize, "%"PRId32, x.s); + else + return prtf (buf, bufsize, "%"PRIu32, x.u); + } + case DDS_OP_VAL_8BY: { + const union { int64_t s; uint64_t u; double f; } x = { .u = dds_is_get8 (is) }; + if (flags & DDS_OP_FLAG_FP) + return prtf (buf, bufsize, "%g", x.f); + else if (flags & DDS_OP_FLAG_SGN) + return prtf (buf, bufsize, "%"PRId64, x.s); + else + return prtf (buf, bufsize, "%"PRIu64, x.u); + } case DDS_OP_VAL_STR: case DDS_OP_VAL_BST: return prtf_str (buf, bufsize, is); case DDS_OP_VAL_ARR: case DDS_OP_VAL_SEQ: case DDS_OP_VAL_UNI: case DDS_OP_VAL_STU: abort (); @@ -2010,7 +2038,7 @@ static bool prtf_simple (char * __restrict *buf, size_t * __restrict bufsize, dd return false; } -static bool prtf_simple_array (char * __restrict *buf, size_t * __restrict bufsize, dds_istream_t * __restrict is, uint32_t num, enum dds_stream_typecode type) +static bool prtf_simple_array (char * __restrict *buf, size_t * __restrict bufsize, dds_istream_t * __restrict is, uint32_t num, enum dds_stream_typecode type, unsigned flags) { bool cont = prtf (buf, bufsize, "{"); switch (type) @@ -2033,7 +2061,7 @@ static bool prtf_simple_array (char * __restrict *buf, size_t * __restrict bufsi { if (i != 0) (void) prtf (buf, bufsize, ","); - cont = prtf_simple (buf, bufsize, is, type); + cont = prtf_simple (buf, bufsize, is, type, flags); i++; } } @@ -2045,7 +2073,7 @@ static bool prtf_simple_array (char * __restrict *buf, size_t * __restrict bufsi { if (i != 0) (void) prtf (buf, bufsize, ","); - cont = prtf_simple (buf, bufsize, is, type); + cont = prtf_simple (buf, bufsize, is, type, flags); } break; default: @@ -2070,10 +2098,10 @@ static const uint32_t *prtf_seq (char * __restrict *buf, size_t *bufsize, dds_is switch (subtype) { case DDS_OP_VAL_1BY: case DDS_OP_VAL_2BY: case DDS_OP_VAL_4BY: case DDS_OP_VAL_8BY: - (void) prtf_simple_array (buf, bufsize, is, num, subtype); + (void) prtf_simple_array (buf, bufsize, is, num, subtype, DDS_OP_FLAGS (insn)); return ops + 2; case DDS_OP_VAL_STR: case DDS_OP_VAL_BST: - (void) prtf_simple_array (buf, bufsize, is, num, subtype); + (void) prtf_simple_array (buf, bufsize, is, num, subtype, DDS_OP_FLAGS (insn)); return ops + (subtype == DDS_OP_VAL_STR ? 2 : 3); case DDS_OP_VAL_SEQ: case DDS_OP_VAL_ARR: case DDS_OP_VAL_UNI: case DDS_OP_VAL_STU: { const uint32_t jmp = DDS_OP_ADR_JMP (ops[3]); @@ -2098,10 +2126,10 @@ static const uint32_t *prtf_arr (char * __restrict *buf, size_t *bufsize, dds_is switch (subtype) { case DDS_OP_VAL_1BY: case DDS_OP_VAL_2BY: case DDS_OP_VAL_4BY: case DDS_OP_VAL_8BY: - (void) prtf_simple_array (buf, bufsize, is, num, subtype); + (void) prtf_simple_array (buf, bufsize, is, num, subtype, DDS_OP_FLAGS (insn)); return ops + 3; case DDS_OP_VAL_STR: case DDS_OP_VAL_BST: - (void) prtf_simple_array (buf, bufsize, is, num, subtype); + (void) prtf_simple_array (buf, bufsize, is, num, subtype, DDS_OP_FLAGS (insn)); return ops + (subtype == DDS_OP_VAL_STR ? 3 : 5); case DDS_OP_VAL_SEQ: case DDS_OP_VAL_ARR: case DDS_OP_VAL_UNI: case DDS_OP_VAL_STU: { const uint32_t *jsr_ops = ops + DDS_OP_ADR_JSR (ops[3]); @@ -2132,7 +2160,7 @@ static const uint32_t *prtf_uni (char * __restrict *buf, size_t *bufsize, dds_is { case DDS_OP_VAL_1BY: case DDS_OP_VAL_2BY: case DDS_OP_VAL_4BY: case DDS_OP_VAL_8BY: case DDS_OP_VAL_STR: case DDS_OP_VAL_BST: - (void) prtf_simple (buf, bufsize, is, valtype); + (void) prtf_simple (buf, bufsize, is, valtype, DDS_OP_FLAGS (jeq_op[0])); break; case DDS_OP_VAL_SEQ: case DDS_OP_VAL_ARR: case DDS_OP_VAL_UNI: case DDS_OP_VAL_STU: (void) dds_stream_print_sample1 (buf, bufsize, is, jeq_op + DDS_OP_ADR_JSR (jeq_op[0]), valtype == DDS_OP_VAL_STU); @@ -2161,11 +2189,11 @@ static bool dds_stream_print_sample1 (char * __restrict *buf, size_t * __restric { case DDS_OP_VAL_1BY: case DDS_OP_VAL_2BY: case DDS_OP_VAL_4BY: case DDS_OP_VAL_8BY: case DDS_OP_VAL_STR: - cont = prtf_simple (buf, bufsize, is, DDS_OP_TYPE (insn)); + cont = prtf_simple (buf, bufsize, is, DDS_OP_TYPE (insn), DDS_OP_FLAGS (insn)); ops += 2; break; case DDS_OP_VAL_BST: - cont = prtf_simple (buf, bufsize, is, DDS_OP_TYPE (insn)); + cont = prtf_simple (buf, bufsize, is, DDS_OP_TYPE (insn), DDS_OP_FLAGS (insn)); ops += 3; break; case DDS_OP_VAL_SEQ: @@ -2217,10 +2245,10 @@ size_t dds_stream_print_key (dds_istream_t * __restrict is, const struct ddsi_se { case DDS_OP_VAL_1BY: case DDS_OP_VAL_2BY: case DDS_OP_VAL_4BY: case DDS_OP_VAL_8BY: case DDS_OP_VAL_STR: case DDS_OP_VAL_BST: - cont = prtf_simple (&buf, &bufsize, is, DDS_OP_TYPE (*op)); + cont = prtf_simple (&buf, &bufsize, is, DDS_OP_TYPE (*op), DDS_OP_FLAGS (*op)); break; case DDS_OP_VAL_ARR: - cont = prtf_simple_array (&buf, &bufsize, is, op[2], DDS_OP_SUBTYPE (*op)); + cont = prtf_simple_array (&buf, &bufsize, is, op[2], DDS_OP_SUBTYPE (*op), DDS_OP_FLAGS (*op)); break; case DDS_OP_VAL_SEQ: case DDS_OP_VAL_UNI: case DDS_OP_VAL_STU: abort (); diff --git a/src/idlc/src/org/eclipse/cyclonedds/generator/BasicType.java b/src/idlc/src/org/eclipse/cyclonedds/generator/BasicType.java index 6ddb67b..8964e7f 100644 --- a/src/idlc/src/org/eclipse/cyclonedds/generator/BasicType.java +++ b/src/idlc/src/org/eclipse/cyclonedds/generator/BasicType.java @@ -20,15 +20,15 @@ public class BasicType extends AbstractType { BOOLEAN ("bool", "DDS_OP_TYPE_BOO", "DDS_OP_SUBTYPE_BOO", Alignment.BOOL, "Boolean"), OCTET ("uint8_t", "DDS_OP_TYPE_1BY", "DDS_OP_SUBTYPE_1BY", Alignment.ONE, "Octet"), - CHAR ("char", "DDS_OP_TYPE_1BY", "DDS_OP_SUBTYPE_1BY", Alignment.ONE, "Char"), - SHORT ("int16_t", "DDS_OP_TYPE_2BY", "DDS_OP_SUBTYPE_2BY", Alignment.TWO, "Short"), + CHAR ("char", "DDS_OP_TYPE_1BY | DDS_OP_FLAG_SGN", "DDS_OP_SUBTYPE_1BY | DDS_OP_FLAG_SGN", Alignment.ONE, "Char"), + SHORT ("int16_t", "DDS_OP_TYPE_2BY | DDS_OP_FLAG_SGN", "DDS_OP_SUBTYPE_2BY | DDS_OP_FLAG_SGN", Alignment.TWO, "Short"), USHORT ("uint16_t", "DDS_OP_TYPE_2BY", "DDS_OP_SUBTYPE_2BY", Alignment.TWO, "UShort"), - LONG ("int32_t", "DDS_OP_TYPE_4BY", "DDS_OP_SUBTYPE_4BY", Alignment.FOUR, "Long"), + LONG ("int32_t", "DDS_OP_TYPE_4BY | DDS_OP_FLAG_SGN", "DDS_OP_SUBTYPE_4BY | DDS_OP_FLAG_SGN", Alignment.FOUR, "Long"), ULONG ("uint32_t", "DDS_OP_TYPE_4BY", "DDS_OP_SUBTYPE_4BY", Alignment.FOUR, "ULong"), - LONGLONG ("int64_t", "DDS_OP_TYPE_8BY", "DDS_OP_SUBTYPE_8BY", Alignment.EIGHT, "LongLong"), + LONGLONG ("int64_t", "DDS_OP_TYPE_8BY | DDS_OP_FLAG_SGN", "DDS_OP_SUBTYPE_8BY | DDS_OP_FLAG_SGN", Alignment.EIGHT, "LongLong"), ULONGLONG ("uint64_t", "DDS_OP_TYPE_8BY", "DDS_OP_SUBTYPE_8BY", Alignment.EIGHT, "ULongLong"), - FLOAT ("float", "DDS_OP_TYPE_4BY", "DDS_OP_SUBTYPE_4BY", Alignment.FOUR, "Float"), - DOUBLE ("double", "DDS_OP_TYPE_8BY", "DDS_OP_SUBTYPE_8BY", Alignment.EIGHT, "Double"), + FLOAT ("float", "DDS_OP_TYPE_4BY | DDS_OP_FLAG_FP", "DDS_OP_SUBTYPE_4BY | DDS_OP_FLAG_FP", Alignment.FOUR, "Float"), + DOUBLE ("double", "DDS_OP_TYPE_8BY | DDS_OP_FLAG_FP", "DDS_OP_SUBTYPE_8BY | DDS_OP_FLAG_FP", Alignment.EIGHT, "Double"), STRING ("char *", "DDS_OP_TYPE_STR", "DDS_OP_SUBTYPE_STR", Alignment.PTR, "String"); public final String cType;