• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

Open-Sn / opensn / 20185909776

12 Dec 2025 06:55PM UTC coverage: 74.333% (+0.3%) from 74.037%
20185909776

push

github

web-flow
Merge pull request #859 from wdhawkins/td_source_driver

Adding time-dependent solver and time-dependent sources

367 of 398 new or added lines in 23 files covered. (92.21%)

113 existing lines in 28 files now uncovered.

18610 of 25036 relevant lines covered (74.33%)

68947552.69 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

83.67
/framework/data_types/byte_array.h
1
// SPDX-FileCopyrightText: 2024 The OpenSn Authors <https://open-sn.github.io/opensn/>
2
// SPDX-License-Identifier: MIT
3

4
#pragma once
5

6
#include <vector>
7
#include <stdexcept>
8
#include <string>
9
#include <cstring>
10
#include <cstddef>
11

12
namespace opensn
13
{
14

15
class ByteArray
3,336,722✔
16
{
17
public:
18
  ByteArray() = default;
19
  explicit ByteArray(const size_t raw_data_size) : raw_data_(raw_data_size) {}
20
  explicit ByteArray(std::vector<std::byte>&& raw_data) : raw_data_(std::move(raw_data)) {}
21
  explicit ByteArray(const std::vector<std::byte>& raw_data) : raw_data_(raw_data) {}
22

23
  /**
24
   * Uses the template type T to convert an associated value (of type T)
25
   * to a sub-array of std::bytes and adds it to the internal byte-array.
26
   *
27
   * The template type T must support sizeof.
28
   */
29
  template <typename T>
30
  void Write(const T& value)
67,629,707✔
31
  {
32
    const size_t num_bytes = sizeof(T);
67,629,707✔
33
    std::vector<std::byte> dest(num_bytes);
67,629,707✔
34
    std::memcpy(dest.data(), &value, num_bytes);
67,629,707✔
35
    raw_data_.insert(raw_data_.end(), dest.data(), dest.data() + num_bytes);
67,629,707✔
36
  }
67,629,707✔
37

38
  /**
39
   * Uses the template type `T` to convert `sizeof(T)` number of bytes to
40
   * a value of type `T`. The bytes are pulled from the internal byte-array
41
   * starting at the internal address `m_offset` which can be viewed with a
42
   * call to `Offset()`. This offset is incremented by the amount of bytes
43
   * used and can be reset with a call to `Seek(0)`.
44
   *
45
   * Bounds-checking is performed by checking if the internal byte array
46
   * has the required number of bytes available. If this check fails then
47
   * this call will return an `out_of_range` exception.
48
   */
49
  template <typename T>
50
  T Read()
51
  {
52
    const size_t num_bytes = sizeof(T);
53
    if ((offset_ + num_bytes - 1) >= raw_data_.size())
54
      throw std::out_of_range(
55
        std::string("ByteArray reading error. ") + " Typename: " + std::string(typeid(T).name()) +
56
        " m_offset: " + std::to_string(offset_) + " size: " + std::to_string(raw_data_.size()) +
57
        " num_bytes to read: " + std::to_string(num_bytes));
58

59
    T value;
60
    std::memcpy(&value, &raw_data_[offset_], num_bytes);
61
    offset_ += num_bytes;
62

63
    return value;
64
  }
65

66
  /**
67
   * Uses the template type T to convert sizeof(T) number of bytes to
68
   * a value of type T. The bytes are pulled from the internal byte-array
69
   * starting at the internal address specified by the argument "address".
70
   * An optional argument next_address can be used to return the location
71
   * of the value after the value read.
72
   * This offset is the given "address" argument incremented by the amount
73
   * of bytes used.
74
   *
75
   * Bounds-checking is performed by checking if the internal byte array
76
   * has the required number of bytes available. If this check fails then
77
   * this call will return an `out_of_range` exception.
78
   */
79
  template <typename T>
80
  T Read(const size_t address, size_t* next_address = nullptr) const
194✔
81
  {
82
    const size_t num_bytes = sizeof(T);
194✔
83
    if ((address + num_bytes - 1) >= raw_data_.size())
194✔
UNCOV
84
      throw std::logic_error(
×
UNCOV
85
        std::string("ByteArray reading error. ") + " Typename: " + std::string(typeid(T).name()) +
×
UNCOV
86
        " address: " + std::to_string(address) + " size: " + std::to_string(raw_data_.size()) +
×
UNCOV
87
        " num_bytes to read: " + std::to_string(num_bytes));
×
88

89
    T value;
90
    std::memcpy(&value, &raw_data_[address], num_bytes);
194✔
91
    if (next_address != nullptr)
194✔
92
      *next_address = address + num_bytes;
194✔
93

94
    return value;
194✔
95
  }
96

97
  /// Appends a `ByteArray` to the current internal byte array.
98
  void Append(const ByteArray& other_raw)
13✔
99
  {
100
    auto& master = raw_data_;
13✔
101

102
    const auto& slave = other_raw.Data();
13✔
103
    master.insert(master.end(), slave.begin(), slave.end());
26✔
104
  }
13✔
105

106
  /// Appends bytes from a `std::vector<std::byte>` to the internal byte array.
107
  void Append(const std::vector<std::byte>& other_raw)
108
  {
109
    auto& master = raw_data_;
110

111
    const auto& slave = other_raw;
112
    master.insert(master.end(), slave.begin(), slave.end());
113
  }
114

115
  /// Clears the internal byte array and resets the address offset to zero.
116
  void Clear()
117
  {
118
    raw_data_.clear();
119
    offset_ = 0;
120
  }
121

122
  /// Moves the address marker to the supplied address.
123
  void Seek(const size_t address = 0) { offset_ = address; }
124

125
  /// Returns the internal address marker position.
126
  size_t Offset() const { return offset_; }
127

128
  /// Determines if the internal address marker is beyond the internal byte array.
129
  bool EndOfBuffer() const { return offset_ >= raw_data_.size(); }
130

131
  /// Returns the current size of the internal byte array.
132
  size_t Size() const { return raw_data_.size(); }
133

134
  /// Returns a reference to the internal byte array.
135
  std::vector<std::byte>& Data() { return raw_data_; }
136

137
  /// Returns a const reference of the internal byte array.
138
  const std::vector<std::byte>& Data() const { return raw_data_; }
1,144✔
139

1,107,345✔
140
protected:
1,107,351✔
141
  std::vector<std::byte> raw_data_;
66,427,772✔
142
  size_t offset_ = 0;
66,427,772✔
143
};
66,427,772✔
UNCOV
144

×
UNCOV
145
} // namespace opensn
×
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc