2015-05-24 04:55:12 +00:00
|
|
|
// Copyright 2008 Dolphin Emulator Project
|
2021-07-05 01:22:19 +00:00
|
|
|
// SPDX-License-Identifier: GPL-2.0-or-later
|
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
|
|
|
|
2015-08-27 17:43:04 +00:00
|
|
|
#include "Common/CommonTypes.h"
|
2020-05-03 17:42:12 +00:00
|
|
|
#include "Common/Inline.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:
|
2018-03-18 20:37:07 +00:00
|
|
|
DataReader() = default;
|
|
|
|
DataReader(u8* src, u8* end_) : buffer(src), end(end_) {}
|
|
|
|
u8* GetPointer() { return buffer; }
|
2018-03-18 20:43:27 +00:00
|
|
|
const u8* GetPointer() const { return buffer; }
|
2018-03-18 20:38:05 +00:00
|
|
|
DataReader& operator=(u8* src)
|
2016-06-24 08:43:46 +00:00
|
|
|
{
|
2014-11-24 21:22:42 +00:00
|
|
|
buffer = src;
|
2018-03-18 20:38:05 +00:00
|
|
|
return *this;
|
2016-06-24 08:43:46 +00:00
|
|
|
}
|
2014-11-24 21:22:42 +00:00
|
|
|
|
2018-03-18 20:37:07 +00:00
|
|
|
size_t size() const { return end - buffer; }
|
2014-11-24 21:22:42 +00:00
|
|
|
template <typename T, bool swapped = true>
|
2018-06-09 15:47:01 +00:00
|
|
|
DOLPHIN_FORCE_INLINE T Peek(int offset = 0) const
|
2016-06-24 08:43:46 +00:00
|
|
|
{
|
2014-11-24 21:22:42 +00:00
|
|
|
T data;
|
|
|
|
std::memcpy(&data, &buffer[offset], sizeof(T));
|
|
|
|
|
2020-10-20 12:42:30 +00:00
|
|
|
if constexpr (swapped)
|
2015-01-06 16:49:27 +00:00
|
|
|
data = Common::FromBigEndian(data);
|
2014-11-24 21:22:42 +00:00
|
|
|
|
|
|
|
return data;
|
|
|
|
}
|
|
|
|
|
|
|
|
template <typename T, bool swapped = true>
|
2018-06-09 15:47:01 +00:00
|
|
|
DOLPHIN_FORCE_INLINE T Read()
|
2014-11-24 21:22:42 +00:00
|
|
|
{
|
|
|
|
const T result = Peek<T, swapped>();
|
|
|
|
buffer += sizeof(T);
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
template <typename T, bool swapped = false>
|
2018-06-09 15:47:01 +00:00
|
|
|
DOLPHIN_FORCE_INLINE void Write(T data)
|
2014-11-24 21:22:42 +00:00
|
|
|
{
|
2020-10-20 12:42:30 +00:00
|
|
|
if constexpr (swapped)
|
2015-08-27 17:43:04 +00:00
|
|
|
data = Common::FromBigEndian(data);
|
|
|
|
|
2014-11-24 21:22:42 +00:00
|
|
|
std::memcpy(buffer, &data, sizeof(T));
|
|
|
|
buffer += sizeof(T);
|
2016-06-24 08:43:46 +00:00
|
|
|
}
|
2015-08-27 17:43:04 +00:00
|
|
|
|
2014-11-24 21:22:42 +00:00
|
|
|
template <typename T = u8>
|
2018-03-18 20:37:07 +00:00
|
|
|
void Skip(size_t data = 1)
|
2014-11-24 21:22:42 +00:00
|
|
|
{
|
|
|
|
buffer += sizeof(T) * data;
|
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
2018-03-18 20:34:48 +00:00
|
|
|
u8* __restrict buffer = nullptr;
|
|
|
|
u8* end = nullptr;
|
2014-11-24 21:22:42 +00:00
|
|
|
};
|