Skip to content
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

fix(hesai_hw_interface): add error handling #131

Merged
merged 12 commits into from
May 21, 2024
Merged
Show file tree
Hide file tree
Changes from 11 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 4 additions & 1 deletion .cspell.json
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,9 @@
"srvs",
"manc",
"ipaddr",
"ntoa"
"ntoa",
"piyushk",
"Piyush",
"fout"
]
}
53 changes: 53 additions & 0 deletions nebula_common/include/nebula_common/util/expected.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
#pragma once

#include <variant>

namespace nebula
{
namespace util
{

/// @brief A poor man's backport of C++23's std::expected.
///
/// At any given time, holds exactly one of: expected value, or error value.
/// Provides functions for easy exception handling.
///
/// More info here: https://en.cppreference.com/w/cpp/utility/expected
///
/// @tparam T Type of the expected value
/// @tparam E Error type
template <typename T, typename E>
struct expected
{
bool has_value() { return std::holds_alternative<T>(value_); }

T value() { return std::get<T>(value_); }
mojomex marked this conversation as resolved.
Show resolved Hide resolved

T value_or(const T & default_)
{
if (has_value()) return value();
return default_;
}

T value_or_throw(const std::string & error_msg) {
if (has_value()) return value();
throw std::runtime_error(error_msg);
}

E error() { return std::get<E>(value_); }
mojomex marked this conversation as resolved.
Show resolved Hide resolved

E error_or(const E & default_) {
if (!has_value()) return error();
return default_;
}

expected(const T & value) { value_ = value; }

expected(const E & error) { value_ = error; }

private:
std::variant<T, E> value_;
};

} // namespace util
} // namespace nebula
Loading
Loading