object classes: storing data outside of an object

Get help with using AutoHotkey (v1.1 and older) and its commands and hotkeys
User avatar
jeeswg
Posts: 6902
Joined: 19 Dec 2016, 01:58
Location: UK

object classes: storing data outside of an object

02 May 2018, 15:31

- 'Hidden keys'. What is the best way to store information in an object, that is not accessible via obj.key?
- There are reasons why it might be better to store an object's data somewhere 'outside' of itself, and not inside its keys:
- To take advantage of the get/set meta-functions every time a key is gotten/set. Ordinarily the get/set meta-functions are only called if a key does not yet exist.
- To avoid clashes relating to keys/methods sharing the same name.
- You may want the majority of keys to be shown during a for loop, but some keys (used to contain raw data or used for calculations) to be omitted from the for loop.
- You want certain keys to be read-only, so that the user doesn't accidentally edit the contents, but you need the keys to be editable by the object class.

- Here is *an* attempt at solving some of these problems, I would be interested in any other solutions that people would have to offer. It makes use of a global object to store data for all objects of a particular custom object class.

Code: Select all

q:: ;objects - store data outside the object
oArray1 := new MyDataStoreClass
oArray1.key := "value"
MsgBox, % oArray1.key

oArray2 := new MyDataStoreClass
oArray2.key := "value"
MsgBox, % oArray2.key
return

global oGlobalStore
MyDataStoreClassInit()
{
	static vDummy := MyDataStoreClassInit()
	oGlobalStore := {}
}
class MyDataStoreClass
{
	__Get(vKey)
	{
		vAddr := &this
		return oGlobalStore[vAddr, vKey]
	}
	__Set(vKey, vValue)
	{
		vAddr := &this
		if !oGlobalStore.HasKey(vAddr)
			oGlobalStore[vAddr] := {}
		oGlobalStore[vAddr, vKey] := vValue
		return
	}
	__Delete()
	{
		vAddr := &this
		oGlobalStore[vAddr].Delete(vAddr)
		MsgBox, % "object deleted: " vAddr
	}
}
homepage | tutorials | wish list | fun threads | donate
WARNING: copy your posts/messages before hitting Submit as you may lose them due to CAPTCHA
User avatar
nnnik
Posts: 4500
Joined: 30 Sep 2013, 01:01
Location: Germany

Re: object classes: storing data outside of an object

03 May 2018, 01:38

Generally speaking your method is the one I most commonly use.
You could do minor changes by storing the data inside the base instead of any outside globals.
Get and Set is the only thing where it is really needed though.
You can always rename keys.
Also don't enumerate over your base object - rather than that put the keys you want enumerated over in a separate array - you can either create that array on request or use it for storage purposes.
To make keys readonly you could use either a property or a method.
Using objects raw not as classes is not very useful and accessing their keys directly might cause issues.
Recommends AHK Studio
Helgef
Posts: 4709
Joined: 17 Jul 2016, 01:02
Contact:

Re: object classes: storing data outside of an object

03 May 2018, 02:13

You need to check haskey in __delete (for v2), you do not need it in __set for either v1 or v2.

Cheers.
User avatar
jeeswg
Posts: 6902
Joined: 19 Dec 2016, 01:58
Location: UK

Re: object classes: storing data outside of an object

03 May 2018, 09:59

- Is it simple to store/retrieve data in/from the base object? I can't recall how to do it/if it's possible.
- Even with the new ObjGetBase/ObjSetBase functions that are coming, that gets/sets the entire base object cf. editing one of its keys. Thanks.
- @nnnik: By rename, I suppose you mean oArray.Key2 := oArray.Key1, oArray.Delete("Key1")?
- @Helgef: Thanks re. the AHK v2 pointer. When I fully move to AHK v2, resolving things like that is going to be 'interesting'.
homepage | tutorials | wish list | fun threads | donate
WARNING: copy your posts/messages before hitting Submit as you may lose them due to CAPTCHA
Helgef
Posts: 4709
Joined: 17 Jul 2016, 01:02
Contact:

Re: object classes: storing data outside of an object

03 May 2018, 10:04

Is it simple to store/retrieve data in/from the base object? I can't recall how to do it/if it's possible.
Myfavouritebaseobject.mykey:=myvalue. Getting back the value is left as an exercise for the interested reader. :happybday:
User avatar
jeeswg
Posts: 6902
Joined: 19 Dec 2016, 01:58
Location: UK

Re: object classes: storing data outside of an object

03 May 2018, 10:34

- Oh right, all objects of a particular class would make use of (share) the same object stored within the class object. I think I used that technique a lot in my backport AHK v2 GUI to AHK v1 project.
- Is it my birthday? Wrong emoticon? Cheers.
homepage | tutorials | wish list | fun threads | donate
WARNING: copy your posts/messages before hitting Submit as you may lose them due to CAPTCHA
User avatar
nnnik
Posts: 4500
Joined: 30 Sep 2013, 01:01
Location: Germany

Re: object classes: storing data outside of an object

03 May 2018, 12:09

No by rename I mean to avoid collisions when naming things.
Recommends AHK Studio

Return to “Ask for Help (v1)”

Who is online

Users browsing this forum: Aqualest, Google [Bot], peter_ahk and 328 guests