dolphin/Source/Core/VideoCommon/DataReader.h

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

67 lines
1.4 KiB
C
Raw Normal View History

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