Skip to content

Commit

Permalink
Merge remote-tracking branch 'origin/develop' into single-node-refactor
Browse files Browse the repository at this point in the history
  • Loading branch information
mojomex committed May 21, 2024
2 parents 74388c9 + 2ec268f commit 67225bd
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 7 deletions.
43 changes: 38 additions & 5 deletions nebula_common/include/nebula_common/util/expected.hpp
Original file line number Diff line number Diff line change
@@ -1,12 +1,25 @@
#pragma once

#include <variant>
#include <string>
#include <exception>

namespace nebula
{
namespace util
{

struct bad_expected_access : public std::exception {
bad_expected_access(const std::string & msg) : std::exception(), msg_(msg) {}

const char* what() const noexcept override {
return msg_.c_str();
}

private:
const std::string msg_;
};

/// @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.
Expand All @@ -19,26 +32,46 @@ namespace util
template <typename T, typename E>
struct expected
{
/// @brief Whether the expected instance holds a value (as opposed to an error).
/// Call this before trying to access values via `value()`.
/// @return True if a value is contained, false if an error is contained
bool has_value() { return std::holds_alternative<T>(value_); }

T value() { return std::get<T>(value_); }
/// @brief Retrieve the value, or throw `bad_expected_access` if an error is contained.
/// @return The value of type `T`
T value() {
if (!has_value()) {
throw bad_expected_access("value() called but containing error");
}
return std::get<T>(value_);
}

/// @brief Return the contained value, or, if an error is contained, return the given `default_` instead.
/// @return The contained value, if any, else `default_`
T value_or(const T & default_)
{
if (has_value()) return value();
return default_;
}

/// @brief If the instance has a value, return the value, else throw std::runtime_error(error_msg)
/// @param error_msg The message to be included in the thrown exception
/// @return The value
/// @brief Return the contained value, or, if an error is contained, throw `runtime_error(error_msg)`.
/// @return The contained value if no error is thrown
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_); }
/// @brief Retrieve the error, or throw `bad_expected_access` if a value is contained.
/// @return The error of type `E`
E error() {
if (has_value()) {
throw bad_expected_access("error() called but containing value");
}
return std::get<E>(value_);
}

/// @brief Return the contained error, or, if a value is contained, return the given `default_` instead.
/// @return The contained error, if any, else `default_`
E error_or(const E & default_) {
if (!has_value()) return error();
return default_;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,6 @@ HesaiHwInterface::ptc_cmd_result_t HesaiHwInterface::SendReceive(
auto recv_buf = std::make_shared<std::vector<uint8_t>>();
auto response_complete = std::make_shared<bool>(false);

// Low byte is for PTC error code, the rest is nebula-specific
auto error_code = std::make_shared<ptc_error_t>();

std::stringstream ss;
Expand Down
2 changes: 1 addition & 1 deletion nebula_messages/nebula_msgs/package.xml
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
<license>Apache 2</license>
<author>Tier IV</author>

<buildtool_depend>ament_cmake</buildtool_depend>
<buildtool_depend>ament_cmake_auto</buildtool_depend>
<buildtool_depend>rosidl_default_generators</buildtool_depend>

<depend>builtin_interfaces</depend>
Expand Down

0 comments on commit 67225bd

Please sign in to comment.