dolphin/Source/Core/VideoCommon/DataReader.h

Ignoring revisions in .git-blame-ignore-revs. Click here to bypass and see the normal blame view.

66 lines
1.3 KiB
C
Raw Normal View History

// Copyright 2008 Dolphin Emulator Project
// SPDX-License-Identifier: GPL-2.0-or-later
#pragma once
2015-08-27 19:43:04 +02:00
#include <cstring>
2015-08-27 19:43:04 +02:00
#include "Common/CommonTypes.h"
#include "Common/Inline.h"
#include "Common/Swap.h"
2014-11-24 22:22:42 +01:00
class DataReader
{
public:
DataReader() = default;
DataReader(u8* src, u8* end_) : buffer(src), end(end_) {}
u8* GetPointer() { return buffer; }
const u8* GetPointer() const { return buffer; }
DataReader& operator=(u8* src)
{
2014-11-24 22:22:42 +01:00
buffer = src;
return *this;
}
2014-11-24 22:22:42 +01:00
size_t size() const { return end - buffer; }
2014-11-24 22:22:42 +01:00
template <typename T, bool swapped = true>
DOLPHIN_FORCE_INLINE T Peek(int offset = 0) const
{
2014-11-24 22:22:42 +01:00
T data;
std::memcpy(&data, &buffer[offset], sizeof(T));
if constexpr (swapped)
data = Common::FromBigEndian(data);
2014-11-24 22:22:42 +01:00
return data;
}
template <typename T, bool swapped = true>
DOLPHIN_FORCE_INLINE T Read()
2014-11-24 22:22:42 +01:00
{
const T result = Peek<T, swapped>();
buffer += sizeof(T);
return result;
}
template <typename T, bool swapped = false>
DOLPHIN_FORCE_INLINE void Write(T data)
2014-11-24 22:22:42 +01:00
{
if constexpr (swapped)
2015-08-27 19:43:04 +02:00
data = Common::FromBigEndian(data);
2014-11-24 22:22:42 +01:00
std::memcpy(buffer, &data, sizeof(T));
buffer += sizeof(T);
}
2015-08-27 19:43:04 +02:00
2014-11-24 22:22:42 +01:00
template <typename T = u8>
void Skip(size_t data = 1)
2014-11-24 22:22:42 +01:00
{
buffer += sizeof(T) * data;
}
private:
u8* __restrict buffer = nullptr;
u8* end = nullptr;
2014-11-24 22:22:42 +01:00
};