mirror of
https://github.com/Kbz-8/42_vox.git
synced 2026-01-11 14:43:34 +00:00
62 lines
1.4 KiB
C++
62 lines
1.4 KiB
C++
#ifndef __SCOP_CPU_BUFFER__
|
|
#define __SCOP_CPU_BUFFER__
|
|
|
|
#include <cstdint>
|
|
#include <cstring>
|
|
#include <memory>
|
|
#include <Core/Logs.h>
|
|
|
|
namespace Scop
|
|
{
|
|
class CPUBuffer
|
|
{
|
|
public:
|
|
CPUBuffer() {}
|
|
CPUBuffer(std::size_t size) try : m_data(new std::uint8_t[size]), m_size(size)
|
|
{}
|
|
catch(...)
|
|
{
|
|
FatalError("memory allocation for a CPU buffer failed");
|
|
}
|
|
|
|
[[nodiscard]] inline CPUBuffer Duplicate() const
|
|
{
|
|
CPUBuffer buffer(m_size);
|
|
std::memcpy(buffer.GetData(), m_data.get(), m_size);
|
|
return buffer;
|
|
}
|
|
|
|
inline void Allocate(std::size_t size)
|
|
{
|
|
if(m_data != nullptr)
|
|
FatalError("cannot allocate an already allocated CPU buffer");
|
|
try
|
|
{
|
|
m_data = std::make_shared<std::uint8_t[]>(size);
|
|
m_size = size;
|
|
}
|
|
catch(...)
|
|
{
|
|
FatalError("memory allocation for a CPU buffer failed");
|
|
}
|
|
}
|
|
|
|
inline bool Empty() const { return m_size == 0; }
|
|
|
|
[[nodiscard]] inline std::size_t GetSize() const noexcept { return m_size; }
|
|
|
|
template<typename T>
|
|
[[nodiscard]] inline T* GetDataAs() const { return reinterpret_cast<T*>(m_data.get()); }
|
|
[[nodiscard]] inline std::uint8_t* GetData() const { return m_data.get(); }
|
|
inline operator bool() const { return (bool)m_data; }
|
|
|
|
~CPUBuffer() = default;
|
|
|
|
private:
|
|
std::shared_ptr<std::uint8_t[]> m_data;
|
|
std::size_t m_size = 0;
|
|
};
|
|
}
|
|
|
|
#endif
|