4 #include <ma_config_board.h>
9 #define MA_STORAGE_ENABLE
11 #if not defined(MA_STORAGE_ENABLE)
12 #define MA_STORAGE_CHECK_NULL_PTR_BREAK(p_storage) ((void)0)
13 #define MA_STORAGE_SET_STR(ret, p_storage, key, value) ((void)0)
14 #define MA_STORAGE_NOSTA_SET_STR(p_storage, key, value) ((void)0)
15 #define MA_STORAGE_SET_CSTR(ret, p_storage, key, value) ((void)0)
16 #define MA_STORAGE_NOSTA_SET_CSTR(p_storage, key, value) ((void)0)
17 #define MA_STORAGE_SET_ASTR(ret, p_storage, key, value) ((void)0)
18 #define MA_STORAGE_NOSTA_SET_ASTR(p_storage, key, value) ((void)0)
19 #define MA_STORAGE_SET_POD(ret, p_storage, key, value) ((void)0)
20 #define MA_STORAGE_NOSTA_SET_POD(p_storage, key, value) ((void)0)
21 #define MA_STORAGE_SET_RVPOD(ret, p_storage, key, value) ((void)0)
22 #define MA_STORAGE_NOSTA_SET_RVPOD(p_storage, key, value) ((void)0)
23 #define MA_STORAGE_GET_STR(p_storage, key, value, _default) ((void)0)
24 #define MA_STORAGE_GET_CSTR(p_storage, key, value, size, _default) ((void)0)
25 #define MA_STORAGE_GET_ASTR(p_storage, key, value, _default) ((void)0)
26 #define MA_STORAGE_GET_POD(p_storage, key, value, _default) ((void)0)
27 #define MA_STORAGE_REMOVE(ret, p_storage, key) ((void)0)
28 #define MA_STORAGE_NOSTA_REMOVE(p_storage, key) ((void)0)
29 #define MA_STORAGE_EXISTS(p_storage, key) ((void)0)
32 #if not defined(MA_STORAGE_CHECK_NULL_PTR)
33 #define MA_STORAGE_CHECK_NULL_PTR_BREAK(p_storage) \
39 #if not defined(MA_STORAGE_SET_STR)
40 #define MA_STORAGE_SET_STR(ret, p_storage, key, value) \
42 MA_STORAGE_CHECK_NULL_PTR_BREAK(p_storage) \
43 ret = p_storage->set(key, value.data(), value.size()); \
47 #if not defined(MA_STORAGE_NOSTA_SET_STR)
48 #define MA_STORAGE_NOSTA_SET_STR(p_storage, key, value) \
50 [[maybe_unused]] int ret; \
51 MA_STORAGE_SET_STR(ret, p_storage, key, value); \
55 #if not defined(MA_STORAGE_SET_CSTR)
56 #define MA_STORAGE_SET_CSTR(ret, p_storage, key, value) \
58 MA_STORAGE_CHECK_NULL_PTR_BREAK(p_storage) \
59 ret = p_storage->set(key, value, std::strlen(value)); \
63 #if not defined(MA_STORAGE_NOSTA_SET_CSTR)
64 #define MA_STORAGE_NOSTA_SET_CSTR(p_storage, key, value) \
66 [[maybe_unused]] int ret; \
67 MA_STORAGE_SET_CSTR(ret, p_storage, key, value); \
71 #if not defined(MA_STORAGE_SET_ASTR)
72 #define MA_STORAGE_SET_ASTR(ret, p_storage, key, value) \
74 MA_STORAGE_CHECK_NULL_PTR_BREAK(p_storage) \
75 ret = p_storage->set(key, value, sizeof(value) - 1); \
79 #if not defined(MA_STORAGE_NOSTA_SET_ASTR)
80 #define MA_STORAGE_NOSTA_SET_ASTR(p_storage, key, value) \
82 [[maybe_unused]] int ret; \
83 MA_STORAGE_SET_ASTR(ret, p_storage, key, value); \
87 #if not defined(MA_STORAGE_SET_POD)
88 #define MA_STORAGE_SET_POD(ret, p_storage, key, value) \
90 MA_STORAGE_CHECK_NULL_PTR_BREAK(p_storage) \
91 if constexpr (std::is_same_v<decltype(value), int8_t> || std::is_same_v<decltype(value), uint8_t> || std::is_same_v<decltype(value), int16_t> || std::is_same_v<decltype(value), uint16_t> || \
92 std::is_same_v<decltype(value), uint32_t> || std::is_same_v<decltype(value), int32_t> || std::is_same_v<decltype(value), int64_t> || \
93 std::is_same_v<decltype(value), uint64_t>) { \
94 ret = p_storage->set(key, static_cast<int64_t>(value)); \
95 } else if constexpr (std::is_same_v<decltype(value), float> || std::is_same_v<decltype(value), double>) { \
96 ret = p_storage->set(key, static_cast<double>(value)); \
98 ret = p_storage->set(key, &value, sizeof(value)); \
103 #if not defined(MA_STORAGE_NOSTA_SET_POD)
104 #define MA_STORAGE_NOSTA_SET_POD(p_storage, key, value) \
106 [[maybe_unused]] int ret; \
107 MA_STORAGE_SET_POD(ret, p_storage, key, value); \
111 #if not defined(MA_STORAGE_SET_RVPOD)
112 #define MA_STORAGE_SET_RVPOD(ret, p_storage, key, value) \
114 MA_STORAGE_CHECK_NULL_PTR_BREAK(p_storage) \
115 if constexpr (std::is_same_v<decltype(value), int8_t> || std::is_same_v<decltype(value), uint8_t> || std::is_same_v<decltype(value), int16_t> || std::is_same_v<decltype(value), uint16_t> || \
116 std::is_same_v<decltype(value), uint32_t> || std::is_same_v<decltype(value), int32_t> || std::is_same_v<decltype(value), int64_t> || \
117 std::is_same_v<decltype(value), uint64_t>) { \
118 ret = p_storage->set(key, static_cast<int64_t>(value)); \
119 } else if constexpr (std::is_same_v<decltype(value), float> || std::is_same_v<decltype(value), double>) { \
120 ret = p_storage->set(key, static_cast<double>(value)); \
122 decltype(value) tmp = value; \
123 ret = p_storage->set(key, &tmp, sizeof(tmp)); \
128 #if not defined(MA_STORAGE_NOSTA_SET_RVPOD)
129 #define MA_STORAGE_NOSTA_SET_RVPOD(p_storage, key, value) \
131 [[maybe_unused]] int ret; \
132 MA_STORAGE_SET_RVPOD(ret, p_storage, key, value); \
136 #if not defined(MA_STORAGE_GET_STR)
137 #define MA_STORAGE_GET_STR(p_storage, key, value, _default) \
139 MA_STORAGE_CHECK_NULL_PTR_BREAK(p_storage) \
140 std::string buffer; \
141 auto ret = p_storage->get(key, buffer); \
142 if (ret != MA_OK) { \
146 value = std::move(buffer); \
150 #if not defined(MA_STORAGE_GET_CSTR)
151 #define MA_STORAGE_GET_CSTR(p_storage, key, value, size, _default) \
153 MA_STORAGE_CHECK_NULL_PTR_BREAK(p_storage) \
154 std::string buffer; \
155 auto ret = p_storage->get(key, buffer); \
156 if (ret != MA_OK) { \
157 std::strncpy(value, _default, size); \
159 std::strncpy(value, buffer.c_str(), size); \
164 #if not defined(MA_STORAGE_GET_ASTR)
165 #define MA_STORAGE_GET_ASTR(p_storage, key, value, _default) MA_STORAGE_GET_CSTR(p_storage, key, value, sizeof(value) - 1, _default)
168 #if not defined(MA_STORAGE_GET_POD)
169 #define MA_STORAGE_GET_POD(p_storage, key, value, _default) \
171 MA_STORAGE_CHECK_NULL_PTR_BREAK(p_storage) \
172 MA_STORAGE_CHECK_NULL_PTR_BREAK(p_storage) \
173 if constexpr (std::is_same_v<decltype(value), int8_t> || std::is_same_v<decltype(value), uint8_t> || std::is_same_v<decltype(value), int16_t> || std::is_same_v<decltype(value), uint16_t> || \
174 std::is_same_v<decltype(value), uint32_t> || std::is_same_v<decltype(value), int32_t> || std::is_same_v<decltype(value), int64_t> || \
175 std::is_same_v<decltype(value), uint64_t>) { \
176 int64_t tmp = static_cast<int64_t>(_default); \
177 p_storage->get(key, tmp); \
178 value = static_cast<decltype(value)>(tmp); \
179 } else if constexpr (std::is_same_v<decltype(value), float> || std::is_same_v<decltype(value), double>) { \
180 double tmp = static_cast<double>(_default); \
181 p_storage->get(key, tmp); \
182 value = static_cast<decltype(value)>(tmp); \
184 std::string buffer; \
185 ma_err_t err = p_storage->get(key, buffer); \
186 if (err != MA_OK || sizeof(value) > buffer.size()) { \
190 value = *reinterpret_cast<decltype(value)*>(buffer.data()); \
195 #if not defined(MA_STORAGE_REMOVE)
196 #define MA_STORAGE_REMOVE(ret, p_storage, key) \
198 MA_STORAGE_CHECK_NULL_PTR_BREAK(p_storage) \
199 ret = p_storage->remove(key); \
203 #if not defined(MA_STORAGE_NOSTA_REMOVE)
204 #define MA_STORAGE_NOSTA_REMOVE(p_storage, key) \
206 [[maybe_unused]] int ret; \
207 MA_STORAGE_REMOVE(ret, p_storage, key); \
211 #if not defined(MA_STORAGE_EXISTS)
212 #if defined(MA_STORAGE_CHECK_NULL_PTR)
213 #define MA_STORAGE_EXISTS(p_storage, key) \
214 [](Storage* p_storage, const std::string& str) -> bool { \
215 return p_storage && p_storage->exists(str); \
218 #define MA_STORAGE_EXISTS(p_storage, key) \
219 [](Storage* p_storage, const std::string& str) -> bool { \
220 return p_storage->exists(str); \
238 operator
bool() const noexcept {
242 virtual ma_err_t set(
const std::string& key, int64_t value) noexcept = 0;
243 virtual ma_err_t set(
const std::string& key,
double value) noexcept = 0;
244 virtual ma_err_t get(
const std::string& key, int64_t& value) noexcept = 0;
245 virtual ma_err_t get(
const std::string& key,
double& value) noexcept = 0;
246 virtual ma_err_t set(
const std::string& key,
const void* value,
size_t size) noexcept = 0;
247 virtual ma_err_t get(
const std::string& key, std::string& value) noexcept = 0;
250 virtual bool exists(
const std::string& key) noexcept = 0;
Definition: ma_storage.h:227
virtual ma_err_t remove(const std::string &key) noexcept=0
virtual ma_err_t set(const std::string &key, int64_t value) noexcept=0
virtual ma_err_t get(const std::string &key, double &value) noexcept=0
Storage(const Storage &)=delete
virtual ~Storage()=default
virtual ma_err_t set(const std::string &key, double value) noexcept=0
Storage & operator=(const Storage &)=delete
virtual ma_err_t get(const std::string &key, int64_t &value) noexcept=0
virtual bool exists(const std::string &key) noexcept=0
virtual ma_err_t set(const std::string &key, const void *value, size_t size) noexcept=0
virtual void deInit() noexcept=0
virtual ma_err_t init(const void *config) noexcept=0
virtual ma_err_t get(const std::string &key, std::string &value) noexcept=0
bool m_initialized
Definition: ma_storage.h:253
Storage()
Definition: ma_storage.h:229
ma_err_t
Definition: ma_types.h:21