include/simdb/
Coverage:
low: ≥ 0%
medium: ≥ 75.0%
high: ≥ 90.0%
Lines:
36 / 36
100.0%
Branches:
8 / 9
88.9%
Functions:
12 / 12
100.0%

apps/argos/StreamBuffer.hpp
Line Branch Exec Source
1 // <StreamBuffer.hpp> -*- C++ -*-
2
3 #pragma once
4
5 #include "simdb/utils/TinyStrings.hpp"
6 #include "simdb/utils/TypeTraits.hpp"
7
8 #include <array>
9 #include <cstdint>
10 #include <cstring>
11 #include <string>
12 #include <string_view>
13 #include <type_traits>
14 #include <vector>
15
16 namespace simdb::argos {
17
18 /// \class StreamBuffer
19 /// \brief Utility class which wraps a char buffer with typed append operators.
20 class StreamBuffer
21 {
22 public:
23 258294 StreamBuffer(std::vector<char>& out, TinyStrings<>* tiny_strings = nullptr, bool clear_first = true) :
24 258294 out_(&out),
25 258294 tiny_strings_(tiny_strings)
26 {
27
1/2
✓ Branch 2 → 3 taken 258294 times.
✗ Branch 2 → 4 not taken.
258294 if (clear_first)
28 {
29 258294 out_->clear();
30 }
31 258294 }
32
33 StreamBuffer(const StreamBuffer&) = delete;
34 StreamBuffer(StreamBuffer&&) = default;
35
36 1350548 void append(const void* data, const size_t num_bytes)
37 {
38 1350548 auto bytes = static_cast<const char*>(data);
39
1/1
✓ Branch 4 → 5 taken 1350548 times.
1350548 out_->insert(out_->end(), bytes, bytes + num_bytes);
40 1350548 }
41
42
1/1
✓ Branch 2 → 3 taken 116433 times.
116433 void append(const bool val) { append(static_cast<uint8_t>(val)); }
43
44
2/2
✓ Branch 2 → 3 taken 113865 times.
✓ Branch 3 → 4 taken 113865 times.
113865 void append(const std::string& s) { append(tiny_strings_->getStringID(s)); }
45
46 template <typename T>
47 std::enable_if_t<std::is_trivial_v<T> && std::is_standard_layout_v<T> && !std::is_enum_v<T>, void>
48 1042284 append(const T& val)
49 {
50 static_assert(std::is_trivial_v<T> && std::is_standard_layout_v<T>);
51 1042284 append(&val, sizeof(T));
52 1042284 }
53
54 308264 template <typename T, typename Alloc> void append(const std::vector<T, Alloc>& val)
55 {
56 static_assert(std::is_trivial_v<T> && std::is_standard_layout_v<T>);
57 308264 append(val.data(), val.size() * sizeof(T));
58 308264 }
59
60 template <typename T, size_t N> void append(const T (&val)[N])
61 {
62 static_assert(std::is_trivial_v<T> && std::is_standard_layout_v<T>);
63 append(val, N * sizeof(T));
64 }
65
66 template <typename T, size_t N> void append(const std::array<T, N>& val)
67 {
68 static_assert(std::is_trivial_v<T> && std::is_standard_layout_v<T>);
69 append(val.data(), N * sizeof(T));
70 }
71
72 364155 template <typename T> std::enable_if_t<std::is_enum_v<T>, void> append(const T val)
73 {
74 using underlying_t = std::underlying_type_t<T>;
75
3/3
std::enable_if<is_enum_v<SignedEnum>, void>::type simdb::argos::StreamBuffer::append<SignedEnum>(SignedEnum):
✓ Branch 2 → 3 taken 115071 times.
std::enable_if<is_enum_v<UnsignedEnum>, void>::type simdb::argos::StreamBuffer::append<UnsignedEnum>(UnsignedEnum):
✓ Branch 2 → 3 taken 116364 times.
std::enable_if<is_enum_v<simdb::argos::Action>, void>::type simdb::argos::StreamBuffer::append<simdb::argos::Action>(simdb::argos::Action):
✓ Branch 2 → 3 taken 132720 times.
364155 append(static_cast<underlying_t>(val));
76 364155 }
77
78 size_t size() const { return out_->size(); }
79
80 bool operator==(const StreamBuffer& other) const { return *out_ == *other.out_; }
81
82 bool operator==(const std::vector<char>& other) const { return *out_ == other; }
83
84 private:
85 std::vector<char>* out_ = nullptr;
86 TinyStrings<>* tiny_strings_ = nullptr;
87 };
88
89 } // namespace simdb::argos
90