forked from moai/luamongo
-
Notifications
You must be signed in to change notification settings - Fork 0
/
mongo_gridfschunk.cpp
103 lines (75 loc) · 2.05 KB
/
mongo_gridfschunk.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
#include <iostream>
#include <client/dbclient.h>
#include <client/gridfs.h>
#include "utils.h"
#include "common.h"
using namespace mongo;
namespace {
inline GridFSChunk* userdata_to_gridfschunk(lua_State* L, int index) {
void *ud = 0;
ud = luaL_checkudata(L, index, LUAMONGO_GRIDFSCHUNK);
GridFSChunk *chunk = *((GridFSChunk **)ud);
return chunk;
}
}
/*
* str = chunk:data()
*/
static int gridfschunk_data(lua_State *L) {
GridFSChunk *chunk = userdata_to_gridfschunk(L, 1);
int len;
const char *data = chunk->data(len);
lua_pushlstring(L, data, len);
return 1;
}
/*
* length = chunk:len()
* __len
*/
static int gridfschunk_len(lua_State *L) {
GridFSChunk *chunk = userdata_to_gridfschunk(L, 1);
int len = chunk->len();
lua_pushinteger(L, len);
return 1;
}
/*
* __gc
*/
static int gridfschunk_gc(lua_State *L) {
GridFSChunk *chunk = userdata_to_gridfschunk(L, 1);
delete chunk;
return 0;
}
/*
* __tostring
*/
static int gridfschunk_tostring(lua_State *L) {
GridFSChunk *chunk = userdata_to_gridfschunk(L, 1);
lua_pushfstring(L, "%s: %p", LUAMONGO_GRIDFSCHUNK, chunk);
return 1;
}
int mongo_gridfschunk_register(lua_State *L) {
static const luaL_Reg gridfschunk_methods[] = {
{"data", gridfschunk_data},
{"len", gridfschunk_len},
{NULL, NULL}
};
static const luaL_Reg gridfschunk_class_methods[] = {
{NULL, NULL}
};
luaL_newmetatable(L, LUAMONGO_GRIDFSCHUNK);
//luaL_register(L, 0, gridfschunk_methods);
luaL_setfuncs(L, gridfschunk_methods, 0);
lua_pushvalue(L,-1);
lua_setfield(L, -2, "__index");
lua_pushcfunction(L, gridfschunk_gc);
lua_setfield(L, -2, "__gc");
lua_pushcfunction(L, gridfschunk_tostring);
lua_setfield(L, -2, "__tostring");
lua_pushcfunction(L, gridfschunk_len);
lua_setfield(L, -2, "__len");
lua_pop(L,1);
//luaL_register(L, LUAMONGO_GRIDFSCHUNK, gridfschunk_class_methods);
luaL_newlib(L, gridfschunk_class_methods);
return 1;
}