Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
1 change: 1 addition & 0 deletions cmake/helper.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -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 $<TARGET_FILE:${target_name}>)
print_size(${target_name})
endmacro()
1 change: 1 addition & 0 deletions rt_polymorphysm/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
add_subdirectory(profile)
2 changes: 2 additions & 0 deletions rt_polymorphysm/profile/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
add_subdirectory(cptr)
add_subdirectory(virtual)
2 changes: 2 additions & 0 deletions rt_polymorphysm/profile/cptr/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
set(cptr-interface_SOURCES main.cpp)
configure_target(cptr-interface ${cptr-interface_SOURCES})
24 changes: 24 additions & 0 deletions rt_polymorphysm/profile/cptr/light_sensor.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
#ifndef LIGHT_SENSOR_HPP
#define LIGHT_SENSOR_HPP

#include <cstdio>
#include <cstdint>

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
9 changes: 9 additions & 0 deletions rt_polymorphysm/profile/cptr/light_sensor_policy.hpp
Original file line number Diff line number Diff line change
@@ -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<LightSensor>;

#endif
38 changes: 38 additions & 0 deletions rt_polymorphysm/profile/cptr/main.cpp
Original file line number Diff line number Diff line change
@@ -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<TempSensor, TempSensorPolicy>(ts);

LightSensor ls;
auto light_sensor = SensorT::create<LightSensor, LightSensorPolicy>(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;
}
45 changes: 45 additions & 0 deletions rt_polymorphysm/profile/cptr/sensor_interface.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
#ifndef SENSOR_INTERFACE_HPP
#define SENSOR_INTERFACE_HPP

#include <cstdint>

struct SensorT {
using InitFn = void (*)();
using ReadFn = bool (*)(float&);
using ShutdownFn = void (*)();

InitFn init;
ReadFn read;
ShutdownFn shutdown;

// Create from Impl + Policy adapter
template<typename Impl, typename Policy>
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
19 changes: 19 additions & 0 deletions rt_polymorphysm/profile/cptr/sensor_policy.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
#ifndef SENSOR_POLICY_HPP
#define SENSOR_POLICY_HPP

template <typename Sensor>
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
24 changes: 24 additions & 0 deletions rt_polymorphysm/profile/cptr/temp_sensor.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
#ifndef TEMP_SENSOR_HPP
#define TEMP_SENSOR_HPP

#include <cstdio>
#include <cstdint>

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
9 changes: 9 additions & 0 deletions rt_polymorphysm/profile/cptr/temp_sensor_policy.hpp
Original file line number Diff line number Diff line change
@@ -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<TempSensor>;

#endif
2 changes: 2 additions & 0 deletions rt_polymorphysm/profile/virtual/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
set(cpp-interface_SOURCES main.cpp)
configure_target(cpp-interface ${cpp-interface_SOURCES})
28 changes: 28 additions & 0 deletions rt_polymorphysm/profile/virtual/light_sensor.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
#ifndef LIGHT_SENSOR_HPP
#define LIGHT_SENSOR_HPP

#include <cstdio>
#include <cstdint>

#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
33 changes: 33 additions & 0 deletions rt_polymorphysm/profile/virtual/main.cpp
Original file line number Diff line number Diff line change
@@ -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;
}
15 changes: 15 additions & 0 deletions rt_polymorphysm/profile/virtual/sensor_interface.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
#ifndef SENSOR_INTERFACE_VIRTUAL_HPP
#define SENSOR_INTERFACE_VIRTUAL_HPP

#include <cstdio>

// 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
27 changes: 27 additions & 0 deletions rt_polymorphysm/profile/virtual/temp_sensor.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
#ifndef TEMP_SENSOR_HPP
#define TEMP_SENSOR_HPP

#include <cstdio>
#include <cstdint>

#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