Orion
high-rate readout
utils.hpp
Go to the documentation of this file.
1 
11 #pragma once
12 
13 #include "itk/logger.hpp"
14 
15 #include <cassert>
16 #include <span>
17 #include <chrono>
18 
19 namespace itk::utils {
20 
21 // making string from u8, u16, u32 streams
22 std::string to_string(const std::span<const uint8_t> data);
23 std::string to_string(const std::span<const uint16_t> data);
24 std::string to_string(const std::span<const uint32_t> data);
25 std::string to_string(const std::span<const uint64_t> data);
26 
27 
28 } // itk::utils
29 
30 template <uint n, typename T>
31 inline void trunc_bits(T& val) {
32  static_assert(std::is_unsigned<T>(), "Unsigned type must be passed");
33  constexpr T mask = static_cast<T>(~(static_cast<T>(~0) << n));
34  auto tmp = val;
35  val &= mask;
36  assert(val == tmp);
37 }
38 
39 
40 inline uint32_t hex_to_int(const std::string &str) {
41  std::size_t pos;
42  uint32_t res = stol(str, &pos, 16);
43  if (str.length() != pos) {
44  throw std::runtime_error("incorrect hex string.");
45  }
46  return res;
47 }
48 
49 
50 inline uint delta_msec(std::chrono::steady_clock::duration delta) {
51  using namespace std::chrono;
52  return duration_cast<milliseconds>(delta).count();
53 }
Logger definitions.