|
| 1 | +/** |
| 2 | + * \file utils.hpp |
| 3 | + * \brief Utility functions |
| 4 | + * \author Jiri Havranek <[email protected]> |
| 5 | + * \date 2021 |
| 6 | + */ |
| 7 | +/* |
| 8 | + * Copyright (C) 2021 CESNET |
| 9 | + * |
| 10 | + * LICENSE TERMS |
| 11 | + * |
| 12 | + * Redistribution and use in source and binary forms, with or without |
| 13 | + * modification, are permitted provided that the following conditions |
| 14 | + * are met: |
| 15 | + * 1. Redistributions of source code must retain the above copyright |
| 16 | + * notice, this list of conditions and the following disclaimer. |
| 17 | + * 2. Redistributions in binary form must reproduce the above copyright |
| 18 | + * notice, this list of conditions and the following disclaimer in |
| 19 | + * the documentation and/or other materials provided with the |
| 20 | + * distribution. |
| 21 | + * 3. Neither the name of the Company nor the names of its contributors |
| 22 | + * may be used to endorse or promote products derived from this |
| 23 | + * software without specific prior written permission. |
| 24 | + * |
| 25 | + * |
| 26 | + * |
| 27 | + */ |
| 28 | + |
| 29 | +#ifndef IPXP_UTILS_HPP |
| 30 | +#define IPXP_UTILS_HPP |
| 31 | + |
| 32 | +#include <algorithm> |
| 33 | +#include <cctype> |
| 34 | +#include <cstdint> |
| 35 | +#include <limits> |
| 36 | +#include <set> |
| 37 | +#include <sstream> |
| 38 | +#include <stdexcept> |
| 39 | +#include <string> |
| 40 | +#include <type_traits> |
| 41 | +#include <utility> |
| 42 | +#include <vector> |
| 43 | + |
| 44 | +#include <sys/time.h> |
| 45 | + |
| 46 | +namespace ipxp { |
| 47 | + |
| 48 | +void parse_range( |
| 49 | + const std::string& arg, |
| 50 | + std::string& from, |
| 51 | + std::string& to, |
| 52 | + const std::string& delim = "-"); |
| 53 | +bool str2bool(std::string str); |
| 54 | +void trim_str(std::string& str); |
| 55 | +uint32_t variable2ipfix_buffer(uint8_t* buffer2write, uint8_t* buffer2read, uint16_t len); |
| 56 | + |
| 57 | +template<typename T> |
| 58 | +constexpr T const& max(const T& a, const T& b) |
| 59 | +{ |
| 60 | + return a > b ? a : b; |
| 61 | +} |
| 62 | + |
| 63 | +/* |
| 64 | + * \brief Count number of '1' bits |
| 65 | + * \param [in] num Number to count ones in |
| 66 | + * \return Number of ones counted |
| 67 | + */ |
| 68 | +template<typename T> |
| 69 | +static constexpr unsigned bitcount(T num) |
| 70 | +{ |
| 71 | + static_assert(!std::is_signed<T>(), "bitcount function is for unsigned types only"); |
| 72 | + return num == 0 ? 0 : (bitcount<T>(num >> 1) + (num & 1)); |
| 73 | +} |
| 74 | + |
| 75 | +template<typename T> |
| 76 | +static constexpr bool is_fpoint() |
| 77 | +{ |
| 78 | + return std::is_floating_point<T>(); |
| 79 | +} |
| 80 | + |
| 81 | +template<typename T> |
| 82 | +static constexpr bool is_uint() |
| 83 | +{ |
| 84 | + return std::is_integral<T>() && std::is_unsigned<T>(); |
| 85 | +} |
| 86 | + |
| 87 | +template<typename T> |
| 88 | +static constexpr bool is_sint() |
| 89 | +{ |
| 90 | + return std::is_integral<T>() && std::is_signed<T>(); |
| 91 | +} |
| 92 | + |
| 93 | +// Use of SFINAE to implement specific conversion function variants |
| 94 | + |
| 95 | +template<typename T> |
| 96 | +T str2num(std::string str, typename std::enable_if<is_fpoint<T>()>::type* = nullptr) |
| 97 | +{ |
| 98 | + size_t pos; |
| 99 | + double tmp; |
| 100 | + |
| 101 | + trim_str(str); |
| 102 | + try { |
| 103 | + tmp = std::stold(str, &pos); |
| 104 | + } catch (std::out_of_range& e) { |
| 105 | + throw std::invalid_argument(str); |
| 106 | + } |
| 107 | + if (pos != str.size() || tmp < std::numeric_limits<T>::min() |
| 108 | + || tmp > std::numeric_limits<T>::max()) { |
| 109 | + throw std::invalid_argument(str); |
| 110 | + } |
| 111 | + |
| 112 | + return static_cast<T>(tmp); |
| 113 | +} |
| 114 | + |
| 115 | +template<typename T> |
| 116 | +T str2num(std::string str, typename std::enable_if<is_sint<T>()>::type* = nullptr) |
| 117 | +{ |
| 118 | + long long tmp; |
| 119 | + size_t pos; |
| 120 | + |
| 121 | + trim_str(str); |
| 122 | + try { |
| 123 | + tmp = std::stoll(str, &pos, 0); |
| 124 | + } catch (std::out_of_range& e) { |
| 125 | + throw std::invalid_argument(str); |
| 126 | + } |
| 127 | + if (pos != str.size() || tmp < std::numeric_limits<T>::min() |
| 128 | + || tmp > std::numeric_limits<T>::max()) { |
| 129 | + throw std::invalid_argument(str); |
| 130 | + } |
| 131 | + |
| 132 | + return static_cast<T>(tmp); |
| 133 | +} |
| 134 | + |
| 135 | +template<typename T> |
| 136 | +T str2num(std::string str, typename std::enable_if<is_uint<T>()>::type* = nullptr) |
| 137 | +{ |
| 138 | + unsigned long long tmp; |
| 139 | + size_t pos; |
| 140 | + |
| 141 | + trim_str(str); |
| 142 | + try { |
| 143 | + tmp = std::stoull(str, &pos, 0); |
| 144 | + } catch (std::out_of_range& e) { |
| 145 | + throw std::invalid_argument(str); |
| 146 | + } |
| 147 | + if (pos != str.size() || tmp < std::numeric_limits<T>::min() |
| 148 | + || tmp > std::numeric_limits<T>::max()) { |
| 149 | + throw std::invalid_argument(str); |
| 150 | + } |
| 151 | + |
| 152 | + return static_cast<T>(tmp); |
| 153 | +} |
| 154 | + |
| 155 | +/** |
| 156 | + * @brief Convert struct Timeval to microseconds |
| 157 | + */ |
| 158 | +uint64_t timeval2usec(const struct timeval& tv); |
| 159 | + |
| 160 | +/** |
| 161 | + * @brief Convert vector to string, e.g. for error messages |
| 162 | + */ |
| 163 | +template<typename T> |
| 164 | +std::string vec2str(const std::vector<T>& vec) |
| 165 | +{ |
| 166 | + std::stringstream ss; |
| 167 | + bool first = true; |
| 168 | + for (auto& item : vec) { |
| 169 | + if (!first) |
| 170 | + ss << ", "; |
| 171 | + ss << item; |
| 172 | + first = false; |
| 173 | + } |
| 174 | + return ss.str(); |
| 175 | +} |
| 176 | + |
| 177 | +/** |
| 178 | + * @brief Copy uint32 in little endian byte order to destination in host byte order |
| 179 | + */ |
| 180 | +void memcpy_le32toh(uint32_t* dest, const uint32_t* src); |
| 181 | + |
| 182 | +constexpr static inline |
| 183 | +timeval operator+(const timeval& a, const timeval& b) noexcept |
| 184 | +{ |
| 185 | + constexpr time_t USEC_IN_SEC = 1'000'000; |
| 186 | + |
| 187 | + timeval result{}; |
| 188 | + result.tv_sec = a.tv_sec + b.tv_sec; |
| 189 | + result.tv_usec = a.tv_usec + b.tv_usec; |
| 190 | + if (result.tv_usec >= USEC_IN_SEC) { |
| 191 | + result.tv_sec++; |
| 192 | + result.tv_usec -= USEC_IN_SEC; |
| 193 | + } |
| 194 | + return result; |
| 195 | +} |
| 196 | + |
| 197 | +constexpr static inline |
| 198 | +timeval operator-(const timeval& a) noexcept |
| 199 | +{ |
| 200 | + timeval result{}; |
| 201 | + result.tv_sec = -a.tv_sec; |
| 202 | + result.tv_usec = -a.tv_usec; |
| 203 | + return result; |
| 204 | +} |
| 205 | + |
| 206 | +constexpr static inline |
| 207 | +timeval operator-(const timeval& a, const timeval& b) noexcept |
| 208 | +{ |
| 209 | + return a + (-b); |
| 210 | +} |
| 211 | + |
| 212 | +constexpr static inline |
| 213 | +bool operator>(const timeval& a, const timeval& b) noexcept |
| 214 | +{ |
| 215 | + return std::tie(a.tv_sec, a.tv_usec) > std::tie(b.tv_sec, b.tv_usec); |
| 216 | +} |
| 217 | + |
| 218 | +constexpr static inline |
| 219 | +bool operator<(const timeval& a, const timeval& b) noexcept |
| 220 | +{ |
| 221 | + return std::tie(a.tv_sec, a.tv_usec) < std::tie(b.tv_sec, b.tv_usec); |
| 222 | +} |
| 223 | + |
| 224 | +constexpr static inline |
| 225 | +bool operator==(const timeval& a, const timeval& b) noexcept |
| 226 | +{ |
| 227 | + return not (a < b) and not (b < a); |
| 228 | +} |
| 229 | + |
| 230 | +} // namespace ipxp |
| 231 | + |
| 232 | +#endif /* IPXP_UTILS_HPP */ |
0 commit comments