dolphin/Source/Core/VideoCommon/DataReader.h

66 lines
1.3 KiB
C
Raw Normal View History

// Copyright 2008 Dolphin Emulator Project
2015-05-17 23:08:10 +00:00
// Licensed under GPLv2+
// Refer to the license.txt file included.
#pragma once
2015-08-27 17:43:04 +00:00
#include <cstring>
#include "Common/Common.h"
2015-08-27 17:43:04 +00:00
#include "Common/CommonTypes.h"
#include "Common/Swap.h"
2014-11-24 21:22:42 +00:00
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;
}
2014-11-24 21:22:42 +00:00
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));
2014-11-24 21:22:42 +00:00
if (swapped)
data = Common::FromBigEndian(data);
2014-11-24 21:22:42 +00:00
return data;
}
2014-11-24 21:22:42 +00:00
template <typename T, bool swapped = true>
__forceinline T Read()
{
const T result = Peek<T, swapped>();
buffer += sizeof(T);
return result;
}
2014-11-24 21:22:42 +00:00
template <typename T, bool swapped = false>
__forceinline void Write(T data)
{
if (swapped)
data = Common::FromBigEndian(data);
2015-08-27 17:43:04 +00:00
std::memcpy(buffer, &data, sizeof(T));
buffer += sizeof(T);
}
2015-08-27 17:43:04 +00:00
template <typename T = u8>
void Skip(size_t data = 1)
{
buffer += sizeof(T) * data;
}
2014-11-24 21:22:42 +00:00
private:
u8* __restrict buffer = nullptr;
u8* end = nullptr;
2014-11-24 21:22:42 +00:00
};