yes
This commit is contained in:
44
Runtime/Includes/Utils/Ansi.h
git.filemode.normal_file
44
Runtime/Includes/Utils/Ansi.h
git.filemode.normal_file
@@ -0,0 +1,44 @@
|
||||
#ifndef __SCOPE_UTILS_ANSI__
|
||||
#define __SCOPE_UTILS_ANSI__
|
||||
|
||||
#include <cstdint>
|
||||
#include <ostream>
|
||||
|
||||
namespace Scop
|
||||
{
|
||||
enum class Ansi : std::uint32_t
|
||||
{
|
||||
red = 31,
|
||||
green = 32,
|
||||
blue = 34,
|
||||
def = 0,
|
||||
black = 30,
|
||||
yellow = 33,
|
||||
magenta = 35,
|
||||
cyan = 36,
|
||||
white = 37,
|
||||
bg_red = 41,
|
||||
bg_green = 42,
|
||||
bg_blue = 44,
|
||||
bg_def = 0,
|
||||
bg_black = 40,
|
||||
bg_yellow = 43,
|
||||
bg_magenta = 45,
|
||||
bg_cyan = 46,
|
||||
bg_white = 47,
|
||||
reset = 0,
|
||||
bold = 1,
|
||||
underline = 4,
|
||||
inverse = 7,
|
||||
bold_off = 21,
|
||||
underline_off = 24,
|
||||
inverse_off = 27
|
||||
};
|
||||
}
|
||||
|
||||
inline std::ostream& operator<<(std::ostream& os, Scop::Ansi ansi)
|
||||
{
|
||||
return os << "\033[1;" << std::to_string(static_cast<std::uint32_t>(ansi)) << "m";
|
||||
}
|
||||
|
||||
#endif
|
||||
101
Runtime/Includes/Utils/Buffer.h
git.filemode.normal_file
101
Runtime/Includes/Utils/Buffer.h
git.filemode.normal_file
@@ -0,0 +1,101 @@
|
||||
#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(size > 0 ? std::make_unique<std::uint8_t[]>(size) : nullptr), m_size(size)
|
||||
{}
|
||||
catch(...)
|
||||
{
|
||||
FatalError("memory allocation for a CPU buffer failed");
|
||||
}
|
||||
|
||||
CPUBuffer(const CPUBuffer& other) try : m_data(other.m_size > 0 ? std::make_unique<std::uint8_t[]>(other.m_size) : nullptr), m_size(other.m_size)
|
||||
{
|
||||
if(m_data)
|
||||
std::memcpy(m_data.get(), other.m_data.get(), m_size);
|
||||
}
|
||||
catch (...)
|
||||
{
|
||||
FatalError("memory allocation for a CPU buffer failed");
|
||||
}
|
||||
|
||||
CPUBuffer& operator=(const CPUBuffer& other)
|
||||
{
|
||||
if(this != &other)
|
||||
{
|
||||
if(other.m_data)
|
||||
{
|
||||
std::unique_ptr<std::uint8_t[]> new_data;
|
||||
try
|
||||
{
|
||||
new_data = std::make_unique<std::uint8_t[]>(other.m_size);
|
||||
}
|
||||
catch (...)
|
||||
{
|
||||
FatalError("memory allocation for a CPU buffer failed");
|
||||
}
|
||||
std::memcpy(new_data.get(), other.m_data.get(), other.m_size);
|
||||
|
||||
m_data = std::move(new_data);
|
||||
m_size = other.m_size;
|
||||
}
|
||||
else
|
||||
{
|
||||
m_data.reset();
|
||||
m_size = 0;
|
||||
}
|
||||
}
|
||||
return *this;
|
||||
}
|
||||
|
||||
[[nodiscard]] inline CPUBuffer Duplicate() const
|
||||
{
|
||||
CPUBuffer buffer(m_size);
|
||||
if(m_data)
|
||||
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_unique<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::unique_ptr<std::uint8_t[]> m_data;
|
||||
std::size_t m_size = 0;
|
||||
};
|
||||
}
|
||||
|
||||
#endif
|
||||
20
Runtime/Includes/Utils/NonCopyable.h
git.filemode.normal_file
20
Runtime/Includes/Utils/NonCopyable.h
git.filemode.normal_file
@@ -0,0 +1,20 @@
|
||||
#ifndef __SCOPE_UTILS_NON_COPYABLE__
|
||||
#define __SCOPE_UTILS_NON_COPYABLE__
|
||||
|
||||
namespace Scop
|
||||
{
|
||||
class NonCopyable
|
||||
{
|
||||
protected:
|
||||
NonCopyable() = default;
|
||||
virtual ~NonCopyable() = default;
|
||||
|
||||
public:
|
||||
NonCopyable(const NonCopyable&) = delete;
|
||||
NonCopyable(NonCopyable&&) noexcept = default;
|
||||
NonCopyable& operator=(const NonCopyable&) = delete;
|
||||
NonCopyable& operator=(NonCopyable&&) noexcept = default;
|
||||
};
|
||||
}
|
||||
|
||||
#endif
|
||||
20
Runtime/Includes/Utils/NonMovable.h
git.filemode.normal_file
20
Runtime/Includes/Utils/NonMovable.h
git.filemode.normal_file
@@ -0,0 +1,20 @@
|
||||
#ifndef __SCOPE_UTILS_NON_MOVABLE__
|
||||
#define __SCOPE_UTILS_NON_MOVABLE__
|
||||
|
||||
namespace Scop
|
||||
{
|
||||
class NonMovable
|
||||
{
|
||||
protected:
|
||||
NonMovable() = default;
|
||||
virtual ~NonMovable() = default;
|
||||
|
||||
public:
|
||||
NonMovable(const NonMovable&) = default;
|
||||
NonMovable(NonMovable&&) noexcept = delete;
|
||||
NonMovable& operator=(const NonMovable&) = default;
|
||||
NonMovable& operator=(NonMovable&&) noexcept = delete;
|
||||
};
|
||||
}
|
||||
|
||||
#endif
|
||||
33
Runtime/Includes/Utils/NonOwningPtr.h
git.filemode.normal_file
33
Runtime/Includes/Utils/NonOwningPtr.h
git.filemode.normal_file
@@ -0,0 +1,33 @@
|
||||
#ifndef __SCOPE_UTILS_NON_OWNING_PTR__
|
||||
#define __SCOPE_UTILS_NON_OWNING_PTR__
|
||||
|
||||
namespace Scop
|
||||
{
|
||||
template<typename T>
|
||||
class NonOwningPtr
|
||||
{
|
||||
public:
|
||||
NonOwningPtr(T* ptr = nullptr);
|
||||
NonOwningPtr(const NonOwningPtr&) = default;
|
||||
NonOwningPtr(NonOwningPtr&& ptr) noexcept;
|
||||
|
||||
NonOwningPtr& operator=(T* ptr);
|
||||
NonOwningPtr& operator=(const NonOwningPtr&) = default;
|
||||
NonOwningPtr& operator=(NonOwningPtr&& ptr) noexcept;
|
||||
|
||||
[[nodiscard]] inline operator bool() const noexcept;
|
||||
|
||||
[[nodiscard]] inline T* Get() const noexcept;
|
||||
[[nodiscard]] inline T* operator->() const noexcept;
|
||||
[[nodiscard]] inline T& operator*() const noexcept;
|
||||
|
||||
~NonOwningPtr() = default;
|
||||
|
||||
private:
|
||||
T* m_ptr = nullptr;
|
||||
};
|
||||
}
|
||||
|
||||
#include <Utils/NonOwningPtr.inl>
|
||||
|
||||
#endif
|
||||
53
Runtime/Includes/Utils/NonOwningPtr.inl
git.filemode.normal_file
53
Runtime/Includes/Utils/NonOwningPtr.inl
git.filemode.normal_file
@@ -0,0 +1,53 @@
|
||||
#pragma once
|
||||
#include <Utils/NonOwningPtr.h>
|
||||
|
||||
namespace Scop
|
||||
{
|
||||
template<typename T>
|
||||
NonOwningPtr<T>::NonOwningPtr(T* ptr) : m_ptr(ptr) {}
|
||||
|
||||
template<typename T>
|
||||
NonOwningPtr<T>::NonOwningPtr(NonOwningPtr<T>&& ptr) noexcept : m_ptr(ptr.m_ptr)
|
||||
{
|
||||
ptr.m_ptr = nullptr;
|
||||
}
|
||||
|
||||
template<typename T>
|
||||
NonOwningPtr<T>& NonOwningPtr<T>::operator=(T* ptr)
|
||||
{
|
||||
m_ptr = ptr;
|
||||
return *this;
|
||||
}
|
||||
|
||||
template<typename T>
|
||||
NonOwningPtr<T>& NonOwningPtr<T>::operator=(NonOwningPtr&& ptr) noexcept
|
||||
{
|
||||
m_ptr = ptr.m_ptr;
|
||||
ptr.m_ptr = nullptr;
|
||||
return *this;
|
||||
}
|
||||
|
||||
template<typename T>
|
||||
NonOwningPtr<T>::operator bool() const noexcept
|
||||
{
|
||||
return m_ptr != nullptr;
|
||||
}
|
||||
|
||||
template<typename T>
|
||||
T* NonOwningPtr<T>::Get() const noexcept
|
||||
{
|
||||
return m_ptr;
|
||||
}
|
||||
|
||||
template<typename T>
|
||||
T* NonOwningPtr<T>::operator->() const noexcept
|
||||
{
|
||||
return m_ptr;
|
||||
}
|
||||
|
||||
template<typename T>
|
||||
T& NonOwningPtr<T>::operator*() const noexcept
|
||||
{
|
||||
return *m_ptr;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user