|
| 1 | + |
| 2 | + |
| 3 | +#include <stdexcept> |
| 4 | + |
| 5 | +#include "fieldView.hpp" |
| 6 | + |
| 7 | +uint64_t |
| 8 | +FieldView::as_uint() const |
| 9 | +{ |
| 10 | + uint64_t result; |
| 11 | + int ret; |
| 12 | + |
| 13 | + ret = fds_get_uint_be(m_field.data, m_field.size, &result); |
| 14 | + if (ret != FDS_OK) { |
| 15 | + throw std::invalid_argument("Field value conversion error (unsinged)"); |
| 16 | + } |
| 17 | + |
| 18 | + return result; |
| 19 | +} |
| 20 | + |
| 21 | +int64_t |
| 22 | +FieldView::as_int() const |
| 23 | +{ |
| 24 | + int64_t result; |
| 25 | + int ret; |
| 26 | + |
| 27 | + ret = fds_get_int_be(m_field.data, m_field.size, &result); |
| 28 | + if (ret != FDS_OK) { |
| 29 | + throw std::invalid_argument("Conversion error (signed)"); |
| 30 | + } |
| 31 | + |
| 32 | + return result; |
| 33 | +} |
| 34 | + |
| 35 | +double |
| 36 | +FieldView::as_float() const |
| 37 | +{ |
| 38 | + double result; |
| 39 | + int ret; |
| 40 | + |
| 41 | + ret = fds_get_float_be(m_field.data, m_field.size, &result); |
| 42 | + if (ret != FDS_OK) { |
| 43 | + throw std::invalid_argument("Conversion error (float)"); |
| 44 | + } |
| 45 | + |
| 46 | + return result; |
| 47 | +} |
| 48 | + |
| 49 | +bool |
| 50 | +FieldView::as_bool() const |
| 51 | +{ |
| 52 | + bool result; |
| 53 | + int ret; |
| 54 | + |
| 55 | + ret = fds_get_bool(m_field.data, m_field.size, &result); |
| 56 | + if (ret != FDS_OK) { |
| 57 | + throw std::invalid_argument("Conversion error (boolean)"); |
| 58 | + } |
| 59 | + |
| 60 | + return result; |
| 61 | +} |
| 62 | + |
| 63 | +struct timespec |
| 64 | +FieldView::as_datetime() const |
| 65 | +{ |
| 66 | + const struct fds_iemgr_elem *elem = m_field.info->def; |
| 67 | + struct timespec result; |
| 68 | + int ret; |
| 69 | + |
| 70 | + if (!elem) { |
| 71 | + throw std::invalid_argument("Conversion error (undefined datetime type)"); |
| 72 | + } |
| 73 | + |
| 74 | + ret = fds_get_datetime_hp_be(m_field.data, m_field.size, elem->data_type, &result); |
| 75 | + if (ret != FDS_OK) { |
| 76 | + throw std::invalid_argument("Conversion error (datetime)"); |
| 77 | + } |
| 78 | + |
| 79 | + return result; |
| 80 | +} |
| 81 | + |
| 82 | +IPAddr |
| 83 | +FieldView::as_ipaddr() const |
| 84 | +{ |
| 85 | + if (m_field.size == 4U) { |
| 86 | + const uint32_t *value = reinterpret_cast<uint32_t *>(m_field.data); |
| 87 | + return IPAddr(*value); |
| 88 | + } |
| 89 | + |
| 90 | + if (m_field.size == 16U) { |
| 91 | + return IPAddr(m_field.data); |
| 92 | + } |
| 93 | + |
| 94 | + throw std::invalid_argument("Conversion error (ipaddr)"); |
| 95 | +} |
0 commit comments