merged cleanup
This commit is contained in:
@ -1,5 +1,5 @@
|
||||
#ifndef __ARRAY_INCLUDE_H
|
||||
#define __ARRAY_INCLUDE_H
|
||||
#ifndef ARRAY_INCLUDE_H
|
||||
#define ARRAY_INCLUDE_H
|
||||
|
||||
#include "user/lib/Iterator.h"
|
||||
#include <utility>
|
||||
@ -15,22 +15,30 @@ namespace bse {
|
||||
T buf[N];
|
||||
|
||||
public:
|
||||
array() {}; // If i write default something like bse::array<int, 10> arr; is not initialized...
|
||||
array() = default;; // If i write default something like bse::array<int, 10> arr; is not initialized...
|
||||
|
||||
array(std::initializer_list<T> list) {
|
||||
typename std::initializer_list<T>::iterator it = list.begin();
|
||||
for (unsigned int i = 0; i < N; ++i) {
|
||||
buf[i] = *it;
|
||||
++it;
|
||||
}
|
||||
}
|
||||
|
||||
iterator begin() { return iterator(&buf[0]); }
|
||||
iterator begin() const { return iterator(&buf[0]); }
|
||||
iterator end() { return iterator(&buf[N]); }
|
||||
iterator end() const { return iterator(&buf[N]); }
|
||||
|
||||
T& operator[](std::size_t i) { return this->buf[i]; }
|
||||
constexpr const T& operator[](std::size_t i) const { return this->buf[i]; }
|
||||
T& operator[](std::size_t i) { return buf[i]; }
|
||||
constexpr const T& operator[](std::size_t i) const { return buf[i]; }
|
||||
|
||||
T* operator&() { return &buf[0]; } // Not standard, I don't know yet if this will turn out to be a bad idea
|
||||
const T* operator&() const { return &buf[0]; }
|
||||
T* data() { return &buf[0]; } // Not standard, I don't know yet if this will turn out to be a bad idea
|
||||
const T* data() const { return &buf[0]; }
|
||||
|
||||
void swap(array<T, N>& other) {
|
||||
for (std::size_t i = 0; i < N; ++i) {
|
||||
std::swap(this->buf[i], other[i]);
|
||||
std::swap(buf[i], other[i]);
|
||||
}
|
||||
}
|
||||
|
||||
@ -39,7 +47,7 @@ namespace bse {
|
||||
template<std::size_t n>
|
||||
void swap_n(array<T, n>& other) {
|
||||
for (std::size_t i = 0; i < n; ++i) {
|
||||
std::swap(this->buf[i], other[i]);
|
||||
std::swap(buf[i], other[i]);
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -1,5 +1,5 @@
|
||||
#ifndef __Iterator_Include_H_
|
||||
#define __Iterator_Include_H_
|
||||
#ifndef Iterator_Include_H_
|
||||
#define Iterator_Include_H_
|
||||
|
||||
namespace bse {
|
||||
|
||||
@ -9,45 +9,41 @@ namespace bse {
|
||||
private:
|
||||
T* ptr = nullptr;
|
||||
|
||||
public:
|
||||
ContinuousIterator() = delete;
|
||||
|
||||
public:
|
||||
ContinuousIterator(T* ptr) : ptr(ptr) {}
|
||||
|
||||
T* get() const {
|
||||
return ptr;
|
||||
}
|
||||
|
||||
ContinuousIterator& operator++() {
|
||||
++this->ptr;
|
||||
++ptr;
|
||||
return *this;
|
||||
}
|
||||
|
||||
ContinuousIterator& operator--() {
|
||||
--this->ptr;
|
||||
--ptr;
|
||||
return *this;
|
||||
}
|
||||
|
||||
ContinuousIterator operator+(unsigned int add) {
|
||||
return ContinuousIterator(this->ptr + add);
|
||||
return ContinuousIterator(ptr + add);
|
||||
}
|
||||
|
||||
ContinuousIterator operator-(unsigned int sub) {
|
||||
return ContinuousIterator(this->ptr - sub);
|
||||
return ContinuousIterator(ptr - sub);
|
||||
}
|
||||
|
||||
// Convenience
|
||||
T* operator->() { return this->ptr; }
|
||||
const T* operator->() const { return this->ptr; }
|
||||
T& operator*() { return *this->ptr; }
|
||||
const T& operator*() const { return *this->ptr; }
|
||||
T* operator->() { return ptr; }
|
||||
const T* operator->() const { return ptr; }
|
||||
T& operator*() { return *ptr; }
|
||||
const T& operator*() const { return *ptr; }
|
||||
|
||||
bool operator<(const ContinuousIterator& other) const { return this->ptr < other.ptr; }
|
||||
bool operator<=(const ContinuousIterator& other) const { return this->ptr <= other.ptr; }
|
||||
bool operator>(const ContinuousIterator& other) const { return this->ptr > other.ptr; }
|
||||
bool operator>=(const ContinuousIterator& other) const { return this->ptr >= other.ptr; }
|
||||
bool operator==(const ContinuousIterator& other) const { return this->ptr == other.ptr; }
|
||||
bool operator!=(const ContinuousIterator& other) const { return this->ptr != other.ptr; }
|
||||
bool operator<(const ContinuousIterator& other) const { return ptr < other.ptr; }
|
||||
bool operator<=(const ContinuousIterator& other) const { return ptr <= other.ptr; }
|
||||
bool operator>(const ContinuousIterator& other) const { return ptr > other.ptr; }
|
||||
bool operator>=(const ContinuousIterator& other) const { return ptr >= other.ptr; }
|
||||
bool operator==(const ContinuousIterator& other) const { return ptr == other.ptr; }
|
||||
bool operator!=(const ContinuousIterator& other) const { return ptr != other.ptr; }
|
||||
|
||||
template<typename t>
|
||||
friend unsigned int distance(const ContinuousIterator<t>& first, const ContinuousIterator<t>& last);
|
||||
|
@ -19,61 +19,61 @@ void Logger::log(const char* message, CGA::color col) const {
|
||||
if (Logger::kout_enabled) {
|
||||
CGA::color old_col = kout.color_fg;
|
||||
kout << fgc(col)
|
||||
<< Logger::level_to_string(this->current_message_level) << "::"
|
||||
<< Logger::level_to_string(current_message_level) << "::"
|
||||
<< message << fgc(old_col);
|
||||
kout.flush(); // Don't add newline, Logger already does that
|
||||
}
|
||||
if (Logger::serial_enabled) {
|
||||
switch (col) {
|
||||
case CGA::WHITE:
|
||||
serial.write(ansi_white);
|
||||
SerialOut::write(ansi_white);
|
||||
break;
|
||||
case CGA::LIGHT_MAGENTA:
|
||||
serial.write(ansi_magenta);
|
||||
SerialOut::write(ansi_magenta);
|
||||
break;
|
||||
case CGA::LIGHT_RED:
|
||||
serial.write(ansi_red);
|
||||
SerialOut::write(ansi_red);
|
||||
break;
|
||||
case CGA::LIGHT_BLUE:
|
||||
serial.write(ansi_blue);
|
||||
SerialOut::write(ansi_blue);
|
||||
break;
|
||||
default:
|
||||
serial.write(ansi_default);
|
||||
SerialOut::write(ansi_default);
|
||||
}
|
||||
serial.write(Logger::level_to_string(this->current_message_level));
|
||||
serial.write(":: ");
|
||||
serial.write(message);
|
||||
serial.write('\r');
|
||||
SerialOut::write(Logger::level_to_string(current_message_level));
|
||||
SerialOut::write(":: ");
|
||||
SerialOut::write(message);
|
||||
SerialOut::write('\r');
|
||||
// serial.write("\r\n");
|
||||
}
|
||||
}
|
||||
|
||||
void Logger::flush() {
|
||||
this->buffer[this->pos] = '\0';
|
||||
buffer[pos] = '\0';
|
||||
|
||||
switch (this->current_message_level) {
|
||||
switch (current_message_level) {
|
||||
case Logger::TRACE:
|
||||
this->trace(&buffer);
|
||||
trace(buffer.data());
|
||||
break;
|
||||
case Logger::DEBUG:
|
||||
this->debug(&buffer);
|
||||
debug(buffer.data());
|
||||
break;
|
||||
case Logger::ERROR:
|
||||
this->error(&buffer);
|
||||
error(buffer.data());
|
||||
break;
|
||||
case Logger::INFO:
|
||||
this->info(&buffer);
|
||||
info(buffer.data());
|
||||
break;
|
||||
}
|
||||
|
||||
this->current_message_level = Logger::INFO;
|
||||
this->pos = 0;
|
||||
current_message_level = Logger::INFO;
|
||||
pos = 0;
|
||||
Logger::unlock();
|
||||
}
|
||||
|
||||
void Logger::trace(const char* message) const {
|
||||
if (Logger::level <= Logger::TRACE) {
|
||||
this->log(message, CGA::WHITE);
|
||||
log(message, CGA::WHITE);
|
||||
}
|
||||
}
|
||||
void Logger::trace(const bse::string& message) const {
|
||||
@ -82,7 +82,7 @@ void Logger::trace(const bse::string& message) const {
|
||||
|
||||
void Logger::debug(const char* message) const {
|
||||
if (Logger::level <= Logger::DEBUG) {
|
||||
this->log(message, CGA::LIGHT_MAGENTA);
|
||||
log(message, CGA::LIGHT_MAGENTA);
|
||||
}
|
||||
}
|
||||
void Logger::debug(const bse::string& message) const {
|
||||
@ -91,7 +91,7 @@ void Logger::debug(const bse::string& message) const {
|
||||
|
||||
void Logger::error(const char* message) const {
|
||||
if (Logger::level <= Logger::ERROR) {
|
||||
this->log(message, CGA::LIGHT_RED);
|
||||
log(message, CGA::LIGHT_RED);
|
||||
}
|
||||
}
|
||||
void Logger::error(const bse::string& message) const {
|
||||
@ -100,7 +100,7 @@ void Logger::error(const bse::string& message) const {
|
||||
|
||||
void Logger::info(const char* message) const {
|
||||
if (Logger::level <= Logger::INFO) {
|
||||
this->log(message, CGA::LIGHT_BLUE);
|
||||
log(message, CGA::LIGHT_BLUE);
|
||||
}
|
||||
}
|
||||
void Logger::info(const bse::string& message) const {
|
||||
|
@ -1,5 +1,5 @@
|
||||
#ifndef __Logger_Include_H_
|
||||
#define __Logger_Include_H_
|
||||
#ifndef Logger_Include_H_
|
||||
#define Logger_Include_H_
|
||||
|
||||
#include "devices/CGA.h"
|
||||
#include "lib/OutStream.h"
|
||||
@ -15,8 +15,6 @@ public:
|
||||
|
||||
private:
|
||||
Logger() = default;
|
||||
Logger(const Logger& copy) = delete;
|
||||
void operator=(const Logger& copy) = delete;
|
||||
|
||||
static bool kout_enabled;
|
||||
static bool serial_enabled;
|
||||
@ -24,6 +22,7 @@ private:
|
||||
void log(const char* message, CGA::color col) const;
|
||||
|
||||
friend class NamedLogger; // Allow NamedLogger to lock/unlock
|
||||
|
||||
SpinLock sem; // Semaphore would be a cyclic include
|
||||
static void lock() { Logger::instance().sem.acquire(); }
|
||||
static void unlock() { Logger::instance().sem.release(); }
|
||||
@ -31,6 +30,11 @@ private:
|
||||
// static void unlock() {}
|
||||
|
||||
public:
|
||||
// ~Logger() override = default;
|
||||
|
||||
Logger(const Logger& copy) = delete;
|
||||
void operator=(const Logger& copy) = delete;
|
||||
|
||||
enum LogLevel {
|
||||
TRACE,
|
||||
DEBUG,
|
||||
@ -51,13 +55,13 @@ public:
|
||||
void info(const char* message) const;
|
||||
void info(const bse::string& message) const;
|
||||
|
||||
// TODO: Make level change accessible over menu
|
||||
static void set_level(LogLevel level) {
|
||||
Logger::level = level;
|
||||
// TODO: Make lvl change accessible over menu
|
||||
static void set_level(LogLevel lvl) {
|
||||
Logger::level = lvl;
|
||||
}
|
||||
|
||||
static char* level_to_string(LogLevel level) {
|
||||
switch (level) {
|
||||
static char* level_to_string(LogLevel lvl) {
|
||||
switch (lvl) {
|
||||
case Logger::TRACE:
|
||||
return "TRACE";
|
||||
case Logger::DEBUG:
|
||||
@ -94,7 +98,7 @@ private:
|
||||
const char* name;
|
||||
|
||||
public:
|
||||
NamedLogger(const char* name) : name(name) {}
|
||||
explicit NamedLogger(const char* name) : name(name) {}
|
||||
|
||||
Logger& trace() {
|
||||
Logger::lock();
|
||||
|
@ -1,12 +1,19 @@
|
||||
#ifndef __MATH_INCLUDE_H_
|
||||
#define __MATH_INCLUDE_H_
|
||||
#ifndef MATH_INCLUDE_H_
|
||||
#define MATH_INCLUDE_H_
|
||||
|
||||
namespace bse {
|
||||
|
||||
// I didn't add any numeric constraints, will do with c++20
|
||||
|
||||
template<typename T>
|
||||
T& min(const T& a, const T& b) { return a < b ? a : b; }
|
||||
|
||||
template<typename T>
|
||||
T& max(const T& a, const T& b) { return a > b ? a : b; }
|
||||
|
||||
template<typename T>
|
||||
std::make_unsigned<T> abs(const T& a) { return a < 0 ? -a : a; }
|
||||
|
||||
} // namespace bse
|
||||
|
||||
#endif
|
||||
|
@ -1,8 +1,10 @@
|
||||
#ifndef __MYSTDLIB_INCLUDE_H_
|
||||
#define __MYSTDLIB_INCLUDE_H_
|
||||
#ifndef MYSTDLIB_INCLUDE_H_
|
||||
#define MYSTDLIB_INCLUDE_H_
|
||||
|
||||
namespace bse {
|
||||
|
||||
// add using byte or sth to replace char
|
||||
|
||||
template<typename T>
|
||||
void memcpy(T* destination, const T* source, const unsigned int count = 1) {
|
||||
for (unsigned int i = 0; i < count; ++i) {
|
||||
@ -14,7 +16,7 @@ namespace bse {
|
||||
|
||||
template<typename T>
|
||||
void zero(T* destination) {
|
||||
memset((char*)destination, '\0', sizeof(T));
|
||||
memset(reinterpret_cast<char*>(destination), '\0', sizeof(T));
|
||||
}
|
||||
|
||||
// void strcpy(char* destination, char* source);
|
||||
|
@ -1,5 +1,5 @@
|
||||
#ifndef __String_Include_H_
|
||||
#define __String_Include_H_
|
||||
#ifndef String_Include_H_
|
||||
#define String_Include_H_
|
||||
|
||||
#include "user/lib/Array.h"
|
||||
#include "user/lib/Iterator.h"
|
||||
@ -24,8 +24,9 @@ namespace bse {
|
||||
strncpy(buf, len + 1, str);
|
||||
}
|
||||
|
||||
// Convert char array to string
|
||||
template<unsigned int N>
|
||||
string(const array<char, N>& arr) : len(N), buf(new char[len + 1]) {
|
||||
explicit string(const array<char, N>& arr) : len(N), buf(new char[len + 1]) {
|
||||
for (unsigned int i = 0; i < N; ++i) {
|
||||
buf[i] = arr[i];
|
||||
}
|
||||
|
@ -1,5 +1,5 @@
|
||||
#ifndef __VECTOR_INCLUDE_H_
|
||||
#define __VECTOR_INCLUDE_H_
|
||||
#ifndef VECTOR_INCLUDE_H_
|
||||
#define VECTOR_INCLUDE_H_
|
||||
|
||||
// NOTE: I decided to implement this because I wanted some sort of dynamic array (for example for the keyeventmanager).
|
||||
// Also I wanted to template the Queue (for the scheduler) but with this I can just replace the Queue and use the
|
||||
@ -101,16 +101,14 @@ namespace bse {
|
||||
}
|
||||
|
||||
public:
|
||||
vector(bool lazy = false) {
|
||||
explicit vector(bool lazy = false) {
|
||||
if (!lazy) { // I added this as a work around, the scheduler can't initialize the queues right
|
||||
// away because when the scheduler is started the allocator is not ready.
|
||||
init();
|
||||
}
|
||||
};
|
||||
|
||||
vector(const vector& copy) {
|
||||
buf_cap = copy.buf_cap;
|
||||
buf_pos = copy.buf_pos;
|
||||
vector(const vector& copy) : buf_pos(copy.buf_pos), buf_cap(copy.buf_cap) {
|
||||
buf = new T[buf_cap];
|
||||
for (unsigned int i = 0; i < buf_pos; ++i) {
|
||||
buf[i] = copy[i]; // Does a copy since copy is marked const reference
|
||||
@ -131,9 +129,7 @@ namespace bse {
|
||||
return *this;
|
||||
}
|
||||
|
||||
vector(vector&& move) noexcept {
|
||||
buf_cap = move.buf_cap;
|
||||
buf_pos = move.buf_pos;
|
||||
vector(vector&& move) noexcept : buf_pos(move.buf_pos), buf_cap(move.buf_cap) {
|
||||
buf = move.buf;
|
||||
|
||||
move.buf_cap = 0;
|
||||
|
@ -1,5 +1,5 @@
|
||||
#ifndef __UniquePointer_Include_H_
|
||||
#define __UniquePointer_Include_H_
|
||||
#ifndef UniquePointer_Include_H_
|
||||
#define UniquePointer_Include_H_
|
||||
|
||||
#include <utility>
|
||||
|
||||
@ -34,11 +34,11 @@ namespace bse {
|
||||
ptr = nullptr;
|
||||
}
|
||||
|
||||
public:
|
||||
// Forbid copying
|
||||
unique_ptr(const unique_ptr& copy) = delete;
|
||||
unique_ptr& operator=(const unique_ptr& copy) = delete;
|
||||
|
||||
public:
|
||||
// Construction
|
||||
unique_ptr() = default; // Allow declaration without explicit definition
|
||||
|
||||
@ -74,9 +74,9 @@ namespace bse {
|
||||
|
||||
// Resetting: Replaces managed object, deleting the old one
|
||||
void reset() { del(); }
|
||||
void reset(T_* ptr) {
|
||||
void reset(T_* pt) {
|
||||
del();
|
||||
this->ptr = ptr;
|
||||
ptr = pt;
|
||||
}
|
||||
|
||||
// Release: Releases ownership without deletion
|
||||
@ -92,7 +92,7 @@ namespace bse {
|
||||
return ptr;
|
||||
}
|
||||
|
||||
// Nice to have operators
|
||||
// Pointer operators
|
||||
T_* operator->() { return ptr; }
|
||||
const T_* operator->() const { return ptr; }
|
||||
T_& operator*() { return *ptr; }
|
||||
@ -103,7 +103,7 @@ namespace bse {
|
||||
|
||||
bool operator==(const unique_ptr& other) const { return ptr == other.ptr; }
|
||||
|
||||
// These are only for array unique_ptr but I didn't want to define a full template specialization just for this
|
||||
// These are only for array unique_ptr but I didn't enforce that
|
||||
T_& operator[](std::size_t i) { return ptr[i]; }
|
||||
const T_& operator[](std::size_t i) const { return ptr[i]; }
|
||||
};
|
||||
|
Reference in New Issue
Block a user