jak-project/common/util/BinaryReader.h
water111 de5aa7e5e4
Move duplicated utilities to the common util folder and remove NEXT_DIR (#29)
* move things to the common library and remove next_dir

* fix for windows

* one last windows fix

* last fix for real this time

* debug listener test

* fix listener threading bug
2020-09-10 20:03:31 -04:00

41 lines
829 B
C++

#ifndef JAK_V2_BINARYREADER_H
#define JAK_V2_BINARYREADER_H
#include <cstdint>
#include <cassert>
#include <vector>
class BinaryReader {
public:
BinaryReader(uint8_t* _buffer, uint32_t _size) : buffer(_buffer), size(_size) {}
explicit BinaryReader(std::vector<uint8_t>& _buffer)
: buffer((uint8_t*)_buffer.data()), size(_buffer.size()) {}
template <typename T>
T read() {
assert(seek + sizeof(T) <= size);
T& obj = *(T*)(buffer + seek);
seek += sizeof(T);
return obj;
}
void ffwd(int amount) {
seek += amount;
assert(seek <= size);
}
uint32_t bytes_left() const { return size - seek; }
uint8_t* here() { return buffer + seek; }
uint32_t get_seek() { return seek; }
private:
uint8_t* buffer;
uint32_t size;
uint32_t seek = 0;
};
#endif // JAK_V2_BINARYREADER_H