Repository navigation
[rocprofiler-systems] Add result<T, E> and use it in number parsers #15053
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
Merged
Changes from 2 commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
ce2cb75
feat(rocprofiler-systems): add result<T, E> type
adjordje-amd 5adc024
refactor(rocprofiler-systems): return result from number parsers
adjordje-amd db470ec
Refactor documentation in result.hpp
adjordje-amd f8b9a9d
fix(rocprofiler-systems): address result<T, E> review feedback
adjordje-amd 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
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
102 changes: 102 additions & 0 deletions
102
projects/rocprofiler-systems/source/lib/common/result.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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,102 @@ | ||
| // Copyright (c) Advanced Micro Devices, Inc. | ||
| // SPDX-License-Identifier: MIT | ||
|
|
||
| #pragma once | ||
|
|
||
| #include <stdexcept> | ||
| #include <utility> | ||
| #include <variant> | ||
|
|
||
| namespace rocprofsys::inline common | ||
| { | ||
| /** | ||
| * @brief Tags a value as the error side, so `result<T, E>` stays unambiguous when `T` | ||
| * and `E` are convertible to each other. | ||
| */ | ||
| template <typename E> | ||
| struct failure | ||
| { | ||
| E error; | ||
| }; | ||
|
|
||
| template <typename E> | ||
| failure(E) -> failure<E>; | ||
|
|
||
| /** | ||
| * @brief Holds either a success value `T` or an error `E`, never both. | ||
| * | ||
| * Use it for operations that can fail with a reason the caller may need. Use | ||
| * `std::optional` when "no value" is a valid answer and not a failure. | ||
| * | ||
| * @code | ||
| * using parse_result = result<int, parse_error>; | ||
| * | ||
| * parse_result parse(std::string_view text) | ||
| * { | ||
| * if(text.empty()) | ||
| * return parse_result{ failure{ parse_error::empty } }; | ||
| * return parse_result{ 42 }; | ||
| * } | ||
| * | ||
| * auto parsed = parse(text); | ||
| * if(parsed.is_error()) | ||
| * report(parsed.error()); | ||
| * else | ||
| * use(parsed.value()); | ||
| * @endcode | ||
| */ | ||
| template <typename T, typename E> | ||
| class [[nodiscard]] result | ||
| { | ||
| public: | ||
| constexpr explicit result(T value) | ||
| : m_data{ std::in_place_index<0>, std::move(value) } | ||
| {} | ||
|
|
||
| constexpr explicit result(failure<E> fail) | ||
| : m_data{ std::in_place_index<1>, std::move(fail.error) } | ||
| {} | ||
|
|
||
| [[nodiscard]] constexpr bool is_ok() const noexcept { return m_data.index() == 0; } | ||
| [[nodiscard]] constexpr bool is_error() const noexcept { return m_data.index() == 1; } | ||
|
|
||
| /// @throws std::logic_error if this holds an error. | ||
| [[nodiscard]] constexpr T& value() & | ||
| { | ||
| if(is_error()) | ||
| { | ||
| throw std::logic_error{ "result::value() called on a failure" }; | ||
| } | ||
| return std::get<0>(m_data); | ||
| } | ||
|
|
||
| /// @throws std::logic_error if this holds an error. | ||
| [[nodiscard]] constexpr const T& value() const& | ||
| { | ||
| if(is_error()) | ||
| { | ||
| throw std::logic_error{ "result::value() called on a failure" }; | ||
| } | ||
| return std::get<0>(m_data); | ||
| } | ||
|
|
||
| /// @throws std::logic_error if this holds a value. | ||
| [[nodiscard]] constexpr const E& error() const& | ||
| { | ||
| if(is_ok()) | ||
| { | ||
| throw std::logic_error{ "result::error() called on a success" }; | ||
| } | ||
| return std::get<1>(m_data); | ||
| } | ||
|
|
||
| template <typename U> | ||
| [[nodiscard]] constexpr T value_or(U&& fallback) const& | ||
| { | ||
| return is_ok() ? std::get<0>(m_data) : static_cast<T>(std::forward<U>(fallback)); | ||
| } | ||
|
|
||
| private: | ||
| std::variant<T, E> m_data; | ||
| }; | ||
| } // namespace rocprofsys::inline common | ||
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
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.