[*] Update AuMemory::SharableByteBuffer to be useful with the transition to AuSPtr<AuMemoryViewXXX>'less APIs

This commit is contained in:
Reece Wilson 2024-07-13 17:08:03 +01:00
parent 3d652ed605
commit e665720f24

View File

@ -510,15 +510,16 @@ namespace Aurora::Memory
} }
/// @deprecated (partially) (wont remove. AuSPtr<AuMemoryViewXXX>s are now an anti-pattern)
inline AuSPtr<MemoryViewWrite> ToSharedWriteView() inline AuSPtr<MemoryViewWrite> ToSharedWriteView()
{ {
MemoryViewWrite view = *this; MemoryViewWrite view = ByteBuffer::operator MemoryViewWrite();
if (!view) if (!view)
{ {
return {}; return {};
} }
if (auto pView = AuMakeShared<MemoryViewWrite>(view.ptr, view.length, &this->uInUseCounter, AuSharedFromThis())) if (auto pView = AuMakeShared<MemoryViewWrite>(view.ptr, view.length, &this->uInUseCounter, this->GetSharedBlock()))
{ {
return pView; return pView;
} }
@ -528,15 +529,16 @@ namespace Aurora::Memory
} }
} }
inline AuSPtr<MemoryViewRead> ToSharedReadView() /// @deprecated (partially) (wont remove. AuSPtr<AuMemoryViewXXX>s are now an anti-pattern)
inline AuSPtr<MemoryViewRead> ToSharedReadView() const
{ {
MemoryViewRead view = *this; MemoryViewRead view = ByteBuffer::operator MemoryViewRead();
if (!view) if (!view)
{ {
return {}; return {};
} }
if (auto pView = AuMakeShared<MemoryViewRead>(view.ptr, view.length, &this->uInUseCounter, AuSharedFromThis())) if (auto pView = AuMakeShared<MemoryViewRead>(view.ptr, view.length, (AuAUInt32 *)&this->uInUseCounter, this->GetSharedBlock()))
{ {
return pView; 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<MemoryViewWrite>() inline operator AuSPtr<MemoryViewWrite>()
{ {
return this->ToSharedWriteView(); return this->ToSharedWriteView();
@ -555,6 +579,28 @@ namespace Aurora::Memory
{ {
return this->ToSharedReadView(); 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<void> GetSharedBlock() const
{
return AuSPtr<void>(this->shared_from_this(), (void *)this);
}
}; };
static ByteBuffer NewResizableBuffer(AuUInt32 length = 0) static ByteBuffer NewResizableBuffer(AuUInt32 length = 0)