-
Notifications
You must be signed in to change notification settings - Fork 2
/
flexiblecache.h
73 lines (57 loc) · 1.91 KB
/
flexiblecache.h
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
#pragma once
#include <memory>
#include <string_view>
namespace bpatch
{
/// <summary>
/// this constant will be used to read data in chunks of such size
/// </summary>
constexpr static const std::size_t SZBUFF_FC = 1024 * 1024;
/// <summary>
/// accumulating data in dynamic memory in chunks.
/// </summary>
class FlexibleCache
{
public:
/// <summary>
/// accumulating data in such chunks
/// </summary>
struct Chunk
{
std::unique_ptr<Chunk> next;
char data[bpatch::SZBUFF_FC];
size_t accumulated = 0;
};
public:
FlexibleCache();
/// <summary>
/// accumulate data inside
/// </summary>
/// <param name="data"> data to accumulate</param>
/// <returns>true if root chunk completely filled with data</returns>
bool Accumulate(const std::string_view adata);
/// <summary>
/// accumulate character inside
/// </summary>
/// <param name="toProcess"> character to accumulate</param>
/// <returns>true if root chunk completely filled with data</returns>
bool Accumulate(const char toProcess);
/// <summary>
/// Translates ownership of the root chunk to the requestor
/// </summary>
/// <param name="achunk"> root chunk will be returned here</param>
/// <returns>true in case if the data in the data chain remains</returns>
bool Next(std::unique_ptr<Chunk>& achunk);
/// <summary>
/// returns true if the very first chunk is full with data
/// </summary>
/// <returns>true if the very first chunk is full with data</returns>
bool RootChunkFull()const
{
return rootChunk->accumulated == bpatch::SZBUFF_FC;
}
protected:
std::unique_ptr<Chunk> rootChunk; // very first chunk
std::unique_ptr<Chunk>* currentChunk; // chunk where current accumulate process happens
};
};// namespace bpatch