Freeciv21
Develop your civilization from humble roots to a global empire
api_signal_base.cpp
Go to the documentation of this file.
1 /*
2  Copyright (c) 1996-2020 Freeciv21 and Freeciv contributors. This file is
3  part of Freeciv21. Freeciv21 is free software: you can redistribute it
4  and/or modify it under the terms of the GNU General Public License as
5  published by the Free Software Foundation, either version 3 of the
6  License, or (at your option) any later version. You should have received
7  a copy of the GNU General Public License along with Freeciv21. If not,
8  see https://www.gnu.org/licenses/.
9  */
10 /* common/scriptcore */
11 #include "luascript.h"
12 #include "luascript_signal.h"
13 
14 #include "api_signal_base.h"
15 
19 void api_signal_connect(lua_State *L, const char *signal_name,
20  const char *callback_name)
21 {
22  struct fc_lua *fcl;
23 
25  LUASCRIPT_CHECK_ARG_NIL(L, signal_name, 2, string);
26  LUASCRIPT_CHECK_ARG_NIL(L, callback_name, 3, string);
27 
29 
30  LUASCRIPT_CHECK(L, fcl != nullptr, "Undefined Freeciv21 lua state!");
31 
32  luascript_signal_callback(fcl, signal_name, callback_name, true);
33 }
34 
38 void api_signal_remove(lua_State *L, const char *signal_name,
39  const char *callback_name)
40 {
41  struct fc_lua *fcl;
42 
44  LUASCRIPT_CHECK_ARG_NIL(L, signal_name, 2, string);
45  LUASCRIPT_CHECK_ARG_NIL(L, callback_name, 3, string);
46 
48 
49  LUASCRIPT_CHECK(L, fcl != nullptr, "Undefined Freeciv21 lua state!");
50 
51  luascript_signal_callback(fcl, signal_name, callback_name, false);
52 }
53 
57 bool api_signal_defined(lua_State *L, const char *signal_name,
58  const char *callback_name)
59 {
60  struct fc_lua *fcl;
61 
62  LUASCRIPT_CHECK_STATE(L, false);
63  LUASCRIPT_CHECK_ARG_NIL(L, signal_name, 2, string, false);
64  LUASCRIPT_CHECK_ARG_NIL(L, callback_name, 3, string, false);
65 
67 
68  LUASCRIPT_CHECK(L, fcl != nullptr, "Undefined Freeciv21 lua state!",
69  false);
70 
71  return luascript_signal_callback_defined(fcl, signal_name, callback_name);
72 }
73 
77 const char *api_signal_callback_by_index(lua_State *L,
78  const char *signal_name, int sindex)
79 {
80  struct fc_lua *fcl;
81 
82  LUASCRIPT_CHECK_STATE(L, nullptr);
83  LUASCRIPT_CHECK_ARG_NIL(L, signal_name, 2, string, nullptr);
84 
86 
87  LUASCRIPT_CHECK(L, fcl != nullptr, "Undefined Freeciv21 lua state!",
88  nullptr);
89 
90  return luascript_signal_callback_by_index(fcl, signal_name, sindex);
91 }
92 
97 const char *api_signal_by_index(lua_State *L, int sindex)
98 {
99  struct fc_lua *fcl;
100 
101  LUASCRIPT_CHECK_STATE(L, nullptr);
102 
103  fcl = luascript_get_fcl(L);
104 
105  LUASCRIPT_CHECK(L, fcl != nullptr, "Undefined Freeciv21 lua state!",
106  nullptr);
107  auto callback = luascript_signal_by_index(fcl, sindex);
108  // FIXME memory leak
109  return callback.isEmpty() ? nullptr : qstrdup(qUtf8Printable(callback));
110 }
const char * api_signal_by_index(lua_State *L, int sindex)
Return the name of the 'index' callback function of the signal with the name 'signal_name'.
bool api_signal_defined(lua_State *L, const char *signal_name, const char *callback_name)
Returns if a callback function to a certain signal is defined.
const char * api_signal_callback_by_index(lua_State *L, const char *signal_name, int sindex)
Return the name of the signal with the given index.
void api_signal_connect(lua_State *L, const char *signal_name, const char *callback_name)
Connects a callback function to a certain signal.
void api_signal_remove(lua_State *L, const char *signal_name, const char *callback_name)
Removes a callback function to a certain signal.
struct fc_lua * luascript_get_fcl(lua_State *L)
Get the freeciv lua struct from a lua state.
Definition: luascript.cpp:315
#define LUASCRIPT_CHECK_STATE(L,...)
Definition: luascript.h:110
#define LUASCRIPT_CHECK_ARG_NIL(L, value, narg, type,...)
Definition: luascript.h:131
#define LUASCRIPT_CHECK(L, check, msg,...)
Definition: luascript.h:117
bool luascript_signal_callback_defined(struct fc_lua *fcl, const char *signal_name, const char *callback_name)
Returns if a callback function to a certain signal is defined.
const char * luascript_signal_callback_by_index(struct fc_lua *fcl, const char *signal_name, int sindex)
Return the name of the 'index' callback function of the signal with the name 'signal_name'.
void luascript_signal_callback(struct fc_lua *fcl, const char *signal_name, const char *callback_name, bool create)
Connects a callback function to a certain signal.
QString luascript_signal_by_index(struct fc_lua *fcl, int sindex)
Return the name of the signal with the given index.
static void static sol::state * fcl
Lua virtual machine state.
Definition: script_fcdb.cpp:48