diff --git a/CMakeLists.txt b/CMakeLists.txt index 6e70259..04c9c61 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -11,6 +11,7 @@ add_subdirectory(smart_ptr) add_subdirectory(array) add_subdirectory(string) add_subdirectory(heap) +add_subdirectory(rt_polymorphysm) if (CMAKE_SYSTEM_NAME STREQUAL "Linux") add_subdirectory(threads) diff --git a/cmake/helper.cmake b/cmake/helper.cmake index 3ffd1b1..a1275c7 100644 --- a/cmake/helper.cmake +++ b/cmake/helper.cmake @@ -26,4 +26,5 @@ macro(configure_target target_name source_file) target_compile_options(${target_name} PRIVATE -Os -fno-rtti -fno-exceptions) enable_map_file_for_target(${target_name}) add_test(NAME ${target_name} COMMAND $) + print_size(${target_name}) endmacro() diff --git a/rt_polymorphysm/CMakeLists.txt b/rt_polymorphysm/CMakeLists.txt new file mode 100644 index 0000000..79ae563 --- /dev/null +++ b/rt_polymorphysm/CMakeLists.txt @@ -0,0 +1 @@ +add_subdirectory(profile) \ No newline at end of file diff --git a/rt_polymorphysm/profile/CMakeLists.txt b/rt_polymorphysm/profile/CMakeLists.txt new file mode 100644 index 0000000..88932ad --- /dev/null +++ b/rt_polymorphysm/profile/CMakeLists.txt @@ -0,0 +1,2 @@ +add_subdirectory(cptr) +add_subdirectory(virtual) \ No newline at end of file diff --git a/rt_polymorphysm/profile/cptr/CMakeLists.txt b/rt_polymorphysm/profile/cptr/CMakeLists.txt new file mode 100644 index 0000000..ee6a70c --- /dev/null +++ b/rt_polymorphysm/profile/cptr/CMakeLists.txt @@ -0,0 +1,2 @@ +set(cptr-interface_SOURCES main.cpp) +configure_target(cptr-interface ${cptr-interface_SOURCES}) diff --git a/rt_polymorphysm/profile/cptr/light_sensor.hpp b/rt_polymorphysm/profile/cptr/light_sensor.hpp new file mode 100644 index 0000000..cb81905 --- /dev/null +++ b/rt_polymorphysm/profile/cptr/light_sensor.hpp @@ -0,0 +1,24 @@ +#ifndef LIGHT_SENSOR_HPP +#define LIGHT_SENSOR_HPP + +#include +#include + +struct LightSensor { + bool init(uint8_t mode) { + printf("[LightSensor] Init with mode: %d\n", mode); + return true; + } + + bool read(float& value) { + value = 25.3f; // Simulated light intensity + printf("[LightSensor] Read value: %.2f\n", value); + return true; + } + + void shutdown() { + printf("[LightSensor] Shutdown\n"); + } +}; + +#endif diff --git a/rt_polymorphysm/profile/cptr/light_sensor_policy.hpp b/rt_polymorphysm/profile/cptr/light_sensor_policy.hpp new file mode 100644 index 0000000..62907e7 --- /dev/null +++ b/rt_polymorphysm/profile/cptr/light_sensor_policy.hpp @@ -0,0 +1,9 @@ +#ifndef LIGHT_SENSOR_POLICY_HPP +#define LIGHT_SENSOR_POLICY_HPP + +#include "light_sensor.hpp" +#include "sensor_policy.hpp" + +using LightSensorPolicy = SensorPolicy; + +#endif diff --git a/rt_polymorphysm/profile/cptr/main.cpp b/rt_polymorphysm/profile/cptr/main.cpp new file mode 100644 index 0000000..e7e874b --- /dev/null +++ b/rt_polymorphysm/profile/cptr/main.cpp @@ -0,0 +1,38 @@ +#include "sensor_interface.hpp" +#include "sensor_policy.hpp" +#include "temp_sensor_policy.hpp" +#include "light_sensor_policy.hpp" +#include "temp_sensor.hpp" +#include "light_sensor.hpp" + +int main() { + TempSensor ts; + auto temp_sensor = SensorT::create(ts); + + LightSensor ls; + auto light_sensor = SensorT::create(ls); + + SensorT &sensor = temp_sensor; + + sensor.init(); + + float temperature = 0; + if (sensor.read(temperature)) { + printf("Temperature: %.2f°C\n", temperature); + } + + sensor.shutdown(); + + sensor = light_sensor; + + sensor.init(); + + float light = 0; + if (sensor.read(light)) { + printf("Light: %.2f\n", light); + } + + sensor.shutdown(); + + return 0; +} diff --git a/rt_polymorphysm/profile/cptr/sensor_interface.hpp b/rt_polymorphysm/profile/cptr/sensor_interface.hpp new file mode 100644 index 0000000..43c9d2a --- /dev/null +++ b/rt_polymorphysm/profile/cptr/sensor_interface.hpp @@ -0,0 +1,45 @@ +#ifndef SENSOR_INTERFACE_HPP +#define SENSOR_INTERFACE_HPP + +#include + +struct SensorT { + using InitFn = void (*)(); + using ReadFn = bool (*)(float&); + using ShutdownFn = void (*)(); + + InitFn init; + ReadFn read; + ShutdownFn shutdown; + + // Create from Impl + Policy adapter + template + static SensorT create(Impl& impl) { + struct Adapter { + Impl& sensor; + + explicit Adapter(Impl& sensor) : sensor(sensor) {} + + void init() { + Policy::init(sensor); + } + + bool read(float& out) { + return Policy::read(sensor, out); + } + + void shutdown() { + Policy::shutdown(sensor); + } + }; + + static Adapter adapter(impl); + return SensorT{ + []() { adapter.init(); }, + [](float& out) { return adapter.read(out); }, + []() { adapter.shutdown(); } + }; + } +}; + +#endif // SENSOR_INTERFACE_HPP diff --git a/rt_polymorphysm/profile/cptr/sensor_policy.hpp b/rt_polymorphysm/profile/cptr/sensor_policy.hpp new file mode 100644 index 0000000..3e70efb --- /dev/null +++ b/rt_polymorphysm/profile/cptr/sensor_policy.hpp @@ -0,0 +1,19 @@ +#ifndef SENSOR_POLICY_HPP +#define SENSOR_POLICY_HPP + +template +struct SensorPolicy { + static void init(Sensor& sensor) { + sensor.init(1); // example mode + } + + static bool read(Sensor& sensor, float& out) { + return sensor.read(out); + } + + static void shutdown(Sensor& sensor) { + sensor.shutdown(); + } +}; + +#endif diff --git a/rt_polymorphysm/profile/cptr/temp_sensor.hpp b/rt_polymorphysm/profile/cptr/temp_sensor.hpp new file mode 100644 index 0000000..a865550 --- /dev/null +++ b/rt_polymorphysm/profile/cptr/temp_sensor.hpp @@ -0,0 +1,24 @@ +#ifndef TEMP_SENSOR_HPP +#define TEMP_SENSOR_HPP + +#include +#include + +struct TempSensor { + bool init(uint8_t mode) { + printf("[TempSensor] Init with mode: %d\n", mode); + return true; + } + + bool read(float& value) { + value = 25.3f; // Simulated temperature + printf("[TempSensor] Read value: %.2f\n", value); + return true; + } + + void shutdown() { + printf("[TempSensor] Shutdown\n"); + } +}; + +#endif diff --git a/rt_polymorphysm/profile/cptr/temp_sensor_policy.hpp b/rt_polymorphysm/profile/cptr/temp_sensor_policy.hpp new file mode 100644 index 0000000..208b750 --- /dev/null +++ b/rt_polymorphysm/profile/cptr/temp_sensor_policy.hpp @@ -0,0 +1,9 @@ +#ifndef TEMP_SENSOR_POLICY_HPP +#define TEMP_SENSOR_POLICY_HPP + +#include "temp_sensor.hpp" +#include "sensor_policy.hpp" + +using TempSensorPolicy = SensorPolicy; + +#endif diff --git a/rt_polymorphysm/profile/virtual/CMakeLists.txt b/rt_polymorphysm/profile/virtual/CMakeLists.txt new file mode 100644 index 0000000..f264454 --- /dev/null +++ b/rt_polymorphysm/profile/virtual/CMakeLists.txt @@ -0,0 +1,2 @@ +set(cpp-interface_SOURCES main.cpp) +configure_target(cpp-interface ${cpp-interface_SOURCES}) diff --git a/rt_polymorphysm/profile/virtual/light_sensor.hpp b/rt_polymorphysm/profile/virtual/light_sensor.hpp new file mode 100644 index 0000000..90a1bc4 --- /dev/null +++ b/rt_polymorphysm/profile/virtual/light_sensor.hpp @@ -0,0 +1,28 @@ +#ifndef LIGHT_SENSOR_HPP +#define LIGHT_SENSOR_HPP + +#include +#include + +#include "sensor_interface.hpp" + +// Concrete light sensor class +class LightSensor : public ISensor { + public: + + void init(uint8_t mode) override { + printf("[LightSensor] Init with mode: %d\n", mode); + } + + bool read(float & value) override { + value = 350.0f; + printf("[TempSensor] Read value: %.2f\n", value); + return true; + } + + void shutdown() override { + printf("[LightSensor] Power Off\n"); + } + }; + +#endif diff --git a/rt_polymorphysm/profile/virtual/main.cpp b/rt_polymorphysm/profile/virtual/main.cpp new file mode 100644 index 0000000..0bef623 --- /dev/null +++ b/rt_polymorphysm/profile/virtual/main.cpp @@ -0,0 +1,33 @@ + +#include "temp_sensor.hpp" +#include "light_sensor.hpp" + +int main() { + TempSensor ts; + + LightSensor ls; + + ISensor &sensor = ts; + + sensor.init(0); + + float temperature = 0; + if (sensor.read(temperature)) { + printf("Temperature: %.2f°C\n", temperature); + } + + sensor.shutdown(); + + sensor = ls; + + sensor.init(0); + + float light = 0; + if (sensor.read(light)) { + printf("Light: %.2f\n", light); + } + + sensor.shutdown(); + + return 0; +} diff --git a/rt_polymorphysm/profile/virtual/sensor_interface.hpp b/rt_polymorphysm/profile/virtual/sensor_interface.hpp new file mode 100644 index 0000000..80c25bf --- /dev/null +++ b/rt_polymorphysm/profile/virtual/sensor_interface.hpp @@ -0,0 +1,15 @@ +#ifndef SENSOR_INTERFACE_VIRTUAL_HPP +#define SENSOR_INTERFACE_VIRTUAL_HPP + +#include + +// Abstract base class for sensors +class ISensor { +public: + virtual ~ISensor() = default; + virtual void init(uint8_t mode) = 0; + virtual bool read(float & value) = 0; + virtual void shutdown() = 0; +}; + +#endif // SENSOR_INTERFACE_VIRTUAL_HPP diff --git a/rt_polymorphysm/profile/virtual/temp_sensor.hpp b/rt_polymorphysm/profile/virtual/temp_sensor.hpp new file mode 100644 index 0000000..61bada0 --- /dev/null +++ b/rt_polymorphysm/profile/virtual/temp_sensor.hpp @@ -0,0 +1,27 @@ +#ifndef TEMP_SENSOR_HPP +#define TEMP_SENSOR_HPP + +#include +#include + +#include "sensor_interface.hpp" + +// Concrete temperature sensor class +class TempSensor : public ISensor { + public: + void init(uint8_t mode) override { + printf("[TempSensor] Init with mode: %d\n", mode); + } + + bool read(float & out) override { + out = 24.7f; + printf("[TempSensor] Read value: %.2f\n", out); + return true; + } + + void shutdown() override { + printf("[TempSensor] Shutdown\n"); + } + }; + +#endif