I added a new function to the Class Library -
Class_setConstants(ClassNameOrObject, SetConstants, Mode = "")
This allow getting / setting multiple constants at a time - ex. storing and loading of settings. It dynamically calls the Class' "constant" function (if it exists), to set the settings.
Specify the constant to get or set on a line with a leading- "``" - a single accent character (after escaping). This allows support for values that have text on multiple lines. Any line that starts with an "accent" followed by a
variable name is assumed to be the name of a constant. This only causes conflict if you have text that contains a line that starts with an "accent" followed by a "valid"
variable name (see link for what constitutes a "valid" variable name) - this should be a rare occurence.
Getting and Setting values
To set a value, specify the variable name followed by a single space, followed by the value to set.
To get a value (instead of setting it), specify the variable name alone on the line. Any leading / trailing spaces will NOT be ignored.
Update: A single trailing space is permitted. Ex. "``Variable ". This allows the following form (prevents needing two casses) -
``Variable [SetValue]. If
SetValue is omitted, the value will be retrieved, and the previous value will remain unchanged.
The easiest way to build the "set constants" value is via a
continuation section using
LTrim as an option. You can optionally use the
comments option like in the example below.
e.g.
Code:
#SingleInstance Force
;test of the SetConstants function (SetConstants_test.ahk located in the "Class examples" folder)
;includes the SetConstants class, since without it, the dynamic call fails
SetConstants_constant("")
MyValue := "Some Text"
Settings =
(LTrim Comments
;two "accent" marks are required to store a single "accent" mark
; - due to character escaping
``Value1 5 ;stores for Value1 - 5
``Value2 6 ;stores for Value2 - 6
``Quote " ;note, this will set a literal double quote into the constant
``Tab `tText ;you can also use `t for tabs
;
;additionally, variable references can be used
; this is a continuation section, after all
``Value %MyValue%
;
;You can format multi-line text as multi-line text or by using `n in the text (or a mixture of the two)
;For example, the below lines are stored as multi-line text - " Some text with a leading space`nSome more text`nYet another line"
;
;since the new lines in the set value don't start with an "accent" followed by a variable name,
; they will be seen as part of the current set value.
;
``TextWithSpace
` Some text with a leading space ;Since LTrim is used, the leading space needs to be escaped
Some more text ;this line is part of <TextWithSpace> - since it doesn't have the form: ``Variable [SetValue]
Yet another line ;this line is part of <TextWithSpace> - since it doesn't have the form: ``Variable [SetValue]
;
;another variable
``Value3 ;returns the current value - no value is set
)
MsgBox, % "Settings to store:`n`n" Settings
MsgBox, % "Previous settings (return from Class_setConstants):`n`n" . Class_setConstants("SetConstants", settings)
MsgBox, % "Current settings (return from Class_setConstants using mode = ""getOnly""):`n`n" . Class_setConstants("SetConstants", settings, "getOnly")
. "`n`nNote: the value for <quote> and <tab> didn't change because they are both read-only."
MsgBox, % "The individual values..."
MsgBox, % "value: " SetConstants_constant("value")
MsgBox, % "value1: " SetConstants_constant("value1")
MsgBox, % "value2: " SetConstants_constant("value2")
MsgBox, % "value3: " SetConstants_constant("value3")
MsgBox, % "quote: " SetConstants_constant("quote")
MsgBox, % "tab (@ used solely to mark the end): " SetConstants_constant("tab") "@"
MsgBox, % "TextWithSpace:`n`n" SetConstants_constant("TextWithSpace")
Return value
The return value is in the above form, with the constant's "set value" being the value (before the change) for the respective constant. This allows the function's return to be used to easily store and load settings.
To store the settings:
Call the
Class_setConstants function passing a list of variable names (in the above format) and specify "getOnly" (case-insensitive) as the Mode. This allows passing the same "set constants" list for both setting and getting constants. When Mode = "getOnly", no values are modified (even if there is a "set value" for the constant). However, the constant's current value will still be included in the return, as desired.
The return is formatted in the above form with the current values as the set value. This allows for easy loading of the settings by passing this return value as the parameter for
Class_setConstants. For example, you can save the return to a file. Then, to load the settings, pass to
Class_setConstants the contents of the file.
To load settings:
Call
Class_setConstants with a list of settings - in the above form. Because the return from
Class_setConstants is in the propper form, it can be used to restore a list of previous settings. This way, you can have multiple "profiles" - each in the propper from (e.g. the return from
Class_setConstants). You can then load a "profile" by passing it to
Class_setConstants.
Since the return from the Class' "constant" function is used, if the specified variable name isn't a "valid variable" (i.e. not in
ReadOnlyVariables or
ReadWriteVariables), then the empty string will be used for the constant's "set value".
Note: because the Class' "constant" function is called (and handles the getting and setting), the
Class_setConstants function will work regardless of which "version" of
Node_constant was implemented - whether the latest version or a previous one. This also means that read-only values won't be modified - the current value is still used in the return for
Class_setConstants.
Additionally, because the return of this function has the variable name as part of the form, to retrieve a single value, it would be easier to continue using the Class' "constant" function or use
Class_constant to dynamically call the Class' constant function.