Section Syntax
Extend Lua language: assertions, logging, switch/case, bit operations...
Summary
Return type | Function and summary |
---|---|
Deserialize(string str) Deserialize a string into object. | |
table | ElementsToTable(...) Transforme a list (multiple returned values from a function call) to a table. |
IncludeOnce(string file) Include file only and only if it hasn't been included before. | |
boolean | IsLuaClass(string className) Is className the name of a class? |
string | Serialize(obj) Serialize an object to store it as string and later deserialization. |
table | case(string _type, _params, function _func, boolean _break) Create a case for switch() function. |
table | default(function _func) Create a default case for switch() function. |
number | flag_clear(number set, number flag) Clear a bit to 0, the bit can be seen as a flag. |
number | flag_set(number set, number flag) Set a bit to 1, the bit can be seen as a flag. |
boolean | flag_test(number set, number flag) Test if a bit is set, the bit can be seen as a flag. |
string | gettagname(obj) Return, if known, the tag name of an object. |
switch(term, table cases) switch..case. | |
table_insert_multi(table t, ...) Insert multiple items in a table in one call. |
Functions
IncludeOnce(string file)
Include file only and only if it hasn't been included before.
For that, you must always use IncludeOnce instead of Include. Include
is safe and fast for simple script that create constants and functions, but may load the CPU if included file performs heavy tasks.
Parameter | Type | Default | Description |
---|---|---|---|
file | string | Filename to include, file extension is not mandatory. |
- Usage
IncludeOnce("SMStructureReader")
gettagname(obj)
Return, if known, the tag name of an object.
When a tag is created and attached to a table by settag(myTable, tag(MyClass))
, this method retrieves the MyClass name.
Parameter | Type | Default | Description |
---|---|---|---|
obj | A Lua object |
- Return
- string: The tag name,
nil
if not found. - Example
local c = Collection:new("string") print(gettagname(c)) --> "Collection" print(gettagname("Hello world")) --> "string" print(gettagname(FrontScore().FirstStaff)) --> Staff
IsLuaClass(string className)
Is className the name of a class?
Class is a table with a tag, and a :new(...) method.
Symbol, Staff or Score are not Lua class, Interval, Collection and SMDialog are.
Parameter | Type | Default | Description |
---|---|---|---|
className | string | className |
- Return
- boolean:
true
if it's a class,nil
for false if not. - Example
print(IsLuaClass("table")) --> nil print(IsLuaClass("Collection")) --> 1
switch(term, table cases)
switch..case.
Keyword switch doesn't exist in Lua? Here it is. Each case can test more things than C-like equals: equals, not equals, comparison (greater, lower), in a table, between two values (range). See SwitchCaseTest() for examples. Note that it creates tables, so switch is not best solution in huge loops where a set of if..elseif..elseif..end can do the job faster.
Parameter | Type | Default | Description |
---|---|---|---|
term | string or number, that can be compared | ||
cases | table | Table of cases created by case() and default() functions. |
- Return
- a value if cases' "body" functions return a value.
- See
- case
- default
- Usage
switch(term, { case(...), case(...), default(...) }).
- Example
-
local result = switch(my_number, { case("<", 0, function(term) print(term.." is negative") return "negative" end), case("eq", 2, function(term) print(term.." = two") return "=2" end, false), case("in", {3,5,7,11,13,17}, function(term) print(term.." is a prime") return "prime" end), case("range", {20,29}, function(term) print("20 <= "..term.." <= 29") return "between 20 and 29" end), default( function(term) print(term.." doesn't match any case, this is default") return "The answer is... 42" end) })
case(string _type, _params, function _func, boolean _break)
Create a case for switch() function.
Parameter | Type | Default | Description |
---|---|---|---|
_type | string | Accepted values are:
| |
_params | A number, a string, or a table:
| ||
_func | function | _func | |
_break | boolean | true | true to stop after this case, false to continue and execute function of next case(s) until a case breaks. |
default(function _func)
Create a default case for switch() function.
Parameter | Type | Default | Description |
---|---|---|---|
_func | function | that will receive the tested term as parameter |
flag_test(number set, number flag)
Test if a bit is set, the bit can be seen as a flag.
Parameter | Type | Default | Description |
---|---|---|---|
set | number | A sum of flags | |
flag | number | The big to test, a number power of 2. |
- Return
- boolean:
true
if flag is already set,nil
(for false) if not. - Error
- if set is not a number or if flag is not a power of 2.
- Example
Include "SMUtils" local face = FACE_ITALIC + FACE_BOLD local x = flag_test(face, FACE_UNDERLINE) --> x = nil (false) x = flag_test(face, FACE_BOLD) --> x = true
flag_set(number set, number flag)
Set a bit to 1, the bit can be seen as a flag.
Parameter | Type | Default | Description |
---|---|---|---|
set | number | A sum of flags | |
flag | number | The big to set to 1, a number power of 2. |
- Return
- number: The new value for set
- Error
- if set is not a number or if flag is not a power of 2.
- Example
Include "SMUtils" local face = FACE_ITALIC + FACE_BOLD face = flag_set(face, FACE_BOLD) --> face is unchanged, already set face = flag_set(face, FACE_UNDERLINE) --> face equals italic+bold+underline
flag_clear(number set, number flag)
Clear a bit to 0, the bit can be seen as a flag.
Parameter | Type | Default | Description |
---|---|---|---|
set | number | A sum of flags | |
flag | number | The big to clear, a number power of 2. |
- Return
- number: The new value for set
- Error
- if set is not a number or if flag is not a power of 2.
- Example
Include "SMUtils" local face = FACE_ITALIC + FACE_BOLD face = flag_clear(face, FACE_UNDERLINE) --> unchanged, underline not set face = flag_clear(face, FACE_BOLD) --> face now equals FACE_ITALIC
table_insert_multi(table t, ...)
Insert multiple items in a table in one call.
Parameter | Type | Default | Description |
---|---|---|---|
t | table | t | |
... | Undefined number of arguments |
Serialize(obj)
Serialize an object to store it as string and later deserialization.
Parameter | Type | Default | Description |
---|---|---|---|
obj | A number, string, table or a class that own :serialize(...) method. |
- Return
- string: string
- Error
- If something can't be serialized.
Deserialize(string str)
Deserialize a string into object.
Parameter | Type | Default | Description |
---|---|---|---|
str | string | str |
- Return
- Copy of previously serialized object
ElementsToTable(...)
Transforme a list (multiple returned values from a function call) to a table.
Parameter | Type | Default | Description |
---|---|---|---|
... | List of elements |
- Return
- table: Table containing all elements.
- See
- TableToElements
- Example
print(strfind("abcd", "b", 1, true)) --> 2 2 local pos = strfind("abcd", "b", 1, true) --> 2, one data lost local t = ElementsToTable(strfind("abcd", "b", 1, true)) --> {2, 2}