aboutsummaryrefslogtreecommitdiffstats
path: root/library/cpp/actors/core/buffer.cpp
blob: cb8aaeeaddf9e09fb661febcb5ccacab15a1886f (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
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
#include "buffer.h"

#include <util/system/yassert.h>

#include <algorithm> 

TBufferBase::TBufferBase(size_t size) noexcept
    : Size(size)
{
}

size_t
TBufferBase::GetSize() const noexcept {
    return Size;
}

void TBufferBase::SetSize(size_t size) noexcept {
    Size = size;
}

/////////////////////////////////////////////////////////////////////

template <typename PointerType>
TBufferBaseT<PointerType>::TBufferBaseT(PointerType data, size_t size) noexcept
    : TBufferBase(size)
    , Data(data)
{
}

template <typename PointerType>
PointerType
TBufferBaseT<PointerType>::GetPointer() const noexcept {
    return Data;
}

template <typename PointerType>
void TBufferBaseT<PointerType>::Assign(PointerType data, size_t size) noexcept {
    Data = data;
    Size = size;
}

template <>
void TBufferBaseT<void*>::Cut(size_t offset) noexcept {
    Y_VERIFY_DEBUG(offset <= Size);
    Data = static_cast<char*>(Data) + offset;
    TBufferBase::Size -= offset;
}

template <>
void TBufferBaseT<const void*>::Cut(size_t offset) noexcept {
    Y_VERIFY_DEBUG(offset <= Size);
    Data = static_cast<const char*>(Data) + offset;
    TBufferBase::Size -= offset;
}

template class TBufferBaseT<void*>;
template class TBufferBaseT<const void*>;

/////////////////////////////////////////////////////////////////////

TConstBuffer::TConstBuffer(const void* data, size_t size) noexcept
    : TBufferBaseT<const void*>(data, size)
{
}

TConstBuffer::TConstBuffer(const TMutableBuffer& buffer) noexcept
    : TBufferBaseT<const void*>(buffer.GetPointer(), buffer.GetSize())
{
}

TConstBuffer
TConstBuffer::Offset(ptrdiff_t offset, size_t size) const noexcept {
    return TConstBuffer(static_cast<const char*>(Data) + offset, std::min(Size - offset, size));
}

////////////////////////////////////////////////////////////////////////////////

TMutableBuffer::TMutableBuffer(void* data, size_t size) noexcept
    : TBufferBaseT<void*>(data, size)
{
}

TMutableBuffer
TMutableBuffer::Offset(ptrdiff_t offset, size_t size) const noexcept {
    return TMutableBuffer(static_cast<char*>(Data) + offset, std::min(Size - offset, size));
}

size_t
TMutableBuffer::CopyFrom(const TConstBuffer& buffer) const noexcept {
    const auto size = std::min(Size, buffer.Size);
    std::memcpy(Data, buffer.Data, size);
    return size;
}