2021-05-14 07:52:56 +03:00
|
|
|
/*
|
|
|
|
* Copyright (c) 2021, Ali Mohammad Pur <mpfard@serenityos.org>
|
|
|
|
*
|
|
|
|
* SPDX-License-Identifier: BSD-2-Clause
|
|
|
|
*/
|
|
|
|
|
|
|
|
#pragma once
|
|
|
|
|
2021-07-13 18:17:12 +03:00
|
|
|
#include <AK/StdLibExtraDetails.h>
|
2021-05-14 07:52:56 +03:00
|
|
|
#include <AK/Types.h>
|
|
|
|
|
|
|
|
namespace AK {
|
|
|
|
|
|
|
|
struct ByteReader {
|
2021-07-13 18:17:12 +03:00
|
|
|
template<typename T>
|
|
|
|
requires(IsTriviallyCopyable<T>) static void store(u8* addr, T value)
|
2021-05-14 07:52:56 +03:00
|
|
|
{
|
2021-07-13 18:17:12 +03:00
|
|
|
__builtin_memcpy(addr, &value, sizeof(T));
|
2021-05-14 07:52:56 +03:00
|
|
|
}
|
2021-07-13 18:17:12 +03:00
|
|
|
template<typename T>
|
2022-04-01 20:58:27 +03:00
|
|
|
requires(IsTriviallyConstructible<T>) static void load(u8 const* addr, T& value)
|
2021-07-10 23:22:37 +03:00
|
|
|
{
|
2021-07-13 18:17:12 +03:00
|
|
|
__builtin_memcpy(&value, addr, sizeof(T));
|
2021-07-10 23:22:37 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
template<typename T>
|
2022-04-01 20:58:27 +03:00
|
|
|
static T* load_pointer(u8 const* address)
|
2021-07-10 23:22:37 +03:00
|
|
|
{
|
2021-07-13 18:17:12 +03:00
|
|
|
FlatPtr value;
|
|
|
|
load<FlatPtr>(address, value);
|
|
|
|
return reinterpret_cast<T*>(value);
|
2021-07-10 23:22:37 +03:00
|
|
|
}
|
|
|
|
|
2022-04-01 20:58:27 +03:00
|
|
|
static u16 load16(u8 const* address)
|
2021-05-14 07:52:56 +03:00
|
|
|
{
|
|
|
|
u16 value;
|
|
|
|
load(address, value);
|
|
|
|
return value;
|
|
|
|
}
|
|
|
|
|
2022-04-01 20:58:27 +03:00
|
|
|
static u32 load32(u8 const* address)
|
2021-05-14 07:52:56 +03:00
|
|
|
{
|
|
|
|
u32 value;
|
|
|
|
load(address, value);
|
|
|
|
return value;
|
|
|
|
}
|
2021-07-10 23:22:37 +03:00
|
|
|
|
2022-04-01 20:58:27 +03:00
|
|
|
static u64 load64(u8 const* address)
|
2021-07-10 23:22:37 +03:00
|
|
|
{
|
|
|
|
u64 value;
|
|
|
|
load(address, value);
|
|
|
|
return value;
|
|
|
|
}
|
2021-05-14 07:52:56 +03:00
|
|
|
};
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2022-11-26 14:18:30 +03:00
|
|
|
#if USING_AK_GLOBALLY
|
2021-05-14 07:52:56 +03:00
|
|
|
using AK::ByteReader;
|
2022-11-26 14:18:30 +03:00
|
|
|
#endif
|