|
| 1 | +/* Copyright (c) 2018-2026 Steven Varga, steven@vargalabs.com Toronto, ON Canada */ |
| 2 | + |
| 3 | +#pragma once |
| 4 | + |
| 5 | +#include "producer.hpp" |
| 6 | + |
| 7 | +#include <cstdint> |
| 8 | +#include <map> |
| 9 | +#include <ostream> |
| 10 | +#include <string> |
| 11 | +#include <utility> |
| 12 | +#include <vector> |
| 13 | + |
| 14 | +// Tier-1 backend for vargalabs/sandbox/pb.hpp. Emits: |
| 15 | +// template<> struct pb::meta::descriptor_t<T> { |
| 16 | +// static constexpr auto fields = std::tuple{ |
| 17 | +// pb::field<N, &T::m>{}, ... |
| 18 | +// }; |
| 19 | +// }; |
| 20 | +// One specialization per encountered record type, in topological order |
| 21 | +// (nested types before outer types). Field numbers come from per-field |
| 22 | +// [[clang::annotate("pb::field=N")]] attributes — the consumer (consumer_pb.hpp) |
| 23 | +// parses the annotations and supplies N to type_insert. |
| 24 | +struct PbProducer : Producer<PbProducer> { |
| 25 | + |
| 26 | + void file_begin_impl(){ |
| 27 | + io << "/* Generated by h5cpp-compiler --protocol-buffers. Do not edit. */\n"; |
| 28 | + io << "#pragma once\n\n"; |
| 29 | + io << "#include <pb.hpp>\n\n"; |
| 30 | + } |
| 31 | + |
| 32 | + void file_end_impl(){ |
| 33 | + io << "\n"; |
| 34 | + } |
| 35 | + |
| 36 | + // template_decl is the per-record opener. `record` is the qualified type |
| 37 | + // name (e.g. "::sn::sensor::reading_t"). Each record's descriptor_t is a |
| 38 | + // standalone specialization at file scope. |
| 39 | + void template_decl_impl(const std::string& record){ |
| 40 | + record_name = record; |
| 41 | + first_field = true; |
| 42 | + io << "template<> struct pb::meta::descriptor_t<" << record << "> {\n"; |
| 43 | + io << " static constexpr auto fields = std::tuple{"; |
| 44 | + } |
| 45 | + |
| 46 | + // record_decl is called for each topologically-prior record (nested |
| 47 | + // types). For PbProducer we don't emit nested compound declarations — |
| 48 | + // nested message types get their OWN descriptor_t specialization in a |
| 49 | + // prior pass. The variable name is unused in pb output but tracked for |
| 50 | + // the cache contract. |
| 51 | + void record_decl_impl(const std::string& var, const std::string& /*record_name*/){ |
| 52 | + (void)var; |
| 53 | + // no-op for pb; nested messages reference their own descriptor_t<T> |
| 54 | + } |
| 55 | + |
| 56 | + // array_decl: C arrays (T[N]) don't have a clean proto3 wire mapping. |
| 57 | + // Stage 1 refuses; users must use std::vector<T> (FR3 repeated) or |
| 58 | + // std::array<T,N> (planned future). |
| 59 | + void array_decl_impl(const std::string& var, const std::string& /*type*/, uint64_t /*size*/){ |
| 60 | + (void)var; |
| 61 | + io << "\n // ERROR: C arrays not supported by pb backend (use std::vector<T> instead)"; |
| 62 | + } |
| 63 | + |
| 64 | + // type_insert: emit one pb::field<N, &Record::member>{} entry. |
| 65 | + // `var` is overloaded by the consumer to carry the field number as a |
| 66 | + // decimal string ("1", "2", ...) instead of the HDF5 vname. record_name |
| 67 | + // is the qualified record type; field_name is the member identifier. |
| 68 | + void type_insert_impl(const std::string& var, const std::string& field_name, |
| 69 | + const std::string& rec_name, const std::string& /*type*/){ |
| 70 | + if (!first_field) io << ","; |
| 71 | + io << "\n pb::field<" << var << ", &" << rec_name << "::" << field_name << ">{}"; |
| 72 | + first_field = false; |
| 73 | + } |
| 74 | + |
| 75 | + void return_type_impl(const std::string& /*var*/){ |
| 76 | + io << "\n };\n};\n\n"; |
| 77 | + } |
| 78 | + |
| 79 | + void type_release_impl(){ |
| 80 | + // pb has no resource-id lifetime to manage — descriptor_t entries are |
| 81 | + // pure compile-time. No-op. |
| 82 | + } |
| 83 | + |
| 84 | + // The cache contract is HDF5-shaped (cpp-type → hid_t-var mapping). |
| 85 | + // PbProducer doesn't need that vocabulary — pb dispatches via traits at |
| 86 | + // the library level, not via per-type variable aliases. Implement as |
| 87 | + // identity stubs so the existing consumer driver still compiles. |
| 88 | + bool cache_add_impl(const std::string& /*key*/, const std::string& /*type*/){ return true; } |
| 89 | + std::string cache_impl(const std::string& type){ return type; } |
| 90 | + |
| 91 | + // FR6/stage 4: oneof emission. Called directly by PbTemplateCallback for |
| 92 | + // std::variant member fields (not via the CRTP Producer<> hook surface, |
| 93 | + // which is HDF5-shaped). Emits a pb::oneof<&R::m, pb::alt<T1,N1>,...>{} |
| 94 | + // entry in the descriptor's fields tuple. |
| 95 | + void emit_oneof(const std::string& member_name, |
| 96 | + const std::string& record_name, |
| 97 | + const std::vector<std::string>& alt_types, |
| 98 | + const std::vector<std::uint32_t>& tags){ |
| 99 | + if (!first_field) io << ","; |
| 100 | + io << "\n pb::oneof<&" << record_name << "::" << member_name; |
| 101 | + for (std::size_t i = 0; i < alt_types.size(); ++i) { |
| 102 | + io << ", pb::alt<" << alt_types[i] << ", " << tags[i] << ">"; |
| 103 | + } |
| 104 | + io << ">{}"; |
| 105 | + first_field = false; |
| 106 | + } |
| 107 | + |
| 108 | + // Stage 5: wire-spec annotation. Same shape as type_insert but emits an |
| 109 | + // explicit WireSpec template arg on pb::field<N, Ptr, WireSpec>. Called |
| 110 | + // when the consumer sees a [[clang::annotate("pb::wire=spec")]] attribute |
| 111 | + // on a field. wire_spec is the spec name without the `_t` suffix, e.g. |
| 112 | + // "sint32", "fixed64", "sfixed32". |
| 113 | + void emit_field_with_spec(std::uint32_t tag, |
| 114 | + const std::string& field_name, |
| 115 | + const std::string& record_name, |
| 116 | + const std::string& wire_spec){ |
| 117 | + if (!first_field) io << ","; |
| 118 | + io << "\n pb::field<" << tag |
| 119 | + << ", &" << record_name << "::" << field_name |
| 120 | + << ", pb::wire::" << wire_spec << "_t>{}"; |
| 121 | + first_field = false; |
| 122 | + } |
| 123 | + |
| 124 | + // Stage 6 (Tier 4): adapter_field emission. Called when the consumer |
| 125 | + // sees a [[clang::annotate("pb::adapter=Name")]] attribute on a field. |
| 126 | + // Name is the adapter shorthand ("Timestamp", "Duration"), which maps |
| 127 | + // to the library symbol pb::Name_adapter (Timestamp_adapter, etc.). |
| 128 | + void emit_adapter_field(std::uint32_t tag, |
| 129 | + const std::string& field_name, |
| 130 | + const std::string& record_name, |
| 131 | + const std::string& adapter_name){ |
| 132 | + if (!first_field) io << ","; |
| 133 | + io << "\n pb::adapter_field<" << tag |
| 134 | + << ", &" << record_name << "::" << field_name |
| 135 | + << ", pb::" << adapter_name << "_adapter>{}"; |
| 136 | + first_field = false; |
| 137 | + } |
| 138 | + |
| 139 | +private: |
| 140 | + std::string record_name; |
| 141 | + bool first_field = true; |
| 142 | +}; |
0 commit comments