Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add RETDEF() Macro, Improve RETNIL(), fix IS_INTEGER() #537

Merged
merged 4 commits into from Oct 29, 2016
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
29 changes: 25 additions & 4 deletions addons/main/script_macros_common.hpp
Expand Up @@ -365,13 +365,34 @@ Macro: RETNIL()
Example:
(begin example)
// _var is undefined
hintSilent format ["_var=%1", RETNIL(_var) ]; // "_var=any"
hintSilent format ["_var=%1", RETNIL(_var)]; // "_var=any"
(end example)

Author:
Alef (see CBA issue #8514)
------------------------------------------- */
#define RETNIL(VARIABLE) if (isNil{VARIABLE}) then {nil} else {VARIABLE}
#define RETNIL(VARIABLE) ([VARIABLE] param [0])

/* -------------------------------------------
Macro: RETDEF()
If a variable is undefined, return the default value. Otherwise, return the
variable itself.

Parameters:
VARIABLE - the variable to check
DEFAULT_VALUE - the default value to use if variable is undefined

Example:
(begin example)
// _var is undefined
hintSilent format ["_var=%1", RETDEF(_var,5)]; // "_var=5"
_var = 7;
hintSilent format ["_var=%1", RETDEF(_var,5)]; // "_var=7"
(end example)
Author:
654wak654
------------------------------------------- */
#define RETDEF(VARIABLE,DEFAULT_VALUE) ([VARIABLE] param [0,DEFAULT_VALUE])

/* -------------------------------------------
Macros: TRACE_n()
Expand Down Expand Up @@ -1039,7 +1060,7 @@ Macros: IS_x()
Author:
Spooner
------------------------------------------- */
#define IS_META_SYS(VAR,TYPE) (if (isNil {VAR}) then { false } else { (VAR) isEqualType TYPE })
#define IS_META_SYS(VAR,TYPE) (if (isNil {VAR}) then {false} else {(VAR) isEqualType TYPE})
#define IS_ARRAY(VAR) IS_META_SYS(VAR,[])
#define IS_BOOL(VAR) IS_META_SYS(VAR,false)
#define IS_CODE(VAR) IS_META_SYS(VAR,{})
Expand All @@ -1057,7 +1078,7 @@ Macros: IS_x()

#define IS_BOOLEAN(VAR) IS_BOOL(VAR)
#define IS_FUNCTION(VAR) IS_CODE(VAR)
#define IS_INTEGER(VAR) if ( IS_SCALAR(VAR) ) then { (floor(VAR) == (VAR)) } else { false }
#define IS_INTEGER(VAR) (if (IS_SCALAR(VAR)) then {floor (VAR) == (VAR)} else {false})
#define IS_NUMBER(VAR) IS_SCALAR(VAR)

#define FLOAT_TO_STRING(num) (str parseNumber (str (_this%_this) + str floor abs _this) + "." + (str (abs _this-floor abs _this) select [2]) + "0")
Expand Down