SSCMA-Micro CPP SDK  v2.0.0
SSCMA-Micro is a cross-platform machine learning inference framework designed for embedded devices.
ma_storage.h
Go to the documentation of this file.
1 #ifndef _MA_STORAGE_H_
2 #define _MA_STORAGE_H_
3 
4 #include <ma_config_board.h>
5 
6 #include <cstring>
7 #include <string>
8 
9 #define MA_STORAGE_ENABLE
10 
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)
30 #endif
31 
32 #if not defined(MA_STORAGE_CHECK_NULL_PTR)
33 #define MA_STORAGE_CHECK_NULL_PTR_BREAK(p_storage) \
34  if (!p_storage) { \
35  break; \
36  }
37 #endif
38 
39 #if not defined(MA_STORAGE_SET_STR)
40 #define MA_STORAGE_SET_STR(ret, p_storage, key, value) \
41  do { \
42  MA_STORAGE_CHECK_NULL_PTR_BREAK(p_storage) \
43  ret = p_storage->set(key, value.data(), value.size()); \
44  } while (0)
45 #endif
46 
47 #if not defined(MA_STORAGE_NOSTA_SET_STR)
48 #define MA_STORAGE_NOSTA_SET_STR(p_storage, key, value) \
49  do { \
50  [[maybe_unused]] int ret; \
51  MA_STORAGE_SET_STR(ret, p_storage, key, value); \
52  } while (0)
53 #endif
54 
55 #if not defined(MA_STORAGE_SET_CSTR)
56 #define MA_STORAGE_SET_CSTR(ret, p_storage, key, value) \
57  do { \
58  MA_STORAGE_CHECK_NULL_PTR_BREAK(p_storage) \
59  ret = p_storage->set(key, value, std::strlen(value)); \
60  } while (0)
61 #endif
62 
63 #if not defined(MA_STORAGE_NOSTA_SET_CSTR)
64 #define MA_STORAGE_NOSTA_SET_CSTR(p_storage, key, value) \
65  do { \
66  [[maybe_unused]] int ret; \
67  MA_STORAGE_SET_CSTR(ret, p_storage, key, value); \
68  } while (0)
69 #endif
70 
71 #if not defined(MA_STORAGE_SET_ASTR)
72 #define MA_STORAGE_SET_ASTR(ret, p_storage, key, value) \
73  do { \
74  MA_STORAGE_CHECK_NULL_PTR_BREAK(p_storage) \
75  ret = p_storage->set(key, value, sizeof(value) - 1); \
76  } while (0)
77 #endif
78 
79 #if not defined(MA_STORAGE_NOSTA_SET_ASTR)
80 #define MA_STORAGE_NOSTA_SET_ASTR(p_storage, key, value) \
81  do { \
82  [[maybe_unused]] int ret; \
83  MA_STORAGE_SET_ASTR(ret, p_storage, key, value); \
84  } while (0)
85 #endif
86 
87 #if not defined(MA_STORAGE_SET_POD)
88 #define MA_STORAGE_SET_POD(ret, p_storage, key, value) \
89  do { \
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)); \
97  } else { \
98  ret = p_storage->set(key, &value, sizeof(value)); \
99  } \
100  } while (0)
101 #endif
102 
103 #if not defined(MA_STORAGE_NOSTA_SET_POD)
104 #define MA_STORAGE_NOSTA_SET_POD(p_storage, key, value) \
105  do { \
106  [[maybe_unused]] int ret; \
107  MA_STORAGE_SET_POD(ret, p_storage, key, value); \
108  } while (0)
109 #endif
110 
111 #if not defined(MA_STORAGE_SET_RVPOD)
112 #define MA_STORAGE_SET_RVPOD(ret, p_storage, key, value) \
113  do { \
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)); \
121  } else { \
122  decltype(value) tmp = value; \
123  ret = p_storage->set(key, &tmp, sizeof(tmp)); \
124  } \
125  } while (0)
126 #endif
127 
128 #if not defined(MA_STORAGE_NOSTA_SET_RVPOD)
129 #define MA_STORAGE_NOSTA_SET_RVPOD(p_storage, key, value) \
130  do { \
131  [[maybe_unused]] int ret; \
132  MA_STORAGE_SET_RVPOD(ret, p_storage, key, value); \
133  } while (0)
134 #endif
135 
136 #if not defined(MA_STORAGE_GET_STR)
137 #define MA_STORAGE_GET_STR(p_storage, key, value, _default) \
138  do { \
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) { \
143  value = _default; \
144  break; \
145  } \
146  value = std::move(buffer); \
147  } while (0)
148 #endif
149 
150 #if not defined(MA_STORAGE_GET_CSTR)
151 #define MA_STORAGE_GET_CSTR(p_storage, key, value, size, _default) \
152  do { \
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); \
158  } else { \
159  std::strncpy(value, buffer.c_str(), size); \
160  } \
161  } while (0)
162 #endif
163 
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)
166 #endif
167 
168 #if not defined(MA_STORAGE_GET_POD)
169 #define MA_STORAGE_GET_POD(p_storage, key, value, _default) \
170  do { \
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); \
183  } else { \
184  std::string buffer; \
185  ma_err_t err = p_storage->get(key, buffer); \
186  if (err != MA_OK || sizeof(value) > buffer.size()) { \
187  value = _default; \
188  break; \
189  } \
190  value = *reinterpret_cast<decltype(value)*>(buffer.data()); \
191  } \
192  } while (0)
193 #endif
194 
195 #if not defined(MA_STORAGE_REMOVE)
196 #define MA_STORAGE_REMOVE(ret, p_storage, key) \
197  do { \
198  MA_STORAGE_CHECK_NULL_PTR_BREAK(p_storage) \
199  ret = p_storage->remove(key); \
200  } while (0)
201 #endif
202 
203 #if not defined(MA_STORAGE_NOSTA_REMOVE)
204 #define MA_STORAGE_NOSTA_REMOVE(p_storage, key) \
205  do { \
206  [[maybe_unused]] int ret; \
207  MA_STORAGE_REMOVE(ret, p_storage, key); \
208  } while (0)
209 #endif
210 
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); \
216  }(p_storage, key)
217 #else
218 #define MA_STORAGE_EXISTS(p_storage, key) \
219  [](Storage* p_storage, const std::string& str) -> bool { \
220  return p_storage->exists(str); \
221  }(p_storage, key)
222 #endif
223 #endif
224 
225 namespace ma {
226 
227 class Storage {
228 public:
229  Storage() : m_initialized(false) {}
230  virtual ~Storage() = default;
231 
232  Storage(const Storage&) = delete;
233  Storage& operator=(const Storage&) = delete;
234 
235  virtual ma_err_t init(const void* config) noexcept = 0;
236  virtual void deInit() noexcept = 0;
237 
238  operator bool() const noexcept {
239  return m_initialized;
240  }
241 
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;
248 
249  virtual ma_err_t remove(const std::string& key) noexcept = 0;
250  virtual bool exists(const std::string& key) noexcept = 0;
251 
252 protected:
254 };
255 
256 } // namespace ma
257 
258 #endif // _MA_STORAGE_H_
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
Definition: ma_cv.cpp:7