-
Notifications
You must be signed in to change notification settings - Fork 163
Added rfl::Commented; resolves #597 #602
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
liuzicheng1987
wants to merge
13
commits into
main
Choose a base branch
from
f/comments
base: main
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
13 commits
Select commit
Hold shift + click to select a range
6b82c10
Started developing comments
liuzicheng1987 f2f2f36
Added comments to YAML
liuzicheng1987 d04cd7c
Added comments to XML
liuzicheng1987 ef33360
Add documentation for rfl::Commented feature (#600)
liuzicheng1987 d8cf631
Added comments to the docs README
liuzicheng1987 4629f9f
Add operator*
liuzicheng1987 c1e49cb
class -> struct
liuzicheng1987 40f6e40
Fix operator=
liuzicheng1987 19adda8
More sensible schema definition
liuzicheng1987 1647059
Fixed typo
liuzicheng1987 5368fce
Added XML
liuzicheng1987 89eaa14
Resolved last issue
liuzicheng1987 5409d78
Fixed typo
liuzicheng1987 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(); | ||
| ``` |
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
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,158 @@ | ||
| #ifndef RFL_COMMENTED_HPP_ | ||
| #define RFL_COMMENTED_HPP_ | ||
|
|
||
| #include <optional> | ||
| #include <string> | ||
| #include <type_traits> | ||
| #include <utility> | ||
|
|
||
| #include "default.hpp" | ||
|
|
||
| namespace rfl { | ||
|
|
||
| template <class T> | ||
| struct Commented { | ||
| public: | ||
| using Type = std::remove_cvref_t<T>; | ||
|
|
||
| Commented() : value_(Type()) {} | ||
|
|
||
| Commented(const Type& _value) : value_(_value) {} | ||
|
|
||
| Commented(Type&& _value) noexcept : value_(std::move(_value)) {} | ||
|
|
||
| Commented(const Type& _value, std::optional<std::string> _comment) | ||
| : comment_(std::move(_comment)), value_(_value) {} | ||
|
|
||
| Commented(Type&& _value, std::optional<std::string> _comment) noexcept | ||
| : comment_(std::move(_comment)), value_(std::move(_value)) {} | ||
|
|
||
| Commented(Commented&& _commented) noexcept = default; | ||
|
|
||
| Commented(const Commented& _commented) = default; | ||
|
|
||
| template <class U> | ||
| Commented(const Commented<U>& _commented) | ||
| : comment_(_commented.comment()), value_(_commented.get()) {} | ||
|
|
||
| template <class U> | ||
| Commented(Commented<U>&& _commented) noexcept | ||
| : comment_(std::move(_commented.comment())), | ||
| value_(std::move(_commented.value())) {} | ||
|
|
||
| template <class U> | ||
| requires(std::is_convertible_v<U, Type>) | ||
| Commented(const Commented<U>& _commented) | ||
| : comment_(_commented.comment()), value_(_commented.value()) {} | ||
|
|
||
| template <class U> | ||
| requires(std::is_convertible_v<U, Type>) | ||
| Commented(const U& _value) : value_(_value) {} | ||
|
|
||
| template <class U> | ||
| requires(std::is_convertible_v<U, Type>) | ||
| Commented(U&& _value) noexcept : value_(std::forward<U>(_value)) {} | ||
|
|
||
| /// Assigns the underlying object to its default value. | ||
| template <class U = Type> | ||
| requires(std::is_default_constructible_v<U>) | ||
| Commented(const Default&) : value_(Type()) {} | ||
|
|
||
| ~Commented() = default; | ||
|
|
||
| /// Sets the comment associated with the field. | ||
| void add_comment(std::string _comment) { comment_ = std::move(_comment); } | ||
|
|
||
| /// Returns the comment associated with the field, if any. | ||
| const std::optional<std::string>& comment() const { return comment_; } | ||
|
|
||
| /// Returns the underlying object. | ||
| Type& get() { return value_; } | ||
|
|
||
| /// Returns the underlying object. | ||
| const Type& get() const { return value_; } | ||
|
|
||
| /// Returns the underlying object. | ||
| Type& operator()() { return value_; } | ||
|
|
||
| /// Returns the underlying object. | ||
| const Type& operator()() const { return value_; } | ||
|
|
||
| /// Returns the underlying object. | ||
| Type& operator*() { return value_; } | ||
|
|
||
| /// Returns the underlying object. | ||
| const Type& operator*() const { return value_; } | ||
|
|
||
| /// Assigns the underlying object. | ||
| auto& operator=(const Type& _value) { | ||
| value_ = _value; | ||
| return *this; | ||
| } | ||
|
|
||
| /// Assigns the underlying object. | ||
| auto& operator=(Type&& _value) noexcept { | ||
| value_ = std::move(_value); | ||
| return *this; | ||
| } | ||
|
|
||
| /// Assigns the underlying object. | ||
| template <class U> | ||
| requires std::is_convertible_v<U, Type> | ||
| auto& operator=(const U& _value) { | ||
| value_ = _value; | ||
| return *this; | ||
| } | ||
|
|
||
| /// Assigns the underlying object to its default value. | ||
| template <class U = Type> | ||
| requires std::is_default_constructible_v<U> | ||
| auto& operator=(const Default&) { | ||
| value_ = Type(); | ||
| return *this; | ||
| } | ||
|
|
||
| /// Assigns the underlying object. | ||
| Commented& operator=(const Commented& _commented) = default; | ||
|
|
||
| /// Assigns the underlying object. | ||
| Commented& operator=(Commented&& _commented) = default; | ||
|
|
||
| /// Assigns the underlying object. | ||
| template <class U> | ||
| auto& operator=(const Commented<U>& _commented) { | ||
| value_ = _commented.get(); | ||
| comment_ = _commented.comment(); | ||
| return *this; | ||
| } | ||
|
|
||
| /// Assigns the underlying object. | ||
| template <class U> | ||
| auto& operator=(Commented<U>&& _commented) { | ||
| value_ = std::move(_commented.value_); | ||
| comment_ = std::move(_commented.comment_); | ||
| return *this; | ||
liuzicheng1987 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| } | ||
|
|
||
| /// Assigns the underlying object. | ||
| void set(const Type& _value) { value_ = _value; } | ||
|
|
||
| /// Assigns the underlying object. | ||
| void set(Type&& _value) { value_ = std::move(_value); } | ||
|
|
||
| /// Returns the underlying object. | ||
| Type& value() { return value_; } | ||
|
|
||
| /// Returns the underlying object. | ||
| const Type& value() const { return value_; } | ||
|
|
||
| /// The comment associated with the field. | ||
| std::optional<std::string> comment_; | ||
|
|
||
| /// The underlying value. | ||
| Type value_; | ||
| }; | ||
|
|
||
| } // namespace rfl | ||
|
|
||
| #endif | ||
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
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,57 @@ | ||
| #ifndef RFL_PARSING_PARSER_COMMENTED_HPP_ | ||
| #define RFL_PARSING_PARSER_COMMENTED_HPP_ | ||
|
|
||
| #include <map> | ||
| #include <optional> | ||
| #include <type_traits> | ||
|
|
||
| #include "../Commented.hpp" | ||
| #include "../Ref.hpp" | ||
| #include "../Result.hpp" | ||
| #include "../always_false.hpp" | ||
| #include "Parent.hpp" | ||
| #include "Parser_base.hpp" | ||
| #include "schema/Type.hpp" | ||
| #include "schemaful/IsSchemafulReader.hpp" | ||
| #include "schemaful/IsSchemafulWriter.hpp" | ||
| #include "schemaful/OptionalReader.hpp" | ||
| #include "supports_comments.hpp" | ||
|
|
||
| namespace rfl::parsing { | ||
|
|
||
| template <class R, class W, class T, class ProcessorsType> | ||
| requires AreReaderAndWriter<R, W, Commented<T>> | ||
| struct Parser<R, W, Commented<T>, ProcessorsType> { | ||
| using InputVarType = typename R::InputVarType; | ||
|
|
||
| using ParentType = Parent<W>; | ||
|
|
||
| static Result<Commented<T>> read(const R& _r, | ||
| const InputVarType& _var) noexcept { | ||
| return Parser<R, W, std::remove_cvref_t<T>, ProcessorsType>::read(_r, _var) | ||
| .transform([](auto&& _t) { | ||
| return Commented<T>(std::forward<decltype(_t)>(_t)); | ||
| }); | ||
| } | ||
|
|
||
| template <class P> | ||
| static void write(const W& _w, const Commented<T>& _c, const P& _parent) { | ||
| Parser<R, W, std::remove_cvref_t<T>, ProcessorsType>::write(_w, _c.get(), | ||
| _parent); | ||
| if constexpr (supports_comments<W>) { | ||
| if (_c.comment()) { | ||
| ParentType::add_comment(_w, *_c.comment(), _parent); | ||
| } | ||
| } | ||
| } | ||
|
|
||
| static schema::Type to_schema( | ||
| std::map<std::string, schema::Type>* _definitions) { | ||
| return Parser<R, W, std::remove_cvref_t<T>, ProcessorsType>::to_schema( | ||
| _definitions); | ||
| } | ||
liuzicheng1987 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| }; | ||
|
|
||
| } // namespace rfl::parsing | ||
|
|
||
| #endif | ||
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,25 @@ | ||
| #ifndef RFL_PARSING_SUPPORTSCOMMENTS_HPP_ | ||
liuzicheng1987 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| #define RFL_PARSING_SUPPORTSCOMMENTS_HPP_ | ||
|
|
||
| #include <concepts> | ||
| #include <optional> | ||
| #include <string> | ||
| #include <string_view> | ||
|
|
||
| #include "../Result.hpp" | ||
|
|
||
| namespace rfl::parsing { | ||
|
|
||
| /// Determines whether a writer supports comments. | ||
| template <class W> | ||
| concept supports_comments = | ||
| requires(W w, typename W::OutputArrayType arr, | ||
| typename W::OutputObjectType obj, std::string_view comment) { | ||
| { w.add_comment_to_array(comment, &arr) } -> std::same_as<void>; | ||
|
|
||
| { w.add_comment_to_object(comment, &obj) } -> std::same_as<void>; | ||
| }; | ||
|
|
||
| } // namespace rfl::parsing | ||
|
|
||
| #endif | ||
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.
Uh oh!
There was an error while loading. Please reload this page.