Skip to content
This repository has been archived by the owner on Jan 18, 2024. It is now read-only.

Commit

Permalink
:bang:✨ libhal 3.0.0
Browse files Browse the repository at this point in the history
Migrate from Boost.LEAF to C++ exceptions
  • Loading branch information
Khalil Estell committed Oct 20, 2023
1 parent 4da8b69 commit 9b98f70
Show file tree
Hide file tree
Showing 48 changed files with 380 additions and 836 deletions.
2 changes: 0 additions & 2 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -47,9 +47,7 @@ libhal_unit_test(SOURCES
tests/main.test.cpp

PACKAGES
boost-leaf
tl-function-ref

LINK_LIBRARIES
boost::leaf
tl::function-ref)
15 changes: 1 addition & 14 deletions conanfile.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@

class libhal_conan(ConanFile):
name = "libhal"
version = "2.0.1"
version = "3.0.0"
license = "Apache-2.0"
url = "https://github.com/conan-io/conan-center-index"
homepage = "https://libhal.github.io/libhal"
Expand All @@ -50,10 +50,6 @@ def _compilers_minimum_version(self):
"apple-clang": "14.0.0"
}

@property
def _bare_metal(self):
return self.settings.os == "baremetal"

def validate(self):
if self.settings.get_safe("compiler.cppstd"):
check_min_cppstd(self, self._min_cppstd)
Expand All @@ -65,7 +61,6 @@ def build_requirements(self):

def requirements(self):
self.requires("tl-function-ref/1.0.0")
self.requires("boost-leaf/1.81.0")

def layout(self):
cmake_layout(self)
Expand All @@ -89,13 +84,5 @@ def package_info(self):
self.cpp_info.libdirs = []
self.cpp_info.resdirs = []

if self._bare_metal:
self.cpp_info.defines = [
"BOOST_LEAF_EMBEDDED",
# TODO(#694): Remove this or have it be configurable. Users
# should not be forced to operate without thread support
"BOOST_LEAF_NO_THREADS"
]

def package_id(self):
self.info.clear()
7 changes: 3 additions & 4 deletions include/libhal/accelerometer.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@

#pragma once

#include "error.hpp"
#include "units.hpp"

namespace hal {
Expand Down Expand Up @@ -53,16 +52,16 @@ class accelerometer
/**
* @brief Read the latest acceleration sensed by the device
*
* @return result<read_t> - acceleration data
* @return read_t - acceleration data
*/
[[nodiscard]] result<read_t> read()
[[nodiscard]] read_t read()
{
return driver_read();
}

virtual ~accelerometer() = default;

private:
virtual result<read_t> driver_read() = 0;
virtual read_t driver_read() = 0;
};
} // namespace hal
8 changes: 3 additions & 5 deletions include/libhal/adc.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,6 @@

#pragma once

#include "error.hpp"

namespace hal {
/**
* @brief Analog to Digital Converter (ADC) hardware abstraction interface.
Expand Down Expand Up @@ -50,16 +48,16 @@ class adc
/**
* @brief Sample the analog to digital converter and return the result
*
* @return result<read_t> - the sampled adc value
* @return read_t - the sampled adc value
*/
[[nodiscard]] result<read_t> read()
[[nodiscard]] read_t read()
{
return driver_read();
}

virtual ~adc() = default;

private:
virtual result<read_t> driver_read() = 0;
virtual read_t driver_read() = 0;
};
} // namespace hal
19 changes: 9 additions & 10 deletions include/libhal/can.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@
#include <array>
#include <cstdint>

#include "error.hpp"
#include "functional.hpp"
#include "units.hpp"

Expand Down Expand Up @@ -149,10 +148,10 @@ class can
* @brief Configure this can bus port to match the settings supplied
*
* @param p_settings - settings to apply to can driver
* @return status - success or failure
* @return void - success or failure
* @throws std::errc::invalid_argument if the settings could not be achieved.
*/
[[nodiscard]] status configure(const settings& p_settings)
void configure(const settings& p_settings)
{
return driver_configure(p_settings);
}
Expand All @@ -176,11 +175,11 @@ class can
* If this occurs, this function must be called to re-enable bus
* communication.
*
* @return status - success or failure. In the case this function fails
* @return void - success or failure. In the case this function fails
* repeatedly, it is advised to simply not use the bus anymore as something is
* critical wrong and may not be recoverable.
*/
[[nodiscard]] status bus_on()
void bus_on()
{
return driver_bus_on();
}
Expand All @@ -189,13 +188,13 @@ class can
* @brief Send a can message
*
* @param p_message - the message to be sent
* @return result<send_t> - success or failure
* @return send_t - success or failure
* @throws std::errc::network_down - if the can device is in the "bus-off"
* state. This can happen if a critical fault in the bus has occurred. A call
* to `bus_on()` will need to be issued to attempt to talk on the bus again.
* See `bus_on()` for more details.
*/
[[nodiscard]] result<send_t> send(const message_t& p_message)
send_t send(const message_t& p_message)
{
return driver_send(p_message);
}
Expand All @@ -216,9 +215,9 @@ class can
virtual ~can() = default;

private:
virtual status driver_configure(const settings& p_settings) = 0;
virtual status driver_bus_on() = 0;
virtual result<send_t> driver_send(const message_t& p_message) = 0;
virtual void driver_configure(const settings& p_settings) = 0;
virtual void driver_bus_on() = 0;
virtual send_t driver_send(const message_t& p_message) = 0;
virtual void driver_on_receive(hal::callback<handler> p_handler) = 0;
};
} // namespace hal
9 changes: 4 additions & 5 deletions include/libhal/dac.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,9 @@

#pragma once

#include <algorithm>
#include <cstdint>

#include "error.hpp"

namespace hal {
/**
* @brief Digital to Analog Converter (DAC) hardware abstraction interface.
Expand Down Expand Up @@ -58,9 +57,9 @@ class dac
*
* @param p_percentage - value from 0.0f to +1.0f representing the proportion
* of the output voltage from the Vss to Vcc.
* @return result<write_t> - success or failure
* @return write_t - success or failure
*/
[[nodiscard]] result<write_t> write(float p_percentage)
write_t write(float p_percentage)
{
auto clamped_percentage = std::clamp(p_percentage, 0.0f, 1.0f);
return driver_write(clamped_percentage);
Expand All @@ -69,6 +68,6 @@ class dac
virtual ~dac() = default;

private:
virtual result<write_t> driver_write(float p_percentage) = 0;
virtual write_t driver_write(float p_percentage) = 0;
};
} // namespace hal
7 changes: 3 additions & 4 deletions include/libhal/distance_sensor.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@

#pragma once

#include "error.hpp"
#include "units.hpp"

namespace hal {
Expand Down Expand Up @@ -96,16 +95,16 @@ class distance_sensor
/**
* @brief Read the current distance measured by the device
*
* @return result<read_t> - distance data
* @return read_t - distance data
*/
[[nodiscard]] result<read_t> read()
[[nodiscard]] read_t read()
{
return driver_read();
}

virtual ~distance_sensor() = default;

private:
virtual result<read_t> driver_read() = 0;
virtual read_t driver_read() = 0;
};
} // namespace hal
53 changes: 7 additions & 46 deletions include/libhal/error.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -14,66 +14,27 @@

#pragma once

#include <boost/leaf.hpp>
#include <system_error>

#define HAL_CHECK BOOST_LEAF_CHECK
#include <type_traits>

namespace hal {

template<typename T, T... value>
using match = boost::leaf::match<T, value...>;
template<class T>
using result = boost::leaf::result<T>;
using status = result<void>;
using error_handler = void(void);

inline error_handler* on_error_callback = nullptr;

/**
* @brief a readability function for returning successful results;
*
* For functions that return `status`, rather than returning `{}` to default
* initialize the status object as "success", use this function to make it more
* clear to the reader.
*
* EXAMPLE:
*
* hal::status some_function() {
* return hal::success();
* }
*
* @return status - that is always successful
*/
inline status success()
template<class thrown_t>
void safe_throw(thrown_t&& p_thrown_object)
{
// Default initialize the status object using the brace initialization, which
// will set the status to the default "success" state.
status successful_status{};
return successful_status;
}
static_assert(
std::is_trivially_destructible_v<thrown_t>,
"safe_throw() only works with trivially destructible thrown types");

template<class TryBlock, class... H>
[[nodiscard]] constexpr auto attempt(TryBlock&& p_try_block, H&&... p_handlers)
{
return boost::leaf::try_handle_some(p_try_block, p_handlers...);
}

template<class TryBlock, class... H>
[[nodiscard]] constexpr auto attempt_all(TryBlock&& p_try_block,
H&&... p_handlers)
{
return boost::leaf::try_handle_all(p_try_block, p_handlers...);
}

template<class... Item>
[[nodiscard]] inline auto new_error(Item&&... p_item)
{
if (on_error_callback) {
on_error_callback();
}

return boost::leaf::new_error(std::forward<Item>(p_item)...);
throw p_thrown_object;
}

[[noreturn]] inline void halt()
Expand Down
3 changes: 2 additions & 1 deletion include/libhal/functional.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,10 @@

#include <cstdint>

#include "third_party/inplace_function.hpp"
#include <tl/function_ref.hpp>

#include "third_party/inplace_function.hpp"

namespace hal {
/**
* @brief Definition of a non-owning callable object
Expand Down
7 changes: 3 additions & 4 deletions include/libhal/gyroscope.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@

#pragma once

#include "error.hpp"
#include "units.hpp"

namespace hal {
Expand Down Expand Up @@ -52,16 +51,16 @@ class gyroscope
/**
* @brief Read the latest angular velocity sensed by the device
*
* @return result<read_t> - angular velocity data
* @return read_t - angular velocity data
*/
[[nodiscard]] result<read_t> read()
[[nodiscard]] read_t read()
{
return driver_read();
}

virtual ~gyroscope() = default;

private:
virtual result<read_t> driver_read() = 0;
virtual read_t driver_read() = 0;
};
} // namespace hal
Loading

0 comments on commit 9b98f70

Please sign in to comment.