dolphin/Source/Core/VideoCommon/DataReader.h
Lioncash 552c0d8404 Common: Move byte swapping utilities into their own header
This moves all the byte swapping utilities into a header named Swap.h.

A dedicated header is much more preferable here due to the size of the
code itself. In general usage throughout the codebase, CommonFuncs.h was
generally only included for these functions anyway. These being in their
own header avoids dumping the lesser used utilities into scope. As well
as providing a localized area for more utilities related to byte
swapping in the future (should they be needed). This also makes it nicer
to identify which files depend on the byte swapping utilities in
particular.

Since this is a completely new header, moving the code uncovered a few
indirect includes, as well as making some other inclusions unnecessary.
2017-03-03 17:18:18 -05:00

66 lines
1.3 KiB
C++

// Copyright 2008 Dolphin Emulator Project
// Licensed under GPLv2+
// Refer to the license.txt file included.
#pragma once
#include <cstring>
#include "Common/Common.h"
#include "Common/CommonTypes.h"
#include "Common/Swap.h"
class DataReader
{
public:
__forceinline DataReader() : buffer(nullptr), end(nullptr) {}
__forceinline DataReader(u8* src, u8* _end) : buffer(src), end(_end) {}
__forceinline u8* GetPointer() { return buffer; }
__forceinline u8* operator=(u8* src)
{
buffer = src;
return src;
}
__forceinline size_t size() { return end - buffer; }
template <typename T, bool swapped = true>
__forceinline T Peek(int offset = 0)
{
T data;
std::memcpy(&data, &buffer[offset], sizeof(T));
if (swapped)
data = Common::FromBigEndian(data);
return data;
}
template <typename T, bool swapped = true>
__forceinline T Read()
{
const T result = Peek<T, swapped>();
buffer += sizeof(T);
return result;
}
template <typename T, bool swapped = false>
__forceinline void Write(T data)
{
if (swapped)
data = Common::FromBigEndian(data);
std::memcpy(buffer, &data, sizeof(T));
buffer += sizeof(T);
}
template <typename T = u8>
__forceinline void Skip(size_t data = 1)
{
buffer += sizeof(T) * data;
}
private:
u8* __restrict buffer;
u8* end;
};