-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathBuffer.hpp
129 lines (126 loc) · 3.86 KB
/
Buffer.hpp
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
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
#pragma once
#include "Errors.hpp"
#include <string>
#include <fstream>
#include <iostream>
#include <inttypes.h>
/** container for data **/
class Buffer {
public: /** constructors, destructor and operator= **/
Buffer() = default;
explicit Buffer(size_t size)
{
if(size == 0) return;
_size = size;
_buffer = new uint8_t[size];
}
explicit Buffer(const Buffer & buf, size_t first, size_t second)
{
if(second - first <= 0) return;
_size = second - first;
_buffer = new uint8_t[_size];
for(size_t idx = first; idx < second; idx++)
_buffer[idx - first] = buf[idx];
}
explicit Buffer(const Buffer & buf)
{
_size = buf._size;
_top = buf._top;
_buffer = new uint8_t[_size];
for(size_t idx = 0; idx < _size; idx++) _buffer[idx] = buf._buffer[idx];
}
explicit Buffer(Buffer && buf)
{
buf.swap(*this);
}
Buffer & operator=(const Buffer & buf)
{
if(this != &buf) Buffer(buf).swap(*this);
return *this;
}
Buffer & operator=(Buffer && buf)
{
if(this != &buf) buf.swap(*this);
return *this;
}
~Buffer()
{
delete [] _buffer;
_buffer = nullptr;
_top = 0;
_size = 0;
}
private:
void swap(Buffer & buf) {
std::swap(buf._top, _top);
std::swap(buf._size, _size);
std::swap(buf._buffer, _buffer);
}
public: /** work with input/output operations **/
void saveInFile(std::string filename) const
{
std::ofstream out(filename, std::ios::out | std::ios::binary);
for(size_t idx = 0; idx < _top; idx++) out << _buffer[idx];
}
void loadFromFile(std::string filename)
{
std::ifstream in(filename, std::ios::in | std::ios::binary);
this->~Buffer();
in.seekg(0, std::ios::end);
_size = in.tellg();
_buffer = new uint8_t[_size];
in.seekg(0, std::ios::beg);
char byte;
while(in.get(byte)) this->push8bit((uint8_t)byte);
}
void printConsole() const
{
std::cout << "Buffer[" << std::dec << _top << "] <";
for(size_t idx = 0; idx < _size; idx++)
{
std::cout << " ";
if(_buffer[idx] < 16) std::cout << "0";
std::cout << std::hex << std::uppercase << +_buffer[idx];
}
std::cout << " >" << std::endl;
}
public: /** adding and deleting methods **/
void push8bit(uint8_t value)
{
if(this->full()) throw NSErr::Error("Overflow buffer", NSErr::ErrEnum::OVERFLOW);
_buffer[_top++] = value;
}
void pop8bit()
{
if(_top == 0) throw NSErr::Error("Empty buffer", NSErr::ErrEnum::EMPTY);
_top--;
}
public: /** access element methods **/
bool empty() const { return (_top == 0); }
bool full() const { return (_top == _size); }
uint8_t & operator[](size_t idx) { return _buffer[idx]; }
uint8_t operator[](size_t idx) const { return _buffer[idx]; }
uint8_t & at(size_t idx)
{
if(idx >= _top) throw NSErr::Error("Range out index", NSErr::ErrEnum::RANGE_OUT);
return _buffer[idx];
}
public: /** getters and setters **/
void * buffer() { return reinterpret_cast<void *>(_buffer); }
size_t top() const { return _top; }
size_t size() const { return _size; }
size_t next()
{
if(_top == _size) throw NSErr::Error("Range out index", NSErr::ErrEnum::RANGE_OUT);
return _top++;
};
void updateTop(size_t newTop = 0)
{
if(newTop > _size) throw NSErr::Error("Range out index", NSErr::ErrEnum::RANGE_OUT);
_top = newTop;
}
private:
uint8_t * _buffer = nullptr;
size_t _top = 0;
size_t _size = 0;
};