[Cuis-dev] Unions inside structs

Juan Vuletich juan at cuis.st
Wed Dec 17 06:09:13 PST 2025


Hi Pocho,

What follows is Felipe Zak's private answer (thanks Felipe!).

----

Hi (Javier Pimas), UnionExample.st is an example with a union in Cuis.

An example of a union structure in object memory:

UnionExample new.

(the single handle -aByteArray- changes depending on what is set)


An example in external heap:

UnionExample externalNew.


Then, used that union in another stucture (StructureExampleWithUnion.st):

Hope this helps!


On 2025-12-15 2:46 PM, Javier Pimás via Cuis-dev wrote:
> Hi folks,
>
> I'm trying to define a wrapper (in cuis) of the following C struct:
>
> typedef struct InteropData {
>     uint64_t dataType;
>     union {
>         double quad;
>         int32_t int32;
>         uint32_t uint32;
>         int64_t int64;
>         uint64_t uint64;
>         char16_t *wstr;
>         void *ptr;
>     };
> } InteropData;  // size 24
>
> for now I made an ExternalStructure subclass with the following method:
>
> InteropData class >> #fields
>     "
>     self defineFields
>     "
>     ^#(
>         (dataType 'uint64')
>         (payload 'uint64')
>     )
>
> As you can see, the payload is a bag of cats, and I wonder if there's 
> a way to specify the union fields so that they get generated 
> automatically.
>
> Cheers,
> Javier
>
>
-- 
Juan Vuletich
www.cuis.st
github.com/jvuletich
researchgate.net/profile/Juan-Vuletich
independent.academia.edu/JuanVuletich
patents.justia.com/inventor/juan-manuel-vuletich
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.cuis.st/mailman/archives/cuis-dev/attachments/20251217/f6e67883/attachment-0001.htm>
-------------- next part --------------
A non-text attachment was scrubbed...
Name: Screenshot 2025-12-16 at 4.21.31?AM.png
Type: image/png
Size: 95166 bytes
Desc: not available
URL: <http://lists.cuis.st/mailman/archives/cuis-dev/attachments/20251217/f6e67883/attachment-0003.png>
-------------- next part --------------
A non-text attachment was scrubbed...
Name: Screenshot 2025-12-16 at 4.24.08?AM.png
Type: image/png
Size: 97720 bytes
Desc: not available
URL: <http://lists.cuis.st/mailman/archives/cuis-dev/attachments/20251217/f6e67883/attachment-0004.png>
-------------- next part --------------
A non-text attachment was scrubbed...
Name: Screenshot 2025-12-16 at 5.12.27?AM.png
Type: image/png
Size: 117700 bytes
Desc: not available
URL: <http://lists.cuis.st/mailman/archives/cuis-dev/attachments/20251217/f6e67883/attachment-0005.png>
-------------- next part --------------
'From Cuis7.5 [latest update: #9999] on 16 December 2025 at 4:13:51 am'!
!classDefinition: #UnionExample category: #example!
ExternalUnion subclass: #UnionExample
	instanceVariableNames: ''
	classVariableNames: ''
	poolDictionaries: ''
	category: 'example'!

!UnionExample methodsFor: 'accessing' stamp: ''!
floatValue
	"This method was automatically generated. See UnionExample class>>fields."
	<generated>
	^handle floatAt: 1! !

!UnionExample methodsFor: 'accessing' stamp: ''!
floatValue: anObject
	"This method was automatically generated. See UnionExample class>>fields."
	<generated>
	handle floatAt: 1 put: anObject! !

!UnionExample methodsFor: 'accessing' stamp: ''!
intValue
	"This method was automatically generated. See UnionExample class>>fields."
	<generated>
	^handle int32At: 1! !

!UnionExample methodsFor: 'accessing' stamp: ''!
intValue: anObject
	"This method was automatically generated. See UnionExample class>>fields."
	<generated>
	handle int32At: 1 put: anObject! !

"-- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- "!

!classDefinition: 'UnionExample class' category: #example!
UnionExample class
	instanceVariableNames: ''!

!UnionExample class methodsFor: 'fields' stamp: 'fgz 12/16/2025 04:05:23'!
fields
	"UnionExample defineFields"
	
	^#( (#intValue 'int32') (#floatValue 'float') )! !
-------------- next part --------------
'From Cuis7.5 [latest update: #9999] on 16 December 2025 at 5:10:38 am'!
!classDefinition: #StructureExampleWithUnion category: #'FFI-Kernel'!
ExternalStructure subclass: #StructureExampleWithUnion
	instanceVariableNames: ''
	classVariableNames: ''
	poolDictionaries: ''
	category: 'FFI-Kernel'!

!StructureExampleWithUnion methodsFor: 'accessing' stamp: ''!
charValue
	"This method was automatically generated. See StructureExampleWithUnion class>>fields."
	<generated>
	^handle unsignedCharAt: 1! !

!StructureExampleWithUnion methodsFor: 'accessing' stamp: ''!
charValue: anObject
	"This method was automatically generated. See StructureExampleWithUnion class>>fields."
	<generated>
	handle unsignedCharAt: 1 put: anObject! !

!StructureExampleWithUnion methodsFor: 'accessing' stamp: ''!
floatOrIntValue
	"This method was automatically generated. See StructureExampleWithUnion class>>fields."
	<generated>
	^UnionExample fromHandle: (handle structAt: 2 length: 4)! !

!StructureExampleWithUnion methodsFor: 'accessing' stamp: ''!
floatOrIntValue: anObject
	"This method was automatically generated. See StructureExampleWithUnion class>>fields."
	<generated>
	handle structAt: 2 put: anObject getHandle length: 4! !

"-- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- "!

!classDefinition: 'StructureExampleWithUnion class' category: #'FFI-Kernel'!
StructureExampleWithUnion class
	instanceVariableNames: ''!

!StructureExampleWithUnion class methodsFor: 'fields' stamp: 'fgz 12/16/2025 04:55:31'!
fields
	"StructureExampleWithUnion defineFields"
	
	^#( (#charValue 'char') (#floatOrIntValue 'UnionExample') )! !


More information about the Cuis-dev mailing list