-
Notifications
You must be signed in to change notification settings - Fork 163
Add documentation for rfl::Commented feature #600
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
Merged
liuzicheng1987
merged 2 commits into
f/comments
from
add-commented-docs-15519898346803255693
Feb 7, 2026
Merged
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
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
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,81 @@ | ||
| # `rfl::Commented` | ||
|
|
||
| The `rfl::Commented<T>` wrapper allows you to add comments to fields in your structs. These comments are then serialized in formats that support them, such as YAML and XML. | ||
|
|
||
| Note that `rfl::Commented` is currently unsupported by formats that do not have a standard way of representing comments, such as JSON. Also note that comments are **write-only**: they are ignored during deserialization. | ||
|
|
||
| ## Example (YAML) | ||
|
|
||
| In YAML, the comments are added as line comments (`#`): | ||
|
|
||
| ```cpp | ||
| struct Person { | ||
| std::string first_name; | ||
| std::string last_name; | ||
| rfl::Commented<std::string> town; | ||
| }; | ||
|
|
||
| const auto homer = Person{.first_name = "Homer", | ||
| .last_name = "Simpson", | ||
| .town = rfl::Commented<std::string>( | ||
| "Springfield", "The town where Homer lives")}; | ||
|
|
||
| const auto yaml_str = rfl::yaml::write(homer); | ||
| ``` | ||
|
|
||
| This will result in the following YAML: | ||
|
|
||
| ```yaml | ||
| first_name: Homer | ||
| last_name: Simpson | ||
| town: Springfield # The town where Homer lives | ||
| ``` | ||
|
|
||
| ## Example (XML) | ||
|
|
||
| In XML, the comments are added as `<!-- comment -->` blocks after the field: | ||
|
|
||
| ```cpp | ||
| struct Person { | ||
| std::string first_name; | ||
| std::string last_name; | ||
| rfl::Commented<std::string> town; | ||
| }; | ||
|
|
||
| const auto homer = Person{.first_name = "Homer", | ||
| .last_name = "Simpson", | ||
| .town = rfl::Commented<std::string>( | ||
| "Springfield", "The town where Homer lives")}; | ||
|
|
||
| const auto xml_str = rfl::xml::write(homer); | ||
| ``` | ||
|
|
||
| This will result in the following XML: | ||
|
|
||
| ```xml | ||
| <?xml version="1.0" encoding="UTF-8"?> | ||
| <Person> | ||
| <first_name>Homer</first_name> | ||
| <last_name>Simpson</last_name> | ||
| <town>Springfield</town> | ||
| <!--The town where Homer lives--> | ||
| </Person> | ||
| ``` | ||
|
|
||
| ## API convenience | ||
|
|
||
| `rfl::Commented` provides several ways to access and modify the underlying value and the comment: | ||
|
|
||
| - `.get()`, `.value()`, `operator()()` — access the underlying value (const and non-const overloads). | ||
| - `.comment()` — returns an `std::optional<std::string>` containing the comment. | ||
| - `.add_comment(std::string)` — sets or updates the comment. | ||
| - `.set(...)`, `operator=(...)` — assign the underlying value. | ||
|
|
||
| Example: | ||
|
|
||
| ```cpp | ||
| Person p; | ||
| p.town = "Springfield"; | ||
| p.town.add_comment("The town where Homer lives"); | ||
| std::string s = p.town.value(); | ||
liuzicheng1987 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| ``` | ||
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
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.
Uh oh!
There was an error while loading. Please reload this page.