[ox/std] Add or_value to Optional, Result
All checks were successful
Build / build (push) Successful in 2m4s

This commit is contained in:
Gary Talent 2023-12-25 00:51:17 -06:00
parent caf8d93c21
commit b869f490c3
2 changed files with 52 additions and 4 deletions

View File

@ -269,7 +269,7 @@ struct [[nodiscard]] Result {
* @param alt * @param alt
* @return value of Result or alt * @return value of Result or alt
*/ */
constexpr T orVal(T &&alt) & noexcept { constexpr T or_value(T &&alt) const& noexcept {
if (error) { if (error) {
return std::move(alt); return std::move(alt);
} }
@ -281,7 +281,7 @@ struct [[nodiscard]] Result {
* @param alt * @param alt
* @return value of Result or alt * @return value of Result or alt
*/ */
constexpr T orVal(T &&alt) && noexcept { constexpr T or_value(T &&alt) && noexcept {
if (error) { if (error) {
return std::move(alt); return std::move(alt);
} }
@ -293,7 +293,7 @@ struct [[nodiscard]] Result {
* @param alt * @param alt
* @return value of Result or alt * @return value of Result or alt
*/ */
constexpr T orVal(T const&alt) & noexcept { constexpr T or_value(T const&alt) const& noexcept {
if (error) { if (error) {
return alt; return alt;
} }
@ -305,7 +305,7 @@ struct [[nodiscard]] Result {
* @param alt * @param alt
* @return value of Result or alt * @return value of Result or alt
*/ */
constexpr T orVal(T const&alt) && noexcept { constexpr T or_value(T const&alt) && noexcept {
if (error) { if (error) {
return alt; return alt;
} }

View File

@ -64,6 +64,54 @@ class Optional {
return *m_ptr; return *m_ptr;
} }
/**
* Returns parameter alt if Result contains an error.
* @param alt
* @return value of Result or alt
*/
constexpr T or_value(T &&alt) const& noexcept {
if (!m_ptr) {
return std::move(alt);
}
return *m_ptr;
}
/**
* Returns parameter alt if Result contains an error.
* @param alt
* @return value of Result or alt
*/
constexpr T or_value(T &&alt) && noexcept {
if (!m_ptr) {
return std::move(alt);
}
return std::move(*m_ptr);
}
/**
* Returns parameter alt if Result contains an error.
* @param alt
* @return value of Result or alt
*/
constexpr T or_value(T const&alt) const& noexcept {
if (!m_ptr) {
return alt;
}
return *m_ptr;
}
/**
* Returns parameter alt if Result contains an error.
* @param alt
* @return value of Result or alt
*/
constexpr T or_value(T const&alt) && noexcept {
if (!m_ptr) {
return alt;
}
return std::move(*m_ptr);
}
constexpr T &operator*() & noexcept { constexpr T &operator*() & noexcept {
return *m_ptr; return *m_ptr;
} }