Page 1 of 1
Custom types
Posted: Wed Jul 30, 2025 3:48 pm
by Joubarbe
So you might not know that, but you can name your enumerations:
Code: Select all
Enumeration CUSTOM_TYPE
#CUSTOM_TYPE_1
#CUSTOM_TYPE_2
EndEnumeration
But unlike some other languages, you cannot use that as a custom type, ie. you cannot force values to be either #CUSTOM_TYPE_1 or #CUSTOM_TYPE_2. However, you can use macros to do that:
Code: Select all
Enumeration CUSTOM_TYPE
#CUSTOM_TYPE_1
#CUSTOM_TYPE_2
EndEnumeration
Macro CUSTOM_TYPE : i : EndMacro
Structure _Struc
int.i
custom.CUSTOM_TYPE
EndStructure
Procedure WriteCustom(custom.CUSTOM_TYPE)
; Something.
EndProcedure
Which I find pretty cool. When you have a big program with a ton of constants, you can easily identify what constants you need to use when you call a function or when you assign a value to a structured variable.
Re: Custom types
Posted: Wed Jul 30, 2025 11:45 pm
by idle
thanks it's a good tip and is useful when importing large c libs
Re: Custom types
Posted: Thu Jul 31, 2025 6:53 am
by infratec
I use this since ... a long time ago,
for C imports:
Code: Select all
Enumeration pjsip_inv_state
#PJSIP_INV_STATE_NULL ; Before INVITE is sent Or received
#PJSIP_INV_STATE_CALLING ; After INVITE is sent
#PJSIP_INV_STATE_INCOMING ; After INVITE is received.
#PJSIP_INV_STATE_EARLY ; After response With To tag.
#PJSIP_INV_STATE_CONNECTING ; After 2xx is sent/received.
#PJSIP_INV_STATE_CONFIRMED ; After ACK is sent/received.
#PJSIP_INV_STATE_DISCONNECTED ; Session is terminated.
EndEnumeration
Macro pjsip_inv_state
l
EndMacro

Re: Custom types
Posted: Thu Jul 31, 2025 2:11 pm
by ebs
Thank you - this is
really clever!
As you said, it doesn't enforce the use of the correct type, but it makes your code much more self-documenting.
It makes the C programmer in me very happy!

Re: Custom types
Posted: Thu Jul 31, 2025 2:33 pm
by Axolotl
Thanks for sharing.
Indeed Marcos are very helpful as workaround solutions.
Fun Fact: I recently made a wish.
Enums as a Type
Re: Custom types
Posted: Thu Jul 31, 2025 2:44 pm
by Axolotl
infratec wrote: Thu Jul 31, 2025 6:53 am
...
I use this since ... a long time ago,
for C imports:
Code: Select all
Enumeration pjsip_inv_state
....
EndEnumeration
@infratec, is your code published in the forum?
The search function is a mess these days.
Re: Custom types
Posted: Thu Jul 31, 2025 4:23 pm
by infratec
No, this code is not public.
And I'm definately not allowed to make this public.