mirror of
https://github.com/dolphin-emu/dolphin.git
synced 2025-01-01 19:11:08 +01:00
ce29c1c42f
It's questionable to not return a reference to the instance being assigned to. It's also quite misleading in terms of expected behavior relative to everything else. This fixes it to make it consistent with other classes.
66 lines
1.3 KiB
C++
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:
|
|
DataReader() = default;
|
|
DataReader(u8* src, u8* end_) : buffer(src), end(end_) {}
|
|
u8* GetPointer() { return buffer; }
|
|
DataReader& operator=(u8* src)
|
|
{
|
|
buffer = src;
|
|
return *this;
|
|
}
|
|
|
|
size_t size() const { return end - buffer; }
|
|
template <typename T, bool swapped = true>
|
|
__forceinline T Peek(int offset = 0) const
|
|
{
|
|
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>
|
|
void Skip(size_t data = 1)
|
|
{
|
|
buffer += sizeof(T) * data;
|
|
}
|
|
|
|
private:
|
|
u8* __restrict buffer = nullptr;
|
|
u8* end = nullptr;
|
|
};
|