Boboter
Loading...
Searching...
No Matches
protected_struct.h
Go to the documentation of this file.
1
7
8#pragma once
9
10#include <freertos/FreeRTOS.h>
11#include "lib/logger/logger.h"
12
64template <typename struct_type>
66private:
67 static constexpr const char* TAG = "protected_struct";
68
69 struct_type data;
70 mutable SemaphoreHandle_t mutex;
71
72 struct proxy {
73 const protected_struct& parent;
74
75 explicit proxy(const protected_struct& parent) : parent(parent) {
76 if (parent.mutex == nullptr) {
77 parent.mutex = xSemaphoreCreateMutex();
78 }
79 if (parent.mutex != nullptr) {
80 xSemaphoreTake(parent.mutex, portMAX_DELAY);
81 }
82 }
83 ~proxy() { xSemaphoreGive(parent.mutex); }
84
88 struct_type* operator->() const {
89 return const_cast<struct_type*>(&parent.data);
90 }
91
96 operator struct_type&() const {
97 return *const_cast<struct_type*>(&parent.data);
98 }
99 };
100
101public:
102 explicit protected_struct() : mutex() {}
104 if (mutex != nullptr) {
105 vSemaphoreDelete(mutex);
106 }
107 }
108
111
118 proxy operator->() const {
119 return proxy(*this);
120 }
121
128 proxy operator*() const {
129 return proxy(*this);
130 }
131};
proxy operator->() const
Overload of the get value from dereferenced pointer operator to make it possible to get a value using...
Definition protected_struct.h:118
protected_struct(const protected_struct &)=delete
protected_struct & operator=(const protected_struct &)=delete
protected_struct()
Definition protected_struct.h:102
~protected_struct()
Definition protected_struct.h:103
proxy operator*() const
Overload of the dereference operator to make it possible to pass the whole struct to something.
Definition protected_struct.h:128