-
Notifications
You must be signed in to change notification settings - Fork 74
feat: add C++ header-only exporter for embedded/MCU use #526
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
SoundMatt
wants to merge
4
commits into
COVESA:master
Choose a base branch
from
SoundMatt:feat/cpph-exporter
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
9f2c8b8
feat: add C++ header-only exporter for embedded/MCU use
SoundMatt 37d3f80
fix: resolve ruff lint/format violations in cpp_header exporter
SoundMatt 9ec9a63
docs: add cpp-header exporter documentation
SoundMatt 274e05f
feat(cpp-header): use enums for type/datatype/cpp_type/unit; double f…
SoundMatt File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,144 @@ | ||
| # C++ Header Exporter | ||
|
|
||
| The `cpp-header` exporter generates a single header-only `.hpp` file that encodes the VSS tree as `constexpr` C++ data structures. It is intended for embedded and MCU targets where no filesystem or JSON parser is available at runtime. The output requires only `<cstddef>` and `<cstdint>`. | ||
|
|
||
| ## Exporter Specific Arguments | ||
|
|
||
| ### `--namespace` | ||
|
|
||
| C++ namespace that wraps all generated symbols. Defaults to `vss`. | ||
|
|
||
| ### `--include-branches` / `--no-include-branches` | ||
|
|
||
| When `--include-branches` is set, branch nodes are emitted alongside leaf signals. By default only leaf signals are included. | ||
|
|
||
| ## Output Structure | ||
|
|
||
| The generated header always contains: | ||
|
|
||
| - A `VssSignal` struct with string fields for `path`, `type`, `datatype`, `cpp_type`, `unit`, `description`, `min_value`, `max_value`, `default_value`, and a null-terminated `allowed_values` pointer (or `nullptr`). | ||
| - One `constexpr VssSignal` per leaf signal. If the signal has `allowed:` values, a null-terminated `constexpr const char*[]` is emitted immediately above it. | ||
| - A `constexpr VssSignal kSignals[]` aggregate containing every signal in pre-order. | ||
| - `constexpr std::size_t kSignalCount` derived from `kSignals`. | ||
|
|
||
| VSS datatypes are mapped to C++ types as follows: | ||
|
|
||
| | VSS datatype | C++ type | | ||
| |---|---| | ||
| | `uint8` / `int8` | `uint8_t` / `int8_t` | | ||
| | `uint16` / `int16` | `uint16_t` / `int16_t` | | ||
| | `uint32` / `int32` | `uint32_t` / `int32_t` | | ||
| | `uint64` / `int64` | `uint64_t` / `int64_t` | | ||
| | `float` | `float` | | ||
| | `double` | `double` | | ||
| | `boolean` | `bool` | | ||
| | `string` | `const char*` | | ||
| | `T[]` (array) | `const T*` | | ||
|
|
||
| ## Example | ||
|
|
||
| Input model: | ||
|
|
||
| ```yaml | ||
| # model.vspec | ||
| Vehicle: | ||
| type: branch | ||
| description: High-level vehicle data. | ||
|
|
||
| Vehicle.Speed: | ||
| type: sensor | ||
| datatype: float | ||
| unit: km/h | ||
| min: 0 | ||
| max: 250 | ||
| description: Vehicle speed. | ||
|
|
||
| Vehicle.Powertrain.Type: | ||
| type: attribute | ||
| datatype: string | ||
| allowed: [combustion, hybrid, electric] | ||
| description: The powertrain type of the vehicle. | ||
| ``` | ||
|
|
||
| Generator call: | ||
|
|
||
| ```bash | ||
| vspec export cpp-header --vspec model.vspec --output vss.hpp | ||
| ``` | ||
|
|
||
| Generated file (excerpt): | ||
|
|
||
| ```cpp | ||
| // SPDX-FileCopyrightText: Copyright (c) 2024 Contributors to COVESA | ||
| // SPDX-License-Identifier: MPL-2.0 | ||
|
|
||
| // Auto-generated from the Vehicle Signal Specification. | ||
| // Do not edit manually. | ||
|
|
||
| #pragma once | ||
|
|
||
| #include <cstddef> | ||
| #include <cstdint> | ||
|
|
||
| namespace vss { | ||
|
|
||
| struct VssSignal { | ||
| const char* const path; | ||
| const char* const type; | ||
| const char* const datatype; | ||
| const char* const cpp_type; | ||
| const char* const unit; | ||
| const char* const description; | ||
| const char* const min_value; | ||
| const char* const max_value; | ||
| const char* const* const allowed_values; // null-terminated, or nullptr | ||
| const char* const default_value; | ||
| }; | ||
|
|
||
| constexpr VssSignal Vehicle_Speed = { | ||
| "Vehicle.Speed", | ||
| "sensor", | ||
| "float", | ||
| "float", | ||
| "km/h", | ||
| "Vehicle speed.", | ||
| "0", | ||
| "250", | ||
| nullptr, | ||
| nullptr, | ||
| }; | ||
|
|
||
| constexpr const char* Vehicle_Powertrain_Type_kAllowed[] = { | ||
| "combustion", | ||
| "hybrid", | ||
| "electric", | ||
| nullptr, | ||
| }; | ||
| constexpr VssSignal Vehicle_Powertrain_Type = { | ||
| "Vehicle.Powertrain.Type", | ||
| "attribute", | ||
| "string", | ||
| "const char*", | ||
| nullptr, | ||
| "The powertrain type of the vehicle.", | ||
| nullptr, | ||
| nullptr, | ||
| Vehicle_Powertrain_Type_kAllowed, | ||
| nullptr, | ||
| }; | ||
|
|
||
| constexpr VssSignal kSignals[] = { | ||
| Vehicle_Speed, | ||
| Vehicle_Powertrain_Type, | ||
| }; | ||
|
|
||
| constexpr std::size_t kSignalCount = sizeof(kSignals) / sizeof(kSignals[0]); | ||
|
|
||
| } // namespace vss | ||
| ``` | ||
|
|
||
| The header is self-contained and can be compiled without any vss-tools dependency: | ||
|
|
||
| ```bash | ||
| g++ -std=c++17 -c vss.hpp | ||
| ``` | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -49,6 +49,7 @@ def cli(ctx: click.Context, log_level: str, log_file: Path): | |
| "yaml": "vss_tools.exporters.yaml:cli", | ||
| "tree": "vss_tools.exporters.tree:cli", | ||
| "samm": "vss_tools.exporters.samm:cli", | ||
| "cpp-header": "vss_tools.exporters.cpp_header:cli", | ||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. how about |
||
| "go": "vss_tools.exporters.go:cli", | ||
| "ros2interface": "vss_tools.exporters.ros2interface:cli", | ||
| "s2dm": "vss_tools.exporters.s2dm:cli", | ||
|
|
||
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Why not just
Signal. Its already in thevssnamespace