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 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280
|
// mk4lua.cpp --
// $Id: mk4lua.cpp,v 1.2 2001/11/03 23:59:22 wcvs Exp $
// This is part of MetaKit, see http://www.equi4.com/metakit/
//
// This is the Lua-specific code to turn MetaKit into a Lua extension.
#include "luna2.h"
#include "mk4.h"
#include "mk4io.h"
/////////////////////////////////////////////////////////////////////////////
static c4_Property nullProp ('S', "?");
class MkProperty : public c4_Property
{
public:
MkProperty(Lua& lua) : c4_Property (nullProp) {
char type = ++lua;
const char* name = ++lua;
*(c4_Property*) this = c4_Property (type, name);
}
void name(Lua& lua) { lua << Name(); }
void type(Lua& lua) { lua << Type(); }
void id(Lua& lua) { lua << GetId(); }
static const char className[];
static const Luna<MkProperty>::RegType regTable[];
};
const char MkProperty::className[] = "MkProperty";
const Luna<MkProperty>::RegType MkProperty::regTable[] = {
{"name", &MkProperty::name},
{"type", &MkProperty::type},
{"id", &MkProperty::id},
{0}
};
/////////////////////////////////////////////////////////////////////////////
static c4_Row nullRow;
class MkRowRef : public c4_Cursor
{
c4_Row* _row;
static int getprop(lua_State* L)
{
assert(lua_isuserdata(L, 1) && lua_tag(L, 1) == Luna<MkRowRef>::otag);
MkRowRef* r = (MkRowRef*) lua_touserdata(L, 1);
assert(r != 0);
Lua lua (L, 1);
const char* name = ++lua;
const c4_View& vw = (**r).Container();
int i = vw.FindPropIndexByName(name);
if (i >= 0) {
const c4_Property& p (vw.NthProperty(i));
switch (p.Type()) {
case 'I': lua << ((const c4_IntProp&) p).Get(**r); break;
case 'F': lua << ((const c4_FloatProp&) p).Get(**r); break;
case 'D': lua << ((const c4_DoubleProp&) p).Get(**r); break;
case 'S': lua << ((const c4_StringProp&) p).Get(**r); break;
case 'B':
{
c4_Bytes data = ((const c4_BytesProp&) p).Get(**r);
lua_pushlstring(L, (const char*) data.Contents(), data.Size());
}
return 1;
default:
assert(false);
}
}
return lua.numresults();
}
static int setprop(lua_State* L)
{
assert(lua_isuserdata(L, 1) && lua_tag(L, 1) == Luna<MkRowRef>::otag);
MkRowRef* r = (MkRowRef*) lua_touserdata(L, 1);
assert(r != 0);
Lua lua (L, 1);
const char* name = ++lua;
const c4_View& vw = (**r).Container();
int i = vw.FindPropIndexByName(name);
assert(i >= 0);
const c4_Property& p (vw.NthProperty(i));
switch (p.Type()) {
case 'I': ((const c4_IntProp&) p).Set(**r, ++lua); break;
case 'F': ((const c4_FloatProp&) p).Set(**r, ++lua); break;
case 'D': ((const c4_DoubleProp&) p).Set(**r, ++lua); break;
case 'S': ((const c4_StringProp&) p).Set(**r, ++lua); break;
case 'B':
{
const char* data = ++lua;
int len = lua_strlen(lua, 2);
((const c4_BytesProp&) p).Set(**r, c4_Bytes (data, len));
}
break;
default:
assert(false);
}
return lua.numresults();
}
public:
MkRowRef (Lua& lua);
~MkRowRef () { delete _row; }
static const char className[];
static const Luna<MkRowRef>::RegType regTable[];
};
const char MkRowRef::className[] = "MkRowRef";
const Luna<MkRowRef>::RegType MkRowRef::regTable[] = {
{0}
};
/////////////////////////////////////////////////////////////////////////////
class MkView : public c4_View
{
public:
static c4_View* pushnew(Lua& lua) { return Luna<MkView>::create(lua); }
MkView(Lua& lua) { }
void getsize(Lua& lua) { lua << GetSize(); }
void setsize(Lua& lua) { SetSize(++lua); }
void insert(Lua& lua) {
assert(false);//XXX
}
void append(Lua& lua) {
assert(false);//XXX
}
void xdelete(Lua& lua) {
int pos = ++lua;
int count = 1; lua >> count;
RemoveAt(pos-1, count);
}
void select(Lua& lua) {
assert(false);//XXX
}
void sort(Lua& lua) {
*pushnew(lua) = Sort();
}
void product(Lua& lua) {
MkView* a = Luna<MkView>::getarg(lua); assert(a != 0);
*pushnew(lua) = Product(*a);
}
void xunion(Lua& lua) {
MkView* a = Luna<MkView>::getarg(lua); assert(a != 0);
*pushnew(lua) = Union(*a);
}
void intersect(Lua& lua) {
MkView* a = Luna<MkView>::getarg(lua); assert(a != 0);
*pushnew(lua) = Intersect(*a);
}
void different(Lua& lua) {
MkView* a = Luna<MkView>::getarg(lua); assert(a != 0);
*pushnew(lua) = Different(*a);
}
void minus(Lua& lua) {
MkView* a = Luna<MkView>::getarg(lua); assert(a != 0);
*pushnew(lua) = Minus(*a);
}
void remapwith(Lua& lua) {
MkView* a = Luna<MkView>::getarg(lua); assert(a != 0);
*pushnew(lua) = RemapWith(*a);
}
void pair(Lua& lua) {
MkView* a = Luna<MkView>::getarg(lua); assert(a != 0);
*pushnew(lua) = Pair(*a);
}
static const char className[];
static const Luna<MkView>::RegType regTable[];
};
const char MkView::className[] = "MkView";
const Luna<MkView>::RegType MkView::regTable[] = {
{"getsize", &MkView::getsize},
{"setsize", &MkView::setsize},
{"insert", &MkView::insert},
{"append", &MkView::append},
{"delete", &MkView::xdelete},
{"select", &MkView::select},
{"sort", &MkView::sort},
{"product", &MkView::product},
{"union", &MkView::xunion},
{"intersect", &MkView::intersect},
{"different", &MkView::different},
{"minus", &MkView::minus},
{"remapwith", &MkView::remapwith},
{"pair", &MkView::pair},
{0}
};
/////////////////////////////////////////////////////////////////////////////
class MkStorage : public c4_Storage
{
public:
MkStorage(Lua& lua) {
const char* n = ++lua;
int f = 0; lua >> f;
*(c4_Storage*) this = c4_Storage (n, f);
}
void view(Lua& lua) { *MkView::pushnew(lua) = View(++lua); }
void getas(Lua& lua) { *MkView::pushnew(lua) = GetAs(++lua); }
void autocommit(Lua& lua) { AutoCommit(); }
void commit(Lua& lua) { int n = 0; lua >> n; lua << Commit(n); }
void rollback(Lua& lua) { int n = 0; lua >> n; lua << Rollback(n); }
void contents(Lua& lua) { *MkView::pushnew(lua) = *this; }
void aside(Lua& lua) {
MkStorage* a = Luna<MkStorage>::getarg(lua); assert(a != 0);
SetAside(*a);
}
void description(Lua& lua) {
const char* n = 0; lua >> n;
lua << Description(n);
}
static const char className[];
static const Luna<MkStorage>::RegType regTable[];
};
const char MkStorage::className[] = "MkStorage";
const Luna<MkStorage>::RegType MkStorage::regTable[] = {
{"view", &MkStorage::view},
{"getas", &MkStorage::getas},
{"autocommit", &MkStorage::autocommit},
{"commit", &MkStorage::commit},
{"rollback", &MkStorage::rollback},
{"contents", &MkStorage::contents},
{"aside", &MkStorage::aside},
{"description", &MkStorage::description},
{0}
};
/////////////////////////////////////////////////////////////////////////////
MkRowRef::MkRowRef (Lua& lua) : c4_Cursor (&nullRow), _row (0) {
static bool first = true;
if (first) {
first = false; // take over indexed access
lua_pushcfunction(lua, getprop);
lua_settagmethod(lua, Luna<MkRowRef>::otag, "gettable");
lua_pushcfunction(lua, setprop);
lua_settagmethod(lua, Luna<MkRowRef>::otag, "settable");
}
if (lua.more()) {
MkView* v = (MkView*) lua_touserdata(++lua, 2); // skip table
assert(v != 0);
if (lua.more()) { // got <view,index>, create a rowref from it
*(c4_Cursor*) this = & (*(c4_View*) v)[++lua];
} else { // got <view>, use it as template for an empty row
}
} else {
_row = new c4_Row;
*(c4_Cursor*) this = &(*_row);
}
}
/////////////////////////////////////////////////////////////////////////////
extern "C" int mk4lua_open(lua_State *L);
int mk4lua_open(lua_State *L)
{
Luna<MkProperty>::Register(L, 1); // access as fields, not functions
Luna<MkRowRef>::Register(L);
Luna<MkView>::Register(L);
Luna<MkStorage>::Register(L);
return 0;
}
/////////////////////////////////////////////////////////////////////////////
|