add std::optional example
This commit is contained in:
@@ -84,7 +84,7 @@ project(bare VERSION 1.0.6)
|
|||||||
|
|
||||||
enable_language(C CXX ASM)
|
enable_language(C CXX ASM)
|
||||||
|
|
||||||
set(CMAKE_CXX_STANDARD 20)
|
set(CMAKE_CXX_STANDARD 23)
|
||||||
set(CMAKE_CXX_STANDARD_REQUIRED True)
|
set(CMAKE_CXX_STANDARD_REQUIRED True)
|
||||||
|
|
||||||
# global include directories
|
# global include directories
|
||||||
|
|||||||
@@ -21,7 +21,6 @@ template <class T, std::size_t N> struct ring_buffer {
|
|||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
||||||
int main()
|
int main()
|
||||||
{
|
{
|
||||||
hal::init();
|
hal::init();
|
||||||
|
|||||||
28
Chapter07/error_handling/app/src/main_expected.cpp
Normal file
28
Chapter07/error_handling/app/src/main_expected.cpp
Normal file
@@ -0,0 +1,28 @@
|
|||||||
|
#include <cstdint>
|
||||||
|
#include <cstdio>
|
||||||
|
#include <cstdlib>
|
||||||
|
|
||||||
|
|
||||||
|
#include <stm32f072xb.h>
|
||||||
|
|
||||||
|
#include <hal.hpp>
|
||||||
|
#include <uart_stm32.hpp>
|
||||||
|
|
||||||
|
#include <retarget.hpp>
|
||||||
|
|
||||||
|
int main()
|
||||||
|
{
|
||||||
|
hal::init();
|
||||||
|
|
||||||
|
hal::uart_stm32 uart(USART2);
|
||||||
|
uart.init();
|
||||||
|
|
||||||
|
retarget::set_stdio_uart(&uart);
|
||||||
|
|
||||||
|
printf("std::expected example\r\n");
|
||||||
|
|
||||||
|
while(true)
|
||||||
|
{
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
58
Chapter07/error_handling/app/src/main_optional.cpp
Normal file
58
Chapter07/error_handling/app/src/main_optional.cpp
Normal file
@@ -0,0 +1,58 @@
|
|||||||
|
#include <cstdint>
|
||||||
|
#include <cstdio>
|
||||||
|
#include <cstdlib>
|
||||||
|
#include <optional>
|
||||||
|
|
||||||
|
#include <stm32f072xb.h>
|
||||||
|
|
||||||
|
#include <hal.hpp>
|
||||||
|
#include <uart_stm32.hpp>
|
||||||
|
|
||||||
|
#include <retarget.hpp>
|
||||||
|
|
||||||
|
struct sensor {
|
||||||
|
struct data {
|
||||||
|
int x;
|
||||||
|
int y;
|
||||||
|
};
|
||||||
|
static inline bool ret_val = true;
|
||||||
|
static std::optional<data> get_data() {
|
||||||
|
ret_val = !ret_val;
|
||||||
|
if(ret_val) {
|
||||||
|
return data{4, 5};
|
||||||
|
}
|
||||||
|
else {
|
||||||
|
return std::nullopt;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
int main()
|
||||||
|
{
|
||||||
|
hal::init();
|
||||||
|
|
||||||
|
hal::uart_stm32 uart(USART2);
|
||||||
|
uart.init();
|
||||||
|
|
||||||
|
retarget::set_stdio_uart(&uart);
|
||||||
|
|
||||||
|
printf("std::optional example\r\n");
|
||||||
|
|
||||||
|
auto get_data_from_main = [] () {
|
||||||
|
auto result = sensor::get_data();
|
||||||
|
if(result) {
|
||||||
|
printf("x = %d, y = %d\r\n", (*result).x, (*result).y);
|
||||||
|
}
|
||||||
|
else {
|
||||||
|
printf("No data!\r\n");
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
get_data_from_main();
|
||||||
|
get_data_from_main();
|
||||||
|
|
||||||
|
while(true)
|
||||||
|
{
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
Reference in New Issue
Block a user