aboutsummaryrefslogtreecommitdiff
path: root/zencore/stream.cpp
blob: 361858b6666f31ec4d9cf80f52036e9948fbbaa6 (plain) (blame)
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
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
// Copyright Epic Games, Inc. All Rights Reserved.

#include <stdarg.h>
#include <zencore/memory.h>
#include <zencore/stream.h>
#include <zencore/testing.h>

#include <algorithm>
#include <stdexcept>

namespace zen {

void
BinaryWriter::Write(const void* data, size_t ByteCount, uint64_t Offset)
{
	RwLock::ExclusiveLockScope _(m_Lock);

	const size_t NeedEnd = Offset + ByteCount;

	if (NeedEnd > m_Buffer.size())
	{
		m_Buffer.resize(NeedEnd);
	}

	memcpy(m_Buffer.data() + Offset, data, ByteCount);
}

void
SimpleBinaryWriter::Write(const void* Data, size_t Size)
{
	const size_t NeededSize = m_Offset + Size;

	if (NeededSize > m_Buffer.size())
	{
		const size_t NewCapacity = RoundUp(NeededSize, m_BlockSize);

		m_Buffer.resize(NewCapacity);
	}

	memcpy(m_Buffer.data() + m_Offset, Data, Size);

	m_Offset += Size;
}

void
SimpleBinaryWriter::Clear()
{
	m_Buffer.clear();
	m_Offset = 0;
}

//////////////////////////////////////////////////////////////////////////
//
// Testing related code follows...
//

#if ZEN_WITH_TESTS

void
stream_forcelink()
{
}

#endif

}  // namespace zen