diff --git a/Include/Aurora/Memory/ByteBuffer.hpp b/Include/Aurora/Memory/ByteBuffer.hpp index 56c04248..e69ea481 100644 --- a/Include/Aurora/Memory/ByteBuffer.hpp +++ b/Include/Aurora/Memory/ByteBuffer.hpp @@ -510,15 +510,16 @@ namespace Aurora::Memory } + /// @deprecated (partially) (wont remove. AuSPtrs are now an anti-pattern) inline AuSPtr ToSharedWriteView() { - MemoryViewWrite view = *this; + MemoryViewWrite view = ByteBuffer::operator MemoryViewWrite(); if (!view) { return {}; } - if (auto pView = AuMakeShared(view.ptr, view.length, &this->uInUseCounter, AuSharedFromThis())) + if (auto pView = AuMakeShared(view.ptr, view.length, &this->uInUseCounter, this->GetSharedBlock())) { return pView; } @@ -528,15 +529,16 @@ namespace Aurora::Memory } } - inline AuSPtr ToSharedReadView() + /// @deprecated (partially) (wont remove. AuSPtrs are now an anti-pattern) + inline AuSPtr ToSharedReadView() const { - MemoryViewRead view = *this; + MemoryViewRead view = ByteBuffer::operator MemoryViewRead(); if (!view) { return {}; } - if (auto pView = AuMakeShared(view.ptr, view.length, &this->uInUseCounter, AuSharedFromThis())) + if (auto pView = AuMakeShared(view.ptr, view.length, (AuAUInt32 *)&this->uInUseCounter, this->GetSharedBlock())) { return pView; } @@ -546,6 +548,28 @@ namespace Aurora::Memory } } + inline MemoryViewWrite ToSafeWriteView() + { + MemoryViewWrite view = ByteBuffer::operator MemoryViewWrite(); + if (!view) + { + return {}; + } + + return MemoryViewWrite(view.ptr, view.length, &this->uInUseCounter, this->GetSharedBlock()); + } + + inline MemoryViewRead ToSafeReadView() const + { + MemoryViewRead view = ByteBuffer::operator MemoryViewRead(); + if (!view) + { + return {}; + } + + return MemoryViewRead(view.ptr, view.length, (AuAUInt32 *)&this->uInUseCounter, this->GetSharedBlock()); + } + inline operator AuSPtr() { return this->ToSharedWriteView(); @@ -555,6 +579,28 @@ namespace Aurora::Memory { return this->ToSharedReadView(); } + + inline operator MemoryViewWrite() + { + return this->ToSafeWriteView(); + } + + inline operator MemoryViewRead() const + { + return this->ToSafeReadView(); + } + + private: + // TODO: under const methods, AuSharedFromThis, the static pointer cast, and the AuMemoryView constructor is hopelessly broken + // I've always considered c++s constness fundamentally flawed and designed by idiots. I'm not sinking time into fixes these yet. + // This should be just an AuSharedFromThis() + // But, no, Java, C++, and everybody else gets constness wrong to solve a series of bugs * that can be solved with simple encapsulation * + // (DOP + encapsulated API boundaries ftw) + // I dont care to fix this, or use the stupid mutable keyword on the usage counter, for now. It's a waste of my time. + AuSPtr GetSharedBlock() const + { + return AuSPtr(this->shared_from_this(), (void *)this); + } }; static ByteBuffer NewResizableBuffer(AuUInt32 length = 0)