1 /* 2 * Copyright (C) 2006-2019 Christopho, Solarus - http://www.solarus-games.org 3 * 4 * Solarus is free software; you can redistribute it and/or modify 5 * it under the terms of the GNU General Public License as published by 6 * the Free Software Foundation, either version 3 of the License, or 7 * (at your option) any later version. 8 * 9 * Solarus is distributed in the hope that it will be useful, 10 * but WITHOUT ANY WARRANTY; without even the implied warranty of 11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 12 * GNU General Public License for more details. 13 * 14 * You should have received a copy of the GNU General Public License along 15 * with this program. If not, see <http://www.gnu.org/licenses/>. 16 */ 17 #ifndef SOLARUS_LUA_TOOLS_H 18 #define SOLARUS_LUA_TOOLS_H 19 20 #include "solarus/core/Common.h" 21 #include "solarus/core/Debug.h" 22 #include "solarus/core/EnumInfo.h" 23 #include "solarus/core/SolarusFatal.h" 24 #include "solarus/lua/LuaException.h" 25 #include <map> 26 #include <string> 27 #include <lua.hpp> 28 29 namespace Solarus { 30 31 class Color; 32 class Map; 33 class ScopedLuaRef; 34 35 /** 36 * \brief Provides general Lua helpers functions. 37 * 38 * All check_* and opt_* functions are similar to their equivalent of the 39 * Lua API, but throw an LuaException in case of error instead of calling 40 * lua_error(). This guarantees that stack-allocated objects are properly 41 * destroyed even in case of error. 42 */ 43 namespace LuaTools { 44 45 // Helpers. 46 int get_positive_index(lua_State* l, int index); 47 bool is_valid_lua_identifier(const std::string& name); 48 std::string get_type_name(lua_State*l, int index); 49 std::string get_type_name(const std::string& module_name); 50 51 ScopedLuaRef create_ref(lua_State* l); 52 ScopedLuaRef create_ref(lua_State* l, int index); 53 bool call_function( 54 lua_State* l, 55 int nb_arguments, 56 int nb_results, 57 const char* function_name 58 ); 59 60 // Error handling. 61 template<typename Callable> 62 int exception_boundary_handle( 63 lua_State* l, 64 Callable&& func 65 ); 66 67 [[noreturn]] void error( 68 lua_State* l, 69 const std::string& message 70 ); 71 [[noreturn]] void arg_error( 72 lua_State* l, 73 int arg_index, 74 const std::string& message 75 ); 76 [[noreturn]] void type_error( 77 lua_State* l, 78 int arg_index, 79 const std::string& expected_type_name 80 ); 81 void check_type( 82 lua_State* l, 83 int arg_index, 84 int expected_type 85 ); 86 void check_any( 87 lua_State* l, 88 int arg_index 89 ); 90 91 // int 92 int check_int( 93 lua_State* l, 94 int index 95 ); 96 int check_int_field( 97 lua_State* l, 98 int table_index, 99 const std::string& key 100 ); 101 int opt_int( 102 lua_State* l, 103 int index, 104 int default_value 105 ); 106 int opt_int_field( 107 lua_State* l, 108 int table_index, 109 const std::string& key, 110 int default_value 111 ); 112 113 // double 114 double check_number( 115 lua_State* l, 116 int index 117 ); 118 double check_number_field( 119 lua_State* l, 120 int table_index, 121 const std::string& key 122 ); 123 double opt_number( 124 lua_State* l, 125 int index, 126 double default_value 127 ); 128 double opt_number_field( 129 lua_State* l, 130 int table_index, 131 const std::string& key, 132 double default_value 133 ); 134 135 // std::string 136 std::string check_string( 137 lua_State* l, 138 int index 139 ); 140 std::string check_string_field( 141 lua_State* l, 142 int table_index, 143 const std::string& key 144 ); 145 std::string opt_string( 146 lua_State* l, 147 int index, 148 const std::string& default_value 149 ); 150 std::string opt_string_field( 151 lua_State* l, 152 int table_index, 153 const std::string& key, 154 const std::string& default_value 155 ); 156 157 // bool 158 bool check_boolean( 159 lua_State* l, 160 int index 161 ); 162 bool check_boolean_field( 163 lua_State* l, 164 int table_index, 165 const std::string& key 166 ); 167 bool opt_boolean( 168 lua_State* l, 169 int index, 170 bool default_value 171 ); 172 bool opt_boolean_field( 173 lua_State* l, 174 int table_index, 175 const std::string& key, 176 bool default_value 177 ); 178 179 // function 180 ScopedLuaRef check_function( 181 lua_State* l, 182 int index 183 ); 184 ScopedLuaRef check_function_field( 185 lua_State* l, 186 int table_index, 187 const std::string& key 188 ); 189 ScopedLuaRef opt_function( 190 lua_State* l, 191 int index 192 ); 193 ScopedLuaRef opt_function_field( 194 lua_State* l, 195 int table_index, 196 const std::string& key 197 ); 198 199 // Layer 200 bool is_layer( 201 lua_State* l, 202 int index, 203 const Map& map 204 ); 205 int check_layer( 206 lua_State* l, 207 int index, 208 const Map& map 209 ); 210 int check_layer_field( 211 lua_State* l, 212 int table_index, 213 const std::string& key, 214 const Map& map 215 ); 216 int opt_layer( 217 lua_State* l, 218 int index, 219 const Map& map, 220 int default_value 221 ); 222 int opt_layer_field( 223 lua_State* l, 224 int table_index, 225 const std::string& key, 226 const Map& map, 227 int default_value 228 ); 229 230 // Color 231 bool is_color( 232 lua_State* l, 233 int index 234 ); 235 Color check_color( 236 lua_State* l, 237 int index 238 ); 239 Color check_color_field( 240 lua_State* l, 241 int table_index, 242 const std::string& key 243 ); 244 Color opt_color( 245 lua_State* l, 246 int index, 247 const Color& default_value 248 ); 249 Color opt_color_field( 250 lua_State* l, 251 int table_index, 252 const std::string& key, 253 const Color& default_value 254 ); 255 256 // enum 257 template<typename E> 258 E check_enum( 259 lua_State* l, 260 int index 261 ); 262 template<typename E> 263 E check_enum_field( 264 lua_State* l, 265 int table_index, 266 const std::string& key 267 ); 268 template<typename E> 269 E opt_enum( 270 lua_State* l, 271 int index, 272 E default_value 273 ); 274 template<typename E> 275 E opt_enum_field( 276 lua_State* l, 277 int table_index, 278 const std::string& key, 279 E default_value 280 ); 281 282 template<typename E> 283 E check_enum( 284 lua_State* l, 285 int index, 286 const std::map<E, std::string>& names 287 ); 288 template<typename E> 289 E check_enum_field( 290 lua_State* l, 291 int table_index, 292 const std::string& key, 293 const std::map<E, std::string>& names 294 ); 295 template<typename E> 296 E opt_enum( 297 lua_State* l, 298 int index, 299 const std::map<E, std::string>& names, 300 E default_value 301 ); 302 template<typename E> 303 E opt_enum_field( 304 lua_State* l, 305 int table_index, 306 const std::string& key, 307 const std::map<E, std::string>& names, 308 E default_value 309 ); 310 311 } 312 313 } 314 315 #include "solarus/lua/LuaTools.inl" 316 317 #endif 318