Section Syntax

Extend Lua language: assertions, logging, switch/case, bit operations...

See
http://lua-users.org/wiki/SwitchStatement

Summary

Return typeFunction and summary
 Deserialize(string str)
Deserialize a string into object.
tableElementsToTable(...)
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.
booleanIsLuaClass(string className)
Is className the name of a class?
stringSerialize(obj)
Serialize an object to store it as string and later deserialization.
tablecase(string _type, _params, function _func, boolean _break)
Create a case for switch() function.
tabledefault(function _func)
Create a default case for switch() function.
numberflag_clear(number set, number flag)
Clear a bit to 0, the bit can be seen as a flag.
numberflag_set(number set, number flag)
Set a bit to 1, the bit can be seen as a flag.
booleanflag_test(number set, number flag)
Test if a bit is set, the bit can be seen as a flag.
stringgettagname(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.

ParameterTypeDefaultDescription
filestring 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.

ParameterTypeDefaultDescription
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.

ParameterTypeDefaultDescription
classNamestring 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.

ParameterTypeDefaultDescription
term  string or number, that can be compared
casestable 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.

ParameterTypeDefaultDescription
_typestring Accepted values are:
  • "=", "==", "eq" for equals,
  • "~=", "!=", "~eq", "!eq" for not equals,
  • "<", "<=", ">", ">=", "lt", "le", "gt", "ge" for comparison,
  • "in", "!in" to check if in an array of values,
  • "range", "!range" to check if between two values.
_params  A number, a string, or a table:
  • For "in" and "!in", table can have 1 to n items.
  • For "range" and "!range"; table must have 2 items.
_funcfunction _func
_breakbooleantruetrue to stop after this case, false to continue and execute function of next case(s) until a case breaks.
Return
table: a case for switch() function
See
switch
switch

default(function _func)

Create a default case for switch() function.

ParameterTypeDefaultDescription
_funcfunction that will receive the tested term as parameter
Return
table: a case for switch() function
See
switch
switch

flag_test(number set, number flag)

Test if a bit is set, the bit can be seen as a flag.

ParameterTypeDefaultDescription
setnumber A sum of flags
flagnumber 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.

ParameterTypeDefaultDescription
setnumber A sum of flags
flagnumber 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.

ParameterTypeDefaultDescription
setnumber A sum of flags
flagnumber 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.

ParameterTypeDefaultDescription
ttable t
...  Undefined number of arguments

Serialize(obj)

Serialize an object to store it as string and later deserialization.

ParameterTypeDefaultDescription
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.

ParameterTypeDefaultDescription
strstring str
Return
Copy of previously serialized object

ElementsToTable(...)

Transforme a list (multiple returned values from a function call) to a table.

ParameterTypeDefaultDescription
...  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}