Avoid the create always new closure with path object methods - gsl-shell.git - gsl-shell

index : gsl-shell.git
gsl-shell
summary refs log tree commit diff
path: root/agg-plot/lua-draw.cpp
diff options
context:
space:
mode:
authorFrancesco Abbate <francesco.bbt@gmail.com>2012年08月21日 19:14:04 +0200
committerFrancesco Abbate <francesco.bbt@gmail.com>2012年08月21日 19:15:12 +0200
commit5579e4c781a8928acd0998b3cd70a5289e7759d2 (patch)
tree004a6c443ac2e85b35670e7836ef4c0fd73769e8 /agg-plot/lua-draw.cpp
parent0103cd7eefa668a02e3ab4349a36c1bd8f2a42bb (diff)
downloadgsl-shell-5579e4c781a8928acd0998b3cd70a5289e7759d2.tar.gz
Avoid the create always new closure with path object methods
Diffstat (limited to 'agg-plot/lua-draw.cpp')
-rw-r--r--agg-plot/lua-draw.cpp 76
1 files changed, 35 insertions, 41 deletions
diff --git a/agg-plot/lua-draw.cpp b/agg-plot/lua-draw.cpp
index e8fba890..744b89e9 100644
--- a/agg-plot/lua-draw.cpp
+++ b/agg-plot/lua-draw.cpp
@@ -19,6 +19,7 @@
*/
#include <pthread.h>
+#include <assert.h>
extern "C" {
#include "lua.h"
@@ -37,13 +38,13 @@ extern "C" {
pthread_mutex_t agg_mutex[1];
enum path_cmd_e {
- CMD_ERROR = -1,
CMD_MOVE_TO = 0,
CMD_LINE_TO,
CMD_CLOSE,
CMD_ARC_TO,
CMD_CURVE3,
CMD_CURVE4,
+ CMD_ERROR,
};
struct cmd_call_stack {
@@ -52,13 +53,11 @@ struct cmd_call_stack {
};
struct path_cmd_reg {
- enum path_cmd_e id;
const char *cmd;
const char *signature;
};
static int agg_path_free (lua_State *L);
-static int agg_path_index (lua_State *L);
static int agg_ellipse_new (lua_State *L);
static int agg_circle_new (lua_State *L);
@@ -73,13 +72,13 @@ static int marker_free (lua_State *L);
static void path_cmd (draw::path *p, int cmd, struct cmd_call_stack *stack);
static struct path_cmd_reg cmd_table[] = {
- {CMD_MOVE_TO, "move_to", "ff"},
- {CMD_LINE_TO, "line_to", "ff"},
- {CMD_CLOSE, "close", ""},
- {CMD_ARC_TO, "arc_to", "fffbbff"},
- {CMD_CURVE3, "curve3", "ffff"},
- {CMD_CURVE4, "curve4", "ffffff"},
- {CMD_ERROR, NULL, NULL}
+ {"move_to", "ff"},
+ {"line_to", "ff"},
+ {"close", ""},
+ {"arc_to", "fffbbff"},
+ {"curve3", "ffff"},
+ {"curve4", "ffffff"},
+ {NULL, NULL}
};
static const struct luaL_Reg draw_functions[] = {
@@ -92,7 +91,6 @@ static const struct luaL_Reg draw_functions[] = {
};
static const struct luaL_Reg agg_path_methods[] = {
- {"__index", agg_path_index},
{"__gc", agg_path_free},
{NULL, NULL}
};
@@ -175,8 +173,8 @@ path_cmd (draw::path *p, int _cmd, struct cmd_call_stack *s)
case CMD_CURVE4:
ps.curve4 (s->f[0], s->f[1], s->f[2], s->f[3], s->f[4], s->f[5]);
break;
- case CMD_ERROR:
- ;
+ default:
+ /* */;
}
}
@@ -185,7 +183,11 @@ agg_path_cmd (lua_State *L)
{
draw::path *p = check_agg_path (L, 1);
int id = lua_tointeger (L, lua_upvalueindex(1));
- const char *signature = lua_tostring (L, lua_upvalueindex(2));
+
+ assert(id >= 0 && id < CMD_ERROR);
+
+ path_cmd_reg* cmd = cmd_table + id;
+ const char *signature = cmd->signature;
int argc = 2, f_count = 0, b_count = 0;
struct cmd_call_stack s[1];
const char *fc;
@@ -212,33 +214,6 @@ agg_path_cmd (lua_State *L)
}
int
-agg_path_index (lua_State *L)
-{
- struct path_cmd_reg *r;
- const char *key;
-
- if (! lua_isstring (L, 2))
- return 0;
-
- key = lua_tostring (L, 2);
- for (r = cmd_table; r->cmd; r++)
- {
- if (strcmp (key, r->cmd) == 0)
- break;
- }
-
- if (r->cmd)
- {
- lua_pushinteger (L, (int) r->id);
- lua_pushstring (L, r->signature);
- lua_pushcclosure (L, agg_path_cmd, 2);
- return 1;
- }
-
- return 0;
-}
-
-int
agg_ellipse_new (lua_State *L)
{
draw::ellipse *vs = push_new_object<draw::ellipse>(L, GS_DRAW_ELLIPSE);
@@ -310,12 +285,31 @@ marker_free (lua_State *L)
return object_free<sg_object>(L, 1, GS_DRAW_MARKER);
}
+/* create a __index table with methods for agg_path */
+static void
+agg_path_create_index (lua_State* L)
+{
+ /* we assume that on top of the stack we have the metatable */
+ lua_pushstring(L, "__index");
+ lua_newtable(L); /* creata a new table to hold the methods */
+ for (int k = 0; cmd_table[k].cmd; k++) /* for each possible command */
+ {
+ path_cmd_reg* r = cmd_table + k;
+ lua_pushstring(L, r->cmd); /* push the name of the command */
+ lua_pushinteger(L, k); /* use the id of the command as an upvalue */
+ lua_pushcclosure(L, agg_path_cmd, 1); /* to create a closure, the actual method */
+ lua_rawset(L, -3); /* and associate the method to the command name */
+ }
+ lua_rawset(L, -3); /* bind the the new table to the __index key */
+}
+
void
draw_register (lua_State *L)
{
pthread_mutex_init (agg_mutex, NULL);
luaL_newmetatable (L, GS_METATABLE(GS_DRAW_PATH));
+ agg_path_create_index(L);
luaL_register (L, NULL, agg_path_methods);
lua_pop (L, 1);
generated by cgit v1.2.3 (git 2.39.1) at 2025年09月16日 16:44:57 +0000

AltStyle によって変換されたページ (->オリジナル) /