Add headers, clang-format, decompctx.py & more

Former-commit-id: 53f8d3cba7
This commit is contained in:
2022-04-09 20:17:06 -04:00
parent 73b7b4df3a
commit 27d94af97b
83 changed files with 3919 additions and 2665 deletions

View File

@@ -0,0 +1,46 @@
#ifndef _RSTL_CONSTRUCT_HPP
#define _RSTL_CONSTRUCT_HPP
#include "types.h"
namespace rstl {
template < typename T >
inline void construct(void* dest, const T& src) {
*static_cast< T* >(dest) = src;
}
template < typename T >
inline void destroy(T* in) {
in->~T();
}
template < typename Iter >
inline void destroy(Iter begin, Iter end) {
Iter current = begin;
while (current != end) {
current.destroy();
++current;
}
}
template < typename Iter, typename T >
inline void uninitialized_copy(Iter begin, Iter end, T* in) {
Iter current = begin;
while (current != end) {
current = *in;
++current;
}
}
template < typename T >
inline void uninitialized_copy_n(T* dest, size_t count, T* src) {
for (size_t i = 0; i < count; ++i) {
construct(dest, *src);
destroy(src);
++dest;
++src;
}
}
} // namespace rstl
#endif

37
include/rstl/list.hpp Normal file
View File

@@ -0,0 +1,37 @@
#ifndef _RSTL_LIST_HPP
#define _RSTL_LIST_HPP
#include "types.h"
#include "rmemory_allocator.hpp"
namespace rstl {
template < typename T, typename Alloc = rmemory_allocator >
class list {
public:
~list() {
node* cur = x4_start;
while (cur != nullptr) {
delete cur->x8_item;
cur = cur->x4_next;
}
}
private:
struct node {
node* x0_prev;
node* x4_next;
union {
T* x8_item;
u32 x8_count;
};
};
Alloc x0_allocator;
node* x4_start;
node* x8_end;
node xc_empty;
};
} // namespace rstl
#endif

15
include/rstl/map.hpp Normal file
View File

@@ -0,0 +1,15 @@
#ifndef _RSTL_MAP_HPP
#define _RSTL_MAP_HPP
#include "types.h"
#include "rmemory_allocator.hpp"
namespace rstl {
template < typename K, typename V, typename Alloc = rmemory_allocator >
class map {
u8 pad[0x10];
};
} // namespace rstl
#endif

View File

@@ -0,0 +1,29 @@
#ifndef _RSTL_OPTIONAL_OBJECT_HPP
#define _RSTL_OPTIONAL_OBJECT_HPP
#include "types.h"
#include "construct.hpp"
namespace rstl {
template < typename T >
class optional_object {
public:
optional_object() : x4_valid(false) {}
optional_object(const T& item) : x0_item(item), x4_valid(true) {}
~optional_object() { clear(); }
T& data() { return x0_item; }
operator bool() const { return x4_valid; }
void clear() {
rstl::destroy(&x0_item);
x4_valid = false;
}
private:
T x0_item;
bool x4_valid;
};
} // namespace rstl
#endif

18
include/rstl/pair.hpp Normal file
View File

@@ -0,0 +1,18 @@
#ifndef _RSTL_PAIR_HPP
#define _RSTL_PAIR_HPP
#include "types.h"
namespace rstl {
template < typename L, typename R >
class pair {
public:
L first;
R second;
inline pair() {}
inline pair(const L& first, const R& second) : first(first), second(second) {}
};
} // namespace rstl
#endif

View File

@@ -0,0 +1,55 @@
#ifndef _RSTL_POINTER_ITERATOR_HPP
#define _RSTL_POINTER_ITERATOR_HPP
#include "types.h"
#include "construct.hpp"
namespace rstl {
template < typename T, typename Vec, typename Alloc >
class const_pointer_iterator {
protected:
const T* current;
public:
const_pointer_iterator() : current(nullptr) {}
const_pointer_iterator(const T* begin) : current(begin) {}
void operator++() { ++current; }
void operator--() { --current; }
T* get_pointer() const { return const_cast< T* >(current); }
const T& operator*() const { return *get_pointer(); }
const T* operator->() const { return get_pointer(); }
bool CheckValid() const { return current != nullptr; }
bool operator==(const const_pointer_iterator& other) { return current == other.current; }
bool operator!=(const const_pointer_iterator& other) { return current != other.current; }
friend const_pointer_iterator operator+(const const_pointer_iterator& x, int v) { return const_pointer_iterator(x.current + v); }
friend const_pointer_iterator operator-(const const_pointer_iterator& x, int v) { return const_pointer_iterator(x.current - v); }
};
template < typename T, typename Vec, typename Alloc >
class pointer_iterator : public const_pointer_iterator< T, Vec, Alloc > {
public:
pointer_iterator() : const_pointer_iterator(nullptr) {}
pointer_iterator(T* begin) : const_pointer_iterator(begin) {}
void operator=(const T& other) {
if (CheckValid()) {
*get_pointer() = other;
}
}
T* get_pointer() const { return const_cast< T* >(current); }
// T* operator*() const { if (CheckValid()) return get_pointer(); else return
// nullptr; }
T* operator->() const { return get_pointer(); }
void destroy() const {
if (CheckValid()) {
rstl::destroy(get_pointer());
}
}
friend pointer_iterator operator+(const pointer_iterator& x, int v) { return pointer_iterator(x.get_pointer() + v); }
friend pointer_iterator operator-(const pointer_iterator& x, int v) { return pointer_iterator(x.get_pointer() - v); }
};
} // namespace rstl
#endif

25
include/rstl/rc_ptr.hpp Normal file
View File

@@ -0,0 +1,25 @@
#ifndef _RSTL_RC_PTR_HPP
#define _RSTL_RC_PTR_HPP
#include "types.h"
template < typename T >
class CRefData {
public:
T* x0_ptr;
unsigned int x4_refCount;
};
namespace rstl {
template < typename T >
class rc_ptr {
CRefData< T >* x0_refData;
public:
~rc_ptr();
T* get() { return x0_refData->x0_ptr; }
T* operator->() { return get(); }
};
} // namespace rstl
#endif

View File

@@ -0,0 +1,51 @@
#ifndef _RSTL_RESERVED_VECTOR_HPP
#define _RSTL_RESERVED_VECTOR_HPP
#include "types.h"
#include "construct.hpp"
#include "pointer_iterator.hpp"
namespace rstl {
template < typename T, size_t N >
class reserved_vector {
size_t x0_count;
T x4_items[N];
public:
typedef pointer_iterator< T, reserved_vector< T, N >, void > iterator;
typedef const_pointer_iterator< T, reserved_vector< T, N >, void > const_iterator;
inline iterator begin() { return iterator(x4_items); }
inline const_iterator begin() const { return const_iterator(x4_items); }
inline iterator end() { return iterator(x4_items + x0_count); }
inline const_iterator end() const { return const_iterator(x4_items + x0_count); }
~reserved_vector() {
for (u32 i = x0_count; i > 0; --i) {
rstl::destroy(&x4_items[i]);
}
}
void push_back(const T& in) {
if (x0_count < N) {
iterator out = begin() + x0_count;
out = in;
++x0_count;
}
}
inline T* data() { return x4_items; }
inline const T* data() const { return x4_items; }
inline size_t size() const { return x0_count; }
inline size_t capacity() const { return N; }
inline T& front() { return x4_items[0]; }
inline const T& front() const { return x4_items[0]; }
inline T& back() { return x4_items[x0_count - 1]; }
inline const T& back() const { return x4_items[x0_count - 1]; }
inline T& operator[](size_t idx) { return data()[idx]; }
inline const T& operator[](size_t idx) const { return data()[idx]; }
};
} // namespace rstl
#endif

View File

@@ -0,0 +1,22 @@
#ifndef _RSTL_RMEMORY_ALLOCATOR_HPP
#define _RSTL_RMEMORY_ALLOCATOR_HPP
#include "types.h"
#include "../Kyoto_CWD/CMemory.hpp"
namespace rstl {
struct rmemory_allocator {
template < typename T >
static void allocate(T*& out, size_t count) {
out = new T[count];
}
template < typename T >
static void deallocate(T* ptr) {
if (ptr != nullptr)
delete ptr;
}
};
} // namespace rstl
#endif

View File

@@ -0,0 +1,24 @@
#ifndef _RSTL_SINGLE_PTR_HPP
#define _RSTL_SINGLE_PTR_HPP
#include "types.h"
namespace rstl {
template < typename T >
class single_ptr {
T* x0_ptr;
public:
single_ptr() : x0_ptr(nullptr) {}
single_ptr(T* ptr) : x0_ptr(ptr) {}
~single_ptr() { delete x0_ptr; }
T* get() { return x0_ptr; }
T* operator->() { return x0_ptr; }
void operator=(T* ptr) {
delete x0_ptr;
x0_ptr = ptr;
}
};
} // namespace rstl
#endif

120
include/rstl/string.hpp Normal file
View File

@@ -0,0 +1,120 @@
#ifndef _RSTL_STRING_HPP
#define _RSTL_STRING_HPP
#include "types.h"
#include "rmemory_allocator.hpp"
namespace rstl {
template < typename _CharTp >
struct char_traits {};
template < typename _CharTp, typename Traits = char_traits< _CharTp >, typename Alloc = rmemory_allocator >
class basic_string {
struct COWData {
u32 x0_capacity;
u32 x4_refCount;
_CharTp* x8_data;
};
const _CharTp* x0_ptr;
COWData* x4_cow;
u32 x8_size;
u32 _pad; // Alloc?
// void internal_allocate(int size)
// {
// x4_cow = reinterpret_cast<COWData*>(new u8[size * sizeof(_CharTp) +
// 8]); x0_ptr = x4_cow->x8_data; x4_cow->x0_capacity = u32(size);
// x4_cow->x4_refCount = 1;
// }
void internal_dereference();
// {
// if (x4_cow && --x4_cow->x4_refCount == 0)
// delete[] x4_cow;
// }
static const _CharTp _EmptyString;
public:
struct literal_t {};
basic_string() : x0_ptr(&_EmptyString), x4_cow(nullptr), x8_size(0) {}
basic_string(literal_t, const _CharTp* data);
// {
// x0_ptr = data;
// x4_cow = nullptr;
// const _CharTp* it = data;
// while (*it)
// ++it;
// x8_size = u32((it - data) / sizeof(_CharTp));
// }
basic_string(const basic_string& str);
// {
// x0_ptr = str.x0_ptr;
// x4_cow = str.x4_cow;
// x8_size = str.x8_size;
// if (x4_cow)
// ++x4_cow->x4_refCount;
// }
basic_string(const _CharTp* data, int size, const Alloc&);
// {
// if (size <= 0 && !data)
// {
// x0_ptr = &_EmptyString;
// x4_cow = nullptr;
// x8_size = 0;
// return;
// }
// const _CharTp* it = data;
// u32 len = 0;
// while (*it)
// {
// if (size != -1 && len >= size)
// break;
// ++it;
// ++len;
// }
// internal_allocate(len + 1);
// x8_size = len;
// for (int i = 0; i < len; ++i)
// x4_cow->x8_data[i] = data[i];
// x4_cow->x8_data[len] = 0;
// }
~basic_string() { internal_dereference(); }
basic_string& operator=(const basic_string&);
basic_string operator+(const basic_string&);
const char* data() const { return x0_ptr; }
};
// template <>
// const char basic_string<char>::_EmptyString = 0;
// template <>
// const wchar_t basic_string<wchar_t>::_EmptyString = 0;
typedef basic_string< wchar_t > wstring;
typedef basic_string< char > string;
wstring wstring_l(const wchar_t* data);
// {
// return wstring(wstring::literal_t(), data);
// }
string string_l(const char* data);
// {
// return string(string::literal_t(), data);
// }
} // namespace rstl
#endif

87
include/rstl/vector.hpp Normal file
View File

@@ -0,0 +1,87 @@
#ifndef _RSTL_VECTOR_HPP
#define _RSTL_VECTOR_HPP
#include "types.h"
#include "pointer_iterator.hpp"
#include "rmemory_allocator.hpp"
namespace rstl {
template < typename T, typename Alloc = rmemory_allocator >
class vector {
Alloc x0_allocator;
size_t x4_count;
size_t x8_capacity;
T* xc_items;
public:
typedef pointer_iterator< T, vector< T, Alloc >, Alloc > iterator;
typedef const_pointer_iterator< T, vector< T, Alloc >, Alloc > const_iterator;
inline iterator begin() { return iterator(xc_items); }
inline const_iterator begin() const { return const_iterator(xc_items); }
inline iterator end() { return iterator(xc_items + x4_count); }
inline const_iterator end() const { return const_iterator(xc_items + x4_count); }
inline vector() : x4_count(0), x8_capacity(0), xc_items(NULL) {}
inline vector(size_t count) : x4_count(0), x8_capacity(0), xc_items(0) { reserve(count); }
vector(const vector& other) {
x4_count = other.x4_count;
x8_capacity = other.x8_capacity;
if (x4_count == 0 && x8_capacity == 0) {
xc_items = NULL;
} else {
if (x8_capacity == 0) {
xc_items = NULL;
} else {
Alloc::allocate(xc_items, x8_capacity);
}
// what's going on here?
iterator iter;
const_iterator otherIter;
otherIter = other.begin();
iter = begin();
for (size_t i = 0; i < x4_count; ++i) {
iter = *otherIter;
++iter;
++otherIter;
}
}
}
~vector() {
rstl::destroy(begin(), end());
Alloc::deallocate(xc_items);
}
void reserve(size_t size); /* {
if (x8_capacity >= size) return;
T* newData;
Alloc::allocate(newData, size);
uninitialized_copy_n(newData, x4_count, xc_items);
rstl::destroy(begin(), end());
Alloc::deallocate(xc_items);
xc_items = newData;
x8_capacity = size;
}*/
void push_back(const T& in) {
if (x4_count >= x8_capacity) {
reserve(x8_capacity != 0 ? x8_capacity * 2 : 4);
}
iterator out = begin() + x4_count;
out = in;
++x4_count;
}
inline T* data() { return xc_items; }
inline const T* data() const { return xc_items; }
inline size_t size() const { return x4_count; }
inline size_t capacity() const { return x8_capacity; }
inline T& front() { return xc_items[0]; }
inline const T& front() const { return xc_items[0]; }
inline T& back() { return xc_items[x4_count - 1]; }
inline const T& back() const { return xc_items[x4_count - 1]; }
inline T& operator[](size_t idx) { return xc_items[idx]; }
inline const T& operator[](size_t idx) const { return xc_items[idx]; }
};
} // namespace rstl
#endif