BAN: Cleanup includes

This commit is contained in:
Bananymous 2023-01-17 11:38:16 +02:00
parent a7a55cd0fb
commit cf63552b2a
9 changed files with 75 additions and 65 deletions

13
BAN/BAN/Memory.cpp Normal file
View File

@ -0,0 +1,13 @@
#include <BAN/Memory.h>
namespace std { enum class align_val_t : size_t {}; }
void* operator new(size_t size) { return BAN::allocator(size); }
void* operator new[](size_t size) { return BAN::allocator(size); }
void* operator new(size_t size, std::align_val_t align) { return BAN::allocator_align(size, (size_t)align); }
void* operator new[](size_t size, std::align_val_t align) { return BAN::allocator_align(size, (size_t)align); }
void operator delete(void* addr) { BAN::deallocator(addr); }
void operator delete[](void* addr) { BAN::deallocator(addr); }
void operator delete(void* addr, size_t) { BAN::deallocator(addr); }
void operator delete[](void* addr, size_t) { BAN::deallocator(addr); }

View File

@ -1,5 +1,6 @@
#include <BAN/Errors.h>
#include <BAN/Math.h>
#include <BAN/Memory.h>
#include <BAN/Move.h>
#include <BAN/String.h>
#include <BAN/StringView.h>

View File

@ -31,6 +31,7 @@ BUILDDIR=$(abspath build)
FREEOBJS= \
$(ARCH_FREEOBJS) \
BAN/Memory.o \
BAN/String.o \
BAN/StringView.o \

View File

@ -1,7 +1,6 @@
#pragma once
#include <BAN/Formatter.h>
#include <BAN/Memory.h>
#include <string.h>
@ -15,62 +14,65 @@
#define TRY(error) ({ auto e = error; if (e.IsError()) return e; e.Value(); })
class Error
namespace BAN
{
public:
static Error FromString(const char* message)
class Error
{
Error result;
strncpy(result.m_message, message, sizeof(m_message));
result.m_message[sizeof(result.m_message) - 1] = '\0';
result.m_error_code = 0xFF;
return result;
}
public:
static Error FromString(const char* message)
{
Error result;
strncpy(result.m_message, message, sizeof(m_message));
result.m_message[sizeof(result.m_message) - 1] = '\0';
result.m_error_code = 0xFF;
return result;
}
uint8_t GetErrorCode() const { return m_error_code; }
const char* GetMessage() const { return m_message; }
uint8_t GetErrorCode() const { return m_error_code; }
const char* GetMessage() const { return m_message; }
private:
char m_message[128];
uint8_t m_error_code;
};
private:
char m_message[128];
uint8_t m_error_code;
};
template<typename T>
class ErrorOr
{
public:
ErrorOr(const T& value) : m_has_error(false) { m_data = (void*)new T(value); }
ErrorOr(const Error& error) : m_has_error(true) { m_data = (void*)new Error(error); }
template<typename S> ErrorOr(const ErrorOr<S>& other) : ErrorOr(other.GetError()) {}
~ErrorOr() { IsError() ? (delete reinterpret_cast<Error*>(m_data)) : (delete reinterpret_cast<T*>(m_data)); }
template<typename T>
class ErrorOr
{
public:
ErrorOr(const T& value) : m_has_error(false) { m_data = (void*)new T(value); }
ErrorOr(const Error& error) : m_has_error(true) { m_data = (void*)new Error(error); }
template<typename S> ErrorOr(const ErrorOr<S>& other) : ErrorOr(other.GetError()) {}
~ErrorOr() { IsError() ? (delete reinterpret_cast<Error*>(m_data)) : (delete reinterpret_cast<T*>(m_data)); }
bool IsError() const { return m_has_error; }
const Error& GetError() const { return *reinterpret_cast<Error*>(m_data); }
T& Value() { return *reinterpret_cast<T*>(m_data); }
bool IsError() const { return m_has_error; }
const Error& GetError() const { return *reinterpret_cast<Error*>(m_data); }
T& Value() { return *reinterpret_cast<T*>(m_data); }
private:
bool m_has_error = false;
void* m_data = nullptr;
};
private:
bool m_has_error = false;
void* m_data = nullptr;
};
template<>
class ErrorOr<void>
{
public:
ErrorOr() { }
ErrorOr(const Error& error) : m_error(error) { }
~ErrorOr() { }
template<>
class ErrorOr<void>
{
public:
ErrorOr() { }
ErrorOr(const Error& error) : m_error(error) { }
~ErrorOr() { }
bool IsError() const { return m_has_error; }
const Error& GetError() const { return m_error; }
void Value() { }
bool IsError() const { return m_has_error; }
const Error& GetError() const { return m_error; }
void Value() { }
private:
Error m_error;
bool m_has_error = false;
};
private:
Error m_error;
bool m_has_error = false;
};
}
namespace BAN::Formatter
{

View File

@ -1,14 +1,17 @@
#pragma once
template<typename T> class ErrorOr;
#include <stddef.h>
namespace BAN
{
template<typename, size_t> class Array;
template<typename> class ErrorOr;
template<typename> class Function;
template<typename> class Queue;
class String;
class StringView;
template<typename T> class Vector;
template<typename T> class Queue;
template<typename> class Vector;
}

View File

@ -38,16 +38,5 @@ namespace BAN
}
namespace std { enum class align_val_t : size_t {}; }
inline void* operator new(size_t size) { return BAN::allocator(size); }
inline void* operator new[](size_t size) { return BAN::allocator(size); }
inline void* operator new(size_t size, std::align_val_t align) { return BAN::allocator_align(size, (size_t)align); }
inline void* operator new[](size_t size, std::align_val_t align) { return BAN::allocator_align(size, (size_t)align); }
inline void* operator new(size_t, void* addr) { return addr; }
inline void* operator new[](size_t, void* addr) { return addr; }
inline void operator delete(void* addr) { BAN::deallocator(addr); }
inline void operator delete[](void* addr) { BAN::deallocator(addr); }
inline void operator delete(void* addr, size_t) { BAN::deallocator(addr); }
inline void operator delete[](void* addr, size_t) { BAN::deallocator(addr); }
inline void* operator new(size_t, void* addr) { return addr; }
inline void* operator new[](size_t, void* addr) { return addr; }

View File

@ -2,6 +2,7 @@
#include <BAN/Errors.h>
#include <BAN/Math.h>
#include <BAN/Memory.h>
#include <BAN/Move.h>
namespace BAN

View File

@ -1,6 +1,5 @@
#pragma once
#include <BAN/Errors.h>
#include <BAN/ForwardList.h>
#include <BAN/Formatter.h>

View File

@ -2,6 +2,7 @@
#include <BAN/Errors.h>
#include <BAN/Math.h>
#include <BAN/Memory.h>
#include <BAN/Move.h>
namespace BAN