micromenu/inc/gfx/Buffer.h
2021-02-24 20:37:27 +01:00

54 lines
No EOL
1.3 KiB
C++

//
// Created by jedi on 11/2/18.
//
#include <cstdlib>
#include <cstdint>
namespace micromenu {
enum bufferBlocks {
_1bit = 1, _2bit = 2, _4bit = 4, _8bit = 8, _16bit = 16, _24bit = 24, _32bit = 32
};
template<bufferBlocks N>
class Buffer {
private:
int *ptr_;
int size_;
constexpr static const int block_ = 8 * sizeof(int);
public:
Buffer(int size) : size_(size) {
ptr_ = (int *) malloc(size * N / 8);
}
int get(int i) {
if(i >= size_) return 0;
return ptr_[i * N / block_] & ((1 << N) - 1) << (i * N % block_);
}
int getBlock(int i) {
if(i >= size_) return 0;
return ptr_[i * N / block_];
}
void set(int i, int v) {
if(i < size_) {
ptr_[i * N / block_] &= ~(((1 << N) - 1) << (i * N % block_));
ptr_[i * N / block_] |= (((1 << N) - 1) & v) << (i * N % block_);
}
}
~Buffer() {
free(ptr_);
}
};
template<>
int Buffer<_32bit>::get(int i);
template<>
void Buffer<_32bit>::set(int i, int v);
}