|
| 1 | +--- |
| 2 | +title: Types |
| 3 | +description: How to use the helpers module. |
| 4 | +keywords: helpers, cmake |
| 5 | +author: RSP Systems A/S |
| 6 | +--- |
| 7 | + |
| 8 | +# Type Utilities |
| 9 | + |
| 10 | +The following helper functions can help you distinguish between the value type¹ of variables. |
| 11 | + |
| 12 | +¹: _In CMake all variable values are strings. See [official documentation](https://cmake.org/cmake/help/latest/manual/cmake-language.7.html#variables) for details._ |
| 13 | + |
| 14 | +[TOC] |
| 15 | + |
| 16 | +## `is_int` |
| 17 | + |
| 18 | +Determines if value is an integer. |
| 19 | + |
| 20 | +```cmake |
| 21 | +is_int(22 a) |
| 22 | +is_int("hi there" b) |
| 23 | +
|
| 24 | +message("${a}") # true |
| 25 | +message("${b}") # false |
| 26 | +``` |
| 27 | + |
| 28 | +## `is_float` |
| 29 | + |
| 30 | +Determines if value is a float point. |
| 31 | + |
| 32 | +```cmake |
| 33 | +is_float(2.34 a) |
| 34 | +is_float(2 b) |
| 35 | +is_int("hi there" c) |
| 36 | +
|
| 37 | +message("${a}") # true |
| 38 | +message("${b}") # false |
| 39 | +message("${c}") # false |
| 40 | +``` |
| 41 | + |
| 42 | +## `is_numeric` |
| 43 | + |
| 44 | +Determines if value is numeric. |
| 45 | + |
| 46 | +```cmake |
| 47 | +is_numeric(22 a) |
| 48 | +is_numeric(1.234 b) |
| 49 | +is_numeric("hi there" c) |
| 50 | +
|
| 51 | +message("${a}") # true |
| 52 | +message("${b}") # true |
| 53 | +message("${c}") # false |
| 54 | +``` |
| 55 | + |
| 56 | +## `is_bool` |
| 57 | + |
| 58 | +Determines if value is a boolean (_`true` or `false`_). |
| 59 | + |
| 60 | +```cmake |
| 61 | +is_bool(true a) |
| 62 | +is_bool(false b) |
| 63 | +is_bool("on" c) |
| 64 | +
|
| 65 | +message("${a}") # true |
| 66 | +message("${b}") # true |
| 67 | +message("${c}") # false |
| 68 | +``` |
| 69 | + |
| 70 | +## `is_bool_like` |
| 71 | + |
| 72 | +Determine if value is a boolean like. |
| 73 | +See CMake's official documentation for additional details: |
| 74 | + |
| 75 | +* [constant](https://cmake.org/cmake/help/latest/command/if.html#constant) |
| 76 | +* [logic operators](https://cmake.org/cmake/help/latest/command/if.html#logic-operators) |
| 77 | + |
| 78 | + |
| 79 | +```cmake |
| 80 | +is_bool_like(true a) |
| 81 | +is_bool_like("yes" b) |
| 82 | +is_bool_like(-2 c) |
| 83 | +
|
| 84 | +message("${a}") # true |
| 85 | +message("${b}") # true |
| 86 | +message("${c}") # false |
| 87 | +``` |
| 88 | + |
| 89 | +## `is_list` |
| 90 | + |
| 91 | +Determines if value is a list of values. |
| 92 | +See CMake's official [documentation](https://cmake.org/cmake/help/latest/manual/cmake-language.7.html#cmake-language-lists) |
| 93 | +for additional details. |
| 94 | + |
| 95 | +```cmake |
| 96 | +is_list("aa;bbb;ccc" a) |
| 97 | +
|
| 98 | +set(my_list "aa;bbb;ccc") |
| 99 | +is_list(my_list b) |
| 100 | +
|
| 101 | +set(my_other_list aa bbb ccc) |
| 102 | +is_list(my_other_list c) |
| 103 | +
|
| 104 | +is_list("aa bbb ccc" d) |
| 105 | +
|
| 106 | +message("${a}") # true |
| 107 | +message("${b}") # true |
| 108 | +message("${c}") # true |
| 109 | +message("${d}") # false |
| 110 | +``` |
| 111 | + |
| 112 | +## `is_command` |
| 113 | + |
| 114 | +Determines if value is command, [macro](https://cmake.org/cmake/help/latest/command/macro.html#macro) or |
| 115 | +[function](https://cmake.org/cmake/help/latest/command/function.html#command:function). |
| 116 | + |
| 117 | +```cmake |
| 118 | +macro(my_macro) |
| 119 | +endmacro() |
| 120 | +is_command(my_macro a) |
| 121 | +
|
| 122 | +function(my_function) |
| 123 | +endfunction() |
| 124 | +is_command(my_function b) |
| 125 | +
|
| 126 | +set(my_var "") |
| 127 | +is_command(my_var c) |
| 128 | +
|
| 129 | +message("${a}") # true |
| 130 | +message("${b}") # true |
| 131 | +message("${c}") # false |
| 132 | +``` |
| 133 | + |
| 134 | +## `is_string` |
| 135 | + |
| 136 | +Determines if value is a string. |
| 137 | +In this context, a value is considered to be a string, if it is: |
| 138 | + |
| 139 | +* not [numeric](#is_numeric) |
| 140 | +* not [boolean](#is_bool) (_`true` or `false`_) |
| 141 | +* not a [list](#is_list) (_semicolon separated list_) |
| 142 | +* not a [command](#is_command) |
| 143 | + |
| 144 | +```cmake |
| 145 | +is_string("abc" a) |
| 146 | +
|
| 147 | +set(my_str "foo") |
| 148 | +is_string(my_str b) |
| 149 | +
|
| 150 | +is_string(42 c) |
| 151 | +
|
| 152 | +message("${a}") # true |
| 153 | +message("${b}") # true |
| 154 | +message("${c}") # false |
| 155 | +``` |
| 156 | + |
| 157 | +## `get_type` |
| 158 | + |
| 159 | +Determines the type of given value. |
| 160 | +Function assigns a string representation of the value's type (_int, float, bool, list, command, or string_), to |
| 161 | +the `output` argument. |
| 162 | + |
| 163 | +* See [`is_int()`](#is_int) |
| 164 | +* See [`is_float()`](#is_float) |
| 165 | +* See [`is_bool()`](#is_bool) |
| 166 | +* See [`is_list()`](#is_list) |
| 167 | +* See [`is_command()`](#is_command) |
| 168 | +* See [`is_string()`](#is_string) |
| 169 | + |
| 170 | +```cmake |
| 171 | +get_type("abc" a) |
| 172 | +get_type("aaa;bbb;ccc" b) |
| 173 | +get_type(true c) |
| 174 | +get_type(12 d) |
| 175 | +
|
| 176 | +message("${a}") # string |
| 177 | +message("${b}") # list |
| 178 | +message("${c}") # bool |
| 179 | +message("${c}") # int |
| 180 | +``` |
0 commit comments