gomuks-pharo/Gomuks/GmksFFIBorrowedBuffer.class.st
2026-04-01 19:23:53 +02:00

71 lines
1.7 KiB
Smalltalk

"
I am a byte array that is ""borrowed"" from the creator to recipient. The recipient MUST NOT try to destroy me.
As such, my creator is responsible to destroy me using #destroy.
"
Class {
#name : 'GmksFFIBorrowedBuffer',
#superclass : 'FFIStructure',
#classVars : [
'OFFSET_BASE',
'OFFSET_LENGTH'
],
#category : 'Gomuks-FFI',
#package : 'Gomuks',
#tag : 'FFI'
}
{ #category : 'field definition' }
GmksFFIBorrowedBuffer class >> fieldsDesc [
^ #(
uint8* base
size_t length
)
]
{ #category : 'instance creation' }
GmksFFIBorrowedBuffer class >> newFromString: string [
| buf |
buf := FFIExternalArray externalNewType: #uint8 size: string size.
string asByteArray withIndexDo: [ :c :i | buf at: i put: c ].
^ self new
base: buf getHandle;
length: string size;
yourself
]
{ #category : 'accessing - structure variables' }
GmksFFIBorrowedBuffer >> base [
"This method was automatically generated"
^ExternalData fromHandle: (handle pointerAt: OFFSET_BASE) type: ExternalType byte asPointerType
]
{ #category : 'accessing - structure variables' }
GmksFFIBorrowedBuffer >> base: anObject [
"This method was automatically generated"
handle pointerAt: OFFSET_BASE put: anObject getHandle.
]
{ #category : 'deleting' }
GmksFFIBorrowedBuffer >> destroy [
self base free.
^ nil
]
{ #category : 'accessing - structure variables' }
GmksFFIBorrowedBuffer >> length [
"This method was automatically generated"
^handle platformSizeTAt: OFFSET_LENGTH
]
{ #category : 'accessing - structure variables' }
GmksFFIBorrowedBuffer >> length: anObject [
"This method was automatically generated"
^handle platformSizeTAt: OFFSET_LENGTH put: anObject
]
{ #category : 'printing' }
GmksFFIBorrowedBuffer >> toString [
^ (self base copyFrom: 1 to: self length) asString
]