rename StringImpl -> StringData, StringImplAdapter -> StringImpl

This commit is contained in:
matcool 2023-12-23 10:36:24 -03:00
parent a82ba30a70
commit d6af9af2b4
6 changed files with 83 additions and 92 deletions

View file

@ -7,12 +7,12 @@
#include <compare>
namespace geode::stl {
class StringImplAdapter;
class StringImpl;
struct StringImpl;
struct StringData;
#if defined(GEODE_IS_WINDOWS)
struct StringImpl {
struct StringData {
union {
std::array<char, 16> m_smallStorage;
char* m_bigStorage;
@ -22,7 +22,7 @@ namespace geode::stl {
size_t m_capacity;
};
#elif defined(GEODE_IS_MACOS) || defined(GEODE_IS_ANDROID)
struct StringImpl {
struct StringData {
struct Internal {
size_t m_size;
size_t m_capacity;
@ -31,7 +31,7 @@ namespace geode::stl {
Internal* m_data = nullptr;
};
#elif defined(GEODE_IS_IOS)
struct StringImpl {
struct StringData {
struct Short {
uint8_t sizex2;
std::array<char, 23> shortStorage;
@ -48,15 +48,13 @@ namespace geode::stl {
Long m_long;
};
};
#else
using StringImpl = void;
#endif
}
namespace gd {
class GEODE_DLL string {
geode::stl::StringImpl m_impl;
friend geode::stl::StringImplAdapter;
geode::stl::StringData m_data;
friend geode::stl::StringImpl;
public:
string();
string(string const&);

View file

@ -2,10 +2,10 @@
#include <Geode/c++stl/gdstdlib.hpp>
namespace geode::stl {
struct StringImplAdapter {
StringImpl& impl;
struct StringImpl {
StringData& data;
// clear but assumes the existing impl is uninit,
// clear but assumes the existing data is uninit,
// so basically a default ctor
void setEmpty();

View file

@ -9,33 +9,33 @@ Type& intoMutRef(const Type& x) {
return const_cast<Type&>(x);
}
using geode::stl::StringImplAdapter;
using geode::stl::StringImpl;
#define getAdap(x) StringImplAdapter{intoMutRef(x)}
#define adap getAdap(m_impl)
#define implFor(x) StringImpl{intoMutRef(x.m_data)}
#define impl implFor((*this))
namespace gd {
string::string() {
adap.setEmpty();
impl.setEmpty();
}
string::string(string const& str) {
adap.setStorage(str);
impl.setStorage(str);
}
// string::string(string&& other) {
// // TODO: do this better :-)
// adap.setStorage(other);
// getAdap(other.m_impl).free();
// getAdap(other.m_impl).setEmpty();
// impl.setStorage(other);
// implFor(other).free();
// implFor(other).setEmpty();
// }
string::string(char const* str) {
adap.setStorage(str);
impl.setStorage(str);
}
string::string(std::string const& str) {
adap.setStorage(str);
impl.setStorage(str);
}
string::~string() {
@ -44,53 +44,53 @@ namespace gd {
string& string::operator=(string const& other) {
if (this != &other) {
adap.free();
adap.setStorage(other);
impl.free();
impl.setStorage(other);
}
return *this;
}
string& string::operator=(string&& other) {
// TODO: do this better :-)
adap.free();
adap.setStorage(other);
getAdap(other.m_impl).free();
getAdap(other.m_impl).setEmpty();
impl.free();
impl.setStorage(other);
implFor(other).free();
implFor(other).setEmpty();
return *this;
}
string& string::operator=(char const* other) {
adap.free();
adap.setStorage(other);
impl.free();
impl.setStorage(other);
return *this;
}
string& string::operator=(std::string const& other) {
adap.free();
adap.setStorage(other);
impl.free();
impl.setStorage(other);
return *this;
}
void string::clear() {
adap.free();
adap.setEmpty();
impl.free();
impl.setEmpty();
}
char& string::at(size_t pos) {
if (pos >= this->size())
throw std::out_of_range("gd::string::at");
return adap.getStorage()[pos];
return impl.getStorage()[pos];
}
char const& string::at(size_t pos) const {
return const_cast<string*>(this)->at(pos);
}
char& string::operator[](size_t pos) { return adap.getStorage()[pos]; }
char const& string::operator[](size_t pos) const { return adap.getStorage()[pos]; }
char& string::operator[](size_t pos) { return impl.getStorage()[pos]; }
char const& string::operator[](size_t pos) const { return impl.getStorage()[pos]; }
char* string::data() { return adap.getStorage(); }
char const* string::data() const { return adap.getStorage(); }
char* string::data() { return impl.getStorage(); }
char const* string::data() const { return impl.getStorage(); }
char const* string::c_str() const { return this->data(); }
size_t string::size() const { return adap.getSize(); }
size_t string::capacity() const { return adap.getCapacity(); }
size_t string::size() const { return impl.getSize(); }
size_t string::capacity() const { return impl.getCapacity(); }
bool string::empty() const { return this->size() == 0; }
bool string::operator==(string const& other) const {

View file

@ -16,27 +16,27 @@ namespace geode::base {
namespace geode::stl {
static inline auto emptyInternalString() {
return reinterpret_cast<StringImpl::Internal*>(
geode::base::get() + (0xaa1c3c - 0x10000) + sizeof(StringImpl::Internal)
return reinterpret_cast<StringData::Internal*>(
geode::base::get() + (0xaa1c3c - 0x10000) + sizeof(StringData::Internal)
);
}
void StringImplAdapter::setEmpty() {
impl.m_data = emptyInternalString();
void StringImpl::setEmpty() {
data.m_data = emptyInternalString();
}
void StringImplAdapter::free() {
if (impl.m_data == nullptr || impl.m_data == emptyInternalString()) return;
void StringImpl::free() {
if (data.m_data == nullptr || data.m_data == emptyInternalString()) return;
// TODO: reimplement this
reinterpret_cast<void (*)(StringImpl*)>(geode::base::get() + (0x7514c8 - 0x10000) + 1)(&impl);
reinterpret_cast<void (*)(StringData*)>(geode::base::get() + (0x7514c8 - 0x10000) + 1)(&data);
}
char* StringImplAdapter::getStorage() {
return reinterpret_cast<char*>(impl.m_data);
char* StringImpl::getStorage() {
return reinterpret_cast<char*>(data.m_data);
}
void StringImplAdapter::setStorage(const std::string_view str) {
void StringImpl::setStorage(const std::string_view str) {
this->free();
if (str.size() == 0) {
@ -45,33 +45,33 @@ namespace geode::stl {
}
// TODO: should be using (char*, size_t) at the very least, or yknow, just reimplement it :-)
reinterpret_cast<void (*)(StringImpl*, char const*)>(geode::base::get() + (0x753a44 - 0x10000) + 1)(&impl, str.data());
reinterpret_cast<void (*)(StringData*, char const*)>(geode::base::get() + (0x753a44 - 0x10000) + 1)(&data, str.data());
return;
StringImpl::Internal internal;
StringData::Internal internal;
internal.m_size = str.size();
internal.m_capacity = str.size();
internal.m_refcount = 0;
auto* data = static_cast<char*>(operator new(str.size() + 1 + sizeof(internal)));
std::memcpy(data, &internal, sizeof(internal));
std::memcpy(data + sizeof(internal), str.data(), str.size());
data[sizeof(internal) + str.size()] = 0;
auto* buffer = static_cast<char*>(operator new(str.size() + 1 + sizeof(internal)));
std::memcpy(buffer, &internal, sizeof(internal));
std::memcpy(buffer + sizeof(internal), str.data(), str.size());
buffer[sizeof(internal) + str.size()] = 0;
impl.m_data = reinterpret_cast<StringImpl::Internal*>(data + sizeof(internal));
data.m_data = reinterpret_cast<StringData::Internal*>(buffer + sizeof(internal));
}
size_t StringImplAdapter::getSize() {
return impl.m_data[-1].m_size;
size_t StringImpl::getSize() {
return data.m_data[-1].m_size;
}
void StringImplAdapter::setSize(size_t size) {
void StringImpl::setSize(size_t size) {
// TODO: implement this, remember its copy-on-write...
}
size_t StringImplAdapter::getCapacity() {
return impl.m_data[-1].m_capacity;
size_t StringImpl::getCapacity() {
return data.m_data[-1].m_capacity;
}
void StringImplAdapter::setCapacity(size_t cap) {
void StringImpl::setCapacity(size_t cap) {
// TODO: implement this, remember its copy-on-write...
}
}

View file

@ -3,44 +3,44 @@
#ifdef GEODE_IS_WINDOWS
namespace geode::stl {
void StringImplAdapter::setEmpty() {
impl.m_size = 0;
impl.m_capacity = 15;
impl.m_smallStorage[0] = 0;
void StringImpl::setEmpty() {
data.m_size = 0;
data.m_capacity = 15;
data.m_smallStorage[0] = 0;
}
void StringImplAdapter::free() {
if (impl.m_capacity > 15) {
delete impl.m_bigStorage;
void StringImpl::free() {
if (data.m_capacity > 15) {
delete data.m_bigStorage;
}
}
char* StringImplAdapter::getStorage() {
return impl.m_capacity <= 15 ? impl.m_smallStorage.data() : impl.m_bigStorage;
char* StringImpl::getStorage() {
return data.m_capacity <= 15 ? data.m_smallStorage.data() : data.m_bigStorage;
}
void StringImplAdapter::setStorage(const std::string_view str) {
impl.m_size = impl.m_capacity = str.size();
void StringImpl::setStorage(const std::string_view str) {
data.m_size = data.m_capacity = str.size();
if (str.size() <= 15) {
impl.m_capacity = 15;
data.m_capacity = 15;
} else {
impl.m_bigStorage = static_cast<char*>(operator new(str.size() + 1));
data.m_bigStorage = static_cast<char*>(operator new(str.size() + 1));
}
std::memcpy(getStorage(), str.data(), str.size());
getStorage()[str.size()] = 0;
}
size_t StringImplAdapter::getSize() {
return impl.m_size;
size_t StringImpl::getSize() {
return data.m_size;
}
void StringImplAdapter::setSize(size_t size) {
impl.m_size = size;
void StringImpl::setSize(size_t size) {
data.m_size = size;
}
size_t StringImplAdapter::getCapacity() {
return impl.m_capacity;
size_t StringImpl::getCapacity() {
return data.m_capacity;
}
void StringImplAdapter::setCapacity(size_t cap) {
impl.m_capacity = cap;
void StringImpl::setCapacity(size_t cap) {
data.m_capacity = cap;
}
}

View file

@ -238,13 +238,6 @@ CCArray* ModListLayer::createModCells(ModListType type, ModListQuery const& quer
bool ModListLayer::init() {
if (!CCLayer::init()) return false;
{
gd::string hi = "hello";
gd::string hi2 = "hello";
gd::string hi3 = "hella";
log::info("{} {} {} {}", hi == hi2, hi == hi3, hi < hi3, hi > hi3);
}
m_indexListener.bind(this, &ModListLayer::onIndexUpdate);
auto winSize = CCDirector::sharedDirector()->getWinSize();