1eea7ceb3SPulkoMandy/*
2eea7ceb3SPulkoMandy * Copyright 2019, Adrien Destugues, pulkomandy@pulkomandy.tk.
3eea7ceb3SPulkoMandy * Distributed under the terms of the MIT License.
4eea7ceb3SPulkoMandy */
5eea7ceb3SPulkoMandy#ifndef _BYTE_POINTER_H
6eea7ceb3SPulkoMandy#define _BYTE_POINTER_H
7eea7ceb3SPulkoMandy
8eea7ceb3SPulkoMandy
9eea7ceb3SPulkoMandy#include <stdlib.h>
10eea7ceb3SPulkoMandy
11eea7ceb3SPulkoMandy
12eea7ceb3SPulkoMandy// Behaves like a char* pointer, but -> and & return the right pointed type.
13eea7ceb3SPulkoMandy// Assumes the offsets passed to + and += maintain the alignment for the type.
14eea7ceb3SPulkoMandytemplate<class T> struct BytePointer {
15eea7ceb3SPulkoMandy	char* address;
16eea7ceb3SPulkoMandy
17eea7ceb3SPulkoMandy	BytePointer(void* base) { address = (char*)base; }
18eea7ceb3SPulkoMandy
19eea7ceb3SPulkoMandy	T* operator&() { return reinterpret_cast<T*>(address); }
20eea7ceb3SPulkoMandy	T* operator->() { return reinterpret_cast<T*>(address); }
21eea7ceb3SPulkoMandy	void operator+=(size_t offset) { address += offset; }
22eea7ceb3SPulkoMandy	char* operator+(size_t offset) const { return address + offset; }
23eea7ceb3SPulkoMandy};
24eea7ceb3SPulkoMandy
25eea7ceb3SPulkoMandy
26eea7ceb3SPulkoMandy#endif
27