|
| 1 | +/** |
| 2 | + * @file |
| 3 | + * @author Michal Sedlak <[email protected]> |
| 4 | + * @brief Aggregator field value representation |
| 5 | + * |
| 6 | + * Copyright: (C) 2024 CESNET, z.s.p.o. |
| 7 | + * SPDX-License-Identifier: BSD-3-Clause |
| 8 | + */ |
| 9 | + |
| 10 | +#include <aggregator/value.hpp> |
| 11 | + |
| 12 | +#include <cassert> |
| 13 | +#include <stdexcept> |
| 14 | + |
| 15 | +namespace fdsdump { |
| 16 | +namespace aggregator { |
| 17 | + |
| 18 | +std::string |
| 19 | +data_type_to_str(DataType data_type) |
| 20 | +{ |
| 21 | + switch (data_type) { |
| 22 | + case DataType::Unassigned: |
| 23 | + return "Unassigned"; |
| 24 | + case DataType::IPAddress: |
| 25 | + return "IPAddress"; |
| 26 | + case DataType::IPv4Address: |
| 27 | + return "IPv4Address"; |
| 28 | + case DataType::IPv6Address: |
| 29 | + return "IPv6Address"; |
| 30 | + case DataType::MacAddress: |
| 31 | + return "MacAddress"; |
| 32 | + case DataType::Unsigned8: |
| 33 | + return "Unsigned8"; |
| 34 | + case DataType::Signed8: |
| 35 | + return "Signed8"; |
| 36 | + case DataType::Unsigned16: |
| 37 | + return "Unsigned16"; |
| 38 | + case DataType::Signed16: |
| 39 | + return "Signed16"; |
| 40 | + case DataType::Unsigned32: |
| 41 | + return "Unsigned32"; |
| 42 | + case DataType::Signed32: |
| 43 | + return "Signed32"; |
| 44 | + case DataType::Unsigned64: |
| 45 | + return "Unsigned64"; |
| 46 | + case DataType::Signed64: |
| 47 | + return "Signed64"; |
| 48 | + case DataType::DateTime: |
| 49 | + return "DateTime"; |
| 50 | + case DataType::String128B: |
| 51 | + return "String128B"; |
| 52 | + case DataType::Octets128B: |
| 53 | + return "Octets128B"; |
| 54 | + case DataType::VarString: |
| 55 | + return "VarString"; |
| 56 | + } |
| 57 | + assert(0); |
| 58 | + return "<unknown>"; |
| 59 | +} |
| 60 | + |
| 61 | +ValueView::ValueView(DataType data_type, Value &value) : |
| 62 | + m_data_type(data_type), |
| 63 | + m_value(value) |
| 64 | +{ |
| 65 | +} |
| 66 | + |
| 67 | +uint8_t |
| 68 | +ValueView::as_u8() const |
| 69 | +{ |
| 70 | + switch (m_data_type) { |
| 71 | + case DataType::Unsigned8: |
| 72 | + return m_value.u8; |
| 73 | + default: |
| 74 | + throw std::runtime_error("Cannot view value as u8: incompatible data type"); |
| 75 | + } |
| 76 | +} |
| 77 | + |
| 78 | +uint16_t |
| 79 | +ValueView::as_u16() const |
| 80 | +{ |
| 81 | + switch (m_data_type) { |
| 82 | + case DataType::Unsigned8: |
| 83 | + return m_value.u8; |
| 84 | + case DataType::Unsigned16: |
| 85 | + return m_value.u16; |
| 86 | + default: |
| 87 | + throw std::runtime_error("Cannot view value as u16: incompatible data type"); |
| 88 | + } |
| 89 | +} |
| 90 | + |
| 91 | +uint32_t |
| 92 | +ValueView::as_u32() const |
| 93 | +{ |
| 94 | + switch (m_data_type) { |
| 95 | + case DataType::Unsigned8: |
| 96 | + return m_value.u8; |
| 97 | + case DataType::Unsigned16: |
| 98 | + return m_value.u16; |
| 99 | + case DataType::Unsigned32: |
| 100 | + return m_value.u32; |
| 101 | + default: |
| 102 | + throw std::runtime_error("Cannot view value as u32: incompatible data type"); |
| 103 | + } |
| 104 | +} |
| 105 | + |
| 106 | +uint64_t |
| 107 | +ValueView::as_u64() const |
| 108 | +{ |
| 109 | + switch (m_data_type) { |
| 110 | + case DataType::Unsigned8: |
| 111 | + return m_value.u8; |
| 112 | + case DataType::Unsigned16: |
| 113 | + return m_value.u16; |
| 114 | + case DataType::Unsigned32: |
| 115 | + return m_value.u32; |
| 116 | + case DataType::Unsigned64: |
| 117 | + return m_value.u64; |
| 118 | + default: |
| 119 | + throw std::runtime_error("Cannot view value as u64: incompatible data type"); |
| 120 | + } |
| 121 | +} |
| 122 | + |
| 123 | +int8_t |
| 124 | +ValueView::as_i8() const |
| 125 | +{ |
| 126 | + switch (m_data_type) { |
| 127 | + case DataType::Signed8: |
| 128 | + return m_value.i8; |
| 129 | + default: |
| 130 | + throw std::runtime_error("Cannot view value as i8: incompatible data type"); |
| 131 | + } |
| 132 | +} |
| 133 | + |
| 134 | +int16_t |
| 135 | +ValueView::as_i16() const |
| 136 | +{ |
| 137 | + switch (m_data_type) { |
| 138 | + case DataType::Signed8: |
| 139 | + return m_value.i8; |
| 140 | + case DataType::Signed16: |
| 141 | + return m_value.i16; |
| 142 | + default: |
| 143 | + throw std::runtime_error("Cannot view value as i16: incompatible data type"); |
| 144 | + } |
| 145 | +} |
| 146 | + |
| 147 | +int32_t |
| 148 | +ValueView::as_i32() const |
| 149 | +{ |
| 150 | + switch (m_data_type) { |
| 151 | + case DataType::Signed8: |
| 152 | + return m_value.i8; |
| 153 | + case DataType::Signed16: |
| 154 | + return m_value.i16; |
| 155 | + case DataType::Signed32: |
| 156 | + return m_value.i32; |
| 157 | + default: |
| 158 | + throw std::runtime_error("Cannot view value as i32: incompatible data type"); |
| 159 | + } |
| 160 | +} |
| 161 | + |
| 162 | +int64_t |
| 163 | +ValueView::as_i64() const |
| 164 | +{ |
| 165 | + switch (m_data_type) { |
| 166 | + case DataType::Signed8: |
| 167 | + return m_value.i8; |
| 168 | + case DataType::Signed16: |
| 169 | + return m_value.i16; |
| 170 | + case DataType::Signed32: |
| 171 | + return m_value.i32; |
| 172 | + case DataType::Signed64: |
| 173 | + return m_value.i64; |
| 174 | + default: |
| 175 | + throw std::runtime_error("Cannot view value as i64: incompatible data type"); |
| 176 | + } |
| 177 | +} |
| 178 | + |
| 179 | +IPAddr |
| 180 | +ValueView::as_ip() |
| 181 | +{ |
| 182 | + switch (m_data_type) { |
| 183 | + case DataType::IPv4Address: |
| 184 | + return IPAddr::ip4(m_value.ipv4); |
| 185 | + case DataType::IPv6Address: |
| 186 | + return IPAddr::ip6(m_value.ipv6); |
| 187 | + case DataType::IPAddress: |
| 188 | + return m_value.ip; |
| 189 | + default: |
| 190 | + throw std::runtime_error("Cannot view value as ip: incompatible data type"); |
| 191 | + } |
| 192 | +} |
| 193 | + |
| 194 | +} // namespace aggregator |
| 195 | +} // namespace fdsdump |
| 196 | + |
0 commit comments