[View]  [Edit]  [Lock]  [References]  [Attachments]  [History]  [Home]  [Changes]  [Search]  [Help] 

api[node-ffi] 02 S8Struct

NativeObject
 subclass: #S8Struct
 category: #S8Struct!
S8Struct comment: '
	Implement native object access to C structures
	@2016 Alejandro Reimondo - [email protected]
	See https://github.com/TooTallNate/ref-struct
'!

! S8Struct class methodsFor: #private !
s8StructFn

	| result |
	[	result := self require: #Struct
	] on: Error do: [
		result := self require: #S8Struct
	].
	^result! !

! S8Struct class methodsFor: #type !
type: mapping
	" Returns a type structure with mapping. "

	mapping isArray ifTrue: [
		^self type: (ComposedAssociations withAll:
			(mapping collect: [:tuple| tuple first -> tuple last ]))
	].
	^self s8StructFn value: mapping json! !

! S8Struct class methodsFor: #instantiation !
new: type
	" Returns an instance of the receiver of specified type. "

	(self typeOf: type) = #function ifFalse: ["it is assumed to be a mapping"
		^self new: (self type: type)
	].
	^self @ type value! !

! S8Struct class methodsFor: #instantiation !
type: type values: values
	" Returns an instance of the receiver of specified type filled from values. "

	(self typeOf: type) = #function ifFalse: ["it is assumed to be a mapping"
		^self type: (self type: type) values: values
	].
	^self @ (type value: values json)! !

! S8Struct methodsFor: #accessing !
at: fieldName
	^self @ fieldName! !

! S8Struct methodsFor: #accessing !
at: fieldName put: anObject

	self handle
		basicAt: fieldName
		put: ((self isObject: anObject)
			ifTrue: [anObject json]
			ifFalse: [anObject])! !

! S8Struct methodsFor: #accessing !
ref
	^S8Ref @ handle ref! !