Structure Question

Discuss the Firewing language

Structure Question

Postby johngb » Sun Jan 31, 2016 11:02 am

Is there a way to hold a structure as a constant so that you can address elements of the structure as:

MyStructure.text ' String
MyStructure.Length ' integer
etc...

JohnB
johngb
 
Posts: 19
Joined: Thu Dec 25, 2014 12:00 pm

Re: Structure Question

Postby Jerry Messina » Sun Jan 31, 2016 12:21 pm

If you're asking about a "const struct" with an initialiser (or even just a regular struct with an initialiser) sadly the answer is no.
I've been asking for that feature since the days of Swordfish.

Initialisers don't work with any user-defined type or structures... you have to do it in code.

Was that what you're looking for?
Jerry Messina
 
Posts: 280
Joined: Thu Feb 14, 2013 10:16 am

Re: Structure Question

Postby johngb » Sun Jan 31, 2016 12:49 pm

Yes - but not the answer I wanted.

I am writing some code to create objects like buttons check boxes progress bars etc. Many of the properties of these objects can be considered fixed so I was hoping to set these up as structures in code memory. It would make my Code Generator software http://www.firewing.info/forum/viewtopic.php?f=5&t=255 a lot simpler.
johngb
 
Posts: 19
Joined: Thu Dec 25, 2014 12:00 pm

Re: Structure Question

Postby johngb » Mon Feb 01, 2016 4:08 pm

This my approach in the absence of Const Structure
I have rationalised all my parameters to UShort and declared the pseudo structure as a Const Array
I also created an Enum containing elements which correspond to each element of the constant array
See example below:

Code: Select all
public enum ButtonProps
   Left
   Top
   Right
   Bottom
   SolidColor
   BorderColor
   PressColor
   FontColor
   FontRef
   BorderWidth
   DownVector
   UpVector
   ClickVector
end

Const MyButton(13) As UShort = 78,218,78+64,219+32,clSilver,clBlue,clGray,clBlack,Arial,1,0,0,ClickLabel


Now in my code I can use

Code: Select all
MyProperty = MyButton[ButtonProps.Property]


Does that make sense? or am I making things too complicated.
johngb
 
Posts: 19
Joined: Thu Dec 25, 2014 12:00 pm

Re: Structure Question

Postby Jerry Messina » Mon Feb 01, 2016 7:31 pm

Since all the items are the same type you can take advantage of that.
Create a structure/union to hold the button properties, and then declare a const array with the default values , maybe something like:

Code: Select all
const NumProperties = 13
public structure TButtonProps
    prop(NumProperties) as ushort   // properties array
    Left        as prop(0)          // common names (alias names for properties array)
    Top         as prop(1)
    Right       as prop(2)
    Bottom      as prop(3)
    SolidColor  as prop(4)
    BorderColor as prop(5)
    PressColor  as prop(6)
    FontColor   as prop(7)
    FontRef     as prop(8)
    BorderWidth as prop(9)
    DownVector  as prop(10)
    UpVector    as prop(11)
    ClickVector as prop(12)
end structure

// assign some dummy values for the constants so that the 'MyButtonDefaults' initialization statement will compile
const clSilver = 1
const clBlue = 2
const clGray = 3
const clBlack = 4
const Arial = 100
const ClickLabel = 200

// default button values
Const MyButtonDefaults(NumProperties) As UShort = {78,218,78+64,219+32,clSilver,clBlue,clGray,clBlack,Arial,1,0,0,ClickLabel}

' Declarations...
public dim button as TButtonProps

Sub Main()

// copy const array values to the structure 'prop' array (all 13 values)
button.prop = MyButtonDefaults

// access individual members using the union alias name
button.Left = 1
button.Right = button.Left + 99
button.FontColor = clBlue

End Sub


Not sure if that's the kind of thing you're after or not.
Jerry Messina
 
Posts: 280
Joined: Thu Feb 14, 2013 10:16 am

Re: Structure Question

Postby johngb » Mon Feb 01, 2016 8:37 pm

That looks more sensible - I'll have a go. Thanks for the help. Just need to get my head back into language, I have been working in Delphi for some time now and a little bit of Proton PDS Basic.

JohnB
johngb
 
Posts: 19
Joined: Thu Dec 25, 2014 12:00 pm

Re: Structure Question

Postby johngb » Fri Feb 05, 2016 5:49 pm

I have another question for Jerry...

Given your suggested approach where I copy the Code array into the structure.
I want to pass the Identifier of the code array to be copied to a Sub which will populate a local var containing the structure.

e.g.
Code: Select all
const NumProperties = 13
public structure TButtonProps
    prop(NumProperties) as ushort   // properties array
    Left        as prop(0)          // common names (alias names for properties array)
    Top         as prop(1)
    Right       as prop(2)
    Bottom      as prop(3)
    SolidColor  as prop(4)
    BorderColor as prop(5)
    PressColor  as prop(6)
    FontColor   as prop(7)
    FontRef     as prop(8)
    BorderWidth as prop(9)
    DownVector  as prop(10)
    UpVector    as prop(11)
    ClickVector as prop(12)
end structure

Const MyButton(NumProperties ) As UShort = 78,218,78+64,219+32,clSilver,clBlue,clGray,clBlack,Arial,1,0,0,ClickLabel
Const MySecondButton(NumProperties ) As UShort = etc...

' Call Draw button passing the Identifier in the parameter
e.g.  DrawButton(MyButton)

Public DrawButton(ByRefConst BtnName As UShort)
Dim Button As TBtnProps
Button=BtnName  ' will this copy the array of just the contents of the location?

Props.Left = 1
etc
...
end


If the line Button=BtnName is not correct what should it be?

JohnB
johngb
 
Posts: 19
Joined: Thu Dec 25, 2014 12:00 pm

Re: Structure Question

Postby Jerry Messina » Fri Feb 05, 2016 7:59 pm

Let's say you have two buttons...
Code: Select all
' Declarations...
public dim button1 as TButtonProps
public dim button2 as TButtonProps


You can say
Code: Select all
// copy the contents of button1 to button2 (structure to structure copy)
button2 = button1

and the structures (array of ushorts) will be copied

What I think you're looking for is something like this...
Code: Select all
// given a ref to a "button" object (which is actually a TButtonProps structure)
// make a local copy of it and change some of the local copy
Public sub DrawButton(ByRef pButton As TButtonProps)
   Dim Button As TButtonProps   // a local button
   
   Button = pButton  // copies the contents of the array pointed to by 'pButton' (ie Button1) to our local structure

   Button.Left = 5      // change the contents of the local button
   Button.Right = 10
   
    // you can also just use the original pointer to the passed parameter
    dim x, y as ushort
    x = pButton.Left
    y = pButton.Top

    // since the param was passed 'byref' you can even modify the original object
    // as you have a reference (ie pointer) to it
    // in this case it would modify the original 'Button1' object
    pButton.Left = 1000
end sub

// pass a reference to Button1 to the DrawButton routine
// (this passes the address of the button1 structure)
DrawButton(button1)


Since button1 and button2 are objects in ram you use 'byref' to pass the addresses... 'byrefconst' would be used to pass the address of a ROM object, like the const arrays we used to initialize the ram structures for example.
Jerry Messina
 
Posts: 280
Joined: Thu Feb 14, 2013 10:16 am

Re: Structure Question

Postby johngb » Sat Feb 06, 2016 11:01 am

On re-reading what you said originally, I get it and realise my last question was wrong. Just to explain

I am looking at typical embedded applications which use an interactive screen to control or report information about the system behind it. With this in mind I envisage Objects which drawn on the screen will be essentially static. Typically they would have a number of states or display different text of image. Thus many of the properties of the object will remain static. I will hold any variable properties in a separate array. My plan was to hold all these static properties in Code memory and in my Paint Screen routine create a set of property variables for each object on that screen. These will be local variables which will be recycled when moving to another screen.

Does all that make sense to you?

JohnB
johngb
 
Posts: 19
Joined: Thu Dec 25, 2014 12:00 pm

Re: Structure Question

Postby Jerry Messina » Sat Feb 06, 2016 12:42 pm

Ok, then perhaps you'd want another version of DrawButton that would work with the const array...
Code: Select all
Public sub DrawButton(ByRefconst pButton() As ushort)
   Dim Button As TButtonProps   // a local button
   
   Button.prop = pButton  // copies the const array pointed to by 'pButton' to our local structure

   Button.Left = 5      // change the contents of the local button
   Button.Right = 10
   
    // you can also just use the original pointer to the passed parameter
    dim x, y as ushort
    x = pButton(0)
    y = pButton(1)
end sub


Then you can say
Code: Select all
DrawButton(MyButton)
DrawButton(MySecondButton)


Firewing supports overloaded function names, so you can have both versions of DrawButton and it'll figure out which one you want based on the parameters you call it with.
Jerry Messina
 
Posts: 280
Joined: Thu Feb 14, 2013 10:16 am

Next

Return to Language

Who is online

Users browsing this forum: No registered users and 2 guests

cron

x