20 template <
typename T,
int x=-1,
int y=-1>
29 std::vector<std::vector<T>> data;
54 Container(
const std::vector<std::vector<T>>& value);
79 inline bool is_null(
void)
const {
return _null;}
87 inline void set_null(
bool _null =
true) { this->_null = _null; }
145 template <
typename T,
int x>
156 inline bool is_null(
void)
const {
return _null; }
157 inline void set_null(
bool _null =
true) { this->_null = _null; }
173 template <
typename T>
184 inline bool is_null(
void)
const {
return _null; }
185 inline void set_null(
bool _null =
true) { this->_null = _null; }
195 template<
typename T,
int x,
int y>
198 if ((x < -1) || (y < -1))
200 throw std::runtime_error(
"Invalid Dimension smaller than -1");
202 if ((x == -1) && (y > -1))
204 throw std::runtime_error(
"An object can only be one-dimensional in its first dimension!");
209 template<
typename T,
int x,
int y>
212 if ((x != 0) && (value.size() != x))
214 throw std::runtime_error(
"Invalid dimension in assignment!");
222 throw std::runtime_error(
"Invalid dimension in assignment!");
230 template<
typename T,
int x,
int y>
233 std::vector<std::vector<T>> value;
235 for (
auto& vx :
json.as_array())
237 value.push_back(std::vector<T>());
239 for (
auto vy : vx.as_array())
241 value.
at(i).push_back(to_value<T>(vy));
244 if ((x != 0) && (value.size() != x))
246 throw std::runtime_error(
"Invalid dimension in assignment!");
254 throw std::runtime_error(
"Invalid dimension in assignment!");
262 template<
typename T,
int x,
int y>
268 template<
typename T,
int x,
int y>
272 json_root[U(
"dimension")] = HTTP::Json::array({ x, y });
275 json_root[U(
"value")] = HTTP::Json::null();
279 json_root[U(
"value")] = HTTP::Json::array();
280 for (
int i = 0; i < static_cast<int>(data.size()); i++)
282 json_root[U(
"value")][i] = HTTP::Json::array();
283 for (
int j = 0; j < static_cast<int>(data.at(i).size()); j++)
285 json_root[U(
"value")][i][j] = to_json<T>(data.at(i).at(j));
292 template<
typename T,
int x,
int y>
303 if (!range.
check(vy))
312 template<
typename T,
int x,
int y>
317 throw std::logic_error(
"Value is NULL");
319 return data.at(i).at(j);
322 template<
typename T,
int x,
int y>
327 throw std::logic_error(
"Value is NULL");
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);
336 for (
int i = 0; i < current_x ; i++)
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++)
342 serialization.push_back(pointer[j]);
346 return serialization;
349 template<
typename T,
int x,
int y>
352 size_t size =
sizeof(uint8_t) + 2 *
sizeof(uint32_t);
353 std::vector<unsigned char> serialization;
354 serialization.reserve(size);
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;
360 serialization.push_back(n_dimensions);
362 unsigned char* current_x_pointer =
reinterpret_cast<unsigned char*
>(¤t_x);
363 unsigned char* current_y_pointer =
reinterpret_cast<unsigned char*
>(¤t_y);
365 for (
int i = 0; i <
sizeof(uint32_t); i++)
367 serialization.push_back(current_x_pointer[i]);
369 for (
int i = 0; i <
sizeof(uint32_t); i++)
371 serialization.push_back(current_y_pointer[i]);
374 return serialization;
377 template<
typename T,
int x>
382 throw std::runtime_error(
"Invalid dimension in assignment!");
387 template<
typename T,
int x>
388 Container<T, x, -1>::Container(
const std::vector<T>& value)
390 if ((x != 0) && (value.size() != x))
392 throw std::runtime_error(
"Invalid dimension in assignment!");
398 template<
typename T,
int x>
401 std::vector<T> value;
402 for (
auto& v :
json.as_array())
404 value.push_back(to_value<T>(v));
406 if ((x != 0) && (value.size() != x))
408 throw std::runtime_error(
"Invalid dimension in assignment!");
415 template<
typename T,
int x>
416 std::vector<T> Container<T, x, -1>::operator*(
void)
const
421 template<
typename T,
int x>
425 json_root[U(
"dimension")] = HTTP::Json::array();
426 json_root[U(
"dimension")][0] = HTTP::Json::number(x);
429 json_root[U(
"value")] = HTTP::Json::null();
433 json_root[U(
"value")] = HTTP::Json::array();
434 for (
int i = 0; i < static_cast<int>(data.size()); i++)
436 json_root[U(
"value")][i] = to_json<T>(data.at(i));
442 template<
typename T,
int x>
443 bool Container<T, x, -1>::check_range(Range<T> range)
const
451 if (!range.check(vx))
459 template<
typename T,
int x>
460 T& Container<T, x, -1>::at(
int i)
464 throw std::logic_error(
"Value is NULL");
469 template<
typename T,
int x>
470 std::vector<unsigned char> Container<T, x, -1>::serialize_value(
void)
const
474 throw std::logic_error(
"Value is NULL");
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);
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++)
484 serialization.push_back(pointer[i]);
486 return serialization;
489 template<
typename T,
int x>
490 std::vector<unsigned char> Container<T, x, -1>::serialize_dimensions(
void)
const
492 size_t size =
sizeof(uint8_t) +
sizeof(uint32_t);
493 std::vector<unsigned char> serialization;
494 serialization.reserve(size);
496 uint8_t n_dimensions = 1;
497 uint32_t current_x =
static_cast<uint32_t
>(data.size());
499 serialization.push_back(n_dimensions);
500 unsigned char* current_x_pointer =
reinterpret_cast<unsigned char*
>(¤t_x);
502 for (
int i = 0; i <
sizeof(uint32_t); i++)
504 serialization.push_back(current_x_pointer[i]);
506 return serialization;
510 Container<T, -1, -1>::Container()
516 Container<T, -1, -1>::Container(
const T& value)
525 T value = to_value<T>(
json);
531 T Container<T, -1, -1>::operator*(
void)
const
540 json_root[U(
"dimension")] = HTTP::Json::array();
543 json_root[U(
"value")] = HTTP::Json::null();
547 json_root[U(
"value")] = to_json<T>(data);
553 bool Container<T, -1, -1>::check_range(Range<T> range)
const
559 return range.check(data);
563 T& Container<T, -1, -1>::at(
void)
567 throw std::logic_error(
"Value is NULL");
573 std::vector<unsigned char> Container<T, -1, -1>::serialize_value(
void)
const
577 throw std::logic_error(
"Value is NULL");
579 size_t size =
sizeof(T);
580 std::vector<unsigned char> serialization;
581 serialization.reserve(size);
583 const unsigned char* pointer =
reinterpret_cast<const unsigned char*
>(&data);
584 for (
int i = 0; i <
sizeof(T); i++)
586 serialization.push_back(pointer[i]);
588 return serialization;
592 std::vector<unsigned char> Container<T, -1, -1>::serialize_dimensions(
void)
const
594 size_t size =
sizeof(uint8_t);
595 std::vector<unsigned char> serialization;
596 serialization.reserve(size);
598 uint8_t n_dimensions = 0;
600 serialization.push_back(n_dimensions);
601 return serialization;
Container(HTTP::Json json)
Container(const T &value)
bool check_range(Range< T > range) const
std::vector< unsigned char > serialize_dimensions(void) const
void set_null(bool _null=true)
std::vector< unsigned char > serialize_value(void) const
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)
std::vector< unsigned char > serialize_dimensions(void) const
Container(HTTP::Json json)
Container(HTTP::Json json)
JSON Constructor.
HTTP::Json wjson(void)
WJSON representation.
void set_null(bool _null=true)
Set null.
std::vector< unsigned char > serialize_value(void) const
Serialize value.
std::vector< std::vector< T > > operator*(void) const
Deferencing operator.
T & at(int i, int j)
Data Accessor.
bool is_null(void) const
Is Null?
Container(const std::vector< std::vector< T > > &value)
Data copy constructor.
Container()
Empty constructor.
bool check_range(Range< T > range) const
Check range.
std::vector< unsigned char > serialize_dimensions(void) const
Serialize dimensions.
bool check(const T &value)
Check.
web::json::value Json
HTTP JSON.