00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032
00033
00034
00035 #include "rtt.hpp"
00036
00037 using namespace std;
00038 using namespace RTT;
00039 using namespace RTT::detail;
00040 using namespace RTT::base;
00041 using namespace RTT::internal;
00042
00043 static TaskContext* __getTC(lua_State*);
00044
00045 #define DEBUG
00046
00047 #ifdef MSVC
00048 #ifdef DEBUG
00049 # define _DBG(fmt, ...) printf("%s:%d\t" fmt "\n", __FUNCTION__, __LINE__, __VA_ARGS__)
00050 #else
00051 # define _DBG(fmt, ...) do { } while(0);
00052 #endif
00053 #else
00054 #ifdef DEBUG
00055 # define _DBG(fmt, args...) printf("%s:%d\t" fmt "\n", __FUNCTION__, __LINE__, ##args)
00056 #else
00057 # define _DBG(fmt, args...) do { } while(0);
00058 #endif
00059 #endif
00060
00061
00062
00063
00064
00065
00066 void* operator new(size_t size, lua_State* L, const char* mt)
00067 {
00068 void* ptr = lua_newuserdata(L, size);
00069 luaL_getmetatable(L, mt);
00070
00071 lua_setmetatable(L, -2);
00072 return ptr;
00073 }
00074
00075
00076
00077
00078
00079
00080
00081 #define luaM_pushobject(L, T) new(L, #T) T
00082 #define luaM_pushobject_mt(L, MT, T) new(L, MT) T
00083
00084
00085 #define luaM_checkudata(L, pos, T) reinterpret_cast<T*>(luaL_checkudata((L), (pos), #T))
00086 #define luaM_checkudata_mt(L, pos, MT, T) reinterpret_cast<T*>(luaL_checkudata((L), (pos), MT))
00087
00088
00089 #define luaM_testudata(L, pos, T) (T*) (luaL_testudata((L), (pos), #T))
00090 #define luaM_testudata_mt(L, pos, MT, T) (T*) (luaL_testudata((L), (pos), MT))
00091
00092
00093
00094
00095
00096
00097 #define luaM_checkudata_bx(L, pos, T) (T**) (luaL_checkudata((L), (pos), #T))
00098 #define luaM_checkudata_mt_bx(L, pos, MT, T) (T**) (luaL_checkudata((L), (pos), MT))
00099
00100
00101 #define luaM_testudata_bx(L, pos, T) (T**) (luaL_testudata((L), (pos), #T))
00102 #define luaM_testudata_mt_bx(L, pos, MT, T) (T**) (luaL_testudata((L), (pos), MT))
00103
00104
00105 #define gen_push_bxptr(name, MT, T) \
00106 static void name(lua_State *L, T* ptr) \
00107 { \
00108 T** ptrptr = (T**) lua_newuserdata(L, sizeof(T*)); \
00109 *ptrptr = ptr; \
00110 luaL_getmetatable(L, MT); \
00111 lua_setmetatable(L, -2); \
00112 } \
00113
00114
00115 template<typename T>
00116 int GCMethod(lua_State* L)
00117 {
00118 reinterpret_cast<T*>(lua_touserdata(L, 1))->~T();
00119 return 0;
00120 }
00121
00122
00123
00124
00125
00126
00127
00128 void* luaL_testudata (lua_State *L, int ud, const char *tname)
00129 {
00130 void *p = lua_touserdata(L, ud);
00131
00132 if (p == NULL)
00133 goto out;
00134
00135 if (!lua_getmetatable(L, ud)) {
00136 p = NULL;
00137 goto out;
00138 }
00139
00140
00141 lua_getfield(L, LUA_REGISTRYINDEX, tname);
00142 if (!lua_rawequal(L, -1, -2))
00143 p = NULL;
00144
00145 lua_pop(L, 2);
00146 out:
00147 return p;
00148 }
00149
00150
00151 void push_vect_str(lua_State *L, const std::vector<std::string> &v)
00152 {
00153 int key = 1;
00154 lua_createtable(L, v.size(), 0);
00155
00156 for(vector<std::string>::const_iterator it = v.begin(); it != v.end(); ++it) {
00157 lua_pushstring(L, it->c_str());
00158 lua_rawseti(L, -2, key++);
00159 }
00160 }
00161
00162
00163 static DataSourceBase::shared_ptr Variable_fromlua(lua_State *L, const char* type, int valind);
00164
00165
00166
00167
00168
00169
00170 static bool __typenames_cmp(const char* type1, const char* type2)
00171 {
00172 const types::TypeInfo *ti1 = types::TypeInfoRepository::Instance()->type(type1);
00173 const types::TypeInfo *ti2 = types::TypeInfoRepository::Instance()->type(type2);
00174 return ti1 == ti2;
00175 }
00176
00177
00178
00179 static bool Variable_is_a(DataSourceBase::shared_ptr dsb, const std::string & type)
00180 {
00181 const types::TypeInfo *ti1 = dsb->getTypeInfo();
00182 const types::TypeInfo *ti2 = types::TypeInfoRepository::Instance()->type(type);
00183 return ti1 == ti2;
00184 }
00185
00186
00187 static bool __Variable_isbasic(DataSourceBase::shared_ptr dsb)
00188 {
00189 if ( Variable_is_a(dsb, "bool") || Variable_is_a(dsb, "double") || Variable_is_a(dsb, "float") ||
00190 Variable_is_a(dsb, "uint") || Variable_is_a(dsb, "int") || Variable_is_a(dsb, "char") ||
00191 Variable_is_a(dsb, "string") || Variable_is_a(dsb, "void"))
00192 return true;
00193 else
00194 return false;
00195 }
00196
00197 static int Variable_isbasic(lua_State *L)
00198 {
00199 DataSourceBase::shared_ptr dsb = *(luaM_checkudata_mt(L, 1, "Variable", DataSourceBase::shared_ptr));
00200 lua_pushboolean(L, __Variable_isbasic(dsb));
00201 return 1;
00202 }
00203
00204
00205
00206
00207
00208 static int __Variable_tolua(lua_State *L, DataSourceBase::shared_ptr dsb)
00209 {
00210 DataSourceBase *ds = dsb.get();
00211 assert(ds);
00212
00213 if(Variable_is_a(dsb, "bool")) {
00214 DataSource<bool>* dsb = DataSource<bool>::narrow(ds);
00215 if(dsb) lua_pushboolean(L, dsb->get());
00216 else goto out_nodsb;
00217 } else if (Variable_is_a(dsb, "float")) {
00218 DataSource<float>* dsb = DataSource<float>::narrow(ds);
00219 if(dsb) lua_pushnumber(L, ((lua_Number) dsb->get()));
00220 else goto out_nodsb;
00221 } else if (Variable_is_a(dsb, "double")) {
00222 DataSource<double>* dsb = DataSource<double>::narrow(ds);
00223 if(dsb) lua_pushnumber(L, ((lua_Number) dsb->get()));
00224 else goto out_nodsb;
00225 } else if (Variable_is_a(dsb, "uint")) {
00226 DataSource<unsigned int>* dsb = DataSource<unsigned int>::narrow(ds);
00227 if(dsb) lua_pushnumber(L, ((lua_Number) dsb->get()));
00228 else goto out_nodsb;
00229 } else if (Variable_is_a(dsb, "int")) {
00230 DataSource<int>* dsb = DataSource<int>::narrow(ds);
00231 if(dsb) lua_pushnumber(L, ((lua_Number) dsb->get()));
00232 else goto out_nodsb;
00233 } else if (Variable_is_a(dsb, "char")) {
00234 DataSource<char>* dsb = DataSource<char>::narrow(ds);
00235 char c = dsb->get();
00236 if(dsb) lua_pushlstring(L, &c, 1);
00237 else goto out_nodsb;
00238 } else if (Variable_is_a(dsb, "string")) {
00239 DataSource<std::string>* dsb = DataSource<std::string>::narrow(ds);
00240 if(dsb) lua_pushlstring(L, dsb->get().c_str(), dsb->get().size());
00241 else goto out_nodsb;
00242 } else if (Variable_is_a(dsb, "void")) {
00243 DataSource<void>* dsb = DataSource<void>::narrow(ds);
00244 if(dsb) lua_pushnil(L);
00245 else goto out_nodsb;
00246 } else {
00247 goto out_conv_err;
00248 }
00249
00250
00251 return 1;
00252
00253 out_conv_err:
00254 luaL_error(L, "Variable.tolua: can't convert type %s", dsb->getTypeName().c_str());
00255 return 0;
00256
00257 out_nodsb:
00258 luaL_error(L, "Variable.tolua: narrow failed for %s Variable", dsb->getTypeName().c_str());
00259 return 0;
00260 }
00261
00262 static int Variable_tolua(lua_State *L)
00263 {
00264 DataSourceBase::shared_ptr dsb = *(luaM_checkudata_mt(L, 1, "Variable", DataSourceBase::shared_ptr));
00265 return __Variable_tolua(L, dsb);
00266 }
00267
00268
00269
00270
00271
00272 static void Variable_push_coerce(lua_State *L, DataSourceBase::shared_ptr dsb)
00273 {
00274 if (__Variable_isbasic(dsb))
00275 __Variable_tolua(L, dsb);
00276 else
00277 luaM_pushobject_mt(L, "Variable", DataSourceBase::shared_ptr)(dsb);
00278
00279 }
00280
00281 static int Variable_getTypes(lua_State *L)
00282 {
00283 push_vect_str(L, Types()->getTypes());
00284 return 1;
00285 }
00286
00287 static int Variable_getMemberNames(lua_State *L)
00288 {
00289 DataSourceBase::shared_ptr *dsbp = luaM_checkudata_mt(L, 1, "Variable", DataSourceBase::shared_ptr);
00290 push_vect_str(L, (*dsbp)->getMemberNames());
00291 return 1;
00292 }
00293
00294 static int Variable_getMember(lua_State *L)
00295 {
00296 DataSourceBase::shared_ptr *dsbp = luaM_checkudata_mt(L, 1, "Variable", DataSourceBase::shared_ptr);
00297 DataSourceBase::shared_ptr memdsb;
00298 const char *mem = luaL_checkstring(L, 2);
00299
00300 memdsb = (*dsbp)->getMember(mem);
00301
00302 if(memdsb == 0)
00303 lua_pushnil(L);
00304 else
00305 Variable_push_coerce(L, memdsb);
00306 return 1;
00307 }
00308
00309 static int Variable_getMemberRaw(lua_State *L)
00310 {
00311 DataSourceBase::shared_ptr *dsbp = luaM_checkudata_mt(L, 1, "Variable", DataSourceBase::shared_ptr);
00312 DataSourceBase::shared_ptr memdsb;
00313 const char *mem = luaL_checkstring(L, 2);
00314
00315 memdsb = (*dsbp)->getMember(mem);
00316
00317 if(memdsb == 0)
00318 lua_pushnil(L);
00319 else
00320 luaM_pushobject_mt(L, "Variable", DataSourceBase::shared_ptr)(memdsb);
00321 return 1;
00322 }
00323
00324 static int Variable_update(lua_State *L)
00325 {
00326 int ret;
00327 DataSourceBase::shared_ptr dsb;
00328 DataSourceBase::shared_ptr *dsbp;
00329 DataSourceBase::shared_ptr self = *(luaM_checkudata_mt(L, 1, "Variable", DataSourceBase::shared_ptr));
00330
00331 if ((dsbp = luaM_testudata_mt(L, 2, "Variable", DataSourceBase::shared_ptr)) != NULL)
00332 dsb = *dsbp;
00333 else
00334 dsb = Variable_fromlua(L, self->getType().c_str(), 2);
00335
00336 ret = self->update(dsb.get());
00337 lua_pushboolean(L, ret);
00338 return 1;
00339 }
00340
00341
00342 static int Variable_create(lua_State *L)
00343 {
00344 const char *type;
00345 type = luaL_checkstring(L, 1);
00346
00347 if(!strcmp(type, "void"))
00348 luaL_error(L, "Variable.new: can't create void variable");
00349
00350 TypeInfo* ti = Types()->type(type);
00351
00352 if(ti==0)
00353 luaL_error(L, "Variable.new: unknown type %s", type);
00354
00355 luaM_pushobject_mt(L, "Variable", DataSourceBase::shared_ptr)(ti->buildValue());
00356 return 1;
00357 }
00358
00359
00360 static DataSourceBase::shared_ptr Variable_fromlua(lua_State *L, const char* type, int valind)
00361 {
00362 DataSourceBase::shared_ptr dsb;
00363 luaL_checkany(L, valind);
00364 int luatype = lua_type(L, valind);
00365
00366 if(__typenames_cmp(type, "bool")) {
00367 lua_Number x;
00368 if(luatype == LUA_TBOOLEAN)
00369 x = (lua_Number) lua_toboolean(L, valind);
00370 else if (luatype == LUA_TNUMBER)
00371 x = lua_tonumber(L, valind);
00372 else
00373 goto out_conv_err;
00374
00375 dsb = new ValueDataSource<bool>((bool) x);
00376
00377 } else if (__typenames_cmp(type, "int")) {
00378 lua_Number x;
00379 if (luatype == LUA_TNUMBER)
00380 x = lua_tonumber(L, valind);
00381 else
00382 goto out_conv_err;
00383
00384 dsb = new ValueDataSource<int>(x);
00385
00386 } else if (__typenames_cmp(type, "uint")) {
00387 lua_Number x;
00388 if (luatype == LUA_TNUMBER)
00389 x = lua_tonumber(L, valind);
00390 else
00391 goto out_conv_err;
00392
00393 dsb = new ValueDataSource<unsigned int>((unsigned int) x);
00394
00395 } else if (__typenames_cmp(type, "double")) {
00396 lua_Number x;
00397 if (luatype == LUA_TNUMBER)
00398 x = lua_tonumber(L, valind);
00399 else
00400 goto out_conv_err;
00401
00402 dsb = new ValueDataSource<double>((double) x);
00403
00404 } else if (__typenames_cmp(type, "float")) {
00405 lua_Number x;
00406 if (luatype == LUA_TNUMBER)
00407 x = lua_tonumber(L, valind);
00408 else
00409 goto out_conv_err;
00410
00411 dsb = new ValueDataSource<float>((float) x);
00412
00413 } else if (__typenames_cmp(type, "char")) {
00414 const char *x;
00415 size_t l;
00416 if (luatype == LUA_TSTRING)
00417 x = lua_tolstring(L, valind, &l);
00418 else
00419 goto out_conv_err;
00420
00421 dsb = new ValueDataSource<char>(x[0]);
00422
00423 } else if (__typenames_cmp(type, "string")) {
00424 const char *x;
00425 if (luatype == LUA_TSTRING)
00426 x = lua_tostring(L, valind);
00427 else
00428 goto out_conv_err;
00429
00430 dsb = new ValueDataSource<std::string>(x);
00431
00432 } else if (luatype == LUA_TNUMBER) {
00433 TypeInfo* ti = Types()->type(type);
00434 DataSourceBase::shared_ptr double_dsb = Variable_fromlua(L, "double", valind);
00435 dsb = ti->convert(double_dsb);
00436 } else {
00437 goto out_conv_err;
00438 }
00439
00440
00441 return dsb;
00442
00443 out_conv_err:
00444 luaL_error(L, "__lua_todsb: can't convert lua %s to %s variable", lua_typename(L, luatype), type);
00445 return NULL;
00446 }
00447
00448
00449 static int Variable_create_ival(lua_State *L, int typeind, int valind)
00450 {
00451 DataSourceBase::shared_ptr dsb;
00452 luaL_checkany(L, valind);
00453 const char* type = luaL_checkstring(L, typeind);
00454 dsb = Variable_fromlua(L, type, valind);
00455 luaM_pushobject_mt(L, "Variable", DataSourceBase::shared_ptr)(dsb);
00456 return 1;
00457 }
00458
00459 static int Variable_new(lua_State *L)
00460 {
00461 int argc = lua_gettop(L);
00462 if(argc == 1)
00463 return Variable_create(L);
00464 else if(argc == 2)
00465 return Variable_create_ival(L, 1, 2);
00466 else
00467 luaL_error(L, "Variable.new: invalid number of args");
00468
00469 return 0;
00470 }
00471
00472 static int Variable_toString(lua_State *L)
00473 {
00474 DataSourceBase::shared_ptr *dsbp = luaM_checkudata_mt(L, 1, "Variable", DataSourceBase::shared_ptr);
00475 lua_pushstring(L, ((*dsbp)->toString()).c_str());
00476 return 1;
00477 }
00478
00479 static int Variable_getType(lua_State *L)
00480 {
00481 DataSourceBase::shared_ptr *dsbp = luaM_checkudata_mt(L, 1, "Variable", DataSourceBase::shared_ptr);
00482 lua_pushstring(L, (*dsbp)->getType().c_str());
00483 return 1;
00484 }
00485
00486 static int Variable_getTypeName(lua_State *L)
00487 {
00488 DataSourceBase::shared_ptr *dsbp = luaM_checkudata_mt(L, 1, "Variable", DataSourceBase::shared_ptr);
00489 lua_pushstring(L, (*dsbp)->getTypeName().c_str());
00490 return 1;
00491 }
00492
00493 static int Variable_resize(lua_State *L)
00494 {
00495 int size;
00496 DataSourceBase::shared_ptr *dsbp = luaM_checkudata_mt(L, 1, "Variable", DataSourceBase::shared_ptr);
00497 size = luaL_checknumber(L, 2);
00498 const TypeInfo *ti = (*dsbp)->getTypeInfo();
00499 lua_pushboolean(L, ti->resize(*dsbp, size));
00500 return 1;
00501 }
00502
00503
00504
00505
00506
00507 static int Variable_unm(lua_State *L)
00508 {
00509 types::OperatorRepository::shared_ptr opreg = types::OperatorRepository::Instance();
00510 DataSourceBase::shared_ptr arg = *(luaM_checkudata_mt(L, 1, "Variable", DataSourceBase::shared_ptr));
00511 DataSourceBase::shared_ptr res = opreg->applyUnary("-", arg.get());
00512 luaM_pushobject_mt(L, "Variable", DataSourceBase::shared_ptr)(res);
00513 return 1;
00514 }
00515
00516
00517
00518 #define gen_opmet(name, op) \
00519 static int name(lua_State *L) \
00520 { \
00521 DataSourceBase::shared_ptr arg1 = *(luaM_checkudata_mt(L, 1, "Variable", DataSourceBase::shared_ptr)); \
00522 DataSourceBase::shared_ptr arg2 = *(luaM_checkudata_mt(L, 2, "Variable", DataSourceBase::shared_ptr)); \
00523 types::OperatorRepository::shared_ptr opreg = types::OperatorRepository::Instance(); \
00524 DataSourceBase *res = opreg->applyBinary(#op, arg1.get(), arg2.get()); \
00525 if(res == 0) \
00526 luaL_error(L , "%s (operator %s) failed", #name, #op); \
00527 res->evaluate(); \
00528 luaM_pushobject_mt(L, "Variable", DataSourceBase::shared_ptr)(res); \
00529 return 1; \
00530 } \
00531
00532 gen_opmet(Variable_add, +)
00533 gen_opmet(Variable_sub, -)
00534 gen_opmet(Variable_mul, *)
00535 gen_opmet(Variable_div, /)
00536 gen_opmet(Variable_mod, %)
00537 gen_opmet(Variable_pow, ^)
00538
00539
00540 #define gen_opmet_bool(name, op) \
00541 static int name(lua_State *L) \
00542 { \
00543 DataSourceBase::shared_ptr arg1 = *(luaM_checkudata_mt(L, 1, "Variable", DataSourceBase::shared_ptr)); \
00544 DataSourceBase::shared_ptr arg2 = *(luaM_checkudata_mt(L, 2, "Variable", DataSourceBase::shared_ptr)); \
00545 types::OperatorRepository::shared_ptr opreg = types::OperatorRepository::Instance(); \
00546 DataSourceBase *res = opreg->applyBinary(#op, arg1.get(), arg2.get()); \
00547 if(res == 0) \
00548 luaL_error(L , "%s (operator %s) failed", #name, #op); \
00549 res->evaluate(); \
00550 return __Variable_tolua(L, res); \
00551 } \
00552
00553 gen_opmet_bool(Variable_eq, ==)
00554 gen_opmet_bool(Variable_lt, <)
00555 gen_opmet_bool(Variable_le, <=)
00556
00557 static int Variable_opBinary(lua_State *L)
00558 {
00559 types::OperatorRepository::shared_ptr opreg = types::OperatorRepository::Instance();
00560 const char *op = luaL_checkstring(L, 1);
00561 DataSourceBase::shared_ptr arg1 = *(luaM_checkudata_mt(L, 2, "Variable", DataSourceBase::shared_ptr));
00562 DataSourceBase::shared_ptr arg2 = *(luaM_checkudata_mt(L, 3, "Variable", DataSourceBase::shared_ptr));
00563 DataSourceBase *res;
00564
00565 res = opreg->applyBinary(op, arg1.get(), arg2.get());
00566 if(res == 0)
00567 luaL_error(L , "Variable.opBinary '%s' not applicable to args", op);
00568
00569 res->evaluate();
00570
00571 luaM_pushobject_mt(L, "Variable", DataSourceBase::shared_ptr)(res);
00572 return 1;
00573 }
00574
00575
00576
00577
00578
00579
00580 static int Variable_index(lua_State *L)
00581 {
00582 const char* key = luaL_checkstring(L, 2);
00583
00584 lua_getmetatable(L, 1);
00585 lua_getfield(L, -1, key);
00586
00587
00588 if(!lua_isnil(L, -1))
00589 return 1;
00590
00591
00592 lua_settop(L, 2);
00593 return Variable_getMember(L);
00594 }
00595
00596 static int Variable_newindex(lua_State *L)
00597 {
00598 DataSourceBase::shared_ptr *newvalp;
00599 DataSourceBase::shared_ptr newval;
00600 DataSourceBase::shared_ptr master = *(luaM_checkudata_mt(L, 1, "Variable", DataSourceBase::shared_ptr));
00601 const char* member = luaL_checkstring(L, 2);
00602
00603
00604 types::OperatorRepository::shared_ptr opreg = types::OperatorRepository::Instance();
00605 DataSourceBase::shared_ptr curval = master->getMember(member);
00606
00607 if (curval == 0)
00608 luaL_error(L, "Variable.newindex: indexing failed, no member %s", member);
00609
00610 if ((newvalp = luaM_testudata_mt(L, 3, "Variable", DataSourceBase::shared_ptr)) != NULL)
00611 newval = *newvalp;
00612 else
00613 newval = Variable_fromlua(L, curval->getType().c_str(), 3);
00614
00615 lua_pushboolean(L, curval->update(newval.get()));
00616 return 1;
00617 }
00618
00619 static const struct luaL_Reg Variable_f [] = {
00620 { "new", Variable_new },
00621 { "tolua", Variable_tolua },
00622 { "isbasic", Variable_isbasic },
00623 { "toString", Variable_toString },
00624 { "getTypes", Variable_getTypes },
00625 { "getType", Variable_getType },
00626 { "getTypeName", Variable_getTypeName },
00627 { "getMemberNames", Variable_getMemberNames },
00628 { "getMember", Variable_getMember },
00629 { "getMemberRaw", Variable_getMemberRaw },
00630 { "resize", Variable_resize },
00631 { "opBinary", Variable_opBinary },
00632 { "assign", Variable_update },
00633 { "unm", Variable_unm },
00634 { "add", Variable_add },
00635 { "sub", Variable_sub },
00636 { "mul", Variable_mul },
00637 { "div", Variable_div },
00638 { "mod", Variable_mod },
00639 { "pow", Variable_pow },
00640 { "eq", Variable_eq },
00641 { "lt", Variable_lt },
00642 { "le", Variable_le },
00643 { NULL, NULL}
00644 };
00645
00646 static const struct luaL_Reg Variable_m [] = {
00647 { "tolua", Variable_tolua },
00648 { "isbasic", Variable_isbasic },
00649 { "toString", Variable_toString },
00650 { "getType", Variable_getType },
00651 { "getTypeName", Variable_getTypeName },
00652 { "getMemberNames", Variable_getMemberNames },
00653 { "getMember", Variable_getMember },
00654 { "getMemberRaw", Variable_getMemberRaw },
00655 { "resize", Variable_resize },
00656 { "opBinary", Variable_opBinary },
00657 { "assign", Variable_update },
00658 { "__unm", Variable_unm },
00659 { "__add", Variable_add },
00660 { "__sub", Variable_sub },
00661 { "__mul", Variable_mul },
00662 { "__div", Variable_div },
00663 { "__mod", Variable_mod },
00664 { "__pow", Variable_pow },
00665 { "__eq", Variable_eq },
00666 { "__lt", Variable_lt },
00667 { "__le", Variable_le },
00668 { "__index", Variable_index },
00669 { "__newindex", Variable_newindex },
00670 { "__gc", GCMethod<DataSourceBase::shared_ptr> },
00671
00672 { NULL, NULL}
00673 };
00674
00675
00676
00677
00678
00679
00680 gen_push_bxptr(Property_push, "Property", PropertyBase)
00681
00682 static int Property_new(lua_State *L)
00683 {
00684 const char *type, *name, *desc;
00685 PropertyBase *pb;
00686 int argc = lua_gettop(L);
00687 type = luaL_checkstring(L, 1);
00688
00689
00690 name = (argc > 1) ? luaL_checkstring(L, 2) : "";
00691 desc = (argc > 2) ? luaL_checkstring(L, 3) : "";
00692
00693 types::TypeInfo *ti = types::TypeInfoRepository::Instance()->type(type);
00694
00695 if(!ti)
00696 luaL_error(L, "Property.new: unknown type %s", type);
00697
00698 pb = ti->buildProperty(name, desc);
00699 Property_push(L, pb);
00700 return 1;
00701 }
00702
00703 static int Property_get(lua_State *L)
00704 {
00705 PropertyBase *pb = *(luaM_checkudata_mt_bx(L, 1, "Property", PropertyBase));
00706 Variable_push_coerce(L, pb->getDataSource());
00707 return 1;
00708 }
00709
00710 static int Property_set(lua_State *L)
00711 {
00712 DataSourceBase::shared_ptr newdsb;
00713 DataSourceBase::shared_ptr *newdsbp;
00714 PropertyBase *pb = *(luaM_checkudata_mt_bx(L, 1, "Property", PropertyBase));
00715
00716 if ((newdsbp = luaM_testudata_mt(L, 2, "Variable", DataSourceBase::shared_ptr)) != NULL)
00717 newdsb = *newdsbp;
00718 else
00719 newdsb = Variable_fromlua(L, pb->getTypeInfo()->getTypeName().c_str(), 2);
00720
00721 DataSourceBase::shared_ptr propdsb = pb->getDataSource();
00722 propdsb->update(newdsb.get());
00723 return 1;
00724 }
00725
00726 static int Property_info(lua_State *L)
00727 {
00728 PropertyBase *pb = *(luaM_checkudata_mt_bx(L, 1, "Property", PropertyBase));
00729 lua_newtable(L);
00730 lua_pushstring(L, "name"); lua_pushstring(L, pb->getName().c_str()); lua_rawset(L, -3);
00731 lua_pushstring(L, "desc"); lua_pushstring(L, pb->getDescription().c_str()); lua_rawset(L, -3);
00732 lua_pushstring(L, "type"); lua_pushstring(L, pb->getType().c_str()); lua_rawset(L, -3);
00733 return 1;
00734 }
00735
00736 #if NOT_USED_YET
00737
00738
00739
00740
00741
00742 static int Property_gc(lua_State *L)
00743 {
00744 PropertyBase *pb = *(luaM_checkudata_mt_bx(L, 1, "Property", PropertyBase));
00745 delete pb;
00746 return 0;
00747 }
00748 #endif
00749
00750
00751 static int Property_del(lua_State *L)
00752 {
00753 PropertyBase *pb = *(luaM_checkudata_mt_bx(L, 1, "Property", PropertyBase));
00754 delete pb;
00755
00756
00757 luaL_getmetatable(L, "__dead__");
00758 lua_setmetatable(L, -2);
00759 return 0;
00760 }
00761
00762
00763
00764
00765
00766
00767
00768 static int Property_index(lua_State *L)
00769 {
00770 const char* key = luaL_checkstring(L, 2);
00771
00772 lua_getmetatable(L, 1);
00773 lua_getfield(L, -1, key);
00774
00775
00776 if(!lua_isnil(L, -1))
00777 return 1;
00778
00779 lua_settop(L, 2);
00780 Property_get(L);
00781 lua_replace(L, 1);
00782 return Variable_index(L);
00783 }
00784
00785 static int Property_newindex(lua_State *L)
00786 {
00787 Property_get(L);
00788 lua_replace(L, 1);
00789 return Variable_newindex(L);
00790 }
00791
00792 static const struct luaL_Reg Property_f [] = {
00793 {"new", Property_new },
00794 {"get", Property_get },
00795 {"set", Property_set },
00796 {"info", Property_info },
00797 {"delete", Property_del },
00798 {NULL, NULL}
00799 };
00800
00801 static const struct luaL_Reg Property_m [] = {
00802 {"get", Property_get },
00803 {"set", Property_set },
00804 {"info", Property_info },
00805
00806 {"delete", Property_del },
00807 {"__index", Property_index },
00808 {"__newindex", Property_newindex },
00809 {NULL, NULL}
00810 };
00811
00812
00813
00814
00815
00816
00817 static int Port_info(lua_State *L)
00818 {
00819 int arg_type;
00820 const char* port_type = NULL;
00821 PortInterface **pip;
00822 PortInterface *pi = NULL;
00823
00824 if((pip = (PortInterface**) luaL_testudata(L, 1, "InputPort")) != NULL) {
00825 pi = *pip;
00826 port_type = "in";
00827 } else if((pip = (PortInterface**) luaL_testudata(L, 1, "OutputPort")) != NULL) {
00828 pi = *pip;
00829 port_type = "out";
00830 }
00831 else {
00832 arg_type = lua_type(L, 1);
00833 luaL_error(L, "Port.info: invalid argument, expected Port, got %s",
00834 lua_typename(L, arg_type));
00835 }
00836
00837 lua_newtable(L);
00838 lua_pushstring(L, "name"); lua_pushstring(L, pi->getName().c_str()); lua_rawset(L, -3);
00839 lua_pushstring(L, "desc"); lua_pushstring(L, pi->getDescription().c_str()); lua_rawset(L, -3);
00840 lua_pushstring(L, "connected"); lua_pushboolean(L, pi->connected()); lua_rawset(L, -3);
00841 lua_pushstring(L, "isLocal"); lua_pushboolean(L, pi->isLocal()); lua_rawset(L, -3);
00842 lua_pushstring(L, "type"); lua_pushstring(L, pi->getTypeInfo()->getTypeName().c_str()); lua_rawset(L, -3);
00843 lua_pushstring(L, "porttype"); lua_pushstring(L, port_type); lua_rawset(L, -3);
00844
00845 return 1;
00846 }
00847
00848 static int Port_connect(lua_State *L)
00849 {
00850 int arg_type, ret;
00851 PortInterface **pip1, **pip2;
00852 PortInterface *pi1 = NULL;
00853 PortInterface *pi2 = NULL;
00854
00855 if((pip1 = (PortInterface**) luaL_testudata(L, 1, "InputPort")) != NULL) {
00856 pi1= *pip1;
00857 } else if((pip1 = (PortInterface**) luaL_testudata(L, 1, "OutputPort")) != NULL) {
00858 pi1= *pip1;
00859 }
00860 else {
00861 arg_type = lua_type(L, 1);
00862 luaL_error(L, "Port.info: invalid argument 1, expected Port, got %s",
00863 lua_typename(L, arg_type));
00864 }
00865 if((pip2 = (PortInterface**) luaL_testudata(L, 2, "InputPort")) != NULL) {
00866 pi2= *pip2;
00867 } else if((pip2 = (PortInterface**) luaL_testudata(L, 2, "OutputPort")) != NULL) {
00868 pi2= *pip2;
00869 }
00870 else {
00871 arg_type = lua_type(L, 2);
00872 luaL_error(L, "Port.connect: invalid argument 2, expected Port, got %s",
00873 lua_typename(L, arg_type));
00874 }
00875
00876 ret = pi1->connectTo(pi2);
00877 lua_pushboolean(L, ret);
00878
00879 return 1;
00880 }
00881
00882
00883
00884
00885
00886 gen_push_bxptr(InputPort_push, "InputPort", InputPortInterface)
00887
00888 static int InputPort_new(lua_State *L)
00889 {
00890 const char *type, *name, *desc;
00891 InputPortInterface* ipi;
00892 int argc = lua_gettop(L);
00893
00894 type = luaL_checkstring(L, 1);
00895
00896
00897 name = (argc > 1) ? luaL_checkstring(L, 2) : "";
00898 desc = (argc > 2) ? luaL_checkstring(L, 3) : "";
00899
00900 types::TypeInfo *ti = types::TypeInfoRepository::Instance()->type(type);
00901 if(ti==0)
00902 luaL_error(L, "InputPort.new: unknown type %s", type);
00903
00904 ipi = ti->inputPort(name);
00905 ipi->doc(desc);
00906 InputPort_push(L, ipi);
00907 return 1;
00908 }
00909
00910 static int InputPort_read(lua_State *L)
00911 {
00912 int ret = 1;
00913 InputPortInterface *ip = *(luaM_checkudata_mt_bx(L, 1, "InputPort", InputPortInterface));
00914 DataSourceBase::shared_ptr dsb;
00915 DataSourceBase::shared_ptr *dsbp;
00916 FlowStatus fs;
00917
00918
00919 if ((dsbp = luaM_testudata_mt(L, 2, "Variable", DataSourceBase::shared_ptr)) != NULL)
00920 dsb = *dsbp;
00921 else {
00922 dsb = ip->getTypeInfo()->buildValue();
00923 ret = 2;
00924 }
00925
00926 fs = ip->read(dsb);
00927
00928 if(fs == NoData) lua_pushstring(L, "NoData");
00929 else if (fs == NewData) lua_pushstring(L, "NewData");
00930 else if (fs == OldData) lua_pushstring(L, "OldData");
00931 else luaL_error(L, "InputPort.read: unknown FlowStatus returned");
00932
00933 if(ret>1)
00934 Variable_push_coerce(L, dsb);
00935
00936 return ret;
00937 }
00938
00939 #ifdef NOT_USED_YET
00940 static int InputPort_gc(lua_State *L)
00941 {
00942 InputPortInterface *ip = *(luaM_checkudata_mt_bx(L, 1, "InputPort", InputPortInterface));
00943 delete ip;
00944 return 0;
00945 }
00946 #endif
00947
00948
00949 static int InputPort_del(lua_State *L)
00950 {
00951 InputPortInterface *ip = *(luaM_checkudata_mt_bx(L, 1, "InputPort", InputPortInterface));
00952 delete ip;
00953
00954
00955 luaL_getmetatable(L, "__dead__");
00956 lua_setmetatable(L, -2);
00957 return 0;
00958 }
00959
00960 static const struct luaL_Reg InputPort_f [] = {
00961 {"new", InputPort_new },
00962 {"read", InputPort_read },
00963 {"info", Port_info },
00964 {"connect", Port_connect },
00965 {"delete", InputPort_del },
00966 {NULL, NULL}
00967 };
00968
00969 static const struct luaL_Reg InputPort_m [] = {
00970 {"read", InputPort_read },
00971 {"info", Port_info },
00972 {"delete", InputPort_del },
00973 {"connect", Port_connect },
00974
00975 {NULL, NULL}
00976 };
00977
00978
00979
00980 gen_push_bxptr(OutputPort_push, "OutputPort", OutputPortInterface)
00981
00982
00983 static int OutputPort_new(lua_State *L)
00984 {
00985 const char *type, *name, *desc;
00986 OutputPortInterface* opi;
00987 int argc = lua_gettop(L);
00988
00989 type = luaL_checkstring(L, 1);
00990
00991
00992 name = (argc > 1) ? luaL_checkstring(L, 2) : "";
00993 desc = (argc > 2) ? luaL_checkstring(L, 3) : "";
00994
00995 types::TypeInfo *ti = types::TypeInfoRepository::Instance()->type(type);
00996
00997 if(ti==0)
00998 luaL_error(L, "OutputPort.new: unknown type %s", type);
00999
01000 opi = ti->outputPort(name);
01001 opi->doc(desc);
01002 OutputPort_push(L, opi);
01003 return 1;
01004 }
01005
01006 static int OutputPort_write(lua_State *L)
01007 {
01008 DataSourceBase::shared_ptr dsb;
01009 DataSourceBase::shared_ptr *dsbp;
01010
01011 OutputPortInterface *op = *(luaM_checkudata_mt_bx(L, 1, "OutputPort", OutputPortInterface));
01012
01013
01014 if ((dsbp = luaM_testudata_mt(L, 2, "Variable", DataSourceBase::shared_ptr)) != NULL) {
01015 dsb = *dsbp;
01016 } else {
01017
01018 std::string type = op->getTypeInfo()->getTypeName();
01019 dsb = Variable_fromlua(L, type.c_str(), 2);
01020 }
01021 op->write(dsb);
01022 return 0;
01023 }
01024
01025 #ifdef NOT_USED_YET
01026 static int OutputPort_gc(lua_State *L)
01027 {
01028 OutputPortInterface *op = *(luaM_checkudata_mt_bx(L, 1, "OutputPort", OutputPortInterface));
01029 delete op;
01030 return 0;
01031 }
01032 #endif
01033
01034
01035 static int OutputPort_del(lua_State *L)
01036 {
01037 OutputPortInterface *op = *(luaM_checkudata_mt_bx(L, 1, "OutputPort", OutputPortInterface));
01038 delete op;
01039
01040
01041 luaL_getmetatable(L, "__dead__");
01042 lua_setmetatable(L, -2);
01043 return 0;
01044 }
01045
01046 static const struct luaL_Reg OutputPort_f [] = {
01047 {"new", OutputPort_new },
01048 {"write", OutputPort_write },
01049 {"info", Port_info },
01050 {"connect", Port_connect },
01051 {"delete", OutputPort_del },
01052 {NULL, NULL}
01053 };
01054
01055 static const struct luaL_Reg OutputPort_m [] = {
01056 {"write", OutputPort_write },
01057 {"info", Port_info },
01058 {"connect", Port_connect },
01059 {"delete", OutputPort_del },
01060
01061 {NULL, NULL}
01062 };
01063
01064
01065
01066
01067
01068 gen_push_bxptr(Operation_push, "Operation", OperationInterfacePart)
01069
01070 static int Operation_info(lua_State *L)
01071 {
01072 int i=1;
01073 OperationInterfacePart *oip = *(luaM_checkudata_mt_bx(L, 1, "Operation", OperationInterfacePart));
01074 std::vector<ArgumentDescription> args;
01075
01076 lua_pushstring(L, oip->getName().c_str());
01077 lua_pushstring(L, oip->description().c_str());
01078 lua_pushstring(L, oip->resultType().c_str());
01079 lua_pushinteger(L, oip->arity());
01080
01081 args = oip->getArgumentList();
01082
01083 lua_newtable(L);
01084
01085 for (std::vector<ArgumentDescription>::iterator it = args.begin(); it != args.end(); it++) {
01086 lua_newtable(L);
01087 lua_pushstring(L, "name"); lua_pushstring(L, it->name.c_str()); lua_rawset(L, -3);
01088 lua_pushstring(L, "type"); lua_pushstring(L, it->type.c_str()); lua_rawset(L, -3);
01089 lua_pushstring(L, "desc"); lua_pushstring(L, it->description.c_str()); lua_rawset(L, -3);
01090 lua_rawseti(L, -2, i++);
01091 }
01092
01093 return 5;
01094 }
01095
01096 static int __Operation_call(lua_State *L)
01097 {
01098 std::vector<base::DataSourceBase::shared_ptr> args;
01099 DataSourceBase::shared_ptr dsb;
01100 DataSourceBase::shared_ptr *dsbp;
01101 DataSourceBase::shared_ptr ret, ret2;
01102 types::TypeInfo *ti;
01103
01104 unsigned int argc = lua_gettop(L);
01105 TaskContext *this_tc = __getTC(L);
01106 OperationInterfacePart *oip = *(luaM_checkudata_mt_bx(L, 1, "Operation", OperationInterfacePart));
01107
01108 if(oip->arity() != argc-1)
01109 luaL_error(L, "Operation.call: wrong number of args. expected %d, got %d", oip->arity(), argc-1);
01110
01111 for(unsigned int arg=2; arg<=argc; arg++) {
01112
01113 if ((dsbp = luaM_testudata_mt(L, arg, "Variable", DataSourceBase::shared_ptr)) != NULL) {
01114 dsb = *dsbp;
01115 } else {
01116
01117 std::string type = oip->getArgumentType(arg-1)->getTypeName().c_str();
01118 dsb = Variable_fromlua(L, type.c_str(), arg);
01119 }
01120 args.push_back(dsb);
01121 }
01122
01123 ret = oip->produce(args, this_tc->engine());
01124
01125
01126
01127
01128 if(oip->resultType() != "void") {
01129 ti = types::TypeInfoRepository::Instance()->type(oip->resultType());
01130 if(!ti)
01131 luaL_error(L, "Operation.call: can't create return value DSB of type '%s'",
01132 oip->resultType().c_str());
01133 ret2 = ti->buildValue();
01134 ret2->update(ret.get());
01135 Variable_push_coerce(L, ret2);
01136 } else {
01137 ret->evaluate();
01138 lua_pushnil(L);
01139 }
01140 return 1;
01141 }
01142
01143 static int Operation_call(lua_State *L)
01144 {
01145 int ret;
01146 try {
01147 ret = __Operation_call(L);
01148 } catch(...) {
01149 luaL_error(L, "Operation.call: caught exception");
01150 }
01151 return ret;
01152 }
01153
01154 static int __Operation_send(lua_State *L)
01155 {
01156 DataSourceBase::shared_ptr dsb, *dsbp;
01157
01158 unsigned int argc = lua_gettop(L);
01159 TaskContext *this_tc = __getTC(L);
01160 OperationInterfacePart *oip = *(luaM_checkudata_mt_bx(L, 1, "Operation", OperationInterfacePart));
01161
01162 OperationCallerC *occ = new OperationCallerC(oip, oip->getName(), this_tc->engine());
01163
01164 if(oip->arity() != argc-1)
01165 luaL_error(L, "Operation.send: wrong number of args. expected %d, got %d",
01166 oip->arity(), argc);
01167
01168 for(unsigned int arg=2; arg<=argc; arg++) {
01169
01170 if ((dsbp = luaM_testudata_mt(L, arg, "Variable", DataSourceBase::shared_ptr)) != NULL) {
01171 dsb = *dsbp;
01172 } else {
01173
01174 std::string type = oip->getArgumentType(arg-1)->getTypeName().c_str();
01175 dsb = Variable_fromlua(L, type.c_str(), arg);
01176 }
01177 occ->arg(dsb);
01178 }
01179
01180
01181 luaM_pushobject_mt(L, "SendHandle", SendHandleC)(occ->send());
01182 return 1;
01183 }
01184
01185 static int Operation_send(lua_State *L)
01186 {
01187 int ret;
01188 try {
01189 ret = __Operation_send(L);
01190 } catch(...) {
01191 luaL_error(L, "Operation.send: caught exception");
01192 }
01193 return ret;
01194 }
01195
01196
01197 static const struct luaL_Reg Operation_f [] = {
01198 { "info", Operation_info },
01199 { "call", Operation_call },
01200 { "send", Operation_send },
01201 { NULL, NULL }
01202
01203 };
01204
01205 static const struct luaL_Reg Operation_m [] = {
01206 { "info", Operation_info },
01207 { "send", Operation_send },
01208 { "__call", Operation_call },
01209 { NULL, NULL }
01210 };
01211
01212
01213
01214
01215
01216 static int Service_getName(lua_State *L)
01217 {
01218 Service::shared_ptr srv = *(luaM_checkudata_mt(L, 1, "Service", Service::shared_ptr));
01219 lua_pushstring(L, srv->getName().c_str());
01220 return 1;
01221 }
01222
01223 static int Service_doc(lua_State *L)
01224 {
01225 int ret;
01226 const char* doc;
01227 Service::shared_ptr srv = *(luaM_checkudata_mt(L, 1, "Service", Service::shared_ptr));
01228 if(lua_gettop(L) == 1) {
01229 lua_pushstring(L, srv->doc().c_str());
01230 ret = 1;
01231 } else {
01232 doc = luaL_checkstring(L, 2);
01233 srv->doc(doc);
01234 ret = 0;
01235 }
01236
01237 return ret;
01238 }
01239
01240 static int Service_getProviderNames(lua_State *L)
01241 {
01242 Service::shared_ptr srv = *(luaM_checkudata_mt(L, 1, "Service", Service::shared_ptr));
01243 push_vect_str(L, srv->getProviderNames());
01244 return 1;
01245 }
01246
01247 static int Service_getOperationNames(lua_State *L)
01248 {
01249 Service::shared_ptr srv = *(luaM_checkudata_mt(L, 1, "Service", Service::shared_ptr));
01250 push_vect_str(L, srv->getOperationNames());
01251 return 1;
01252 }
01253
01254 static int Service_hasOperation(lua_State *L)
01255 {
01256 int ret;
01257 Service::shared_ptr srv = *(luaM_checkudata_mt(L, 1, "Service", Service::shared_ptr));
01258 const char* op = luaL_checkstring(L, 2);
01259 ret = srv->hasOperation(op);
01260 lua_pushboolean(L, ret);
01261 return 1;
01262 }
01263
01264 static int Service_getPortNames(lua_State *L)
01265 {
01266 Service::shared_ptr srv = *(luaM_checkudata_mt(L, 1, "Service", Service::shared_ptr));
01267 push_vect_str(L, srv->getPortNames());
01268 return 1;
01269 }
01270
01271 static int Service_provides(lua_State *L)
01272 {
01273 int ret, i, argc;
01274 const char* subsrv_str;
01275 Service::shared_ptr srv, subsrv;
01276
01277 srv = *(luaM_checkudata_mt(L, 1, "Service", Service::shared_ptr));
01278 argc=lua_gettop(L);
01279
01280
01281 if(argc == 1) {
01282 ret = 1;
01283 goto out;
01284 }
01285
01286 for(i=2; i<=argc; i++) {
01287 subsrv_str = luaL_checkstring(L, i);
01288
01289 subsrv = srv->getService(subsrv_str);
01290 if (subsrv == 0)
01291 luaL_error(L, "Service.provides: no subservice %s of service %s",
01292 srv->getName().c_str(), subsrv_str);
01293 else
01294 luaM_pushobject_mt(L, "Service", Service::shared_ptr)(subsrv);
01295 }
01296 ret = argc - 1;
01297
01298 out:
01299 return ret;
01300 }
01301
01302 static int Service_getOperation(lua_State *L)
01303 {
01304 const char *op_str;
01305 OperationInterfacePart *oip;
01306 Service::shared_ptr srv;
01307
01308 srv = *(luaM_checkudata_mt(L, 1, "Service", Service::shared_ptr));
01309 op_str = luaL_checkstring(L, 2);
01310 oip = srv->getOperation(op_str);
01311
01312 if(!oip)
01313 luaL_error(L, "Service_getOperation: service %s has no operation %s",
01314 srv->getName().c_str(), op_str);
01315 Operation_push(L, oip);
01316 return 1;
01317 }
01318
01319 static int Service_getPort(lua_State *L)
01320 {
01321 const char* name;
01322 PortInterface *pi;
01323 InputPortInterface *ipi;
01324 OutputPortInterface *opi;
01325
01326 Service::shared_ptr srv;
01327
01328 srv = *(luaM_checkudata_mt(L, 1, "Service", Service::shared_ptr));
01329 name = luaL_checkstring(L, 2);
01330
01331 pi = srv->getPort(name);
01332 if(!pi)
01333 luaL_error(L, "Service.getPort: service %s has no port %",
01334 srv->getName().c_str(), name);
01335
01336
01337 if ((ipi = dynamic_cast<InputPortInterface *> (pi)) != NULL)
01338 InputPort_push(L, ipi);
01339 else if ((opi = dynamic_cast<OutputPortInterface *> (pi)) != NULL)
01340 OutputPort_push(L, opi);
01341 else
01342 luaL_error(L, "Service.getPort: unknown port type returned");
01343
01344 return 1;
01345 }
01346
01347 static const struct luaL_Reg Service_f [] = {
01348 { "getName", Service_getName },
01349 { "doc", Service_doc },
01350 { "getProviderNames", Service_getProviderNames },
01351 { "getOperationNames", Service_getOperationNames },
01352 { "hasOperation", Service_hasOperation },
01353 { "getPortNames", Service_getPortNames },
01354 { "provides", Service_provides },
01355 { "getOperation", Service_getOperation },
01356 { "getPort", Service_getPort },
01357 { NULL, NULL }
01358 };
01359
01360 static const struct luaL_Reg Service_m [] = {
01361 { "getName", Service_getName },
01362 { "doc", Service_doc },
01363 { "getProviderNames", Service_getProviderNames },
01364 { "getOperationNames", Service_getOperationNames },
01365 { "hasOperation", Service_hasOperation },
01366 { "getPortNames", Service_getPortNames },
01367 { "provides", Service_provides },
01368 { "getOperation", Service_getOperation },
01369 { "getPort", Service_getPort },
01370 { "__gc", GCMethod<Service::shared_ptr> },
01371 { NULL, NULL }
01372 };
01373
01374
01375
01376
01377
01378 gen_push_bxptr(ServiceRequester_push, "ServiceRequester", ServiceRequester)
01379
01380 static int ServiceRequester_getRequestName(lua_State *L)
01381 {
01382 ServiceRequester *sr;
01383
01384 sr = *(luaM_checkudata_bx(L, 1, ServiceRequester));
01385 lua_pushstring(L, sr->getRequestName().c_str());
01386 return 1;
01387 }
01388
01389 static int ServiceRequester_getRequesterNames(lua_State *L)
01390 {
01391 ServiceRequester *sr;
01392 sr = *(luaM_checkudata_bx(L, 1, ServiceRequester));
01393 push_vect_str(L, sr->getRequesterNames());
01394 return 1;
01395 }
01396
01397 static int ServiceRequester_ready(lua_State *L)
01398 {
01399 int ret;
01400 ServiceRequester *sr;
01401 sr = *(luaM_checkudata_bx(L, 1, ServiceRequester));
01402 ret = sr->ready();
01403 lua_pushboolean(L, ret);
01404 return 1;
01405 }
01406
01407 static int ServiceRequester_disconnect(lua_State *L)
01408 {
01409 ServiceRequester *sr;
01410 sr = *(luaM_checkudata_bx(L, 1, ServiceRequester));
01411 sr->disconnect();
01412 return 0;
01413 }
01414
01415 static int ServiceRequester_requires(lua_State *L)
01416 {
01417 int argc, ret, i;
01418 const char* subsr_str;
01419 ServiceRequester *sr;
01420 ServiceRequester *subsr;
01421
01422 sr = *(luaM_checkudata_bx(L, 1, ServiceRequester));
01423 argc = lua_gettop(L);
01424
01425
01426 if(argc == 1) {
01427 ret = 1;
01428 goto out;
01429 }
01430
01431 for(i=2; i<=argc; i++) {
01432 subsr_str = luaL_checkstring(L, i);
01433 subsr = sr->requires(subsr_str);
01434 if (subsr == 0)
01435 luaL_error(L, "ServiceRequester: no required subservice %s of service %s",
01436 subsr_str, sr->getRequestName().c_str());
01437 else
01438 ServiceRequester_push(L, subsr);
01439 }
01440 ret = argc - 1;
01441
01442 out:
01443 return ret;
01444 }
01445
01446 static const struct luaL_Reg ServiceRequester_f [] = {
01447 { "getRequestName", ServiceRequester_getRequestName },
01448 { "getRequesterNames", ServiceRequester_getRequesterNames },
01449 { "ready", ServiceRequester_ready },
01450 { "disconnect", ServiceRequester_disconnect },
01451 { "requires", ServiceRequester_requires },
01452 { NULL, NULL }
01453 };
01454
01455 static const struct luaL_Reg ServiceRequester_m [] = {
01456 { "getRequestName", ServiceRequester_getRequestName },
01457 { "getRequesterNames", ServiceRequester_getRequesterNames },
01458 { "ready", ServiceRequester_ready },
01459 { "disconnect", ServiceRequester_disconnect },
01460 { "requires", ServiceRequester_requires },
01461 { NULL, NULL }
01462 };
01463
01464
01465
01466
01467
01468
01469 gen_push_bxptr(TaskContext_push, "TaskContext", TaskContext)
01470
01471 static int TaskContext_getName(lua_State *L)
01472 {
01473 const char *s;
01474 TaskContext *tc = *(luaM_checkudata_bx(L, 1, TaskContext));
01475 s = tc->getName().c_str();
01476 lua_pushstring(L, s);
01477 return 1;
01478 }
01479
01480 static int TaskContext_start(lua_State *L)
01481 {
01482 TaskContext *tc = *(luaM_checkudata_bx(L, 1, TaskContext));
01483 bool b = tc->start();
01484 lua_pushboolean(L, b);
01485 return 1;
01486 }
01487
01488 static int TaskContext_stop(lua_State *L)
01489 {
01490 TaskContext *tc = *(luaM_checkudata_bx(L, 1, TaskContext));
01491 bool b = tc->stop();
01492 lua_pushboolean(L, b);
01493 return 1;
01494 }
01495
01496 static int TaskContext_configure(lua_State *L)
01497 {
01498 TaskContext *tc = *(luaM_checkudata_bx(L, 1, TaskContext));
01499 bool ret = tc->configure();
01500 lua_pushboolean(L, ret);
01501 return 1;
01502 }
01503
01504 static int TaskContext_activate(lua_State *L)
01505 {
01506 TaskContext *tc = *(luaM_checkudata_bx(L, 1, TaskContext));
01507 bool ret = tc->activate();
01508 lua_pushboolean(L, ret);
01509 return 1;
01510 }
01511
01512 static int TaskContext_cleanup(lua_State *L)
01513 {
01514 TaskContext *tc = *(luaM_checkudata_bx(L, 1, TaskContext));
01515 bool ret = tc->cleanup();
01516 lua_pushboolean(L, ret);
01517 return 1;
01518 }
01519
01520 static int TaskContext_getState(lua_State *L)
01521 {
01522 TaskCore::TaskState ts;
01523 TaskContext **tc = (TaskContext**) luaM_checkudata_bx(L, 1, TaskContext);
01524 ts = (*tc)->getTaskState();
01525
01526 switch(ts) {
01527 case TaskCore::Init: lua_pushstring(L, "Init"); break;
01528 case TaskCore::PreOperational: lua_pushstring(L, "PreOperational"); break;
01529 case TaskCore::FatalError: lua_pushstring(L, "FatalError"); break;
01530 case TaskCore::Exception: lua_pushstring(L, "Exception"); break;
01531 case TaskCore::Stopped: lua_pushstring(L, "Stopped"); break;
01532 case TaskCore::Running: lua_pushstring(L, "Running"); break;
01533 case TaskCore::RunTimeError: lua_pushstring(L, "RunTimeError"); break;
01534 default: lua_pushstring(L, "unknown");
01535 }
01536 return 1;
01537 }
01538
01539
01540
01541 static int TaskContext_getPeers(lua_State *L)
01542 {
01543 TaskContext *tc = *(luaM_checkudata_bx(L, 1, TaskContext));
01544 std::vector<std::string> plist = tc->getPeerList();
01545 push_vect_str(L, plist);
01546 return 1;
01547 }
01548
01549
01550 static int TaskContext_addPeer(lua_State *L)
01551 {
01552 bool ret;
01553 TaskContext *self = *(luaM_checkudata_bx(L, 1, TaskContext));
01554 TaskContext *peer = *(luaM_checkudata_bx(L, 2, TaskContext));
01555 ret = self->addPeer(peer);
01556 lua_pushboolean(L, ret);
01557 return 1;
01558 }
01559
01560
01561 static int TaskContext_removePeer(lua_State *L)
01562 {
01563 std::string peer;
01564 TaskContext *self = *(luaM_checkudata_bx(L, 1, TaskContext));
01565 peer = luaL_checkstring(L, 2);
01566 self->removePeer(peer);
01567 return 0;
01568 }
01569
01570
01571 static int TaskContext_getPeer(lua_State *L)
01572 {
01573 std::string strpeer;
01574 TaskContext *peer;
01575 TaskContext *self = *(luaM_checkudata_bx(L, 1, TaskContext));
01576 strpeer = luaL_checkstring(L, 2);
01577 peer = self->getPeer(strpeer);
01578
01579 if(!peer) {
01580 luaL_error(L, "TaskContext.getPeer: no peer %s", strpeer.c_str());
01581 goto out;
01582 }
01583
01584 TaskContext_push(L, peer);
01585 out:
01586 return 1;
01587 }
01588
01589 static int TaskContext_getPortNames(lua_State *L)
01590 {
01591 TaskContext *tc = *(luaM_checkudata_bx(L, 1, TaskContext));
01592 std::vector<std::string> plist = tc->ports()->getPortNames();
01593 push_vect_str(L, plist);
01594 return 1;
01595 }
01596
01597
01598 #if 0
01599 static int __tc_addport(lua_State *L, TaskContext *tc, int tcind,
01600 PortInterface *pi, int piind)
01601 {
01602 int len;
01603 _DBG("in: gettop=%d", lua_gettop(L));
01604
01605
01606
01607 lua_getfield(L, LUA_ENVIRONINDEX, "PortOwners");
01608 if (lua_isnil(L, -1)) {
01609 _DBG("creating new PortOwners");
01610 lua_pop(L, 1);
01611 lua_newtable(L);
01612 } else {
01613 _DBG("PortOwners exists... len=%d", lua_objlen(L, -1));
01614 }
01615
01616
01617 lua_pushvalue(L, piind);
01618 lua_rawget(L, -2);
01619
01620 if (lua_isnil(L, -1)) {
01621 _DBG("creating new PortOwners[0x%x]", (unsigned long) pi);
01622 lua_pop(L, 1);
01623 lua_newtable(L);
01624 } else {
01625 _DBG("PortOwners[0x%x] exists, len=%d", (unsigned long) pi, lua_objlen(L, -1));
01626 }
01627
01628 lua_pushvalue(L, 1);
01629 len = lua_objlen(L, -2);
01630 lua_rawseti(L, -2, len+1);
01631
01632
01633 lua_pushvalue(L, piind);
01634 lua_rawset(L, -3);
01635
01636 lua_setfield(L, LUA_ENVIRONINDEX, "PortOwners");
01637
01638 _DBG("out: gettop=%d", lua_gettop(L));
01639 }
01640 #endif
01641
01642 static int TaskContext_addPort(lua_State *L)
01643 {
01644 const char* name, *desc;
01645 PortInterface **pi;
01646 int argc = lua_gettop(L);
01647 TaskContext *tc = *(luaM_checkudata_bx(L, 1, TaskContext));
01648
01649 pi = (PortInterface**) luaL_testudata(L, 2, "InputPort");
01650 if(pi) goto check_name;
01651
01652 pi = (PortInterface**) luaL_testudata(L, 2, "OutputPort");
01653 if(pi) goto check_name;
01654
01655 return luaL_error(L, "addPort: invalid argument, not a Port");
01656
01657 check_name:
01658 if(argc > 2) {
01659 name = luaL_checkstring(L, 3);
01660 (*pi)->setName(name);
01661 }
01662
01663 if(argc > 3) {
01664 desc = luaL_checkstring(L, 4);
01665 (*pi)->doc(desc);
01666 }
01667
01668 tc->ports()->addPort(**pi);
01669 return 0;
01670 }
01671
01672 static int TaskContext_addEventPort(lua_State *L)
01673 {
01674 const char* name, *desc;
01675 InputPortInterface **ipi;
01676 int argc = lua_gettop(L);
01677 TaskContext *tc = *(luaM_checkudata_bx(L, 1, TaskContext));
01678
01679 if((ipi = (InputPortInterface**) luaL_testudata(L, 2, "InputPort")) == NULL)
01680 return luaL_error(L, "addEventPort: invalid argument, not an InputPort");
01681
01682 if(argc > 2) {
01683 name = luaL_checkstring(L, 3);
01684 (*ipi)->setName(name);
01685 }
01686
01687 if(argc > 3) {
01688 desc = luaL_checkstring(L, 4);
01689 (*ipi)->doc(desc);
01690 }
01691
01692 tc->ports()->addEventPort(**ipi);
01693 return 0;
01694 }
01695
01696 static int TaskContext_getPort(lua_State *L)
01697 {
01698 const char* name;
01699 PortInterface *pi;
01700 InputPortInterface *ipi;
01701 OutputPortInterface *opi;
01702
01703 TaskContext *tc = *(luaM_checkudata_bx(L, 1, TaskContext));
01704 name = luaL_checkstring(L, 2);
01705
01706 pi = tc->getPort(name);
01707 if(!pi)
01708 luaL_error(L, "TaskContext.getPort: no port %s for taskcontext %s",
01709 name, tc->getName().c_str());
01710
01711
01712 if ((ipi = dynamic_cast<InputPortInterface *> (pi)) != NULL)
01713 InputPort_push(L, ipi);
01714 else if ((opi = dynamic_cast<OutputPortInterface *> (pi)) != NULL)
01715 OutputPort_push(L, opi);
01716 else
01717 luaL_error(L, "TaskContext.getPort: unknown port returned");
01718
01719 return 1;
01720 }
01721
01722 static int TaskContext_removePort(lua_State *L)
01723 {
01724 TaskContext *tc = *(luaM_checkudata_bx(L, 1, TaskContext));
01725 const char *port = luaL_checkstring(L, 2);
01726 tc->ports()->removePort(port);
01727 return 0;
01728 }
01729
01730 static int TaskContext_addProperty(lua_State *L)
01731 {
01732 const char *name, *desc;
01733 int argc = lua_gettop(L);
01734 TaskContext *tc = *(luaM_checkudata_bx(L, 1, TaskContext));
01735 PropertyBase *pb = *(luaM_checkudata_mt_bx(L, 2, "Property", PropertyBase));
01736
01737 if(argc > 2) {
01738 name = luaL_checkstring(L, 3);
01739 pb->setName(name);
01740 }
01741
01742 if(argc > 3) {
01743 desc = luaL_checkstring(L, 4);
01744 pb->setDescription(desc);
01745 }
01746
01747
01748 if(!tc->addProperty(*pb))
01749 luaL_error(L, "TaskContext.addProperty: failed to add property %s.",
01750 pb->getName().c_str());
01751
01752 return 0;
01753 }
01754
01755 static int TaskContext_getProperty(lua_State *L)
01756 {
01757 const char *name;
01758 PropertyBase *prop;
01759
01760 TaskContext *tc = *(luaM_checkudata_bx(L, 1, TaskContext));
01761 name = luaL_checkstring(L, 2);
01762
01763 prop = tc->getProperty(name);
01764
01765 if(!prop)
01766 luaL_error(L, "%s failed. No such property", __FILE__);
01767
01768 Property_push(L, prop);
01769 return 1;
01770 }
01771
01772 static int TaskContext_getProperties(lua_State *L)
01773 {
01774 TaskContext *tc = *(luaM_checkudata_bx(L, 1, TaskContext));
01775 vector<PropertyBase*> props = tc->properties()->getProperties();
01776
01777 int key = 1;
01778 lua_createtable(L, props.size(), 0);
01779 for(vector<PropertyBase*>::iterator it = props.begin(); it != props.end(); ++it) {
01780 Property_push(L, *it);
01781 lua_rawseti(L, -2, key++);
01782 }
01783
01784 return 1;
01785 }
01786
01787 static int TaskContext_removeProperty(lua_State *L)
01788 {
01789 const char *name;
01790 PropertyBase *prop;
01791
01792 TaskContext *tc = *(luaM_checkudata_bx(L, 1, TaskContext));
01793 name = luaL_checkstring(L, 2);
01794
01795 prop = tc->getProperty(name);
01796
01797 if(!prop)
01798 luaL_error(L, "%s failed. No such property", __FILE__);
01799
01800 tc->properties()->remove(prop);
01801 return 0;
01802 }
01803
01804
01805 static int TaskContext_getOps(lua_State *L)
01806 {
01807 TaskContext *tc = *(luaM_checkudata_bx(L, 1, TaskContext));
01808 std::vector<std::string> oplst = tc->operations()->getNames();
01809 push_vect_str(L, oplst);
01810 return 1;
01811 }
01812
01813
01814 static int TaskContext_getOpInfo(lua_State *L)
01815 {
01816 int i=1;
01817 TaskContext *tc = *(luaM_checkudata_bx(L, 1, TaskContext));
01818 const char *op = luaL_checkstring(L, 2);
01819 std::vector<ArgumentDescription> args;
01820
01821 if(!tc->operations()->hasMember(op))
01822 luaL_error(L, "TaskContext.getOpInfo failed: no such operation");
01823
01824 lua_pushstring(L, tc->operations()->getResultType(op).c_str());
01825 lua_pushinteger(L, tc->operations()->getArity(op));
01826 lua_pushstring(L, tc->operations()->getDescription(op).c_str());
01827
01828 args = tc->operations()->getArgumentList(op);
01829
01830 lua_newtable(L);
01831
01832 for (std::vector<ArgumentDescription>::iterator it = args.begin(); it != args.end(); it++) {
01833 lua_newtable(L);
01834 lua_pushstring(L, "name"); lua_pushstring(L, it->name.c_str()); lua_rawset(L, -3);
01835 lua_pushstring(L, "type"); lua_pushstring(L, it->type.c_str()); lua_rawset(L, -3);
01836 lua_pushstring(L, "desc"); lua_pushstring(L, it->description.c_str()); lua_rawset(L, -3);
01837 lua_rawseti(L, -2, i++);
01838 }
01839
01840 return 4;
01841 }
01842
01843 static int TaskContext_provides(lua_State *L)
01844 {
01845 TaskContext *tc = *(luaM_checkudata_bx(L, 1, TaskContext));
01846 Service::shared_ptr srv = tc->provides();
01847
01848 if(srv == 0)
01849 luaL_error(L, "TaskContext.provides: no default service");
01850
01851
01852 luaM_pushobject_mt(L, "Service", Service::shared_ptr)(srv);
01853 lua_replace(L, 1);
01854 return Service_provides(L);
01855 }
01856
01857 static int TaskContext_requires(lua_State *L)
01858 {
01859 ServiceRequester *sr;
01860 TaskContext *tc = *(luaM_checkudata_bx(L, 1, TaskContext));
01861 sr = tc->requires();
01862
01863 if(!sr)
01864 luaL_error(L, "TaskContext.requires returned NULL");
01865
01866 ServiceRequester_push(L, sr);
01867 lua_replace(L, 1);
01868 return ServiceRequester_requires(L);
01869 }
01870
01871 static int TaskContext_connectServices(lua_State *L)
01872 {
01873 int ret;
01874 TaskContext *tc = *(luaM_checkudata_bx(L, 1, TaskContext));
01875 TaskContext *peer = *(luaM_checkudata_bx(L, 2, TaskContext));
01876 ret = tc->connectServices(peer);
01877 lua_pushboolean(L, ret);
01878 return 1;
01879 }
01880
01881 static int __TaskContext_call(lua_State *L)
01882 {
01883 unsigned int argc = lua_gettop(L);
01884 TaskContext *tc = *(luaM_checkudata_bx(L, 1, TaskContext));
01885 const char *op = luaL_checkstring(L, 2);
01886
01887 TaskContext *this_tc = __getTC(L);
01888 std::vector<base::DataSourceBase::shared_ptr> args;
01889 DataSourceBase::shared_ptr dsb;
01890 DataSourceBase::shared_ptr *dsbp;
01891 DataSourceBase::shared_ptr ret, ret2;
01892 types::TypeInfo *ti;
01893
01894 OperationInterfacePart *orp = tc->operations()->getPart(op);
01895
01896 if(!orp)
01897 luaL_error(L, "TaskContext.call: no operation %s", op);
01898
01899 if(orp->arity() != argc-2)
01900 luaL_error(L, "TaskContext.call: wrong number of args. expected %d, got %d", orp->arity(), argc-2);
01901
01902 for(unsigned int arg=3; arg<=argc; arg++) {
01903
01904 if ((dsbp = luaM_testudata_mt(L, arg, "Variable", DataSourceBase::shared_ptr)) != NULL) {
01905 dsb = *dsbp;
01906 } else {
01907
01908 std::string type = orp->getArgumentType(arg-2)->getTypeName().c_str();
01909 dsb = Variable_fromlua(L, type.c_str(), arg);
01910 }
01911 args.push_back(dsb);
01912 }
01913
01914 ret = tc->operations()->produce(op, args, this_tc->engine());
01915
01916
01917
01918
01919 if(orp->resultType() != "void") {
01920 ti = types::TypeInfoRepository::Instance()->type(orp->resultType());
01921 if(!ti)
01922 luaL_error(L, "TaskContext.call: failed to construct result type %s",
01923 orp->resultType().c_str());
01924 ret2 = ti->buildValue();
01925 ret2->update(ret.get());
01926 Variable_push_coerce(L, ret2);
01927 } else {
01928 ret->evaluate();
01929 lua_pushnil(L);
01930 }
01931 return 1;
01932 }
01933
01934 static int TaskContext_call(lua_State *L)
01935 {
01936 int ret;
01937 try {
01938 ret = __TaskContext_call(L);
01939 } catch(...) {
01940 luaL_error(L, "TaskContext.call: caught exception");
01941 }
01942 return ret;
01943 }
01944
01945
01946 static int TaskContext_hasOperation(lua_State *L)
01947 {
01948 TaskContext *tc = *(luaM_checkudata_bx(L, 1, TaskContext));
01949 Service::shared_ptr srv = tc->provides();
01950
01951 if(srv == 0)
01952 luaL_error(L, "TaskContext.provides: no default service");
01953
01954
01955 luaM_pushobject_mt(L, "Service", Service::shared_ptr)(srv);
01956 lua_replace(L, 1);
01957 return Service_hasOperation(L);
01958 }
01959
01960
01961 static int TaskContext_getOperation(lua_State *L)
01962 {
01963 TaskContext *tc = *(luaM_checkudata_bx(L, 1, TaskContext));
01964 Service::shared_ptr srv = tc->provides();
01965
01966 if(srv == 0)
01967 luaL_error(L, "TaskContext.getOperation: no default service");
01968
01969
01970 luaM_pushobject_mt(L, "Service", Service::shared_ptr)(srv);
01971 lua_replace(L, 1);
01972 return Service_getOperation(L);
01973 }
01974
01975
01976
01977
01978
01979 static void SendStatus_push(lua_State *L, SendStatus ss)
01980 {
01981 switch (ss) {
01982 case SendSuccess: lua_pushstring(L, "SendSuccess"); break;
01983 case SendNotReady: lua_pushstring(L, "SendNotReady"); break;
01984 case SendFailure: lua_pushstring(L, "SendFailure"); break;
01985 default: lua_pushstring(L, "unkown");
01986 }
01987 }
01988
01989 static int __SendHandle_collect(lua_State *L, bool block)
01990 {
01991 std::vector<DataSourceBase::shared_ptr> coll_args;
01992 SendStatus ss;
01993 const types::TypeInfo *ti;
01994 OperationInterfacePart *orp;
01995 DataSourceBase::shared_ptr tmpdsb;
01996 int coll_argc;
01997
01998 SendHandleC *shc = luaM_checkudata_mt(L, 1, "SendHandle", SendHandleC);
01999
02000
02001 orp = shc->getOrp();
02002 coll_argc = orp->collectArity();
02003
02004
02005 for(int i=1; i <= coll_argc; i++) {
02006 ti = orp->getCollectType(i);
02007 tmpdsb = ti->buildValue();
02008 coll_args.push_back(tmpdsb);
02009 shc->arg(tmpdsb);
02010 }
02011
02012 if(block) ss = shc->collect();
02013 else ss = shc->collectIfDone();
02014
02015 SendStatus_push(L, ss);
02016
02017
02018 for (int i=0; i<coll_argc; i++) {
02019 Variable_push_coerce(L, coll_args[i]);
02020 }
02021
02022 return coll_argc + 1;
02023 }
02024
02025 static int SendHandle_collect(lua_State *L)
02026 {
02027 return __SendHandle_collect(L, true);
02028 }
02029
02030 static int SendHandle_collectIfDone(lua_State *L)
02031 {
02032 return __SendHandle_collect(L, false);
02033 }
02034
02035
02036 static const struct luaL_Reg SendHandle_f [] = {
02037 { "collect", SendHandle_collect },
02038 { "collectIfDone", SendHandle_collectIfDone },
02039 { NULL, NULL }
02040 };
02041
02042 static const struct luaL_Reg SendHandle_m [] = {
02043 { "collect", SendHandle_collect },
02044 { "collectIfDone", SendHandle_collectIfDone },
02045 { "__gc", GCMethod<SendHandleC> },
02046 { NULL, NULL }
02047 };
02048
02049
02050 static int __TaskContext_send(lua_State *L)
02051 {
02052 unsigned int argc = lua_gettop(L);
02053 TaskContext *tc = *(luaM_checkudata_bx(L, 1, TaskContext));
02054 TaskContext *this_tc = __getTC(L);
02055 const char *op = luaL_checkstring(L, 2);
02056
02057 OperationInterfacePart *orp = tc->operations()->getPart(op);
02058 OperationCallerC *occ = new OperationCallerC(orp, op, this_tc->engine());
02059 DataSourceBase::shared_ptr dsb;
02060 DataSourceBase::shared_ptr *dsbp;
02061
02062 if(!orp)
02063 luaL_error(L, "TaskContext.send: no operation %s for TaskContext %s",
02064 op, tc->getName().c_str());
02065
02066 if(orp->arity() != argc-2)
02067 luaL_error(L, "TaskContext.send: wrong number of args. expected %d, got %d",
02068 orp->arity(), argc);
02069
02070 for(unsigned int arg=3; arg<=argc; arg++) {
02071
02072 if ((dsbp = luaM_testudata_mt(L, arg, "Variable", DataSourceBase::shared_ptr)) != NULL) {
02073 dsb = *dsbp;
02074 } else {
02075
02076 std::string type = orp->getArgumentType(arg-2)->getTypeName().c_str();
02077 dsb = Variable_fromlua(L, type.c_str() ,arg);
02078 }
02079 occ->arg(dsb);
02080 }
02081
02082
02083 luaM_pushobject_mt(L, "SendHandle", SendHandleC)(occ->send());
02084 return 1;
02085 }
02086
02087 static int TaskContext_send(lua_State *L)
02088 {
02089 int ret;
02090 try {
02091 ret = __TaskContext_send(L);
02092 } catch(...) {
02093 luaL_error(L, "TaskContext.send: caught exception");
02094 }
02095 return ret;
02096 }
02097
02098
02099 static int TaskContext_del(lua_State *L)
02100 {
02101 TaskContext *tc = *(luaM_checkudata_bx(L, 1, TaskContext));
02102 delete tc;
02103
02104
02105 luaL_getmetatable(L, "__dead__");
02106 lua_setmetatable(L, -2);
02107 return 0;
02108 }
02109
02110 #if 0
02111
02112
02113
02114
02115
02116
02117
02118
02119
02120
02121 static int TaskContext_index(lua_State *L)
02122 {
02123 Service::shared_ptr srv;
02124 TaskContext *tc = *(luaM_checkudata_bx(L, 1, TaskContext));
02125 const char* key = luaL_checkstring(L, 2);
02126
02127 lua_getmetatable(L, 1);
02128 lua_getfield(L, -1, key);
02129
02130
02131 if(!lua_isnil(L, -1))
02132 return 1;
02133
02134
02135 lua_settop(L, 2);
02136 srv = tc->provides();
02137 luaM_pushobject_mt(L, "Service", Service::shared_ptr)(srv);
02138 lua_replace(L, 1);
02139
02140 return Service_getOperation(L);
02141 }
02142 #endif
02143
02144 static const struct luaL_Reg TaskContext_f [] = {
02145 { "getName", TaskContext_getName },
02146 { "start", TaskContext_start },
02147 { "stop", TaskContext_stop },
02148 { "configure", TaskContext_configure },
02149 { "activate", TaskContext_activate },
02150 { "cleanup", TaskContext_cleanup },
02151 { "getState", TaskContext_getState },
02152 { "getPeers", TaskContext_getPeers },
02153 { "addPeer", TaskContext_addPeer },
02154 { "removePeer", TaskContext_removePeer },
02155 { "getPeer", TaskContext_getPeer },
02156 { "getPortNames", TaskContext_getPortNames },
02157 { "addPort", TaskContext_addPort },
02158 { "addEventPort", TaskContext_addEventPort },
02159 { "getPort", TaskContext_getPort },
02160 { "removePort", TaskContext_removePort },
02161 { "addProperty", TaskContext_addProperty },
02162 { "getProperty", TaskContext_getProperty },
02163 { "getProperties", TaskContext_getProperties },
02164 { "removeProperty", TaskContext_removeProperty },
02165 { "getOps", TaskContext_getOps },
02166 { "getOpInfo", TaskContext_getOpInfo },
02167 { "hasOperation", TaskContext_hasOperation },
02168 { "provides", TaskContext_provides },
02169 { "connectServices", TaskContext_connectServices },
02170 { "call", TaskContext_call },
02171 { "getOperation", TaskContext_getOperation },
02172 { "send", TaskContext_send },
02173 { "delete", TaskContext_del },
02174 { NULL, NULL}
02175 };
02176
02177 static const struct luaL_Reg TaskContext_m [] = {
02178 { "getName", TaskContext_getName },
02179 { "start", TaskContext_start },
02180 { "stop", TaskContext_stop },
02181 { "configure", TaskContext_configure },
02182 { "activate", TaskContext_activate },
02183 { "cleanup", TaskContext_cleanup },
02184 { "getState", TaskContext_getState },
02185 { "getPeers", TaskContext_getPeers },
02186 { "addPeer", TaskContext_addPeer },
02187 { "removePeer", TaskContext_removePeer },
02188 { "getPeer", TaskContext_getPeer },
02189 { "getPortNames", TaskContext_getPortNames },
02190 { "addPort", TaskContext_addPort },
02191 { "addEventPort", TaskContext_addEventPort },
02192 { "getPort", TaskContext_getPort },
02193 { "removePort", TaskContext_removePort },
02194 { "addProperty", TaskContext_addProperty },
02195 { "getProperty", TaskContext_getProperty },
02196 { "getProperties", TaskContext_getProperties },
02197 { "removeProperty", TaskContext_removeProperty },
02198 { "getOps", TaskContext_getOps },
02199 { "getOpInfo", TaskContext_getOpInfo },
02200 { "hasOperation", TaskContext_hasOperation },
02201 { "provides", TaskContext_provides },
02202 { "requires", TaskContext_requires },
02203 { "connectServices", TaskContext_connectServices },
02204 { "call", TaskContext_call },
02205 { "getOperation", TaskContext_getOperation },
02206 { "send", TaskContext_send },
02207 { "delete", TaskContext_del },
02208
02209
02210
02211 { NULL, NULL}
02212 };
02213
02214
02215
02216
02217
02218
02219 class EEHook : public base::ExecutableInterface
02220 {
02221 protected:
02222 std::string func;
02223 lua_State *L;
02224 TaskContext *tc;
02225
02226 public:
02227 EEHook(lua_State *_L, std::string _func) { L = _L; func = _func; tc = __getTC(L); }
02228 bool execute() { return call_func(L, func.c_str(), tc, 1, 1); }
02229 };
02230
02231 static int EEHook_new(lua_State *L)
02232 {
02233 const char *func;
02234 func = luaL_checkstring(L, 1);
02235 luaM_pushobject(L, EEHook)(L, func);
02236 return 1;
02237 }
02238
02239 static int EEHook_enable(lua_State *L)
02240 {
02241 EEHook *eeh = luaM_checkudata(L, 1, EEHook);
02242 TaskContext *tc = __getTC(L);
02243 lua_pushboolean(L, tc->engine()->runFunction(eeh));
02244 return 1;
02245 }
02246
02247 static int EEHook_disable(lua_State *L)
02248 { EEHook *eeh = luaM_checkudata(L, 1, EEHook);
02249 TaskContext *tc = __getTC(L);
02250 lua_pushboolean(L, tc->engine()->removeFunction(eeh));
02251 return 1;
02252 }
02253
02254 static int EEHook_gc(lua_State *L)
02255 {
02256 EEHook_disable(L);
02257 lua_settop(L, 1);
02258 reinterpret_cast<EEHook*>(lua_touserdata(L, 1))->~EEHook();
02259 return 0;
02260 }
02261
02262 static const struct luaL_Reg EEHook_f [] = {
02263 { "new", EEHook_new },
02264 { "enable", EEHook_enable },
02265 { "disable", EEHook_disable },
02266 };
02267
02268
02269 static const struct luaL_Reg EEHook_m [] = {
02270 { "enable", EEHook_enable },
02271 { "disable", EEHook_disable },
02272 { "__gc", EEHook_gc },
02273 };
02274
02275
02276
02277
02278
02279 static const char *const loglevels[] = {
02280 "Never", "Fatal", "Critical", "Error", "Warning", "Info", "Debug", "RealTime", NULL
02281 };
02282
02283 static int Logger_setLogLevel(lua_State *L)
02284 {
02285 Logger::LogLevel ll = (Logger::LogLevel) luaL_checkoption(L, 1, NULL, loglevels);
02286 log().setLogLevel(ll);
02287 return 0;
02288 }
02289
02290 static int Logger_getLogLevel(lua_State *L)
02291 {
02292 Logger::LogLevel ll = log().getLogLevel();
02293
02294 switch(ll) {
02295 case Logger::Never: lua_pushstring(L, "Never"); break;
02296 case Logger::Fatal: lua_pushstring(L, "Fatal"); break;
02297 case Logger::Critical: lua_pushstring(L, "Critical"); break;
02298 case Logger::Error: lua_pushstring(L, "Error"); break;
02299 case Logger::Warning: lua_pushstring(L, "Warning"); break;
02300 case Logger::Info: lua_pushstring(L, "Info"); break;
02301 case Logger::Debug: lua_pushstring(L, "Debug"); break;
02302 case Logger::RealTime: lua_pushstring(L, "RealTime"); break;
02303 default:
02304 lua_pushstring(L, "unknown");
02305 }
02306 return 1;
02307 }
02308
02309 static int Logger_log(lua_State *L)
02310 {
02311 const char *mes;
02312 for(int i=1; i<=lua_gettop(L); i++) {
02313 mes = luaL_checkstring(L, i);
02314 Logger::log() << mes;
02315 }
02316 Logger::log() << endlog();
02317 return 0;
02318 }
02319
02320 static int Logger_logl(lua_State *L)
02321 {
02322 const char *mes;
02323 Logger::LogLevel ll = (Logger::LogLevel) luaL_checkoption(L, 1, NULL, loglevels);
02324 for(int i=2; i<=lua_gettop(L); i++) {
02325 mes = luaL_checkstring(L, i);
02326 Logger::log(ll) << mes;
02327 }
02328 Logger::log(ll) << endlog();
02329 return 0;
02330 }
02331
02332
02333
02334 static int getTime(lua_State *L)
02335 {
02336 unsigned long nsec, sec;
02337 RTT::os::TimeService::nsecs total_nsec = TimeService::Instance()->getNSecs();
02338 sec = total_nsec / 1000000000;
02339 nsec = total_nsec % 1000000000;
02340 lua_pushinteger(L, sec);
02341 lua_pushinteger(L, nsec);
02342 return 2;
02343 }
02344
02345 static int getTC(lua_State *L)
02346 {
02347 lua_pushstring(L, "this_TC");
02348 lua_gettable(L, LUA_REGISTRYINDEX);
02349 return 1;
02350 }
02351
02352 static TaskContext* __getTC(lua_State *L)
02353 {
02354 TaskContext *tc;
02355 getTC(L);
02356 tc = *(luaM_checkudata_bx(L, -1, TaskContext));
02357 lua_pop(L, 1);
02358 return tc;
02359 }
02360
02361
02362 static int provides_global(lua_State *L)
02363 {
02364 luaM_pushobject_mt(L, "Service", Service::shared_ptr)(GlobalService::Instance());
02365 lua_insert(L, 1);
02366 return Service_provides(L);
02367 }
02368
02369 static int rtt_services(lua_State *L)
02370 {
02371 push_vect_str(L, PluginLoader::Instance()->listServices());
02372 return 1;
02373 }
02374
02375 static int rtt_typekits(lua_State *L)
02376 {
02377 push_vect_str(L, PluginLoader::Instance()->listTypekits());
02378 return 1;
02379 }
02380
02381 static int rtt_types(lua_State *L)
02382 {
02383 push_vect_str(L, TypeInfoRepository::Instance()->getTypes());
02384 return 1;
02385 }
02386
02387 static const struct luaL_Reg rtt_f [] = {
02388 {"getTime", getTime },
02389 {"getTC", getTC },
02390 {"provides", provides_global },
02391 {"services", rtt_services },
02392 {"typekits", rtt_typekits },
02393 {"types", rtt_types },
02394 {"setLogLevel", Logger_setLogLevel },
02395 {"getLogLevel", Logger_getLogLevel },
02396 {"log", Logger_log },
02397 {"logl", Logger_logl },
02398 {NULL, NULL}
02399 };
02400
02401 extern "C" int luaopen_rtt(lua_State *L);
02402
02403 int luaopen_rtt(lua_State *L)
02404 {
02405 lua_newtable(L);
02406 lua_replace(L, LUA_ENVIRONINDEX);
02407
02408 luaL_newmetatable(L, "__dead__");
02409
02410
02411
02412
02413
02414
02415
02416
02417
02418 luaL_newmetatable(L, "TaskContext");
02419 lua_pushvalue(L, -1);
02420 lua_setfield(L, -2, "__index");
02421 luaL_register(L, NULL, TaskContext_m);
02422 luaL_register(L, "rtt.TaskContext", TaskContext_f);
02423
02424 luaL_newmetatable(L, "Operation");
02425 lua_pushvalue(L, -1);
02426 lua_setfield(L, -2, "__index");
02427 luaL_register(L, NULL, Operation_m);
02428 luaL_register(L, "rtt.Operation", Operation_f);
02429
02430 luaL_newmetatable(L, "Service");
02431 lua_pushvalue(L, -1);
02432 lua_setfield(L, -2, "__index");
02433 luaL_register(L, NULL, Service_m);
02434 luaL_register(L, "rtt.Service", Service_f);
02435
02436 luaL_newmetatable(L, "ServiceRequester");
02437 lua_pushvalue(L, -1);
02438 lua_setfield(L, -2, "__index");
02439 luaL_register(L, NULL, ServiceRequester_m);
02440 luaL_register(L, "rtt.ServiceRequester", ServiceRequester_f);
02441
02442 luaL_newmetatable(L, "SendHandle");
02443 lua_pushvalue(L, -1);
02444 lua_setfield(L, -2, "__index");
02445 luaL_register(L, NULL, SendHandle_m);
02446 luaL_register(L, "rtt.SendHandle", SendHandle_f);
02447
02448 luaL_newmetatable(L, "InputPort");
02449 lua_pushvalue(L, -1);
02450 lua_setfield(L, -2, "__index");
02451 luaL_register(L, NULL, InputPort_m);
02452 luaL_register(L, "rtt.InputPort", InputPort_f);
02453
02454 luaL_newmetatable(L, "OutputPort");
02455 lua_pushvalue(L, -1);
02456 lua_setfield(L, -2, "__index");
02457 luaL_register(L, NULL, OutputPort_m);
02458 luaL_register(L, "rtt.OutputPort", OutputPort_f);
02459
02460 luaL_newmetatable(L, "Variable");
02461 lua_pushvalue(L, -1);
02462 lua_setfield(L, -2, "__index");
02463 luaL_register(L, NULL, Variable_m);
02464 luaL_register(L, "rtt.Variable", Variable_f);
02465
02466 luaL_newmetatable(L, "Property");
02467 lua_pushvalue(L, -1);
02468 lua_setfield(L, -2, "__index");
02469 luaL_register(L, NULL, Property_m);
02470 luaL_register(L, "rtt.Property", Property_f);
02471
02472 luaL_newmetatable(L, "EEHook");
02473 lua_pushvalue(L, -1);
02474 lua_setfield(L, -2, "__index");
02475 luaL_register(L, NULL, EEHook_m);
02476 luaL_register(L, "rtt.EEHook", EEHook_f);
02477
02478
02479 luaL_register(L, "rtt", rtt_f);
02480
02481
02482
02483
02484
02485
02486
02487
02488 return 1;
02489 }
02490
02491
02492 int set_context_tc(TaskContext *tc, lua_State *L)
02493 {
02494 TaskContext **new_tc;
02495 lua_pushstring(L, "this_TC");
02496 new_tc = (TaskContext**) lua_newuserdata(L, sizeof(TaskContext*));
02497 *new_tc = (TaskContext*) tc;
02498 luaL_getmetatable(L, "TaskContext");
02499 lua_setmetatable(L, -2);
02500 lua_settable(L, LUA_REGISTRYINDEX);
02501 return 0;
02502 }
02503
02504
02505
02506
02507 bool call_func(lua_State *L, const std::string &fname, TaskContext *tc,
02508 int require_function, int require_result)
02509 {
02510 bool ret = true;
02511 lua_getglobal(L, fname.c_str());
02512
02513 if(lua_isnil(L, -1)) {
02514 if(require_function)
02515 luaL_error(L, "%s: no (required) Lua function %s", tc->getName().c_str(), fname.c_str());
02516 else
02517 goto out;
02518 }
02519
02520 if (lua_pcall(L, 0, 1, 0) != 0) {
02521 Logger::log(Logger::Error) << "LuaComponent '"<< tc->getName() <<"': error calling function "
02522 << fname << ": " << lua_tostring(L, -1) << endlog();
02523 ret = false;
02524 goto out;
02525 }
02526
02527 if(require_result) {
02528 if (!lua_isboolean(L, -1)) {
02529 Logger::log(Logger::Error) << "LuaComponent '" << tc->getName() << "': " << fname
02530 << " must return a bool but returned a "
02531 << lua_typename(L, lua_type(L, -1)) << endlog();
02532 ret = false;
02533 goto out;
02534 }
02535 ret = lua_toboolean(L, -1);
02536 }
02537 out:
02538 return ret;
02539 }