From 138b68b9f5d60fe128407573d82dda292a9616d5 Mon Sep 17 00:00:00 2001 From: funworks Date: Fri, 6 Jun 2025 05:02:08 +0000 Subject: [PATCH] chore: add cptr type polymorphism --- CMakeLists.txt | 3 ++ rt_polymorphysm/profile/CMakeLists.txt | 2 + rt_polymorphysm/profile/cptr1/CMakeLists.txt | 2 + .../profile/cptr1/light_sensor.hpp | 36 ++++++++++++++++++ rt_polymorphysm/profile/cptr1/main.cpp | 28 ++++++++++++++ .../profile/cptr1/sensor_interface.hpp | 14 +++++++ rt_polymorphysm/profile/cptr1/temp_sensor.hpp | 33 +++++++++++++++++ rt_polymorphysm/profile/cptr2/CMakeLists.txt | 2 + .../profile/cptr2/light_sensor.hpp | 29 +++++++++++++++ rt_polymorphysm/profile/cptr2/main.cpp | 37 +++++++++++++++++++ .../profile/cptr2/sensor_interface.hpp | 12 ++++++ rt_polymorphysm/profile/cptr2/temp_sensor.hpp | 29 +++++++++++++++ 12 files changed, 227 insertions(+) create mode 100644 rt_polymorphysm/profile/cptr1/CMakeLists.txt create mode 100644 rt_polymorphysm/profile/cptr1/light_sensor.hpp create mode 100644 rt_polymorphysm/profile/cptr1/main.cpp create mode 100644 rt_polymorphysm/profile/cptr1/sensor_interface.hpp create mode 100644 rt_polymorphysm/profile/cptr1/temp_sensor.hpp create mode 100644 rt_polymorphysm/profile/cptr2/CMakeLists.txt create mode 100644 rt_polymorphysm/profile/cptr2/light_sensor.hpp create mode 100644 rt_polymorphysm/profile/cptr2/main.cpp create mode 100644 rt_polymorphysm/profile/cptr2/sensor_interface.hpp create mode 100644 rt_polymorphysm/profile/cptr2/temp_sensor.hpp diff --git a/CMakeLists.txt b/CMakeLists.txt index 04c9c61..6d944eb 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -1,6 +1,9 @@ cmake_minimum_required(VERSION 3.10) project(profile_map) +set(CMAKE_CXX_STANDARD 11) +set(CMAKE_CXX_STANDARD_REQUIRED ON) + include (cmake/helper.cmake) enable_testing() diff --git a/rt_polymorphysm/profile/CMakeLists.txt b/rt_polymorphysm/profile/CMakeLists.txt index 88932ad..c3af428 100644 --- a/rt_polymorphysm/profile/CMakeLists.txt +++ b/rt_polymorphysm/profile/CMakeLists.txt @@ -1,2 +1,4 @@ add_subdirectory(cptr) +add_subdirectory(cptr1) +add_subdirectory(cptr2) add_subdirectory(virtual) \ No newline at end of file diff --git a/rt_polymorphysm/profile/cptr1/CMakeLists.txt b/rt_polymorphysm/profile/cptr1/CMakeLists.txt new file mode 100644 index 0000000..b990cd7 --- /dev/null +++ b/rt_polymorphysm/profile/cptr1/CMakeLists.txt @@ -0,0 +1,2 @@ +set(cptr1-interface_SOURCES main.cpp) +configure_target(cptr1-interface ${cptr1-interface_SOURCES}) diff --git a/rt_polymorphysm/profile/cptr1/light_sensor.hpp b/rt_polymorphysm/profile/cptr1/light_sensor.hpp new file mode 100644 index 0000000..29d1cc7 --- /dev/null +++ b/rt_polymorphysm/profile/cptr1/light_sensor.hpp @@ -0,0 +1,36 @@ +#ifndef LIGHT_SENSOR_HPP +#define LIGHT_SENSOR_HPP + +#include +#include +#include "sensor_interface.hpp" + +struct LightSensor { + float bias = 0; + + void init() { + bias = 10.0f; + printf("[LightSensor] Initialized\n"); + } + + bool read(float& out) { + out = 1000.0f + bias; + printf("[LightSensor] Read: %.2f\n", out); + return true; + } + + void shutdown() { + printf("[LightSensor] Shutdown\n"); + } + + SensorT to_interface() { + return { + this, + [](void* ctx) { static_cast(ctx)->init(); }, + [](void* ctx, float& out) { return static_cast(ctx)->read(out); }, + [](void* ctx) { static_cast(ctx)->shutdown(); } + }; + } +}; + +#endif diff --git a/rt_polymorphysm/profile/cptr1/main.cpp b/rt_polymorphysm/profile/cptr1/main.cpp new file mode 100644 index 0000000..ce1abee --- /dev/null +++ b/rt_polymorphysm/profile/cptr1/main.cpp @@ -0,0 +1,28 @@ +#include "temp_sensor.hpp" +#include "light_sensor.hpp" + +void use_sensor(SensorT& sensor) { + sensor.init(sensor.context); + float value; + if (sensor.read(sensor.context, value)) { + printf("-> Value: %.2f\n", value); + } + sensor.shutdown(sensor.context); +} + +// ======= Main ======= +int main() { + TempSensor ts; + LightSensor ls; + + SensorT tsIf = ts.to_interface(); + SensorT lsIf = ls.to_interface(); + + printf("\n== Temp Sensor ==\n"); + use_sensor(tsIf); + + printf("\n== Light Sensor ==\n"); + use_sensor(lsIf); + + return 0; +} diff --git a/rt_polymorphysm/profile/cptr1/sensor_interface.hpp b/rt_polymorphysm/profile/cptr1/sensor_interface.hpp new file mode 100644 index 0000000..78a79c7 --- /dev/null +++ b/rt_polymorphysm/profile/cptr1/sensor_interface.hpp @@ -0,0 +1,14 @@ +#ifndef SENSOR_INTERFACE_HPP +#define SENSOR_INTERFACE_HPP + +#include + +struct SensorT { + void* context; + + void (*init)(void* ctx); + bool (*read)(void* ctx, float& out); + void (*shutdown)(void* ctx); +}; + +#endif // SENSOR_INTERFACE_HPP diff --git a/rt_polymorphysm/profile/cptr1/temp_sensor.hpp b/rt_polymorphysm/profile/cptr1/temp_sensor.hpp new file mode 100644 index 0000000..d401972 --- /dev/null +++ b/rt_polymorphysm/profile/cptr1/temp_sensor.hpp @@ -0,0 +1,33 @@ +#ifndef TEMP_SENSOR_HPP +#define TEMP_SENSOR_HPP + +#include +#include +#include "sensor_interface.hpp" + +struct TempSensor { + void init(uint8_t mode) { + printf("[TempSensor] Init with mode: %d\n", mode); + } + + bool read(float& out) { + out = 25.0f; + printf("[TempSensor] Read value: %.2f\n", out); + return true; + } + + void shutdown() { + printf("[TempSensor] Shutdown\n"); + } + + SensorT to_interface() { + return { + this, + [](void* ctx) { static_cast(ctx)->init(0); }, + [](void* ctx, float& out) { return static_cast(ctx)->read(out); }, + [](void* ctx) { static_cast(ctx)->shutdown(); } + }; + } +}; + +#endif diff --git a/rt_polymorphysm/profile/cptr2/CMakeLists.txt b/rt_polymorphysm/profile/cptr2/CMakeLists.txt new file mode 100644 index 0000000..3749c61 --- /dev/null +++ b/rt_polymorphysm/profile/cptr2/CMakeLists.txt @@ -0,0 +1,2 @@ +set(cptr2-interface_SOURCES main.cpp temp_sensor.cpp light_sensor.cpp) +configure_target(cptr2-interface ${cptr2-interface_SOURCES}) diff --git a/rt_polymorphysm/profile/cptr2/light_sensor.hpp b/rt_polymorphysm/profile/cptr2/light_sensor.hpp new file mode 100644 index 0000000..daec9f5 --- /dev/null +++ b/rt_polymorphysm/profile/cptr2/light_sensor.hpp @@ -0,0 +1,29 @@ +#ifndef LIGHT_SENSOR_HPP +#define LIGHT_SENSOR_HPP + +#include +#include +#include "sensor_interface.hpp" + +struct LightSensor { + float bias; + float value; +}; + +void light_sensor_init(LightSensor* sensor) { + sensor->bias = 10.0f; + printf("[LightSensor] Initialized\n"); +} + +bool light_sensor_read(LightSensor* sensor, float& out) { + sensor->value = 1000.0f + sensor->bias; + out = sensor->value; + printf("[LightSensor] Read: %.2f\n", out); + return true; +} + +void light_sensor_shutdown(LightSensor* sensor) { + printf("[LightSensor] Shutdown\n"); +} + +#endif diff --git a/rt_polymorphysm/profile/cptr2/main.cpp b/rt_polymorphysm/profile/cptr2/main.cpp new file mode 100644 index 0000000..c5fb175 --- /dev/null +++ b/rt_polymorphysm/profile/cptr2/main.cpp @@ -0,0 +1,37 @@ +#include "temp_sensor.hpp" +#include "light_sensor.hpp" + +void use_sensor(SensorT& sensor, void* sensor_instance) { + sensor.init(sensor_instance); + float value; + if (sensor.read(sensor_instance, value)) { + printf("-> Value: %.2f\n", value); + } + sensor.shutdown(sensor_instance); +} + +// ======= Main ======= +int main() { + TempSensor ts; + LightSensor ls; + + SensorT tsIf = { + (void (*)(void*))temp_sensor_init, + (bool (*)(void*, float&))temp_sensor_read, + (void (*)(void*))temp_sensor_shutdown + }; + + SensorT lsIf = { + (void (*)(void*))light_sensor_init, + (bool (*)(void*, float&))light_sensor_read, + (void (*)(void*))light_sensor_shutdown + }; + + printf("\n== Temp Sensor ==\n"); + use_sensor(tsIf, &ts); + + printf("\n== Light Sensor ==\n"); + use_sensor(lsIf, &ls); + + return 0; +} diff --git a/rt_polymorphysm/profile/cptr2/sensor_interface.hpp b/rt_polymorphysm/profile/cptr2/sensor_interface.hpp new file mode 100644 index 0000000..c00a13f --- /dev/null +++ b/rt_polymorphysm/profile/cptr2/sensor_interface.hpp @@ -0,0 +1,12 @@ +#ifndef SENSOR_INTERFACE_HPP +#define SENSOR_INTERFACE_HPP + +#include + +struct SensorT { + void (*init)(void* sensor_instance); + bool (*read)(void* sensor_instance, float& out); + void (*shutdown)(void* sensor_instance); +}; + +#endif // SENSOR_INTERFACE_HPP diff --git a/rt_polymorphysm/profile/cptr2/temp_sensor.hpp b/rt_polymorphysm/profile/cptr2/temp_sensor.hpp new file mode 100644 index 0000000..157d613 --- /dev/null +++ b/rt_polymorphysm/profile/cptr2/temp_sensor.hpp @@ -0,0 +1,29 @@ +#ifndef TEMP_SENSOR_HPP +#define TEMP_SENSOR_HPP + +#include +#include +#include "sensor_interface.hpp" + +struct TempSensor { + uint8_t mode; + float value; +}; + +void temp_sensor_init(TempSensor* sensor) { + sensor->mode = 0; + printf("[TempSensor] Init with mode: %d\n", sensor->mode); +} + +bool temp_sensor_read(TempSensor* sensor, float& out) { + sensor->value = 25.0f; + out = sensor->value; + printf("[TempSensor] Read value: %.2f\n", out); + return true; +} + +void temp_sensor_shutdown(TempSensor* sensor) { + printf("[TempSensor] Shutdown\n"); +} + +#endif