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/Errors.h>
#include <BAN/Math.h> #include <BAN/Math.h>
#include <BAN/Memory.h>
#include <BAN/Move.h> #include <BAN/Move.h>
#include <BAN/String.h> #include <BAN/String.h>
#include <BAN/StringView.h> #include <BAN/StringView.h>

View File

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

View File

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

View File

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

View File

@ -38,16 +38,5 @@ namespace BAN
} }
namespace std { enum class align_val_t : size_t {}; } inline void* operator new(size_t, void* addr) { return addr; }
inline void* operator new[](size_t, void* addr) { return addr; }
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); }

View File

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

View File

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

View File

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