SOIL C++
C++ Unified Device Interface
Container.h
Go to the documentation of this file.
1#pragma once
2#include "constants.h"
3#include "REST/Types.h"
4#include "json_helpers.h"
5#include "Range.h"
6namespace SOIL
7{
20 template <typename T, int x=-1, int y=-1>
22 {
23 private:
29 std::vector<std::vector<T>> data;
30
37 bool _null;
38 public:
47
54 Container(const std::vector<std::vector<T>>& value);
55
63
70 std::vector<std::vector<T>> operator*(void) const;
71
79 inline bool is_null(void) const {return _null;}
80
87 inline void set_null(bool _null = true) { this->_null = _null; }
88
98
107 bool check_range(Range<T> range) const;
108
117 T& at(int i, int j);
118
125 std::vector<unsigned char> serialize_value(void) const;
126
133 std::vector<unsigned char> serialize_dimensions(void) const;
134
135 };
136
145 template <typename T, int x>
146 class Container<T, x, -1>
147 {
148 private:
149 std::vector<T> data;
150 bool _null;
151 public:
153 Container(const std::vector<T>& value);
155 std::vector<T> operator*(void) const;
156 inline bool is_null(void) const { return _null; }
157 inline void set_null(bool _null = true) { this->_null = _null; }
159 bool check_range(Range<T> range) const;
160 T& at(int i);
161 std::vector<unsigned char> serialize_value(void) const;
162 std::vector<unsigned char> serialize_dimensions(void) const;
163
164 };
165
173 template <typename T>
174 class Container<T, -1, -1>
175 {
176 private:
177 T data;
178 bool _null;
179 public:
181 Container(const T& value);
183 T operator*(void) const;
184 inline bool is_null(void) const { return _null; }
185 inline void set_null(bool _null = true) { this->_null = _null; }
187 bool check_range(Range<T> range) const;
188 T& at(void);
189 std::vector<unsigned char> serialize_value(void) const;
190 std::vector<unsigned char> serialize_dimensions(void) const;
191
192 };
193
194
195 template<typename T, int x, int y>
197 {
198 if ((x < -1) || (y < -1))
199 {
200 throw std::runtime_error("Invalid Dimension smaller than -1");
201 }
202 if ((x == -1) && (y > -1))
203 {
204 throw std::runtime_error("An object can only be one-dimensional in its first dimension!");
205 }
206 _null = true;
207 }
208
209 template<typename T, int x, int y>
210 Container<T, x, y>::Container(const std::vector<std::vector<T>>& value)
211 {
212 if ((x != 0) && (value.size() != x))
213 {
214 throw std::runtime_error("Invalid dimension in assignment!");
215 }
216 if (y != 0)
217 {
218 for (auto v : value)
219 {
220 if (v.size() != y)
221 {
222 throw std::runtime_error("Invalid dimension in assignment!");
223 }
224 }
225 }
226 data = value;
227 _null = false;
228 }
229
230 template<typename T, int x, int y>
232 {
233 std::vector<std::vector<T>> value;
234 int i = 0;
235 for (auto& vx : json.as_array())
236 {
237 value.push_back(std::vector<T>());
238 i++;
239 for (auto vy : vx.as_array())
240 {
241 value.at(i).push_back(to_value<T>(vy));
242 }
243 }
244 if ((x != 0) && (value.size() != x))
245 {
246 throw std::runtime_error("Invalid dimension in assignment!");
247 }
248 if (y != 0)
249 {
250 for (auto v : value)
251 {
252 if (v.size() != y)
253 {
254 throw std::runtime_error("Invalid dimension in assignment!");
255 }
256 }
257 }
258 data = value;
259 _null = false;
260 }
261
262 template<typename T, int x, int y>
263 std::vector<std::vector<T>> Container<T, x, y>::operator*(void) const
264 {
265 return data;
266 }
267
268 template<typename T, int x, int y>
270 {
271 HTTP::Json json_root = HTTP::Json();
272 json_root[U("dimension")] = HTTP::Json::array({ x, y });
273 if (is_null())
274 {
275 json_root[U("value")] = HTTP::Json::null();
276 }
277 else
278 {
279 json_root[U("value")] = HTTP::Json::array();
280 for (int i = 0; i < static_cast<int>(data.size()); i++)
281 {
282 json_root[U("value")][i] = HTTP::Json::array();
283 for (int j = 0; j < static_cast<int>(data.at(i).size()); j++)
284 {
285 json_root[U("value")][i][j] = to_json<T>(data.at(i).at(j));
286 }
287 }
288 }
289 return json_root;
290 }
291
292 template<typename T, int x, int y>
294 {
295 if (is_null())
296 {
297 return true;
298 }
299 for (auto vx : data)
300 {
301 for (auto vy : vx)
302 {
303 if (!range.check(vy))
304 {
305 return false;
306 }
307 }
308 }
309 return true;
310 }
311
312 template<typename T, int x, int y>
313 T& Container<T, x, y>::at(int i, int j)
314 {
315 if (is_null())
316 {
317 throw std::logic_error("Value is NULL");
318 }
319 return data.at(i).at(j);
320 }
321
322 template<typename T, int x, int y>
323 std::vector<unsigned char> Container<T, x, y>::serialize_value(void) const
324 {
325 if (is_null())
326 {
327 throw std::logic_error("Value is NULL");
328 }
329 int current_x = static_cast<int>(data.size());
330 int current_y = current_x > 0 ? static_cast<int>(data.at(0).size()) : 0;
331 size_t size = current_x * current_y * sizeof(T);
332 std::vector<unsigned char> serialization;
333 serialization.reserve(size);
334
335
336 for (int i = 0; i < current_x ; i++)
337 {
338 {
339 const unsigned char* pointer = reinterpret_cast<const unsigned char*>(data.at(i).data());
340 for (int j = 0; j < static_cast<int>(current_y*sizeof(T)); j++)
341 {
342 serialization.push_back(pointer[j]);
343 }
344 }
345 }
346 return serialization;
347 }
348
349 template<typename T, int x, int y>
350 std::vector<unsigned char> Container<T, x, y>::serialize_dimensions(void) const
351 {
352 size_t size = sizeof(uint8_t) + 2 * sizeof(uint32_t);
353 std::vector<unsigned char> serialization;
354 serialization.reserve(size);
355
356 uint8_t n_dimensions = 2;
357 uint32_t current_x = data.size();
358 uint32_t current_y = current_x > 0 ? data.at(0).size : 0;
359
360 serialization.push_back(n_dimensions);
361
362 unsigned char* current_x_pointer = reinterpret_cast<unsigned char*>(&current_x);
363 unsigned char* current_y_pointer = reinterpret_cast<unsigned char*>(&current_y);
364
365 for (int i = 0; i < sizeof(uint32_t); i++)
366 {
367 serialization.push_back(current_x_pointer[i]);
368 }
369 for (int i = 0; i < sizeof(uint32_t); i++)
370 {
371 serialization.push_back(current_y_pointer[i]);
372 }
373
374 return serialization;
375 }
376
377 template<typename T, int x>
379 {
380 if (x < -1)
381 {
382 throw std::runtime_error("Invalid dimension in assignment!");
383 }
384 _null = true;
385 }
386
387 template<typename T, int x>
388 Container<T, x, -1>::Container(const std::vector<T>& value)
389 {
390 if ((x != 0) && (value.size() != x))
391 {
392 throw std::runtime_error("Invalid dimension in assignment!");
393 }
394 data = value;
395 _null = false;
396 }
397
398 template<typename T, int x>
399 Container<T, x, -1>::Container(HTTP::Json json)
400 {
401 std::vector<T> value;
402 for (auto& v : json.as_array())
403 {
404 value.push_back(to_value<T>(v));
405 }
406 if ((x != 0) && (value.size() != x))
407 {
408 throw std::runtime_error("Invalid dimension in assignment!");
409 }
410 data = value;
411 _null = false;
412 }
413
414
415 template<typename T, int x>
416 std::vector<T> Container<T, x, -1>::operator*(void) const
417 {
418 return data;
419 }
420
421 template<typename T, int x>
422 HTTP::Json Container<T, x, -1>::wjson(void)
423 {
424 HTTP::Json json_root = HTTP::Json();
425 json_root[U("dimension")] = HTTP::Json::array();
426 json_root[U("dimension")][0] = HTTP::Json::number(x);
427 if (is_null())
428 {
429 json_root[U("value")] = HTTP::Json::null();
430 }
431 else
432 {
433 json_root[U("value")] = HTTP::Json::array();
434 for (int i = 0; i < static_cast<int>(data.size()); i++)
435 {
436 json_root[U("value")][i] = to_json<T>(data.at(i));
437 }
438 }
439 return json_root;
440 }
441
442 template<typename T, int x>
443 bool Container<T, x, -1>::check_range(Range<T> range) const
444 {
445 if (is_null())
446 {
447 return true;
448 }
449 for (auto vx : data)
450 {
451 if (!range.check(vx))
452 {
453 return false;
454 }
455 }
456 return true;
457 }
458
459 template<typename T, int x>
460 T& Container<T, x, -1>::at(int i)
461 {
462 if (is_null())
463 {
464 throw std::logic_error("Value is NULL");
465 }
466 return data.at(i);
467 }
468
469 template<typename T, int x>
470 std::vector<unsigned char> Container<T, x, -1>::serialize_value(void) const
471 {
472 if (is_null())
473 {
474 throw std::logic_error("Value is NULL");
475 }
476 int current_x = static_cast<int>(data.size());
477 size_t size = current_x * sizeof(T);
478 std::vector<unsigned char> serialization;
479 serialization.reserve(size);
480
481 const unsigned char* pointer = reinterpret_cast<const unsigned char*>(data.data());
482 for (int i = 0; i < static_cast<int>(current_x * sizeof(T)); i++)
483 {
484 serialization.push_back(pointer[i]);
485 }
486 return serialization;
487 }
488
489 template<typename T, int x>
490 std::vector<unsigned char> Container<T, x, -1>::serialize_dimensions(void) const
491 {
492 size_t size = sizeof(uint8_t) + sizeof(uint32_t);
493 std::vector<unsigned char> serialization;
494 serialization.reserve(size);
495
496 uint8_t n_dimensions = 1;
497 uint32_t current_x = static_cast<uint32_t>(data.size());
498
499 serialization.push_back(n_dimensions);
500 unsigned char* current_x_pointer = reinterpret_cast<unsigned char*>(&current_x);
501
502 for (int i = 0; i < sizeof(uint32_t); i++)
503 {
504 serialization.push_back(current_x_pointer[i]);
505 }
506 return serialization;
507 }
508
509 template<typename T>
510 Container<T, -1, -1>::Container()
511 {
512 _null = true;
513 }
514
515 template<typename T>
516 Container<T, -1, -1>::Container(const T& value)
517 {
518 data = value;
519 _null = false;
520 }
521
522 template<typename T>
523 Container<T, -1, -1>::Container(HTTP::Json json)
524 {
525 T value = to_value<T>(json);
526 data = value;
527 _null = false;
528 }
529
530 template<typename T>
531 T Container<T, -1, -1>::operator*(void) const
532 {
533 return data;
534 }
535
536 template<typename T>
537 HTTP::Json Container<T, -1, -1>::wjson(void)
538 {
539 HTTP::Json json_root = HTTP::Json();
540 json_root[U("dimension")] = HTTP::Json::array();
541 if (is_null())
542 {
543 json_root[U("value")] = HTTP::Json::null();
544 }
545 else
546 {
547 json_root[U("value")] = to_json<T>(data);
548 }
549 return json_root;
550 }
551
552 template<typename T>
553 bool Container<T, -1, -1>::check_range(Range<T> range) const
554 {
555 if (is_null())
556 {
557 return true;
558 }
559 return range.check(data);
560 }
561
562 template<typename T>
563 T& Container<T, -1, -1>::at(void)
564 {
565 if (is_null())
566 {
567 throw std::logic_error("Value is NULL");
568 }
569 return data;
570 }
571
572 template<typename T>
573 std::vector<unsigned char> Container<T, -1, -1>::serialize_value(void) const
574 {
575 if (is_null())
576 {
577 throw std::logic_error("Value is NULL");
578 }
579 size_t size = sizeof(T);
580 std::vector<unsigned char> serialization;
581 serialization.reserve(size);
582
583 const unsigned char* pointer = reinterpret_cast<const unsigned char*>(&data);
584 for (int i = 0; i < sizeof(T); i++)
585 {
586 serialization.push_back(pointer[i]);
587 }
588 return serialization;
589 }
590
591 template<typename T>
592 std::vector<unsigned char> Container<T, -1, -1>::serialize_dimensions(void) const
593 {
594 size_t size = sizeof(uint8_t);
595 std::vector<unsigned char> serialization;
596 serialization.reserve(size);
597
598 uint8_t n_dimensions = 0;
599
600 serialization.push_back(n_dimensions);
601 return serialization;
602 }
603}
nlohmann::json json
Container(HTTP::Json json)
bool check_range(Range< T > range) const
std::vector< unsigned char > serialize_dimensions(void) const
void set_null(bool _null=true)
Definition: Container.h:185
std::vector< unsigned char > serialize_value(void) const
bool is_null(void) const
Definition: Container.h:184
bool is_null(void) const
Definition: Container.h:156
bool check_range(Range< T > range) const
std::vector< unsigned char > serialize_value(void) const
std::vector< T > operator*(void) const
Container(const std::vector< T > &value)
void set_null(bool _null=true)
Definition: Container.h:157
std::vector< unsigned char > serialize_dimensions(void) const
Container(HTTP::Json json)
Data Container.
Definition: Container.h:22
Container(HTTP::Json json)
JSON Constructor.
Definition: Container.h:231
HTTP::Json wjson(void)
WJSON representation.
Definition: Container.h:269
void set_null(bool _null=true)
Set null.
Definition: Container.h:87
std::vector< unsigned char > serialize_value(void) const
Serialize value.
Definition: Container.h:323
std::vector< std::vector< T > > operator*(void) const
Deferencing operator.
Definition: Container.h:263
T & at(int i, int j)
Data Accessor.
Definition: Container.h:313
bool is_null(void) const
Is Null?
Definition: Container.h:79
Container(const std::vector< std::vector< T > > &value)
Data copy constructor.
Definition: Container.h:210
Container()
Empty constructor.
Definition: Container.h:196
bool check_range(Range< T > range) const
Check range.
Definition: Container.h:293
std::vector< unsigned char > serialize_dimensions(void) const
Serialize dimensions.
Definition: Container.h:350
Range Helper Class.
Definition: Range.h:25
bool check(const T &value)
Check.
Definition: Range.cpp:85
web::json::value Json
HTTP JSON.
Definition: Types.h:39
Type definitions.
Definition: Container.h:7