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

Commit bedc4f4

Browse files
author
Khalil Estell
committed
💥✨ libhal 3.0.0
Migrate from Boost.LEAF to C++ exceptions
1 parent 9d3a84f commit bedc4f4

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

48 files changed

+465
-853
lines changed

CMakeLists.txt

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -47,9 +47,7 @@ libhal_unit_test(SOURCES
4747
tests/main.test.cpp
4848

4949
PACKAGES
50-
boost-leaf
5150
tl-function-ref
5251

5352
LINK_LIBRARIES
54-
boost::leaf
5553
tl::function-ref)

conanfile.py

Lines changed: 1 addition & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@
2626

2727
class libhal_conan(ConanFile):
2828
name = "libhal"
29-
version = "2.0.1"
29+
version = "3.0.0-alpha.1"
3030
license = "Apache-2.0"
3131
url = "https://github.com/conan-io/conan-center-index"
3232
homepage = "https://libhal.github.io/libhal"
@@ -50,10 +50,6 @@ def _compilers_minimum_version(self):
5050
"apple-clang": "14.0.0"
5151
}
5252

53-
@property
54-
def _bare_metal(self):
55-
return self.settings.os == "baremetal"
56-
5753
def validate(self):
5854
if self.settings.get_safe("compiler.cppstd"):
5955
check_min_cppstd(self, self._min_cppstd)
@@ -65,7 +61,6 @@ def build_requirements(self):
6561

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

7065
def layout(self):
7166
cmake_layout(self)
@@ -89,13 +84,5 @@ def package_info(self):
8984
self.cpp_info.libdirs = []
9085
self.cpp_info.resdirs = []
9186

92-
if self._bare_metal:
93-
self.cpp_info.defines = [
94-
"BOOST_LEAF_EMBEDDED",
95-
# TODO(#694): Remove this or have it be configurable. Users
96-
# should not be forced to operate without thread support
97-
"BOOST_LEAF_NO_THREADS"
98-
]
99-
10087
def package_id(self):
10188
self.info.clear()

include/libhal/accelerometer.hpp

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,6 @@
1414

1515
#pragma once
1616

17-
#include "error.hpp"
1817
#include "units.hpp"
1918

2019
namespace hal {
@@ -53,16 +52,16 @@ class accelerometer
5352
/**
5453
* @brief Read the latest acceleration sensed by the device
5554
*
56-
* @return result<read_t> - acceleration data
55+
* @return read_t - acceleration data
5756
*/
58-
[[nodiscard]] result<read_t> read()
57+
[[nodiscard]] read_t read()
5958
{
6059
return driver_read();
6160
}
6261

6362
virtual ~accelerometer() = default;
6463

6564
private:
66-
virtual result<read_t> driver_read() = 0;
65+
virtual read_t driver_read() = 0;
6766
};
6867
} // namespace hal

include/libhal/adc.hpp

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,6 @@
1414

1515
#pragma once
1616

17-
#include "error.hpp"
18-
1917
namespace hal {
2018
/**
2119
* @brief Analog to Digital Converter (ADC) hardware abstraction interface.
@@ -50,16 +48,16 @@ class adc
5048
/**
5149
* @brief Sample the analog to digital converter and return the result
5250
*
53-
* @return result<read_t> - the sampled adc value
51+
* @return read_t - the sampled adc value
5452
*/
55-
[[nodiscard]] result<read_t> read()
53+
[[nodiscard]] read_t read()
5654
{
5755
return driver_read();
5856
}
5957

6058
virtual ~adc() = default;
6159

6260
private:
63-
virtual result<read_t> driver_read() = 0;
61+
virtual read_t driver_read() = 0;
6462
};
6563
} // namespace hal

include/libhal/can.hpp

Lines changed: 9 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,6 @@
1717
#include <array>
1818
#include <cstdint>
1919

20-
#include "error.hpp"
2120
#include "functional.hpp"
2221
#include "units.hpp"
2322

@@ -149,10 +148,10 @@ class can
149148
* @brief Configure this can bus port to match the settings supplied
150149
*
151150
* @param p_settings - settings to apply to can driver
152-
* @return status - success or failure
151+
* @return void - success or failure
153152
* @throws std::errc::invalid_argument if the settings could not be achieved.
154153
*/
155-
[[nodiscard]] status configure(const settings& p_settings)
154+
void configure(const settings& p_settings)
156155
{
157156
return driver_configure(p_settings);
158157
}
@@ -176,11 +175,11 @@ class can
176175
* If this occurs, this function must be called to re-enable bus
177176
* communication.
178177
*
179-
* @return status - success or failure. In the case this function fails
178+
* @return void - success or failure. In the case this function fails
180179
* repeatedly, it is advised to simply not use the bus anymore as something is
181180
* critical wrong and may not be recoverable.
182181
*/
183-
[[nodiscard]] status bus_on()
182+
void bus_on()
184183
{
185184
return driver_bus_on();
186185
}
@@ -189,13 +188,13 @@ class can
189188
* @brief Send a can message
190189
*
191190
* @param p_message - the message to be sent
192-
* @return result<send_t> - success or failure
191+
* @return send_t - success or failure
193192
* @throws std::errc::network_down - if the can device is in the "bus-off"
194193
* state. This can happen if a critical fault in the bus has occurred. A call
195194
* to `bus_on()` will need to be issued to attempt to talk on the bus again.
196195
* See `bus_on()` for more details.
197196
*/
198-
[[nodiscard]] result<send_t> send(const message_t& p_message)
197+
send_t send(const message_t& p_message)
199198
{
200199
return driver_send(p_message);
201200
}
@@ -216,9 +215,9 @@ class can
216215
virtual ~can() = default;
217216

218217
private:
219-
virtual status driver_configure(const settings& p_settings) = 0;
220-
virtual status driver_bus_on() = 0;
221-
virtual result<send_t> driver_send(const message_t& p_message) = 0;
218+
virtual void driver_configure(const settings& p_settings) = 0;
219+
virtual void driver_bus_on() = 0;
220+
virtual send_t driver_send(const message_t& p_message) = 0;
222221
virtual void driver_on_receive(hal::callback<handler> p_handler) = 0;
223222
};
224223
} // namespace hal

include/libhal/dac.hpp

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -14,10 +14,9 @@
1414

1515
#pragma once
1616

17+
#include <algorithm>
1718
#include <cstdint>
1819

19-
#include "error.hpp"
20-
2120
namespace hal {
2221
/**
2322
* @brief Digital to Analog Converter (DAC) hardware abstraction interface.
@@ -58,9 +57,9 @@ class dac
5857
*
5958
* @param p_percentage - value from 0.0f to +1.0f representing the proportion
6059
* of the output voltage from the Vss to Vcc.
61-
* @return result<write_t> - success or failure
60+
* @return write_t - success or failure
6261
*/
63-
[[nodiscard]] result<write_t> write(float p_percentage)
62+
write_t write(float p_percentage)
6463
{
6564
auto clamped_percentage = std::clamp(p_percentage, 0.0f, 1.0f);
6665
return driver_write(clamped_percentage);
@@ -69,6 +68,6 @@ class dac
6968
virtual ~dac() = default;
7069

7170
private:
72-
virtual result<write_t> driver_write(float p_percentage) = 0;
71+
virtual write_t driver_write(float p_percentage) = 0;
7372
};
7473
} // namespace hal

include/libhal/distance_sensor.hpp

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,6 @@
1414

1515
#pragma once
1616

17-
#include "error.hpp"
1817
#include "units.hpp"
1918

2019
namespace hal {
@@ -96,16 +95,16 @@ class distance_sensor
9695
/**
9796
* @brief Read the current distance measured by the device
9897
*
99-
* @return result<read_t> - distance data
98+
* @return read_t - distance data
10099
*/
101-
[[nodiscard]] result<read_t> read()
100+
[[nodiscard]] read_t read()
102101
{
103102
return driver_read();
104103
}
105104

106105
virtual ~distance_sensor() = default;
107106

108107
private:
109-
virtual result<read_t> driver_read() = 0;
108+
virtual read_t driver_read() = 0;
110109
};
111110
} // namespace hal

include/libhal/error.hpp

Lines changed: 7 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -14,66 +14,27 @@
1414

1515
#pragma once
1616

17-
#include <boost/leaf.hpp>
1817
#include <system_error>
19-
20-
#define HAL_CHECK BOOST_LEAF_CHECK
18+
#include <type_traits>
2119

2220
namespace hal {
2321

24-
template<typename T, T... value>
25-
using match = boost::leaf::match<T, value...>;
26-
template<class T>
27-
using result = boost::leaf::result<T>;
28-
using status = result<void>;
2922
using error_handler = void(void);
3023

3124
inline error_handler* on_error_callback = nullptr;
3225

33-
/**
34-
* @brief a readability function for returning successful results;
35-
*
36-
* For functions that return `status`, rather than returning `{}` to default
37-
* initialize the status object as "success", use this function to make it more
38-
* clear to the reader.
39-
*
40-
* EXAMPLE:
41-
*
42-
* hal::status some_function() {
43-
* return hal::success();
44-
* }
45-
*
46-
* @return status - that is always successful
47-
*/
48-
inline status success()
26+
template<class thrown_t>
27+
void safe_throw(thrown_t&& p_thrown_object)
4928
{
50-
// Default initialize the status object using the brace initialization, which
51-
// will set the status to the default "success" state.
52-
status successful_status{};
53-
return successful_status;
54-
}
29+
static_assert(
30+
std::is_trivially_destructible_v<thrown_t>,
31+
"safe_throw() only works with trivially destructible thrown types");
5532

56-
template<class TryBlock, class... H>
57-
[[nodiscard]] constexpr auto attempt(TryBlock&& p_try_block, H&&... p_handlers)
58-
{
59-
return boost::leaf::try_handle_some(p_try_block, p_handlers...);
60-
}
61-
62-
template<class TryBlock, class... H>
63-
[[nodiscard]] constexpr auto attempt_all(TryBlock&& p_try_block,
64-
H&&... p_handlers)
65-
{
66-
return boost::leaf::try_handle_all(p_try_block, p_handlers...);
67-
}
68-
69-
template<class... Item>
70-
[[nodiscard]] inline auto new_error(Item&&... p_item)
71-
{
7233
if (on_error_callback) {
7334
on_error_callback();
7435
}
7536

76-
return boost::leaf::new_error(std::forward<Item>(p_item)...);
37+
throw p_thrown_object;
7738
}
7839

7940
[[noreturn]] inline void halt()

include/libhal/functional.hpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,9 +16,10 @@
1616

1717
#include <cstdint>
1818

19-
#include "third_party/inplace_function.hpp"
2019
#include <tl/function_ref.hpp>
2120

21+
#include "third_party/inplace_function.hpp"
22+
2223
namespace hal {
2324
/**
2425
* @brief Definition of a non-owning callable object

include/libhal/gyroscope.hpp

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,6 @@
1414

1515
#pragma once
1616

17-
#include "error.hpp"
1817
#include "units.hpp"
1918

2019
namespace hal {
@@ -52,16 +51,16 @@ class gyroscope
5251
/**
5352
* @brief Read the latest angular velocity sensed by the device
5453
*
55-
* @return result<read_t> - angular velocity data
54+
* @return read_t - angular velocity data
5655
*/
57-
[[nodiscard]] result<read_t> read()
56+
[[nodiscard]] read_t read()
5857
{
5958
return driver_read();
6059
}
6160

6261
virtual ~gyroscope() = default;
6362

6463
private:
65-
virtual result<read_t> driver_read() = 0;
64+
virtual read_t driver_read() = 0;
6665
};
6766
} // namespace hal

0 commit comments

Comments
 (0)