geode/loader/include/Geode/c++stl/msvcstl.hpp

132 lines
3.2 KiB
C++
Raw Normal View History

2022-10-30 14:24:06 -04:00
#pragma once
2022-11-30 10:43:44 -05:00
#include <Geode/binding/GDString.hpp>
2023-01-19 06:27:42 -05:00
#include <cstring>
2022-10-30 14:59:20 -04:00
#include <string>
#include <stdexcept>
#include <utility>
#include <map>
2022-10-30 14:59:20 -04:00
#include <vector>
2022-10-30 14:24:06 -04:00
namespace gd {
2022-10-30 14:59:20 -04:00
struct InternalString {
union {
char m_storage[16];
char* m_pointer;
};
size_t m_length;
size_t m_capacity;
};
class string : GDString {
2022-10-30 14:59:20 -04:00
private:
InternalString m_data;
char* get_data() {
if (m_data.m_capacity < 16) return m_data.m_storage;
return m_data.m_pointer;
}
const char* get_data() const {
if (m_data.m_capacity < 16) return m_data.m_storage;
return m_data.m_pointer;
}
2022-10-30 14:59:20 -04:00
public:
2023-01-19 06:31:32 -05:00
string() {
m_data.m_storage[0] = 0;
m_data.m_length = 0;
m_data.m_capacity = 15;
}
2023-01-19 06:27:42 -05:00
string(string const& param) : string() {
(void)this->winAssign(param.c_str(), param.size());
2022-10-30 14:59:20 -04:00
}
2023-01-19 06:27:42 -05:00
string(string&& param) : string() {
(void)this->winAssign(param.c_str(), param.size());
}
string(char const* param) : string() {
(void)this->winAssign(param, std::strlen(param));
}
string(std::string const& param) : string() {
(void)this->winAssign(param.c_str(), param.size());
}
string& operator=(string const& param) {
(void)this->winAssign(param.c_str(), param.size());
return *this;
}
string& operator=(string&& param) {
(void)this->winAssign(param.c_str(), param.size());
2022-11-22 08:47:04 -05:00
return *this;
}
2023-01-19 06:27:42 -05:00
string& operator=(char const* param) {
(void)this->winAssign(param, std::strlen(param));
return *this;
}
string& operator=(std::string const& param) {
(void)this->winAssign(param.c_str(), param.size());
return *this;
}
void clear() {
(void)this->winDtor();
}
2022-10-30 14:59:20 -04:00
~string() {
(void)this->winDtor();
}
char& at(size_t pos) {
if (pos >= m_data.m_length) throw std::out_of_range("gd::string::at");
return (*this)[pos];
2022-10-30 14:59:20 -04:00
}
char const& at(size_t pos) const {
if (pos >= m_data.m_length) throw std::out_of_range("gd::string::at");
return (*this)[pos];
2022-10-30 14:59:20 -04:00
}
char& operator[](size_t pos) {
return this->get_data()[pos];
2022-10-30 14:59:20 -04:00
}
char const& operator[](size_t pos) const {
return this->get_data()[pos];
2022-10-30 14:59:20 -04:00
}
char* data() {
return this->get_data();
2022-10-30 14:59:20 -04:00
}
char const* data() const {
return this->get_data();
2022-10-30 14:59:20 -04:00
}
char const* c_str() const {
return this->get_data();
}
size_t size() const {
return m_data.m_length;
}
operator std::string() const {
return std::string(this->c_str(), this->size());
2022-10-30 14:59:20 -04:00
}
};
2022-10-30 14:24:06 -04:00
template <class T>
using vector = std::vector<T>;
template <class K, class V>
using map = std::map<K, V>;
}