Loading callback: How to pass a user variable?

Just starting out? Need help? Post your questions and find answers here.
User avatar
Kurzer
Posts: 96
Joined: Mon May 26, 2014 9:33 am

Loading callback: How to pass a user variable?

Post by Kurzer »

Hello colleagues,

in SpiderBasic the loading of data must be realized with a callback and I need a hint how to pass a structure variable to the loading callback within a module.

I work on a module which works with AllocateStructur() and each procedure of the module gets the address of the allocated structure passed by the caller.

This is also the case with the planned JSON load function. My problem is, that inside the CallBack procedure the adress of the structure, which is needed to process the JSON file, is not known. I would have to pass it to the CallBack function, but I don't know how to do that.

I do not want to use a temporary global variable inside the module, because this can lead to data errors, if the load function is called several times from different callers.

In Purebasic the whole thing is just a "two-liner" which does not cause any problems:

Code: Select all

; PureBasic
LoadJSON(0, "mystuff.json")
ExtractJSONStructure(JSONValue(0), *StructPointer, myStruct)
Here is a code fragment, which is necessary under SpiderBasic.

Code: Select all

Procedure LoadOk(iType.i, sFilename.s, iObjectId.i)
	If iType = #PB_Loading_JSON
		Debug "JSON Loaded"
		Debug sFilename
		Debug iObjectId
		ExtractJSONStructure(JSONValue(iObjectId), *StructPointer, myStruct)
	EndIf
EndProcedure

Procedure LoadError(iType.i, sFilename.s, iObjectId.i)
	If iType = #PB_Loading_JSON
		Debug "JSON Loading Error"
		Debug sFilename
		Debug iObjectId
	EndIf
EndProcedure

Procedure.i LoadStuff(*StructPointer.myStruct, sFilename.s)
	If *StructPointer <> 0
		BindEvent(#PB_Event_Loading, @LoadOk())
		BindEvent(#PB_Event_LoadingError, @LoadError())
		ProcedureReturn LoadJSON(#PB_Any, sFilename)
	EndIf
EndProcedure
How do I get *StructPointer into the procedure LoadOk() ?

Many thanks for your tips.

Markus
SB 2.32 x86, Browser: Iron Portable V. 88.0.4500.0 (Chromium based), User age in 2023: 55y
"Happiness is a pet." | "Never run a changing system!"
the.weavster
Posts: 229
Joined: Sat Mar 01, 2014 3:02 pm

Re: Loading callback: How to pass a user variable?

Post by the.weavster »

kurzer wrote:I do not want to use a temporary global variable inside the module, because this can lead to data errors, if the load function is called several times from different callers.
How about using a Map with the filename as the key?
User avatar
Kurzer
Posts: 96
Joined: Mon May 26, 2014 9:33 am

Re: Loading callback: How to pass a user variable?

Post by Kurzer »

Wow, this is a remarkable method. :shock: But it would work, I suppose. Thanks for the food for thought.

Meanwhile, I've moved the loading part of the file out of my module. The user who is going to use the module has to take care of it.
SB 2.32 x86, Browser: Iron Portable V. 88.0.4500.0 (Chromium based), User age in 2023: 55y
"Happiness is a pet." | "Never run a changing system!"
Post Reply