Attributes

BlitzMax Forums/BlitzMax Programming/Attributes

BLaBZ(Posted 2013) [#1]
Does blitzMAX supports attributes like those seen in c#?

IE is there a way to tag classes and methods with information for other applications that will be using the module?

Thanks!


Yasha(Posted 2013) [#2]
It supports metadata, which isn't the same thing but perhaps as close as you can get using builtin features (I might have got the syntax wrong here):

Type Foo { FooAttr }
    Field a    { deprecated }
    Field x    { status="untested" }
End Type


You can read metadata using the reflection module. It doesn't "do" anything unless you write code to seek out and act on it. Some people have written modules that can make use of it though, e.g. to mark methods for automated testing or profiling.


Hardcoal(Posted 2013) [#3]
Wth are this {…} metadata.. first time i see it.
Please. Down to earth explanation.
Thnks )


BLaBZ(Posted 2013) [#4]
This is exactly what I'm looking for!

@Hardcoal - Metadata allows you to provide information about types, fields, and methods that may be useful for other applications that use the module.

For example - Say you have a module with a class with fields that are being populated by a GUI form. You have some fields that you want to be represented as checkboxes on a GUI form. When the user checks the box this field will be set to true and when its uncheck then to false.

One approach is to do this manually. create a checkbox for each field check for events from the control, on the onchange event check to see if the box is checked or not and set the field in our class accordingly.

By using metadata, the gui can use reflection to read all of the fields with specific metadata (ie { IsCheckBox = 1 }) and automatically create create the checkbox and handle the events(you code this once).

Now our GUI app knows how to read the Metadata and what it should do based on what metadata each class, field, or method has.

Whenever we tag a new field with { IsCheckBox = 1 } our form will automatically create a checkbox for that field.

Does that make sense?


GfK(Posted 2013) [#5]
No.

What is this witchcraft? Can you post a working example?


BLaBZ(Posted 2013) [#6]
In this example everything is contained within the same file. Typically, you would have another application reading the metadata within a module.

Just add a { CheckBox } metadata tag to one of the byte fields and it shows up on the form. The user can then change the value of the field by checking the box.

SuperStrict

Import maxgui.maxgui
Import maxgui.drivers

Local instance:Class = New Class

Local wnd:TGadget = CreateWindow("test", 100, 100, 300, 400, Null, WINDOW_TITLEBAR)

'Create checkboxes based on the metadata of our class
Local id:TTypeId = TTypeId.ForObject(instance)
Local y:Int = 5
For Local fld:TField = EachIn id.EnumFields()
	If fld.MetaData() = "CheckBox=1"
		Local but:TGadget = CreateButton(fld.Name(), 5, y, 200, 20, wnd, BUTTON_CHECKBOX)
		but.context = fld
		y:+20
	EndIf
Next

Repeat

	Select WaitEvent()
		Case EVENT_GADGETACTION
			Local e:Object = EventSource()
			If TGadget(e)
				Local g:TGadget = TGadget(e)
				If ButtonState(g)
					Local fld:TField = TField(g.context)
					fld.SetInt(instance, 1)
				Else
					Local fld:TField = TField(g.context)
					fld.SetInt(instance, 0)
				End If
				
				'Notify us of all "instance's" field values
				Local str:String
				For Local fld:TField = EachIn id.EnumFields()
					str:+fld.Name() + " " + fld.GetInt(instance) + "~n"
				Next
				Notify str
			End If
		Case EVENT_WINDOWCLOSE
			End
			
	End Select
	

Forever


'To show checkbox and give the ability to automatically change a fields value just add the { CheckBox } metadata tag
Type Class

	Field bit1:Byte { CheckBox }
	Field bit2:Byte
	Field bit3:Byte { CheckBox }
	Field bit4:Byte

End Type



Hardcoal(Posted 2013) [#7]
Ok i just learnt new stuff.
Gfk is right! One code sample worth 1000 explanations.