Fawkes API Fawkes Development Version
trigger.cpp
1
2/***************************************************************************
3 * trigger.cpp - Fawkes Lua Trigger Support
4 *
5 * Created: Mon Jun 23 10:28:05 2008
6 * Copyright 2006-2008 Tim Niemueller [www.niemueller.de]
7 *
8 ****************************************************************************/
9
10/* This program is free software; you can redistribute it and/or modify
11 * it under the terms of the GNU General Public License as published by
12 * the Free Software Foundation; either version 2 of the License, or
13 * (at your option) any later version.
14 *
15 * This program is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 * GNU Library General Public License for more details.
19 *
20 * Read the full text in the LICENSE.GPL file in the doc directory.
21 */
22
23#include <core/exceptions/system.h>
24#include <lua/context.h>
25#include <lua/trigger.h>
26
27#include <cstdio>
28#include <cstdlib>
29#include <cstring>
30
31namespace fawkes {
32
33/** @class LuaTriggerManager <lua/trigger.h>
34 * Lua Trigger Manager.
35 * This class interfaces with a trigger sub-system running inside Lua (with
36 * the trigger system provided by Fawkes' Lua packages).
37 * @author Tim Niemueller
38 */
39
40/** Constructor.
41 * @param lua Lua context to use that has a running trigger system
42 * @param trigger_var the name of the (global) variable pointing to the
43 * trigger system
44 */
46{
47 lua_ = lua;
48 trigger_var_ = strdup(trigger_var);
49}
50
51/** Destructor. */
53{
54 free(trigger_var_);
55}
56
57/** Cause a trigger event.
58 * @param event name of the event to trigger
59 * @param param_format a format string for a string passed plain as Lua code
60 * in the trigger() function call as second argument. The code executed looks
61 * like "lua_trigger_var:trigger(event, string)" with string being what you
62 * pass, so it can be any number of arguments, for instance you could pass
63 * @code
64 * {x=%f, y=%f}
65 * @endcode
66 * which would result in a table set with the two floats you provide in the
67 * ellipsis.
68 */
69void
70LuaTriggerManager::trigger(const char *event, const char *param_format, ...)
71{
72 va_list args;
73 char * params = NULL;
74 if (param_format) {
75 va_start(args, param_format);
76 if (vasprintf(&params, param_format, args) == -1) {
77 throw OutOfMemoryException("Lua trigger: Could not allocate param string");
78 }
79 va_end(args);
80
81 lua_->do_string("%s:trigger(\"%s\", %s)", trigger_var_, event, params);
82 free(params);
83 } else {
84 lua_->do_string("%s:trigger(\"%s\")", trigger_var_, event);
85 }
86}
87
88} // end of namespace fawkes
Lua C++ wrapper.
Definition: context.h:44
void do_string(const char *format,...)
Execute string.
Definition: context.cpp:532
LuaTriggerManager(LuaContext *lua, const char *trigger_var)
Constructor.
Definition: trigger.cpp:45
~LuaTriggerManager()
Destructor.
Definition: trigger.cpp:52
void trigger(const char *event, const char *param_format=0,...)
Cause a trigger event.
Definition: trigger.cpp:70
System ran out of memory and desired operation could not be fulfilled.
Definition: system.h:32
Fawkes library namespace.