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 1704a2f..c3af428 100644 --- a/rt_polymorphysm/profile/CMakeLists.txt +++ b/rt_polymorphysm/profile/CMakeLists.txt @@ -1,3 +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/light_sensor.hpp b/rt_polymorphysm/profile/cptr1/light_sensor.hpp index 2d2073f..29d1cc7 100644 --- a/rt_polymorphysm/profile/cptr1/light_sensor.hpp +++ b/rt_polymorphysm/profile/cptr1/light_sensor.hpp @@ -3,6 +3,7 @@ #include #include +#include "sensor_interface.hpp" struct LightSensor { float bias = 0; @@ -23,17 +24,11 @@ struct LightSensor { } SensorT to_interface() { - return SensorT{ - .context = this, - .init = [](void* ctx) { - static_cast(ctx)->init(); - }, - .read = [](void* ctx, float& out) { - return static_cast(ctx)->read(out); - }, - .shutdown = [](void* ctx) { - static_cast(ctx)->shutdown(); - } + return { + this, + [](void* ctx) { static_cast(ctx)->init(); }, + [](void* ctx, float& out) { return static_cast(ctx)->read(out); }, + [](void* ctx) { static_cast(ctx)->shutdown(); } }; } }; diff --git a/rt_polymorphysm/profile/cptr1/main.cpp b/rt_polymorphysm/profile/cptr1/main.cpp index 72df2f0..ce1abee 100644 --- a/rt_polymorphysm/profile/cptr1/main.cpp +++ b/rt_polymorphysm/profile/cptr1/main.cpp @@ -1,7 +1,3 @@ -#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" @@ -16,17 +12,17 @@ void use_sensor(SensorT& sensor) { // ======= Main ======= int main() { - TemperatureSensor temp; - PressureSensor pressure; + TempSensor ts; + LightSensor ls; - SensorT tempIf = temp.to_interface(); - SensorT pressureIf = pressure.to_interface(); + SensorT tsIf = ts.to_interface(); + SensorT lsIf = ls.to_interface(); - printf("\n== Temperature Sensor ==\n"); - use_sensor(tempIf); + printf("\n== Temp Sensor ==\n"); + use_sensor(tsIf); - printf("\n== Pressure Sensor ==\n"); - use_sensor(pressureIf); + printf("\n== Light Sensor ==\n"); + use_sensor(lsIf); return 0; } diff --git a/rt_polymorphysm/profile/cptr1/temp_sensor.hpp b/rt_polymorphysm/profile/cptr1/temp_sensor.hpp index 2267b8c..d401972 100644 --- a/rt_polymorphysm/profile/cptr1/temp_sensor.hpp +++ b/rt_polymorphysm/profile/cptr1/temp_sensor.hpp @@ -3,18 +3,16 @@ #include #include +#include "sensor_interface.hpp" struct TempSensor { - float offset = 0; - - void init() { - offset = 1.0f; - printf("[TempSensor] Initialized\n"); + void init(uint8_t mode) { + printf("[TempSensor] Init with mode: %d\n", mode); } bool read(float& out) { - out = 25.0f + offset; - printf("[TempSensor] Read: %.2f\n", out); + out = 25.0f; + printf("[TempSensor] Read value: %.2f\n", out); return true; } @@ -23,17 +21,11 @@ struct TempSensor { } SensorT to_interface() { - return SensorT{ - .context = this, - .init = [](void* ctx) { - static_cast(ctx)->init(); - }, - .read = [](void* ctx, float& out) { - return static_cast(ctx)->read(out); - }, - .shutdown = [](void* ctx) { - static_cast(ctx)->shutdown(); - } + 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(); } }; } }; 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