From 73bfdb5bfa6234dcdea0505ee7ffe6983fc0a67e Mon Sep 17 00:00:00 2001 From: Amar Mahmutbegovic Date: Tue, 10 Dec 2024 21:42:01 +0100 Subject: [PATCH] add compile-time observer examples --- .../observer/app/src/main_observer_ct.cpp | 82 +++++++++++++++++++ .../app/src/main_observer_ct_basic.cpp | 54 ++++++++++++ 2 files changed, 136 insertions(+) create mode 100644 Chapter15/observer/app/src/main_observer_ct.cpp create mode 100644 Chapter15/observer/app/src/main_observer_ct_basic.cpp diff --git a/Chapter15/observer/app/src/main_observer_ct.cpp b/Chapter15/observer/app/src/main_observer_ct.cpp new file mode 100644 index 0000000..b28fa8a --- /dev/null +++ b/Chapter15/observer/app/src/main_observer_ct.cpp @@ -0,0 +1,82 @@ +#include +#include + +#include + +#include + +#include +#include +#include + +#include + +#include "etl/vector.h" + +struct display { + static void update(float temp) { + printf("Displaying temperature %.2f \r\n", temp); + } +}; + +struct data_sender { + static void update(float temp) { + printf("Sending temperature %.2f \r\n", temp); + } +}; + +struct logger { + static void update(float temp) { + printf("Logging temperature %.2f \r\n", temp); + } +}; + +template +concept Updatable = requires (T, float f) { + { T::update(f) } -> std::same_as; +}; + +template +concept Notifiable = requires (T, float f) { + { T::notify(f) } -> std::same_as; +}; + +template +struct publisher { + static void notify(float temp) { + (call_update_or_notify(temp), ...); + } +private: + template + static void call_update_or_notify(float temp) { + if constexpr (Updatable) { + T::update(temp); + } else if constexpr (Notifiable) { + T::notify(temp); + } + else { + static_assert(false, "Type is not Updatable or Notifiable"); + } + } +}; + +int main() +{ + hal::init(); + + hal::uart_stm32 uart(USART2); + uart.init(); + + retarget::set_stdio_uart(&uart); + + using temp_publisher = publisher; + temp_publisher::notify(23.47); + + using temp_publisher_new = publisher; + temp_publisher_new::notify(42.42); + + + while(true) + { + } +} diff --git a/Chapter15/observer/app/src/main_observer_ct_basic.cpp b/Chapter15/observer/app/src/main_observer_ct_basic.cpp new file mode 100644 index 0000000..04cc00e --- /dev/null +++ b/Chapter15/observer/app/src/main_observer_ct_basic.cpp @@ -0,0 +1,54 @@ +#include +#include + +#include + +#include + +#include +#include +#include + +#include + +struct display { + static void update(float temp) { + printf("Displaying temperature %.2f \r\n", temp); + } +}; + +struct data_sender { + static void update(float temp) { + printf("Sending temperature %.2f \r\n", temp); + } +}; + +struct logger { + static void update(float temp) { + printf("Logging temperature %.2f \r\n", temp); + } +}; + +template +struct publisher { + static void notify(float temp) { + (Subs::update(temp), ...); + } +}; + +int main() +{ + hal::init(); + + hal::uart_stm32 uart(USART2); + uart.init(); + + retarget::set_stdio_uart(&uart); + + using temp_publisher = publisher; + temp_publisher::notify(23.47); + + while(true) + { + } +}