zerodds-idl-cpp 1.0.0-rc.3

OMG IDL4 → C++17 code generator (idl4-cpp-1.0 + DDS-PSM-Cxx + DDS-RPC C++ PSM) for ZeroDDS.
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
// SPDX-License-Identifier: Apache-2.0
// Copyright 2026 ZeroDDS Contributors
//! Block-F: DDS status mapping (spec idl4-cpp-1.0 §7.4 + dds-1.4 §2.2.4.1).
//!
//! Emits the 13 OMG DCPS status structures as a C++17 header in the
//! `dds::core::status` namespace. The classes follow the reference pattern
//! from C5.1-a (private storage `field_`, mutable + const getter, setter).
//!
//! Spec sources:
//! - DDS-PSM-CXX 1.0 §7.5.5 (Listener / Status / Condition).
//! - DDS 1.4 §2.2.4.1 (communication status).
//!
//! # List of the 13 status classes
//!
//! | Status                          | Counter fields                                       |
//! |---------------------------------|------------------------------------------------------|
//! | InconsistentTopicStatus         | total_count, total_count_change                      |
//! | SampleLostStatus                | total_count, total_count_change                      |
//! | SampleRejectedStatus            | total_count, total_count_change, last_reason, last_instance_handle |
//! | LivelinessChangedStatus         | alive_count, not_alive_count, alive_count_change, not_alive_count_change, last_publication_handle |
//! | RequestedDeadlineMissedStatus   | total_count, total_count_change, last_instance_handle |
//! | RequestedIncompatibleQosStatus  | total_count, total_count_change, last_policy_id, policies |
//! | OfferedDeadlineMissedStatus     | total_count, total_count_change, last_instance_handle |
//! | OfferedIncompatibleQosStatus    | total_count, total_count_change, last_policy_id, policies |
//! | LivelinessLostStatus            | total_count, total_count_change                      |
//! | PublicationMatchedStatus        | total_count, total_count_change, current_count, current_count_change, last_subscription_handle |
//! | SubscriptionMatchedStatus       | total_count, total_count_change, current_count, current_count_change, last_publication_handle |
//! | DataAvailableStatus             | (marker only)                                        |
//! | DataOnReadersStatus             | (marker only)                                        |
//!
//! The statuses marked `(marker only)` are pure notification markers without
//! counter (spec DDS 1.4 §2.2.4.1.1.1). They are emitted as empty
//! classes anyway so that listener signatures are uniform.

use core::fmt::Write;

use crate::error::CppGenError;

/// Description of a DDS status field.
#[derive(Debug, Clone, Copy)]
struct StatusField {
    /// Field name (snake_case as in the spec).
    name: &'static str,
    /// C++ type expression.
    cpp_ty: &'static str,
}

/// Description of a DDS status class.
#[derive(Debug, Clone, Copy)]
struct StatusSpec {
    /// Class name (PascalCase as in the spec).
    name: &'static str,
    /// Fields of the status class.
    fields: &'static [StatusField],
    /// Doc comment with a spec reference.
    spec_ref: &'static str,
}

/// Full list of the 13 DCPS status structures.
const STATUSES: &[StatusSpec] = &[
    StatusSpec {
        name: "InconsistentTopicStatus",
        spec_ref: "DDS 1.4 §2.2.4.1.4",
        fields: &[
            StatusField {
                name: "total_count",
                cpp_ty: "int32_t",
            },
            StatusField {
                name: "total_count_change",
                cpp_ty: "int32_t",
            },
        ],
    },
    StatusSpec {
        name: "SampleLostStatus",
        spec_ref: "DDS 1.4 §2.2.4.1.5",
        fields: &[
            StatusField {
                name: "total_count",
                cpp_ty: "int32_t",
            },
            StatusField {
                name: "total_count_change",
                cpp_ty: "int32_t",
            },
        ],
    },
    StatusSpec {
        name: "SampleRejectedStatus",
        spec_ref: "DDS 1.4 §2.2.4.1.6",
        fields: &[
            StatusField {
                name: "total_count",
                cpp_ty: "int32_t",
            },
            StatusField {
                name: "total_count_change",
                cpp_ty: "int32_t",
            },
            StatusField {
                name: "last_reason",
                cpp_ty: "::dds::core::status::SampleRejectedState",
            },
            StatusField {
                name: "last_instance_handle",
                cpp_ty: "::dds::core::InstanceHandle",
            },
        ],
    },
    StatusSpec {
        name: "LivelinessChangedStatus",
        spec_ref: "DDS 1.4 §2.2.4.1.7",
        fields: &[
            StatusField {
                name: "alive_count",
                cpp_ty: "int32_t",
            },
            StatusField {
                name: "not_alive_count",
                cpp_ty: "int32_t",
            },
            StatusField {
                name: "alive_count_change",
                cpp_ty: "int32_t",
            },
            StatusField {
                name: "not_alive_count_change",
                cpp_ty: "int32_t",
            },
            StatusField {
                name: "last_publication_handle",
                cpp_ty: "::dds::core::InstanceHandle",
            },
        ],
    },
    StatusSpec {
        name: "RequestedDeadlineMissedStatus",
        spec_ref: "DDS 1.4 §2.2.4.1.8",
        fields: &[
            StatusField {
                name: "total_count",
                cpp_ty: "int32_t",
            },
            StatusField {
                name: "total_count_change",
                cpp_ty: "int32_t",
            },
            StatusField {
                name: "last_instance_handle",
                cpp_ty: "::dds::core::InstanceHandle",
            },
        ],
    },
    StatusSpec {
        name: "RequestedIncompatibleQosStatus",
        spec_ref: "DDS 1.4 §2.2.4.1.9",
        fields: &[
            StatusField {
                name: "total_count",
                cpp_ty: "int32_t",
            },
            StatusField {
                name: "total_count_change",
                cpp_ty: "int32_t",
            },
            StatusField {
                name: "last_policy_id",
                cpp_ty: "::dds::core::policy::QosPolicyId",
            },
            StatusField {
                name: "policies",
                cpp_ty: "std::vector<::dds::core::status::QosPolicyCount>",
            },
        ],
    },
    StatusSpec {
        name: "OfferedDeadlineMissedStatus",
        spec_ref: "DDS 1.4 §2.2.4.1.10",
        fields: &[
            StatusField {
                name: "total_count",
                cpp_ty: "int32_t",
            },
            StatusField {
                name: "total_count_change",
                cpp_ty: "int32_t",
            },
            StatusField {
                name: "last_instance_handle",
                cpp_ty: "::dds::core::InstanceHandle",
            },
        ],
    },
    StatusSpec {
        name: "OfferedIncompatibleQosStatus",
        spec_ref: "DDS 1.4 §2.2.4.1.11",
        fields: &[
            StatusField {
                name: "total_count",
                cpp_ty: "int32_t",
            },
            StatusField {
                name: "total_count_change",
                cpp_ty: "int32_t",
            },
            StatusField {
                name: "last_policy_id",
                cpp_ty: "::dds::core::policy::QosPolicyId",
            },
            StatusField {
                name: "policies",
                cpp_ty: "std::vector<::dds::core::status::QosPolicyCount>",
            },
        ],
    },
    StatusSpec {
        name: "LivelinessLostStatus",
        spec_ref: "DDS 1.4 §2.2.4.1.12",
        fields: &[
            StatusField {
                name: "total_count",
                cpp_ty: "int32_t",
            },
            StatusField {
                name: "total_count_change",
                cpp_ty: "int32_t",
            },
        ],
    },
    StatusSpec {
        name: "PublicationMatchedStatus",
        spec_ref: "DDS 1.4 §2.2.4.1.13",
        fields: &[
            StatusField {
                name: "total_count",
                cpp_ty: "int32_t",
            },
            StatusField {
                name: "total_count_change",
                cpp_ty: "int32_t",
            },
            StatusField {
                name: "current_count",
                cpp_ty: "int32_t",
            },
            StatusField {
                name: "current_count_change",
                cpp_ty: "int32_t",
            },
            StatusField {
                name: "last_subscription_handle",
                cpp_ty: "::dds::core::InstanceHandle",
            },
        ],
    },
    StatusSpec {
        name: "SubscriptionMatchedStatus",
        spec_ref: "DDS 1.4 §2.2.4.1.14",
        fields: &[
            StatusField {
                name: "total_count",
                cpp_ty: "int32_t",
            },
            StatusField {
                name: "total_count_change",
                cpp_ty: "int32_t",
            },
            StatusField {
                name: "current_count",
                cpp_ty: "int32_t",
            },
            StatusField {
                name: "current_count_change",
                cpp_ty: "int32_t",
            },
            StatusField {
                name: "last_publication_handle",
                cpp_ty: "::dds::core::InstanceHandle",
            },
        ],
    },
    StatusSpec {
        name: "DataAvailableStatus",
        spec_ref: "DDS 1.4 §2.2.4.1.2 (marker)",
        fields: &[],
    },
    StatusSpec {
        name: "DataOnReadersStatus",
        spec_ref: "DDS 1.4 §2.2.4.1.3 (marker)",
        fields: &[],
    },
];

/// Writes the complete status header into `out`.
///
/// The header contains a block comment with a spec reference, opens the
/// `dds::core::status` namespace, emits the 13 status classes and
/// closes the namespaces again.
///
/// # Errors
/// Returns [`CppGenError::Internal`] if writing to the
/// `String` buffer fails (should practically never happen).
pub fn emit_status_header(out: &mut String) -> Result<(), CppGenError> {
    writeln!(
        out,
        "// Block-F: DDS-Status-Strukturen (Spec dds-1.4 §2.2.4.1)."
    )
    .map_err(fmt_err)?;
    writeln!(
        out,
        "namespace dds {{ namespace core {{ namespace status {{"
    )
    .map_err(fmt_err)?;
    writeln!(out).map_err(fmt_err)?;

    // Forward decls for helper types; full definitions live outside
    // this codegen (in the DDS-PSM-CXX runtime).
    writeln!(out, "// Forward declarations for helper types.").map_err(fmt_err)?;
    writeln!(out, "enum class SampleRejectedState : int32_t;").map_err(fmt_err)?;
    writeln!(out, "class QosPolicyCount;").map_err(fmt_err)?;
    writeln!(out).map_err(fmt_err)?;

    for s in STATUSES {
        emit_status_class(out, s)?;
    }

    writeln!(out, "}} }} }} // namespace dds::core::status").map_err(fmt_err)?;
    writeln!(out).map_err(fmt_err)?;
    Ok(())
}

/// Returns the list of all emitted status class names.
#[must_use]
pub fn status_class_names() -> Vec<&'static str> {
    STATUSES.iter().map(|s| s.name).collect()
}

fn emit_status_class(out: &mut String, s: &StatusSpec) -> Result<(), CppGenError> {
    writeln!(out, "/// {} ({})", s.name, s.spec_ref).map_err(fmt_err)?;
    writeln!(out, "class {} {{", s.name).map_err(fmt_err)?;
    writeln!(out, "public:").map_err(fmt_err)?;
    writeln!(out, "    {}() = default;", s.name).map_err(fmt_err)?;
    writeln!(out, "    ~{}() = default;", s.name).map_err(fmt_err)?;
    writeln!(out, "    {0}(const {0}&) = default;", s.name).map_err(fmt_err)?;
    writeln!(out, "    {0}({0}&&) noexcept = default;", s.name).map_err(fmt_err)?;
    writeln!(out, "    {0}& operator=(const {0}&) = default;", s.name).map_err(fmt_err)?;
    writeln!(out, "    {0}& operator=({0}&&) noexcept = default;", s.name).map_err(fmt_err)?;

    if !s.fields.is_empty() {
        writeln!(out).map_err(fmt_err)?;
        // Reset helper: sets all *_change fields to 0 (spec DDS 1.4
        // §2.2.4.1.1: reset on read).
        writeln!(out, "    /// Reset-on-read: sets all *_change fields to 0.").map_err(fmt_err)?;
        writeln!(out, "    void reset_changes() {{").map_err(fmt_err)?;
        for f in s.fields {
            if f.name.ends_with("_change") {
                writeln!(out, "        {}_ = 0;", f.name).map_err(fmt_err)?;
            }
        }
        writeln!(out, "    }}").map_err(fmt_err)?;
    }

    if !s.fields.is_empty() {
        writeln!(out).map_err(fmt_err)?;
        // Const + mutable Getter, Setter (const-ref + Move-Setter).
        for f in s.fields {
            writeln!(
                out,
                "    const {ty}& {name}() const {{ return {name}_; }}",
                ty = f.cpp_ty,
                name = f.name
            )
            .map_err(fmt_err)?;
            writeln!(
                out,
                "    {ty}& {name}() {{ return {name}_; }}",
                ty = f.cpp_ty,
                name = f.name
            )
            .map_err(fmt_err)?;
            writeln!(
                out,
                "    void {name}(const {ty}& value) {{ {name}_ = value; }}",
                ty = f.cpp_ty,
                name = f.name
            )
            .map_err(fmt_err)?;
            writeln!(
                out,
                "    void {name}({ty}&& value) {{ {name}_ = std::move(value); }}",
                ty = f.cpp_ty,
                name = f.name
            )
            .map_err(fmt_err)?;
        }

        writeln!(out).map_err(fmt_err)?;
        writeln!(out, "private:").map_err(fmt_err)?;
        for f in s.fields {
            writeln!(out, "    {ty} {name}_{{}};", ty = f.cpp_ty, name = f.name)
                .map_err(fmt_err)?;
        }
    }

    writeln!(out, "}};").map_err(fmt_err)?;
    writeln!(out).map_err(fmt_err)?;
    Ok(())
}

fn fmt_err(_: core::fmt::Error) -> CppGenError {
    CppGenError::Internal("string formatting failed".into())
}

#[cfg(test)]
mod tests {
    #![allow(clippy::expect_used, clippy::panic)]
    use super::*;

    fn render() -> String {
        let mut s = String::new();
        emit_status_header(&mut s).expect("emit");
        s
    }

    #[test]
    fn status_namespace_is_dds_core_status() {
        let s = render();
        assert!(s.contains("namespace dds { namespace core { namespace status {"));
        assert!(
            s.contains("}}} // namespace dds::core::status")
                || s.contains("} } } // namespace dds::core::status")
        );
    }

    #[test]
    fn sample_lost_status_has_total_count_fields() {
        let s = render();
        assert!(s.contains("class SampleLostStatus {"));
        assert!(s.contains("int32_t total_count_{};"));
        assert!(s.contains("int32_t total_count_change_{};"));
    }

    #[test]
    fn sample_rejected_status_has_last_reason() {
        let s = render();
        assert!(s.contains("class SampleRejectedStatus {"));
        assert!(s.contains("SampleRejectedState"));
        assert!(s.contains("last_instance_handle"));
    }

    #[test]
    fn liveliness_changed_status_has_alive_and_not_alive() {
        let s = render();
        assert!(s.contains("class LivelinessChangedStatus {"));
        assert!(s.contains("alive_count_{};"));
        assert!(s.contains("not_alive_count_{};"));
        assert!(s.contains("alive_count_change_{};"));
        assert!(s.contains("not_alive_count_change_{};"));
    }

    #[test]
    fn matched_status_has_current_count_pair() {
        let s = render();
        assert!(s.contains("class PublicationMatchedStatus {"));
        assert!(s.contains("class SubscriptionMatchedStatus {"));
        assert!(s.contains("current_count_{};"));
        assert!(s.contains("current_count_change_{};"));
    }

    #[test]
    fn incompatible_qos_status_has_policy_count_vector() {
        let s = render();
        assert!(s.contains("class RequestedIncompatibleQosStatus {"));
        assert!(s.contains("class OfferedIncompatibleQosStatus {"));
        assert!(s.contains("std::vector<::dds::core::status::QosPolicyCount> policies_{};"));
    }

    #[test]
    fn marker_statuses_emit_empty_class() {
        let s = render();
        assert!(s.contains("class DataAvailableStatus {"));
        assert!(s.contains("class DataOnReadersStatus {"));
        // Marker statuses have no fields, hence no `int32_t total_count_`.
        // But they emit a default constructor:
        assert!(s.contains("DataAvailableStatus() = default;"));
        assert!(s.contains("DataOnReadersStatus() = default;"));
    }

    #[test]
    fn reset_changes_resets_only_change_fields() {
        let s = render();
        // Look at the InconsistentTopicStatus::reset_changes() body:
        let needle = "class InconsistentTopicStatus {";
        let start = s.find(needle).expect("class header");
        let after = &s[start..];
        let reset_pos = after.find("reset_changes()").expect("reset method");
        let reset_block = &after[reset_pos..];
        // Inside, we set total_count_change_ to 0 but never total_count_.
        let close = reset_block.find("    }").expect("end of method");
        let body = &reset_block[..close];
        assert!(body.contains("total_count_change_ = 0;"));
        assert!(!body.contains("total_count_ = 0;"));
    }

    #[test]
    fn all_thirteen_status_classes_emitted() {
        let names = status_class_names();
        assert_eq!(names.len(), 13);
        let s = render();
        for n in names {
            let class_marker = format!("class {n} {{");
            assert!(s.contains(&class_marker), "missing class: {n}");
        }
    }

    #[test]
    fn move_setter_variant_present() {
        let s = render();
        // Move setter with && + std::move:
        assert!(s.contains("void total_count(int32_t&& value)"));
        assert!(s.contains("std::move(value);"));
    }

    #[test]
    fn const_getter_returns_const_ref() {
        let s = render();
        assert!(s.contains("const int32_t& total_count() const"));
        assert!(s.contains("int32_t& total_count() {"));
    }

    #[test]
    fn copy_and_move_special_members_defaulted() {
        let s = render();
        assert!(s.contains("SampleLostStatus(const SampleLostStatus&) = default;"));
        assert!(s.contains("SampleLostStatus(SampleLostStatus&&) noexcept = default;"));
    }
}