blob: abbade998a902397340cd591d564632039c12c65 (
plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
|
/*
* Copyright (c) 2021, Sahan Fernando <sahan.h.fernando@gmail.com>
*
* SPDX-License-Identifier: BSD-2-Clause
*/
#pragma once
#include <AK/Span.h>
#include <AK/StdLibExtraDetails.h>
namespace AK {
class BinaryBufferWriter {
public:
BinaryBufferWriter(Bytes target)
: m_target(target)
{
}
template<typename T>
requires(IsTriviallyConstructible<T>) T& append_structure()
{
VERIFY((reinterpret_cast<FlatPtr>(m_target.data()) + m_offset) % alignof(T) == 0);
VERIFY(m_offset + sizeof(T) <= m_target.size());
T* allocated = new (m_target.data() + m_offset) T;
m_offset += sizeof(T);
return *allocated;
}
void skip_bytes(size_t num_bytes)
{
VERIFY(m_offset + num_bytes <= m_target.size());
m_offset += num_bytes;
}
[[nodiscard]] size_t current_offset() const
{
return m_offset;
}
private:
Bytes m_target;
size_t m_offset { 0 };
};
}
|