2015-05-24 04:55:12 +00:00
|
|
|
// Copyright 2008 Dolphin Emulator Project
|
2015-05-17 23:08:10 +00:00
|
|
|
// Licensed under GPLv2+
|
2013-04-18 03:09:55 +00:00
|
|
|
// Refer to the license.txt file included.
|
2011-01-10 13:14:56 +00:00
|
|
|
|
2014-02-10 18:54:46 +00:00
|
|
|
#pragma once
|
2008-12-08 04:46:09 +00:00
|
|
|
|
2015-08-27 17:43:04 +00:00
|
|
|
#include <cstring>
|
2017-03-03 19:43:52 +00:00
|
|
|
|
2016-01-17 21:54:31 +00:00
|
|
|
#include "Common/Common.h"
|
2015-08-27 17:43:04 +00:00
|
|
|
#include "Common/CommonTypes.h"
|
2017-03-03 19:43:52 +00:00
|
|
|
#include "Common/Swap.h"
|
2008-12-08 04:46:09 +00:00
|
|
|
|
2014-11-24 21:22:42 +00:00
|
|
|
class DataReader
|
|
|
|
{
|
|
|
|
public:
|
2016-06-24 08:43:46 +00:00
|
|
|
__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;
|
|
|
|
}
|
2014-11-24 21:22:42 +00:00
|
|
|
|
2016-06-24 08:43:46 +00:00
|
|
|
__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));
|
2014-11-24 21:22:42 +00:00
|
|
|
|
2016-06-24 08:43:46 +00:00
|
|
|
if (swapped)
|
|
|
|
data = Common::FromBigEndian(data);
|
2014-11-24 21:22:42 +00:00
|
|
|
|
2016-06-24 08:43:46 +00:00
|
|
|
return data;
|
|
|
|
}
|
2014-11-24 21:22:42 +00:00
|
|
|
|
2016-06-24 08:43:46 +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
|
|
|
|
2016-06-24 08:43:46 +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
|
|
|
|
2016-06-24 08:43:46 +00:00
|
|
|
std::memcpy(buffer, &data, sizeof(T));
|
|
|
|
buffer += sizeof(T);
|
|
|
|
}
|
2015-08-27 17:43:04 +00:00
|
|
|
|
2016-06-24 08:43:46 +00:00
|
|
|
template <typename T = u8>
|
|
|
|
__forceinline void Skip(size_t data = 1)
|
|
|
|
{
|
|
|
|
buffer += sizeof(T) * data;
|
|
|
|
}
|
2014-11-24 21:22:42 +00:00
|
|
|
|
|
|
|
private:
|
2016-06-24 08:43:46 +00:00
|
|
|
u8* __restrict buffer;
|
|
|
|
u8* end;
|
2014-11-24 21:22:42 +00:00
|
|
|
};
|