rtt.cpp
Go to the documentation of this file.
1 /*
2  * Lua-RTT bindings
3  *
4  * (C) Copyright 2010 Markus Klotzbuecher
5  * markus.klotzbuecher@mech.kuleuven.be
6  * Department of Mechanical Engineering,
7  * Katholieke Universiteit Leuven, Belgium.
8  *
9  * This library is free software; you can redistribute it and/or
10  * modify it under the terms of the GNU General Public
11  * License as published by the Free Software Foundation;
12  * version 2 of the License.
13  *
14  * As a special exception, you may use this file as part of a free
15  * software library without restriction. Specifically, if other files
16  * instantiate templates or use macros or inline functions from this
17  * file, or you compile this file and link it with other files to
18  * produce an executable, this file does not by itself cause the
19  * resulting executable to be covered by the GNU General Public
20  * License. This exception does not however invalidate any other
21  * reasons why the executable file might be covered by the GNU General
22  * Public License.
23  *
24  * This library is distributed in the hope that it will be useful,
25  * but WITHOUT ANY WARRANTY; without even the implied warranty of
26  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
27  * Lesser General Public License for more details.
28  *
29  * You should have received a copy of the GNU General Public
30  * License along with this library; if not, write to the Free Software
31  * Foundation, Inc., 59 Temple Place,
32  * Suite 330, Boston, MA 02111-1307 USA
33  */
34 
35 #include "rtt.hpp"
36 
37 using namespace std;
38 using namespace RTT;
39 using namespace RTT::detail;
40 using namespace RTT::base;
41 using namespace RTT::internal;
42 
43 static TaskContext* __getTC(lua_State*);
44 
45 #define DEBUG
46 
47 #ifdef DEBUG
48 # define _DBG(fmt, ...) printf("%s:%d\t" fmt "\n", __FUNCTION__, __LINE__, __VA_ARGS__)
49 #else
50 # define _DBG(fmt, ...) do { } while(0)
51 #endif
52 
53 /*
54  * Inspired by tricks from here: http://lua-users.org/wiki/DoItYourselfCppBinding
55  */
56 
57 /* overloading new */
58 void* operator new(size_t size, lua_State* L, const char* mt)
59 {
60  void* ptr = lua_newuserdata(L, size);
61  luaL_getmetatable(L, mt);
62  /* assert(lua_istable(L, -1)) */ /* if you're paranoid */
63  lua_setmetatable(L, -2);
64  return ptr;
65 }
66 
67 /*
68  * luaM_pushobject_mt(L, "Port", InputPortInterface )(ctr_arg,...)
69  * expands to
70  * new(L, "Port") InputPortInterface(ctr_arg,...)
71  */
72 
73 #define luaM_pushobject(L, T) new(L, #T) T
74 #define luaM_pushobject_mt(L, MT, T) new(L, MT) T
75 
76 /* return udata ptr or fail if wrong metatable */
77 #define luaM_checkudata(L, pos, T) reinterpret_cast<T*>(luaL_checkudata((L), (pos), #T))
78 #define luaM_checkudata_mt(L, pos, MT, T) reinterpret_cast<T*>(luaL_checkudata((L), (pos), MT))
79 
80 /* return udata ptr or NULL if wrong metatable */
81 #define luaM_testudata(L, pos, T) (T*) (luaL_testudata((L), (pos), #T))
82 #define luaM_testudata_mt(L, pos, MT, T) (T*) (luaL_testudata((L), (pos), MT))
83 
84 /*
85  * boxed variants
86  */
87 
88 /* return boxed udata ptr or fail if wrong metatable */
89 #define luaM_checkudata_bx(L, pos, T) (T**) (luaL_checkudata((L), (pos), #T))
90 #define luaM_checkudata_mt_bx(L, pos, MT, T) (T**) (luaL_checkudata((L), (pos), MT))
91 
92 /* return udata ptr or NULL if wrong metatable */
93 #define luaM_testudata_bx(L, pos, T) (T**) (luaL_testudata((L), (pos), #T))
94 #define luaM_testudata_mt_bx(L, pos, MT, T) (T**) (luaL_testudata((L), (pos), MT))
95 
96 /* generate a function to push boxed pointers to lua */
97 #define gen_push_bxptr(name, MT, T) \
98 static void name(lua_State *L, T* ptr) \
99 { \
100  T** ptrptr = (T**) lua_newuserdata(L, sizeof(T*)); \
101  *ptrptr = ptr; \
102  luaL_getmetatable(L, MT); \
103  lua_setmetatable(L, -2); \
104 } \
105 
106 /* template for generating GC function */
107 template<typename T>
109 {
110  reinterpret_cast<T*>(lua_touserdata(L, 1))->~T();
111  return 0;
112 }
113 
114 
115 /***************************************************************
116  * Some generic helpers
117  ***************************************************************/
118 
119 /* test if userdata on position ud has metatable tname */
120 void* luaL_testudata (lua_State *L, int ud, const char *tname)
121 {
122  void *p = lua_touserdata(L, ud);
123 
124  if (p == NULL)
125  goto out;
126 
127  if (!lua_getmetatable(L, ud)) {
128  p = NULL;
129  goto out;
130  }
131 
132  /* it has a MT, is it the right one? */
133  lua_pushstring(L, tname);
135 
136  if (!lua_rawequal(L, -1, -2))
137  p = NULL;
138 
139  lua_pop(L, 2); /* remove both metatables */
140  out:
141  return p;
142 }
143 
144 
145 void push_vect_str(lua_State *L, const std::vector<std::string> &v)
146 {
147  int key = 1;
148  lua_createtable(L, v.size(), 0);
149 
150  for(vector<std::string>::const_iterator it = v.begin(); it != v.end(); ++it) {
151  lua_pushstring(L, it->c_str());
152  lua_rawseti(L, -2, key++);
153  }
154 }
155 
156 /* forw decl */
157 static void Variable_fromlua(lua_State *L, DataSourceBase::shared_ptr& dsb, int valind);
158 static DataSourceBase::shared_ptr Variable_fromlua(lua_State *L, const types::TypeInfo* ti, int valind);
159 static DataSourceBase::shared_ptr Variable_fromlua(lua_State *L, const char* type, int valind);
160 
161 /***************************************************************
162  * Variable (DataSourceBase)
163  ***************************************************************/
164 
165 static const TypeInfo* ti_lookup(lua_State *L, const char *name)
166 {
167 #ifndef TYPEINFO_CACHING
168  return types::TypeInfoRepository::Instance()->type(name);
169 #else
170 /* name-> TypeInfo* cache */
171  int top = lua_gettop(L);
172  const TypeInfo* ti;
173 
174  /* try lookup */
175  lua_pushstring(L, "typeinfo_cache");
177 
178  if(lua_type(L, -1) == LUA_TTABLE)
179  goto table_on_top;
180 
181  /* first lookup, create table */
182  lua_pop(L, 1); /* pop nil */
183  lua_newtable(L); /* stays on top after the next three lines */
184  lua_pushstring(L, "typeinfo_cache"); /* key */
185  lua_pushvalue(L, -2); /* duplicates table */
186  lua_rawset(L, LUA_REGISTRYINDEX); /* REG['typeinfo_cache']={} */
187 
188  table_on_top:
189  /* try to lookup name in table */
190  lua_pushstring(L, name);
191  lua_rawget(L, -2);
192 
193  if(lua_type(L, -1) != LUA_TLIGHTUSERDATA)
194  goto cache_miss;
195 
196  ti = (const TypeInfo*) lua_touserdata(L, -1);
197  goto out;
198 
199  cache_miss:
200  lua_pop(L, 1); /* pop the nil */
201  ti = types::TypeInfoRepository::Instance()->type(name);
202  if (ti) { // only save if type exists !
203  lua_pushstring(L, name);
204  lua_pushlightuserdata(L, (void*) ti);
205  lua_rawset(L, -3);
206  }
207  out:
208  /* everyone happy! */
209  lua_settop(L, top);
210  return ti;
211 #endif /* TYPEINFO_CACHING */
212 }
213 
214 /* helper, check if a type name is an alias to the given TypeInfo */
215 static bool __typenames_cmp(lua_State *L, const types::TypeInfo *ti1, const char* type2)
216 {
217  const types::TypeInfo *ti2 = ti_lookup(L, type2);
218  return ti1 == ti2;
219 }
220 
221 /* helper, check if a dsb is of type type. Works also if dsb is known
222  under an alias of type */
223 static bool Variable_is_a(lua_State *L, const types::TypeInfo *ti1, const char* type)
224 {
225  const types::TypeInfo *ti2 = ti_lookup(L, type);
226  return ti1 == ti2;
227 }
228 
229 /* helper, check if a variable is basic, that is _tolua will succeed */
231 {
232  const types::TypeInfo *ti = dsb->getTypeInfo();
233 
234  if ( Variable_is_a(L, ti, "bool") ||
235  Variable_is_a(L, ti, "double") ||
236  Variable_is_a(L, ti, "float") ||
237  Variable_is_a(L, ti, "uint") ||
238  Variable_is_a(L, ti, "int") ||
239  Variable_is_a(L, ti, "long") ||
240  Variable_is_a(L, ti, "char") ||
241  Variable_is_a(L, ti, "uint8") || Variable_is_a(L, ti, "int8") ||
242  Variable_is_a(L, ti, "uint16") || Variable_is_a(L, ti, "int16") ||
243  Variable_is_a(L, ti, "uint32") || Variable_is_a(L, ti, "int32") ||
244  Variable_is_a(L, ti, "uint64") || Variable_is_a(L, ti, "int64") ||
245  Variable_is_a(L, ti, "string") ||
246  Variable_is_a(L, ti, "void"))
247  return true;
248  else
249  return false;
250 }
251 
253 {
256  return 1;
257 }
258 
259 
260 /*
261  * converts a DataSourceBase to the corresponding Lua value and pushes
262  * that on the stack.
263  */
265 {
266  DataSourceBase *ds = dsb.get();
267  const types::TypeInfo* ti = dsb->getTypeInfo();
268  assert(ds);
269 
270  if(Variable_is_a(L, ti, "bool")) { // bool
272  if(dsb) lua_pushboolean(L, dsb->get());
273  else goto out_nodsb;
274  } else if (Variable_is_a(L, ti, "float")) { // float
276  if(dsb) lua_pushnumber(L, ((lua_Number) dsb->get()));
277  else goto out_nodsb;
278  } else if (Variable_is_a(L, ti, "double")) { // double
280  if(dsb) lua_pushnumber(L, ((lua_Number) dsb->get()));
281  else goto out_nodsb;
282  } else if (Variable_is_a(L, ti, "uint8")) { // uint8_t
284  if(dsb) lua_pushnumber(L, ((lua_Number) dsb->get()));
285  else goto out_nodsb;
286  } else if (Variable_is_a(L, ti, "int8")) { // int8_t
288  if(dsb) lua_pushnumber(L, ((lua_Number) dsb->get()));
289  else goto out_nodsb;
290  } else if (Variable_is_a(L, ti, "uint16")) { // uint16_t
292  if(dsb) lua_pushnumber(L, ((lua_Number) dsb->get()));
293  else goto out_nodsb;
294  } else if (Variable_is_a(L, ti, "int16")) { // int16_t
296  if(dsb) lua_pushnumber(L, ((lua_Number) dsb->get()));
297  else goto out_nodsb;
298  } else if (Variable_is_a(L, ti, "uint32")) { // uint32_t
300  if(dsb) lua_pushnumber(L, ((lua_Number) dsb->get()));
301  else goto out_nodsb;
302  } else if (Variable_is_a(L, ti, "int32")) { // int32_t
304  if(dsb) lua_pushnumber(L, ((lua_Number) dsb->get()));
305  else goto out_nodsb;
306  } else if (Variable_is_a(L, ti, "uint64")) { // uint64_t
308  if(dsb) lua_pushnumber(L, ((lua_Number) dsb->get()));
309  else goto out_nodsb;
310  } else if (Variable_is_a(L, ti, "int64")) { // int64_t
312  if(dsb) lua_pushnumber(L, ((lua_Number) dsb->get()));
313  else goto out_nodsb;
314  } else if (Variable_is_a(L, ti, "uint")) { // uint
316  if(dsb) lua_pushnumber(L, ((lua_Number) dsb->get()));
317  else goto out_nodsb;
318  } else if (Variable_is_a(L, ti, "long")) { //long
320  if(dsb) lua_pushnumber(L, ((lua_Number) dsb->get()));
321  else goto out_nodsb;
322  } else if (Variable_is_a(L, ti, "int")) { // int
324  if(dsb) lua_pushnumber(L, ((lua_Number) dsb->get()));
325  else goto out_nodsb;
326  } else if (Variable_is_a(L, ti, "char")) { // char
328  char c = dsb->get();
329  if(dsb) lua_pushlstring(L, &c, 1);
330  else goto out_nodsb;
331  } else if (Variable_is_a(L, ti, "string")) { //string
333  if(dsb) lua_pushlstring(L, dsb->get().c_str(), dsb->get().size());
334  else goto out_nodsb;
335  } else if (Variable_is_a(L, ti, "void")) {
337  if(dsb) lua_pushnil(L);
338  else goto out_nodsb;
339  } else {
340  goto out_conv_err;
341  }
342 
343  /* all ok */
344  return 1;
345 
346  out_conv_err:
347  luaL_error(L, "Variable.tolua: can't convert type %s", dsb->getTypeName().c_str());
348  return 0;
349 
350  out_nodsb:
351  luaL_error(L, "Variable.tolua: narrow failed for %s Variable", dsb->getTypeName().c_str());
352  return 0;
353 }
354 
355 static int Variable_tolua(lua_State *L)
356 {
358  return __Variable_tolua(L, dsb);
359 }
360 
361 /* Function takes a DSB, that is also expected on the top of the
362  * stack. If the DSB is basic, it replaces the dsb with the
363  * corresponding Lua value. Otherwise it does nothing, leaving the DSB
364  * on the top of the stack.
365  */
367 {
368  if (__Variable_isbasic(L, dsb)) {
369  lua_pop(L, 1);
370  __Variable_tolua(L, dsb);
371  }
372 }
373 
374 /* this function takes a dsb and either pushes it as a Lua type if the
375  * dsb is basic or otherwise as at Variable
376  */
378 {
379  if (__Variable_isbasic(L, dsb))
380  __Variable_tolua(L, dsb);
381  else
382  luaM_pushobject_mt(L, "Variable", DataSourceBase::shared_ptr)(dsb);
383 
384 }
385 
387 {
388  push_vect_str(L, Types()->getTypes());
389  return 1;
390 }
391 
393 {
395  push_vect_str(L, (*dsbp)->getMemberNames());
396  return 1;
397 }
398 
400 {
402  lua_pushlightuserdata(L, dsb->getRawPointer());
403  return 1;
404 }
405 
406 
407 /* caching of DSB members
408  * lookup of DSB using getMember and caches result.
409  * returns DSB (or nil if lookup fails) on top of stack.
410  */
412 {
413  DataSourceBase *varptr;
416  int top = lua_gettop(L);
417 
418  varptr = parent.get();
419 
420  lua_pushlightuserdata(L, (void*) varptr);
422 
423  if(lua_type(L, -1) == LUA_TNIL)
424  goto cache_miss;
425 
426  lua_pushstring(L, mem);
427  lua_rawget(L, -2);
428 
429  if ((dsbp = luaM_testudata_mt(L, -1, "Variable", DataSourceBase::shared_ptr)) != NULL) {
430  memdsb=*dsbp;
431  goto out;
432  }
433 
434  lua_pop(L, 1); /* pop nil from table lookup */
435 
436  cache_miss:
437  /* slowpath */
438  memdsb = parent->getMember(mem);
439 
440  if(memdsb == 0)
441  goto out;
442 
443  /* if nil is on top of stack, we have to create a new table */
444  if(lua_type(L, -1) == LUA_TNIL) {
445  lua_newtable(L); /* member lookup tab for this Variable */
446  lua_pushlightuserdata(L, (void*) varptr); /* index for REGISTRY */
447  lua_pushvalue(L, -2); /* duplicates table */
448  lua_rawset(L, LUA_REGISTRYINDEX); /* REG[varptr]=newtab */
449  }
450 
451  /* cache dsb in table */
452  lua_pushstring(L, mem);
453  luaM_pushobject_mt(L, "Variable", DataSourceBase::shared_ptr)(memdsb);
454  lua_rawset(L, -3); /* newtab[mem]=memdsb, top is newtab */
455  luaM_pushobject_mt(L, "Variable", DataSourceBase::shared_ptr)(memdsb);
456 
457  out:
458  lua_replace(L, top+1); // make new var top of stack
459  lua_settop(L, top+1);
460 
461  return memdsb;
462 }
463 
464 /* set reg[varptr] to nil so table will be garbage collected */
465 static void cache_clear(lua_State *L, DataSourceBase *varptr)
466 {
467  lua_pushlightuserdata(L, (void*) varptr);
468  lua_pushnil(L);
470 }
471 
473 {
476  const char *mem = luaL_checkstring(L, 2);
477 
478  if ((memdsb = lookup_member(L, *dsbp, mem)) == 0)
479  luaL_error(L, "Variable.getMember: indexing failed, no member %s", mem);
480  else
481  Variable_coerce(L, memdsb);
482 
483  return 1;
484 }
485 
487 {
490  const char *mem = luaL_checkstring(L, 2);
491 
492  if ((memdsb = lookup_member(L, (*dsbp), mem)) == 0)
493  luaL_error(L, "Variable.getMemberRaw: indexing failed, no member %s", mem);
494 
495  /* else: Variable is already on top of stack */
496 
497  return 1;
498 }
499 
501 {
502  int ret;
506 
507  if ((dsbp = luaM_testudata_mt(L, 2, "Variable", DataSourceBase::shared_ptr)) != NULL) {
508  dsb = *dsbp;
509  ret = self->update(dsb.get());
510  if (!ret) luaL_error(L, "Variable.assign: assignment failed");
511  } else {
512  Variable_fromlua(L, self, 2);
513  }
514 
515  return 0;
516 }
517 
518 /* create variable */
520 {
521  const char *type;
522  type = luaL_checkstring(L, 1);
523 
524  if(!strcmp(type, "void"))
525  luaL_error(L, "Variable.new: can't create void variable");
526 
527  TypeInfo* ti = Types()->type(type);
528 
529  if(ti==0)
530  luaL_error(L, "Variable.new: unknown type %s", type);
531 
532  luaM_pushobject_mt(L, "Variable", DataSourceBase::shared_ptr)(ti->buildValue());
533  return 1;
534 }
535 
536 #define CONVERT_TO_NUMBER(CTGT) \
537  lua_Number x; \
538  if (luatype == LUA_TNUMBER) x = lua_tonumber(L, valind); \
539  else goto out_conv_err; \
540  AssignableDataSource<CTGT> *ads = ValueDataSource<CTGT>::narrow(dsb.get()); \
541  if (ads == NULL) luaL_error(L, "Variable_fromlua: failed to narrow target dsb to %s.", #CTGT ); \
542  ads->set((CTGT) x)\
543 
544 /* Try to convert the Lua value on stack at valind to given DSB
545  * if it returns, evertthing is ok */
546 static void Variable_fromlua(lua_State *L, DataSourceBase::shared_ptr& dsb, int valind)
547 {
548  const types::TypeInfo* ti = dsb->getTypeInfo();
549 
550  luaL_checkany(L, valind);
551  int luatype = lua_type(L, valind); /* type of lua variable */
552 
553  if(__typenames_cmp(L, ti, "bool")) {
554  lua_Number x;
555  if(luatype == LUA_TBOOLEAN)
556  x = (lua_Number) lua_toboolean(L, valind);
557  else if (luatype == LUA_TNUMBER)
558  x = lua_tonumber(L, valind);
559  else
560  goto out_conv_err;
561 
563  if (ads == NULL)
564  luaL_error(L, "Variable_fromlua: failed to narrow target dsb to bool");
565  ads->set((bool) x);
566  }
567  else if (__typenames_cmp(L, ti, "uint")) { CONVERT_TO_NUMBER(unsigned int); }
568  else if (__typenames_cmp(L, ti, "int")) { CONVERT_TO_NUMBER(int); }
569  else if (__typenames_cmp(L, ti, "double")) { CONVERT_TO_NUMBER(double); }
570  else if (__typenames_cmp(L, ti, "long")) { CONVERT_TO_NUMBER(double); }
571  else if (__typenames_cmp(L, ti, "uint8")) { CONVERT_TO_NUMBER(uint8_t); }
572  else if (__typenames_cmp(L, ti, "int8")) { CONVERT_TO_NUMBER(int8_t); }
573  else if (__typenames_cmp(L, ti, "uint16")) { CONVERT_TO_NUMBER(uint16_t); }
574  else if (__typenames_cmp(L, ti, "int16")) { CONVERT_TO_NUMBER(int16_t); }
575  else if (__typenames_cmp(L, ti, "uint32")) { CONVERT_TO_NUMBER(uint32_t); }
576  else if (__typenames_cmp(L, ti, "int32")) { CONVERT_TO_NUMBER(int32_t); }
577  else if (__typenames_cmp(L, ti, "uint64")) { CONVERT_TO_NUMBER(uint64_t); }
578  else if (__typenames_cmp(L, ti, "int64")) { CONVERT_TO_NUMBER(int64_t); }
579  else if (__typenames_cmp(L, ti, "float")) { CONVERT_TO_NUMBER(float); }
580 
581  else if (__typenames_cmp(L, ti, "char")) {
582  const char *x;
583  size_t l;
584  if (luatype == LUA_TSTRING) x = lua_tolstring(L, valind, &l);
585  else goto out_conv_err;
587  if (ads == NULL) luaL_error(L, "Variable_fromlua: failed to narrow target dsb to char");
588  ads->set((char) x[0]);
589 
590  } else if (__typenames_cmp(L, ti, "string")) {
591  const char *x;
592  if (luatype == LUA_TSTRING) x = lua_tostring(L, valind);
593  else goto out_conv_err;
595  if (ads == NULL) luaL_error(L, "Variable_fromlua: failed to narrow target dsb to std::string");
596  ads->set((std::string) x);
597 
598  } else {
599  goto out_conv_err;
600  }
601 
602  /* everybody happy */
603  return;
604 
605  out_conv_err:
606  luaL_error(L, "__lua_todsb: can't convert lua %s to %s variable",
607  lua_typename(L, luatype), ti->getTypeName().c_str());
608  return;
609 }
610 
611 /* Create a DSB of RTT ti from the Lua value at stack[valind]
612  * This one will create a dsb - NRT!*/
614 {
616  Variable_fromlua(L, dsb, valind);
617  return dsb;
618 }
619 
620 /* Create a DSB of RTT type 'type' from the Lua value at stack[valind]
621  * This one will create a dsb - NRT!
622  * This one should be avoided, to reduce needless name-ti lookups.
623  * preferred variant is the one taking TypeInfo * as second arg */
624 static DataSourceBase::shared_ptr Variable_fromlua(lua_State *L, const char* type, int valind)
625 {
626  const types::TypeInfo* ti = ti_lookup(L, type);
627  if(!ti) luaL_error(L, "Variable_fromlua: %s is not a known type. Load typekit?", type);
628  return Variable_fromlua(L, ti, valind);
629 }
630 
631 
632 static int Variable_create_ival(lua_State *L, int typeind, int valind)
633 {
635  luaL_checkany(L, valind);
636  const char* type = luaL_checkstring(L, typeind); /* target dsb type */
637  dsb = Variable_fromlua(L, type, valind);
638  luaM_pushobject_mt(L, "Variable", DataSourceBase::shared_ptr)(dsb);
639  return 1;
640 }
641 
642 static int Variable_new(lua_State *L)
643 {
644  int argc = lua_gettop(L);
645  if(argc == 1)
646  return Variable_create(L);
647  else if(argc == 2)
648  return Variable_create_ival(L, 1, 2);
649  else
650  luaL_error(L, "Variable.new: invalid number of args");
651 
652  return 0;
653 }
654 
656 {
658  lua_pushstring(L, ((*dsbp)->toString()).c_str());
659  return 1;
660 }
661 
663 {
665  lua_pushstring(L, (*dsbp)->getType().c_str());
666  return 1;
667 }
668 
670 {
672  lua_pushstring(L, (*dsbp)->getTypeInfo()->getTypeIdName());
673  return 1;
674 }
675 
677 {
679  lua_pushstring(L, (*dsbp)->getTypeName().c_str());
680  return 1;
681 }
682 
684 {
685  int size;
687  size = luaL_checknumber(L, 2);
688  const TypeInfo *ti = (*dsbp)->getTypeInfo();
689  lua_pushboolean(L, ti->resize(*dsbp, size));
690  return 1;
691 }
692 
693 
694 /*
695  * Operators
696  */
697 static int Variable_unm(lua_State *L)
698 {
699  types::OperatorRepository::shared_ptr opreg = types::OperatorRepository::Instance();
701  DataSourceBase::shared_ptr res = opreg->applyUnary("-", arg.get());
702  luaM_pushobject_mt(L, "Variable", DataSourceBase::shared_ptr)(res);
703  return 1;
704 }
705 
706 
707 /* don't try this at home */
708 #define gen_opmet(name, op) \
709 static int name(lua_State *L) \
710 { \
711  DataSourceBase::shared_ptr arg1 = *(luaM_checkudata_mt(L, 1, "Variable", DataSourceBase::shared_ptr)); \
712  DataSourceBase::shared_ptr arg2 = *(luaM_checkudata_mt(L, 2, "Variable", DataSourceBase::shared_ptr)); \
713  types::OperatorRepository::shared_ptr opreg = types::OperatorRepository::Instance(); \
714  DataSourceBase *res = opreg->applyBinary(#op, arg1.get(), arg2.get()); \
715  if(res == 0) \
716  luaL_error(L , "%s (operator %s) failed", #name, #op); \
717  res->evaluate(); \
718  luaM_pushobject_mt(L, "Variable", DataSourceBase::shared_ptr)(res); \
719  return 1; \
720 } \
721 
722 gen_opmet(Variable_add, +)
723 gen_opmet(Variable_sub, -)
724 gen_opmet(Variable_mul, *)
725 gen_opmet(Variable_div, /)
726 gen_opmet(Variable_mod, %)
727 gen_opmet(Variable_pow, ^)
728 
729 /* these flavors convert the boolean return dsb to a lua bool */
730 #define gen_opmet_bool(name, op) \
731 static int name(lua_State *L) \
732 { \
733  DataSourceBase::shared_ptr arg1 = *(luaM_checkudata_mt(L, 1, "Variable", DataSourceBase::shared_ptr)); \
734  DataSourceBase::shared_ptr arg2 = *(luaM_checkudata_mt(L, 2, "Variable", DataSourceBase::shared_ptr)); \
735  types::OperatorRepository::shared_ptr opreg = types::OperatorRepository::Instance(); \
736  DataSourceBase *res = opreg->applyBinary(#op, arg1.get(), arg2.get()); \
737  if(res == 0) \
738  luaL_error(L , "%s (operator %s) failed", #name, #op); \
739  res->evaluate(); \
740  return __Variable_tolua(L, res); \
741 } \
742 
743 gen_opmet_bool(Variable_eq, ==)
744 gen_opmet_bool(Variable_lt, <)
745 gen_opmet_bool(Variable_le, <=)
746 
747 static int Variable_opBinary(lua_State *L)
748 {
749  types::OperatorRepository::shared_ptr opreg = types::OperatorRepository::Instance();
750  const char *op = luaL_checkstring(L, 1);
753  DataSourceBase *res;
754 
755  res = opreg->applyBinary(op, arg1.get(), arg2.get());
756  if(res == 0)
757  luaL_error(L , "Variable.opBinary '%s' not applicable to args", op);
758 
759  res->evaluate();
760 
761  luaM_pushobject_mt(L, "Variable", DataSourceBase::shared_ptr)(res);
762  return 1;
763 }
764 
765 /*
766  * this is a dispatcher which checks if the key is a method, otherwise
767  * calls get for looking up the field. Inspired by
768  * http://lua-users.org/wiki/ObjectProperties
769  */
770 static int Variable_index(lua_State *L)
771 {
772  const char* key = luaL_checkstring(L, 2);
773 
774  lua_getmetatable(L, 1);
775  lua_getfield(L, -1, key);
776 
777  /* Either key is name of a method in the metatable */
778  if(!lua_isnil(L, -1))
779  return 1;
780 
781  /* ... or its a field access, so recall as self.get(self, value). */
782  lua_settop(L, 2);
783  return Variable_getMember(L);
784 }
785 
787 {
791  const char* mem = luaL_checkstring(L, 2);
792 
793  /* get dsb to be updated: we need its type before get-or-create'ing arg3 */
794  types::OperatorRepository::shared_ptr opreg = types::OperatorRepository::Instance();
796 
797  if ((curval = lookup_member(L, parent, mem)) == 0)
798  luaL_error(L, "Variable.newindex: indexing failed, no member %s", mem);
799 
800 
801  /* assigning a DSB */
802  if ((newvalp = luaM_testudata_mt(L, 3, "Variable", DataSourceBase::shared_ptr)) != NULL) {
803  newval = *newvalp;
804  if(!curval->update(newval.get())) {
805  luaL_error(L, "Variable.newindex: failed to assign %s to member %s of type %s",
806  newval->getType().c_str(), mem, curval->getType().c_str());
807  }
808  } else /* assigning basic type */
809  Variable_fromlua(L, curval, 3);
810  return 1;
811 }
812 
813 // Why doesn't the following work:
814 // static int Variable_gc(lua_State *L)
815 // {
816 // DataSourceBase::shared_ptr *dsbp = (DataSourceBase::shared_ptr*) lua_touserdata(L, 1);
817 // cache_clear(L, dsbp->get());
818 // dsbp->~DataSourceBase::shared_ptr();
819 // return 0;
820 // }
821 
822 template<typename T>
824 {
825  T* dsbp = (T*) lua_touserdata(L, 1);
826  cache_clear(L, dsbp->get());
827  reinterpret_cast<T*>(dsbp)->~T();
828  return 0;
829 }
830 
831 
832 static const struct luaL_Reg Variable_f [] = {
833  { "new", Variable_new },
834  { "tolua", Variable_tolua },
835  { "isbasic", Variable_isbasic },
836  { "toString", Variable_toString },
837  { "getTypes", Variable_getTypes },
838  { "getType", Variable_getType },
839  { "getTypeName", Variable_getTypeName },
840  { "getTypeIdName", Variable_getTypeIdName },
841  { "getMemberNames", Variable_getMemberNames },
842  { "getMember", Variable_getMember },
843  { "getMemberRaw", Variable_getMemberRaw },
844  { "tolud", Variable_tolightuserdata },
845  { "resize", Variable_resize },
846  { "opBinary", Variable_opBinary },
847  { "assign", Variable_update }, /* assign seems a better name than update */
848  { "unm", Variable_unm },
849  { "add", Variable_add },
850  { "sub", Variable_sub },
851  { "mul", Variable_mul },
852  { "div", Variable_div },
853  { "mod", Variable_mod },
854  { "pow", Variable_pow },
855  { "eq", Variable_eq },
856  { "lt", Variable_lt },
857  { "le", Variable_le },
858  { NULL, NULL}
859 };
860 
861 static const struct luaL_Reg Variable_m [] = {
862  { "tolua", Variable_tolua },
863  { "isbasic", Variable_isbasic },
864  { "toString", Variable_toString },
865  { "getType", Variable_getType },
866  { "getTypeName", Variable_getTypeName },
867  { "getTypeIdName", Variable_getTypeIdName },
868  { "getMemberNames", Variable_getMemberNames },
869  { "getMember", Variable_getMember },
870  { "getMemberRaw", Variable_getMemberRaw },
871  { "tolud", Variable_tolightuserdata },
872  { "resize", Variable_resize },
873  { "opBinary", Variable_opBinary },
874  { "assign", Variable_update }, /* assign seems a better name than update */
875  { "__unm", Variable_unm },
876  { "__add", Variable_add },
877  { "__sub", Variable_sub },
878  { "__mul", Variable_mul },
879  { "__div", Variable_div },
880  { "__mod", Variable_mod },
881  { "__pow", Variable_pow },
882  { "__eq", Variable_eq },
883  { "__lt", Variable_lt },
884  { "__le", Variable_le },
885  { "__index", Variable_index },
886  { "__newindex", Variable_newindex },
887  // { "__gc", GCMethod<DataSourceBase::shared_ptr> },
888  // {"__gc", Variable_gc},
889  {"__gc", VariableGC<DataSourceBase::shared_ptr> },
890  { NULL, NULL}
891 };
892 
893 
894 /***************************************************************
895  * Property (boxed)
896  ***************************************************************/
897 
898 gen_push_bxptr(Property_push, "Property", PropertyBase)
899 
900 static int Property_new(lua_State *L)
901 {
902  const char *type, *name, *desc;
903  PropertyBase *pb;
904  int argc = lua_gettop(L);
905  type = luaL_checkstring(L, 1);
906 
907  /* name and description are optional */
908  name = (argc > 1) ? luaL_checkstring(L, 2) : "";
909  desc = (argc > 2) ? luaL_checkstring(L, 3) : "";
910 
911  types::TypeInfo *ti = types::TypeInfoRepository::Instance()->type(type);
912 
913  if(!ti)
914  luaL_error(L, "Property.new: unknown type %s", type);
915 
916  pb = ti->buildProperty(name, desc);
917  Property_push(L, pb);
918  return 1;
919 }
920 
921 static int Property_get(lua_State *L)
922 {
923  PropertyBase *pb = *(luaM_checkudata_mt_bx(L, 1, "Property", PropertyBase));
925  return 1;
926 }
927 
929 {
930  PropertyBase *pb = *(luaM_checkudata_mt_bx(L, 1, "Property", PropertyBase));
932  return 1;
933 }
934 
935 static int Property_set(lua_State *L)
936 {
940  PropertyBase *pb = *(luaM_checkudata_mt_bx(L, 1, "Property", PropertyBase));
941  propdsb = pb->getDataSource();
942 
943  /* assigning a DSB */
944  if ((newdsbp = luaM_testudata_mt(L, 2, "Variable", DataSourceBase::shared_ptr)) != NULL) {
945  newdsb = *newdsbp;
946  if(!propdsb->update(newdsb.get()))
947  luaL_error(L, "Property.set: failed to assign type %s to type %s",
948  newdsb->getType().c_str(), propdsb->getType().c_str());
949  } else { /* assigning a Lua value */
950  Variable_fromlua(L, propdsb, 2);
951  }
952  return 1;
953 }
954 
955 static int Property_info(lua_State *L)
956 {
957  PropertyBase *pb = *(luaM_checkudata_mt_bx(L, 1, "Property", PropertyBase));
958  lua_newtable(L);
959  lua_pushstring(L, "name"); lua_pushstring(L, pb->getName().c_str()); lua_rawset(L, -3);
960  lua_pushstring(L, "desc"); lua_pushstring(L, pb->getDescription().c_str()); lua_rawset(L, -3);
961  lua_pushstring(L, "type"); lua_pushstring(L, pb->getType().c_str()); lua_rawset(L, -3);
962  return 1;
963 }
964 
965 #if NOT_USED_YET
966 /*
967  * Race condition if we collect properties: if we add this property to
968  * a TC and our life ends before that of the TC, the property will be
969  * deleted before the TaskContext.
970  */
971 static int Property_gc(lua_State *L)
972 {
973  PropertyBase *pb = *(luaM_checkudata_mt_bx(L, 1, "Property", PropertyBase));
974  delete pb;
975  return 0;
976 }
977 #endif
978 
979 /* only explicit destruction allowed */
980 static int Property_del(lua_State *L)
981 {
982  PropertyBase *pb = *(luaM_checkudata_mt_bx(L, 1, "Property", PropertyBase));
983  delete pb;
984 
985  /* this prevents calling rtt methods which would cause a crash */
986  luaL_getmetatable(L, "__dead__");
987  lua_setmetatable(L, -2);
988  return 0;
989 }
990 
991 /* indexability of properties */
992 /*
993  * this is a dispatcher which checks if the key is a method, otherwise
994  * calls get for looking up the field. Inspired by
995  * http://lua-users.org/wiki/ObjectProperties
996  */
997 static int Property_index(lua_State *L)
998 {
999  const char* key = luaL_checkstring(L, 2);
1000 
1001  lua_getmetatable(L, 1);
1002  lua_getfield(L, -1, key); /* this actually calls the method */
1003 
1004  /* Either key is name of a method in the metatable */
1005  if(!lua_isnil(L, -1))
1006  return 1;
1007 
1008  lua_settop(L, 2); /* reset stack */
1009  Property_get(L); /* pushes property var */
1010  lua_replace(L, 1); /* replace prop with var */
1011  return Variable_index(L);
1012 }
1013 
1015 {
1016  Property_get(L);
1017  lua_replace(L, 1);
1018  return Variable_newindex(L);
1019 }
1020 
1021 static const struct luaL_Reg Property_f [] = {
1022  {"new", Property_new },
1023  {"get", Property_get },
1024  {"getRaw", Property_getRaw },
1025  {"set", Property_set },
1026  {"info", Property_info },
1027  {"delete", Property_del },
1028  {NULL, NULL}
1029 };
1030 
1031 static const struct luaL_Reg Property_m [] = {
1032  {"get", Property_get },
1033  {"getRaw", Property_getRaw },
1034  {"set", Property_set },
1035  {"info", Property_info },
1036  // todo: shall we or not? s.o. {"__gc", Property_gc },
1037  {"delete", Property_del },
1038  {"__index", Property_index },
1039  {"__newindex", Property_newindex },
1040  {NULL, NULL}
1041 };
1042 
1043 /***************************************************************
1044  * Attribute (boxed)
1045  ***************************************************************/
1046 
1047 gen_push_bxptr(Attribute_push, "Attribute", AttributeBase)
1048 
1049 static int Attribute_new(lua_State *L)
1050 {
1051  const char *type, *name;
1052  AttributeBase *pb;
1053  int argc = lua_gettop(L);
1054  type = luaL_checkstring(L, 1);
1055 
1056  /* name and description are optional */
1057  name = (argc > 1) ? luaL_checkstring(L, 2) : "";
1058 
1059  types::TypeInfo *ti = types::TypeInfoRepository::Instance()->type(type);
1060 
1061  if(!ti)
1062  luaL_error(L, "Attribute.new: unknown type %s", type);
1063 
1064  pb = ti->buildAttribute(name);
1065  Attribute_push(L, pb);
1066  return 1;
1067 }
1068 
1069 static int Attribute_get(lua_State *L)
1070 {
1071  AttributeBase *pb = *(luaM_checkudata_mt_bx(L, 1, "Attribute", AttributeBase));
1073  return 1;
1074 }
1075 
1077 {
1078  AttributeBase *pb = *(luaM_checkudata_mt_bx(L, 1, "Attribute", AttributeBase));
1080  return 1;
1081 }
1082 
1083 static int Attribute_set(lua_State *L)
1084 {
1086  DataSourceBase::shared_ptr *newdsbp;
1088  AttributeBase *pb = *(luaM_checkudata_mt_bx(L, 1, "Attribute", AttributeBase));
1089  propdsb = pb->getDataSource();
1090 
1091  /* assigning a DSB */
1092  if ((newdsbp = luaM_testudata_mt(L, 2, "Variable", DataSourceBase::shared_ptr)) != NULL) {
1093  newdsb = *newdsbp;
1094  if(!propdsb->update(newdsb.get()))
1095  luaL_error(L, "Attribute.set: failed to assign type %s to type %s",
1096  newdsb->getType().c_str(), propdsb->getType().c_str());
1097  } else { /* assigning a Lua value */
1098  Variable_fromlua(L, propdsb, 2);
1099  }
1100  return 1;
1101 }
1102 
1104 {
1105  AttributeBase *pb = *(luaM_checkudata_mt_bx(L, 1, "Attribute", AttributeBase));
1106  lua_newtable(L);
1107  lua_pushstring(L, "name"); lua_pushstring(L, pb->getName().c_str()); lua_rawset(L, -3);
1108  lua_pushstring(L, "type"); lua_pushstring(L, pb->getDataSource()->getType().c_str()); lua_rawset(L, -3);
1109  return 1;
1110 }
1111 
1112 #if NOT_USED_YET
1113 /*
1114  * Race condition if we collect properties: if we add this attribute to
1115  * a TC and our life ends before that of the TC, the attribute will be
1116  * deleted before the TaskContext.
1117  */
1118 static int Attribute_gc(lua_State *L)
1119 {
1120  AttributeBase *pb = *(luaM_checkudata_mt_bx(L, 1, "Attribute", AttributeBase));
1121  delete pb;
1122  return 0;
1123 }
1124 #endif
1125 
1126 
1127 /* only explicit destruction allowed */
1128 static int Attribute_del(lua_State *L)
1129 {
1130  AttributeBase *pb = *(luaM_checkudata_mt_bx(L, 1, "Attribute", AttributeBase));
1131  delete pb;
1132 
1133  /* this prevents calling rtt methods which would cause a crash */
1134  luaL_getmetatable(L, "__dead__");
1135  lua_setmetatable(L, -2);
1136  return 0;
1137 }
1138 
1139 /* indexability of properties */
1140 /*
1141  * this is a dispatcher which checks if the key is a method, otherwise
1142  * calls get for looking up the field. Inspired by
1143  * http://lua-users.org/wiki/ObjectProperties
1144  */
1146 {
1147  const char* key = luaL_checkstring(L, 2);
1148 
1149  lua_getmetatable(L, 1);
1150  lua_getfield(L, -1, key); /* this actually calls the method */
1151 
1152  /* Either key is name of a method in the metatable */
1153  if(!lua_isnil(L, -1))
1154  return 1;
1155 
1156  lua_settop(L, 2); /* reset stack */
1157  Attribute_get(L); /* pushes attribute var */
1158  lua_replace(L, 1); /* replace prop with var */
1159  return Variable_index(L);
1160 }
1161 
1163 {
1164  Attribute_get(L);
1165  lua_replace(L, 1);
1166  return Variable_newindex(L);
1167 }
1168 
1169 static const struct luaL_Reg Attribute_f [] = {
1170  {"new", Attribute_new },
1171  {"get", Attribute_get },
1172  {"getRaw", Attribute_getRaw },
1173  {"set", Attribute_set },
1174  {"info", Attribute_info },
1175  {"delete", Attribute_del },
1176  {NULL, NULL}
1177 };
1178 
1179 static const struct luaL_Reg Attribute_m [] = {
1180  {"get", Attribute_get },
1181  {"getRaw", Attribute_getRaw },
1182  {"set", Attribute_set },
1183  {"info", Attribute_info },
1184  // todo: shall we or not? s.o. {"__gc", Attribute_gc },
1185  {"delete", Attribute_del },
1186  {"__index", Attribute_index },
1187  {"__newindex", Attribute_newindex },
1188  {NULL, NULL}
1189 };
1190 
1191 /***************************************************************
1192  * Ports (boxed)
1193  ***************************************************************/
1194 
1195 /* both input or output */
1196 static int Port_info(lua_State *L)
1197 {
1198  int arg_type;
1199  const char* port_type = NULL;
1200  PortInterface **pip;
1201  PortInterface *pi = NULL;
1202 
1203  if((pip = (PortInterface**) luaL_testudata(L, 1, "InputPort")) != NULL) {
1204  pi = *pip;
1205  port_type = "in";
1206  } else if((pip = (PortInterface**) luaL_testudata(L, 1, "OutputPort")) != NULL) {
1207  pi = *pip;
1208  port_type = "out";
1209  }
1210  else {
1211  arg_type = lua_type(L, 1);
1212  luaL_error(L, "Port.info: invalid argument, expected Port, got %s",
1213  lua_typename(L, arg_type));
1214  }
1215 
1216  lua_newtable(L);
1217  lua_pushstring(L, "name"); lua_pushstring(L, pi->getName().c_str()); lua_rawset(L, -3);
1218  lua_pushstring(L, "desc"); lua_pushstring(L, pi->getDescription().c_str()); lua_rawset(L, -3);
1219  lua_pushstring(L, "connected"); lua_pushboolean(L, pi->connected()); lua_rawset(L, -3);
1220  lua_pushstring(L, "isLocal"); lua_pushboolean(L, pi->isLocal()); lua_rawset(L, -3);
1221  lua_pushstring(L, "type"); lua_pushstring(L, pi->getTypeInfo()->getTypeName().c_str()); lua_rawset(L, -3);
1222  lua_pushstring(L, "porttype"); lua_pushstring(L, port_type); lua_rawset(L, -3);
1223 
1224  return 1;
1225 }
1226 
1227 static int Port_connect(lua_State *L)
1228 {
1229  int arg_type, ret;
1230  PortInterface **pip1, **pip2;
1231  PortInterface *pi1 = NULL;
1232  PortInterface *pi2 = NULL;
1233  ConnPolicy **cpp;
1234  ConnPolicy *cp = NULL;
1235 
1236  if((pip1 = (PortInterface**) luaL_testudata(L, 1, "InputPort")) != NULL) {
1237  pi1= *pip1;
1238  } else if((pip1 = (PortInterface**) luaL_testudata(L, 1, "OutputPort")) != NULL) {
1239  pi1= *pip1;
1240  }
1241  else {
1242  arg_type = lua_type(L, 1);
1243  luaL_error(L, "Port.info: invalid argument 1, expected Port, got %s",
1244  lua_typename(L, arg_type));
1245  }
1246  if((pip2 = (PortInterface**) luaL_testudata(L, 2, "InputPort")) != NULL) {
1247  pi2= *pip2;
1248  } else if((pip2 = (PortInterface**) luaL_testudata(L, 2, "OutputPort")) != NULL) {
1249  pi2= *pip2;
1250  }
1251  else {
1252  arg_type = lua_type(L, 2);
1253  luaL_error(L, "Port.connect: invalid argument 2, expected Port, got %s",
1254  lua_typename(L, arg_type));
1255  }
1256 
1257  if((cpp = (ConnPolicy**) luaL_testudata(L, 3, "ConnPolicy")) != NULL) {
1258  cp=*cpp;
1259  }
1260 
1261  if ( cp )
1262  ret = pi1->connectTo(pi2, *cp);
1263  else
1264  ret = pi1->connectTo(pi2);
1265 
1266  lua_pushboolean(L, ret);
1267 
1268  return 1;
1269 }
1270 
1272 {
1273  int arg_type, ret;
1274  PortInterface **pip1, **pip2;
1275  PortInterface *pi1 = NULL;
1276  PortInterface *pi2 = NULL;
1277 
1278  if((pip1 = (PortInterface**) luaL_testudata(L, 1, "InputPort")) != NULL) {
1279  pi1= *pip1;
1280  } else if((pip1 = (PortInterface**) luaL_testudata(L, 1, "OutputPort")) != NULL) {
1281  pi1= *pip1;
1282  }
1283  else {
1284  arg_type = lua_type(L, 1);
1285  luaL_error(L, "Port.info: invalid argument 1, expected Port, got %s",
1286  lua_typename(L, arg_type));
1287  }
1288  if((pip2 = (PortInterface**) luaL_testudata(L, 2, "InputPort")) != NULL) {
1289  pi2= *pip2;
1290  } else if((pip2 = (PortInterface**) luaL_testudata(L, 2, "OutputPort")) != NULL) {
1291  pi2= *pip2;
1292  }
1293 
1294  if (pi2 != NULL)
1295  ret = pi1->disconnect(pi2);
1296  else{
1297  pi1->disconnect();
1298  ret = 1;
1299  }
1300  lua_pushboolean(L, ret);
1301 
1302  return 1;
1303 }
1304 
1305 
1306 
1307 /* InputPort (boxed) */
1308 
1309 gen_push_bxptr(InputPort_push, "InputPort", InputPortInterface)
1310 
1311 static int InputPort_new(lua_State *L)
1312 {
1313  const char *type, *name, *desc;
1314  InputPortInterface* ipi;
1315  int argc = lua_gettop(L);
1316 
1317  type = luaL_checkstring(L, 1);
1318 
1319  /* name and description are optional */
1320  name = (argc > 1) ? luaL_checkstring(L, 2) : "";
1321  desc = (argc > 2) ? luaL_checkstring(L, 3) : "";
1322 
1323  types::TypeInfo *ti = types::TypeInfoRepository::Instance()->type(type);
1324  if(ti==0)
1325  luaL_error(L, "InputPort.new: unknown type %s", type);
1326 
1327  ipi = ti->inputPort(name);
1328 
1329  if(!ipi)
1330  luaL_error(L, "InputPort.new: creating port of type %s failed", type);
1331 
1332  ipi->doc(desc);
1333  InputPort_push(L, ipi);
1334  return 1;
1335 }
1336 
1338 {
1339  int ret = 1;
1340  InputPortInterface *ip = *(luaM_checkudata_mt_bx(L, 1, "InputPort", InputPortInterface));
1343  FlowStatus fs;
1344 
1345  /* if we get don't get a DS to store the result, create one */
1346  if ((dsbp = luaM_testudata_mt(L, 2, "Variable", DataSourceBase::shared_ptr)) != NULL)
1347  dsb = *dsbp;
1348  else {
1349  dsb = ip->getTypeInfo()->buildValue();
1350  ret = 2;
1351  }
1352 
1353  fs = ip->read(dsb);
1354 
1355  if(fs == NoData) lua_pushstring(L, "NoData");
1356  else if (fs == NewData) lua_pushstring(L, "NewData");
1357  else if (fs == OldData) lua_pushstring(L, "OldData");
1358  else luaL_error(L, "InputPort.read: unknown FlowStatus returned");
1359 
1360  if(ret>1)
1361  Variable_push_coerce(L, dsb);
1362 
1363  return ret;
1364 }
1365 
1366 #ifdef NOT_USED_YET
1367 static int InputPort_gc(lua_State *L)
1368 {
1369  InputPortInterface *ip = *(luaM_checkudata_mt_bx(L, 1, "InputPort", InputPortInterface));
1370  delete ip;
1371  return 0;
1372 }
1373 #endif
1374 
1375 /* only explicit destruction allowed */
1376 static int InputPort_del(lua_State *L)
1377 {
1378  InputPortInterface *ip = *(luaM_checkudata_mt_bx(L, 1, "InputPort", InputPortInterface));
1379  delete ip;
1380 
1381  /* this prevents calling rtt methods which would cause a crash */
1382  luaL_getmetatable(L, "__dead__");
1383  lua_setmetatable(L, -2);
1384  return 0;
1385 }
1386 
1387 static const struct luaL_Reg InputPort_f [] = {
1388  {"new", InputPort_new },
1389  {"read", InputPort_read },
1390  {"info", Port_info },
1391  {"connect", Port_connect },
1392  {"disconnect", Port_disconnect },
1393  {"delete", InputPort_del },
1394  {NULL, NULL}
1395 };
1396 
1397 static const struct luaL_Reg InputPort_m [] = {
1398  {"read", InputPort_read },
1399  {"info", Port_info },
1400  {"delete", InputPort_del },
1401  {"connect", Port_connect },
1402  {"disconnect", Port_disconnect },
1403  /* {"__gc", InputPort_gc }, */
1404  {NULL, NULL}
1405 };
1406 
1407 /* OutputPort */
1408 
1409 gen_push_bxptr(OutputPort_push, "OutputPort", OutputPortInterface)
1410 
1411 
1412 static int OutputPort_new(lua_State *L)
1413 {
1414  const char *type, *name, *desc;
1415  OutputPortInterface* opi;
1416  int argc = lua_gettop(L);
1417 
1418  type = luaL_checkstring(L, 1);
1419 
1420  /* name and description are optional */
1421  name = (argc > 1) ? luaL_checkstring(L, 2) : "";
1422  desc = (argc > 2) ? luaL_checkstring(L, 3) : "";
1423 
1424  types::TypeInfo *ti = types::TypeInfoRepository::Instance()->type(type);
1425 
1426  if(ti==0)
1427  luaL_error(L, "OutputPort.new: unknown type %s", type);
1428 
1429  opi = ti->outputPort(name);
1430 
1431  if(!opi)
1432  luaL_error(L, "OutputPort.new: creating port of type %s failed", type);
1433 
1434  opi->doc(desc);
1435  OutputPort_push(L, opi);
1436  return 1;
1437 }
1438 
1440 {
1443 
1444  OutputPortInterface *op = *(luaM_checkudata_mt_bx(L, 1, "OutputPort", OutputPortInterface));
1445 
1446  /* fastpath: Variable argument */
1447  if ((dsbp = luaM_testudata_mt(L, 2, "Variable", DataSourceBase::shared_ptr)) != NULL) {
1448  dsb = *dsbp;
1449  } else {
1450  /* slowpath: convert lua value to dsb */
1451  dsb = Variable_fromlua(L, op->getTypeInfo(), 2);
1452  }
1453  op->write(dsb);
1454  return 0;
1455 }
1456 
1457 #ifdef NOT_USED_YET
1458 static int OutputPort_gc(lua_State *L)
1459 {
1460  OutputPortInterface *op = *(luaM_checkudata_mt_bx(L, 1, "OutputPort", OutputPortInterface));
1461  delete op;
1462  return 0;
1463 }
1464 #endif
1465 
1466 /* only explicit destruction allowed */
1468 {
1469  OutputPortInterface *op = *(luaM_checkudata_mt_bx(L, 1, "OutputPort", OutputPortInterface));
1470  delete op;
1471 
1472  /* this prevents calling rtt methods which would cause a crash */
1473  luaL_getmetatable(L, "__dead__");
1474  lua_setmetatable(L, -2);
1475  return 0;
1476 }
1477 
1478 static const struct luaL_Reg OutputPort_f [] = {
1479  {"new", OutputPort_new },
1480  {"write", OutputPort_write },
1481  {"info", Port_info },
1482  {"connect", Port_connect },
1483  {"disconnect", Port_disconnect },
1484  {"delete", OutputPort_del },
1485  {NULL, NULL}
1486 };
1487 
1488 static const struct luaL_Reg OutputPort_m [] = {
1489  {"write", OutputPort_write },
1490  {"info", Port_info },
1491  {"connect", Port_connect },
1492  {"disconnect", Port_disconnect },
1493  {"delete", OutputPort_del },
1494  /* {"__gc", OutputPort_gc }, */
1495  {NULL, NULL}
1496 };
1497 
1498 /***************************************************************
1499  * Operation
1500  ***************************************************************/
1501 
1505  unsigned int arity;
1506  bool is_void;
1507 
1508  /* we need to store references to the dsb which we created
1509  on-the-fly, because the ReferenceDSB does not hold a
1510  shared_ptr, and hence these DSN might get destructed
1511  before/during the call
1512  */
1513  std::vector<base::DataSourceBase::shared_ptr> dsb_store;
1514  std::vector<internal::Reference*> args;
1517 };
1518 
1519 template<typename T>
1521 {
1522  T* oh = (T*) lua_touserdata(L, 1);
1523  delete oh->occ;
1524  reinterpret_cast<T*>(lua_touserdata(L, 1))->~T();
1525  return 0;
1526 }
1527 
1529 {
1530  int i=1;
1531  std::vector<ArgumentDescription> args;
1532  OperationHandle *op = luaM_checkudata_mt(L, 1, "Operation", OperationHandle);
1533 
1534  lua_pushstring(L, op->oip->getName().c_str()); /* name */
1535  lua_pushstring(L, op->oip->description().c_str()); /* description */
1536  lua_pushstring(L, op->oip->resultType().c_str()); /* result type */
1537  lua_pushinteger(L, op->arity); /* arity */
1538 
1539  args = op->oip->getArgumentList();
1540 
1541  lua_newtable(L);
1542 
1543  for (std::vector<ArgumentDescription>::iterator it = args.begin(); it != args.end(); it++) {
1544  lua_newtable(L);
1545  lua_pushstring(L, "name"); lua_pushstring(L, it->name.c_str()); lua_rawset(L, -3);
1546  lua_pushstring(L, "type"); lua_pushstring(L, it->type.c_str()); lua_rawset(L, -3);
1547  lua_pushstring(L, "desc"); lua_pushstring(L, it->description.c_str()); lua_rawset(L, -3);
1548  lua_rawseti(L, -2, i++);
1549  }
1550  return 5;
1551 }
1552 
1554 {
1555  bool ret;
1556  DataSourceBase::shared_ptr dsb, *dsbp;
1557 
1558  OperationHandle *oh = luaM_checkudata_mt(L, 1, "Operation", OperationHandle);
1559  OperationInterfacePart *oip = oh->oip;
1560  unsigned int argc = lua_gettop(L);
1561 
1562  if(oh->arity != argc-1)
1563  luaL_error(L, "Operation.call: wrong number of args. expected %d, got %d", oh->arity, argc-1);
1564 
1565  /* update dsbs */
1566  for(unsigned int arg=2; arg<=argc; arg++) {
1567  /* fastpath: Variable argument */
1568  if ((dsbp = luaM_testudata_mt(L, arg, "Variable", DataSourceBase::shared_ptr)) != NULL) {
1569  dsb = *dsbp;
1570  } else {
1571  /* slowpath: convert lua value to dsb */
1572  dsb = Variable_fromlua(L, oip->getArgumentType(arg-1), arg);
1573  /* this dsb must outlive occ->call (see comment in
1574  OperationHandle def.): */
1575  oh->dsb_store.push_back(dsb);
1576  }
1577  if(!dsb->isAssignable())
1578  luaL_error(L, "Operation.call: argument %d is not assignable.", arg-1);
1579 
1580  ret = oh->args[arg-2]->setReference(dsb);
1581  if (!ret)
1582  luaL_error(L, "Operation_call: setReference failed, wrong type of argument?");
1583  }
1584 
1585  if(!oh->occ->call())
1586  luaL_error(L, "Operation.call: call failed.");
1587 
1588  oh->dsb_store.clear();
1589 
1590  if(!oh->is_void)
1591  Variable_push_coerce(L, oh->ret_dsb);
1592  else
1593  lua_pushnil(L);
1594  return 1;
1595 }
1596 
1598 {
1599  DataSourceBase::shared_ptr dsb, *dsbp;
1600 
1601  OperationHandle *oh = luaM_checkudata_mt(L, 1, "Operation", OperationHandle);
1602  OperationInterfacePart *oip = oh->oip;
1603  unsigned int argc = lua_gettop(L);
1604 
1605  if(oh->arity != argc-1)
1606  luaL_error(L, "Operation.send: wrong number of args. expected %d, got %d", oh->arity, argc-1);
1607 
1608  /* update dsbs */
1609  for(unsigned int arg=2; arg<=argc; arg++) {
1610  /* fastpath: Variable argument */
1611  if ((dsbp = luaM_testudata_mt(L, arg, "Variable", DataSourceBase::shared_ptr)) != NULL) {
1612  dsb = *dsbp;
1613  } else {
1614  /* slowpath: convert lua value to dsb */
1615  dsb = Variable_fromlua(L, oip->getArgumentType(arg-1), arg);
1616  /* this dsb must outlive occ->call (see comment in
1617  OperationHandle def.): */
1618  oh->dsb_store.push_back(dsb);
1619  }
1620  oh->args[arg-2]->setReference(dsb);
1621  }
1622 
1623  luaM_pushobject_mt(L, "SendHandle", SendHandleC)(oh->occ->send());
1624  return 1;
1625 }
1626 
1628 {
1629  int ret;
1630  try {
1631  ret = __Operation_call(L);
1632  } catch(const std::exception &exc) {
1633  luaL_error(L, "Operation.call: caught exception '%s'", exc.what());
1634  } catch(...) {
1635  luaL_error(L, "Operation.call: caught unknown exception");
1636  }
1637  return ret;
1638 }
1639 
1641 {
1642  int ret;
1643  try {
1644  ret = __Operation_send(L);
1645  } catch(const std::exception &exc) {
1646  luaL_error(L, "Operation.send: caught exception '%s'", exc.what());
1647  } catch(...) {
1648  luaL_error(L, "Operation.send: caught unknown exception");
1649  }
1650  return ret;
1651 }
1652 
1653 
1654 static const struct luaL_Reg Operation_f [] = {
1655  { "info", Operation_info },
1656  { "call", Operation_call },
1657  { "send", Operation_send },
1658  { NULL, NULL }
1659 
1660 };
1661 
1662 static const struct luaL_Reg Operation_m [] = {
1663  { "info", Operation_info },
1664  { "send", Operation_send },
1665  { "__call", Operation_call },
1666  { "__gc", OperationGC<OperationHandle> },
1667  { NULL, NULL }
1668 };
1669 
1670 /***************************************************************
1671  * Service (boxed)
1672  ***************************************************************/
1673 
1675 {
1676  Service::shared_ptr srv = *(luaM_checkudata_mt(L, 1, "Service", Service::shared_ptr));
1677  lua_pushstring(L, srv->getName().c_str());
1678  return 1;
1679 }
1680 
1681 static int Service_doc(lua_State *L)
1682 {
1683  int ret;
1684  const char* doc;
1685  Service::shared_ptr srv = *(luaM_checkudata_mt(L, 1, "Service", Service::shared_ptr));
1686  if(lua_gettop(L) == 1) {
1687  lua_pushstring(L, srv->doc().c_str());
1688  ret = 1;
1689  } else {
1690  doc = luaL_checkstring(L, 2);
1691  srv->doc(doc);
1692  ret = 0;
1693  }
1694 
1695  return ret;
1696 }
1697 
1699 {
1700  Service::shared_ptr srv = *(luaM_checkudata_mt(L, 1, "Service", Service::shared_ptr));
1701  push_vect_str(L, srv->getProviderNames());
1702  return 1;
1703 }
1704 
1706 {
1707  Service::shared_ptr srv = *(luaM_checkudata_mt(L, 1, "Service", Service::shared_ptr));
1708  push_vect_str(L, srv->getOperationNames());
1709  return 1;
1710 }
1711 
1712 
1714 {
1715  int ret;
1716  Service::shared_ptr srv = *(luaM_checkudata_mt(L, 1, "Service", Service::shared_ptr));
1717  const char* op = luaL_checkstring(L, 2);
1718  ret = srv->hasOperation(op);
1719  lua_pushboolean(L, ret);
1720  return 1;
1721 }
1722 
1724 {
1725  Service::shared_ptr srv = *(luaM_checkudata_mt(L, 1, "Service", Service::shared_ptr));
1726  push_vect_str(L, srv->getPortNames());
1727  return 1;
1728 }
1729 
1731 {
1732  int ret, i, argc;
1733  const char* subsrv_str;
1734  Service::shared_ptr srv, subsrv;
1735 
1736  srv = *(luaM_checkudata_mt(L, 1, "Service", Service::shared_ptr));
1737  argc=lua_gettop(L);
1738 
1739  /* return "this" if no args given */
1740  if(argc == 1) {
1741  ret = 1;
1742  goto out;
1743  }
1744 
1745  for(i=2; i<=argc; i++) {
1746  subsrv_str = luaL_checkstring(L, i);
1747  subsrv = srv->getService(subsrv_str);
1748  if (subsrv == 0)
1749  luaL_error(L, "Service.provides: no subservice %s of service %s",
1750  subsrv_str, srv->getName().c_str() );
1751  else
1752  luaM_pushobject_mt(L, "Service", Service::shared_ptr)(subsrv);
1753  }
1754  ret = argc - 1;
1755 
1756  out:
1757  return ret;
1758 }
1759 
1760 /* returns restype, arity, table-of-arg-descr */
1762 {
1763  int i=1;
1764  Service *serv = *(luaM_checkudata_bx(L, 1, Service));
1765  const char *op = luaL_checkstring(L, 2);
1766  std::vector<ArgumentDescription> args;
1767 
1768  if(!serv->hasMember(op))
1769  luaL_error(L, "Service.getOperationInfo failed: no such operation");
1770 
1771  lua_pushstring(L, serv->getDescription(op).c_str()); /* description */
1772  lua_pushstring(L, serv->getResultType(op).c_str()); /* result type */
1773  lua_pushinteger(L, serv->getArity(op)); /* arity */
1774 
1775  args = serv->getArgumentList(op);
1776 
1777  lua_newtable(L);
1778 
1779  for (std::vector<ArgumentDescription>::iterator it = args.begin(); it != args.end(); it++) {
1780  lua_newtable(L);
1781  lua_pushstring(L, "name"); lua_pushstring(L, it->name.c_str()); lua_rawset(L, -3);
1782  lua_pushstring(L, "type"); lua_pushstring(L, it->type.c_str()); lua_rawset(L, -3);
1783  lua_pushstring(L, "desc"); lua_pushstring(L, it->description.c_str()); lua_rawset(L, -3);
1784  lua_rawseti(L, -2, i++);
1785  }
1786 
1787  return 4;
1788 }
1789 
1791 {
1792  const char *op_str;
1794  Service::shared_ptr srv;
1796  const types::TypeInfo *ti;
1797  OperationHandle *oh;
1798  TaskContext *this_tc;
1799 
1800  srv = *(luaM_checkudata_mt(L, 1, "Service", Service::shared_ptr));
1801  op_str = luaL_checkstring(L, 2);
1802  oip = srv->getOperation(op_str);
1803 
1804  if(!oip)
1805  luaL_error(L, "Service_getOperation: service %s has no operation %s",
1806  srv->getName().c_str(), op_str);
1807 
1808  oh = (OperationHandle*) luaM_pushobject_mt(L, "Operation", OperationHandle)();
1809  oh->oip = oip;
1810  oh->arity = oip->arity();
1811  oh->args.reserve(oh->arity);
1812  this_tc = __getTC(L);
1813 
1814  oh->occ = new OperationCallerC(oip, op_str, this_tc->engine());
1815 
1816  /* create args
1817  * getArgumentType(0) is return value
1818  */
1819  for(unsigned int arg=1; arg <= oh->arity; arg++) {
1820  std::string type = oip->getArgumentType(arg)->getTypeName();
1821  ti = types::TypeInfoRepository::Instance()->type(type);
1822  if(!ti)
1823  luaL_error(L, "Operation.call: '%s', failed to locate TypeInfo for arg %d of type '%s'",
1824  op_str, arg, type.c_str());
1825 
1826  dsb = ti->buildReference((void*) 0xdeadbeef);
1827  if(!dsb)
1828  luaL_error(L, "Operation.call: '%s', failed to build DSB for arg %d of type '%s'",
1829  op_str, arg, type.c_str());
1830 
1831  oh->args.push_back(dynamic_cast<internal::Reference*>(dsb.get()));
1832  oh->occ->arg(dsb);
1833  }
1834 
1835  /* return value */
1836  if(oip->resultType() != "void"){
1837  ti = oip->getArgumentType(0); // 0 == return type
1838  if(!ti)
1839  luaL_error(L, "Operation.call: '%s', failed to locate TypeInfo for return value of type '%s'",
1840  op_str, oip->resultType().c_str());
1841  oh->ret_dsb=ti->buildValue();
1842  if(!oh->ret_dsb)
1843  luaL_error(L, "Operation.call: '%s', failed to build DSB for return value of type '%s'",
1844  op_str, oip->resultType().c_str());
1845 
1846  oh->occ->ret(oh->ret_dsb);
1847  oh->is_void=false;
1848  } else {
1849  oh->is_void=true;
1850  }
1851 
1852  if(!oh->occ->ready())
1853  luaL_error(L, "Service.getOperation: OperationCallerC not ready!");
1854 
1855  return 1;
1856 }
1857 
1859 {
1860  const char* name;
1861  PortInterface *pi;
1862  InputPortInterface *ipi;
1863  OutputPortInterface *opi;
1864 
1865  Service::shared_ptr srv;
1866 
1867  srv = *(luaM_checkudata_mt(L, 1, "Service", Service::shared_ptr));
1868  name = luaL_checkstring(L, 2);
1869 
1870  pi = srv->getPort(name);
1871  if(!pi)
1872  luaL_error(L, "Service.getPort: service %s has no port %",
1873  srv->getName().c_str(), name);
1874 
1875  /* input or output? */
1876  if ((ipi = dynamic_cast<InputPortInterface *> (pi)) != NULL)
1877  InputPort_push(L, ipi);
1878  else if ((opi = dynamic_cast<OutputPortInterface *> (pi)) != NULL)
1879  OutputPort_push(L, opi);
1880  else
1881  luaL_error(L, "Service.getPort: unknown port type returned");
1882 
1883  return 1;
1884 }
1885 
1887 {
1888  const char *name;
1889  PropertyBase *prop;
1890 
1891  Service::shared_ptr srv = *(luaM_checkudata_mt(L, 1, "Service", Service::shared_ptr));
1892  name = luaL_checkstring(L, 2);
1893 
1894  prop = srv->getProperty(name);
1895 
1896  if(!prop)
1897  luaL_error(L, "%s failed. No such property", __FILE__);
1898 
1899  Property_push(L, prop);
1900  return 1;
1901 }
1902 
1904 {
1905  Service::shared_ptr srv;
1906  srv = *(luaM_checkudata_mt(L, 1, "Service", Service::shared_ptr));
1907  std::vector<std::string> plist = srv->properties()->list();
1908  push_vect_str(L, plist);
1909  return 1;
1910 }
1911 
1913 {
1914  Service::shared_ptr srv;
1915  srv = *(luaM_checkudata_mt(L, 1, "Service", Service::shared_ptr));
1916  vector<PropertyBase*> props = srv->properties()->getProperties();
1917 
1918  int key = 1;
1919  lua_createtable(L, props.size(), 0);
1920  for(vector<PropertyBase*>::iterator it = props.begin(); it != props.end(); ++it) {
1921  Property_push(L, *it);
1922  lua_rawseti(L, -2, key++);
1923  }
1924 
1925  return 1;
1926 }
1927 
1929 {
1930  const char *name;
1931  AttributeBase *prop;
1932 
1933  Service::shared_ptr srv = *(luaM_checkudata_mt(L, 1, "Service", Service::shared_ptr));
1934  name = luaL_checkstring(L, 2);
1935 
1936  prop = srv->getAttribute(name);
1937 
1938  if(!prop)
1939  luaL_error(L, "%s failed. No such Attribute", __FILE__);
1940 
1941  Attribute_push(L, prop);
1942  return 1;
1943 }
1944 
1946 {
1947  Service::shared_ptr srv;
1948  srv = *(luaM_checkudata_mt(L, 1, "Service", Service::shared_ptr));
1949  std::vector<std::string> plist = srv->getAttributeNames();
1950  push_vect_str(L, plist);
1951  return 1;
1952 }
1953 
1955 {
1956  Service::shared_ptr srv;
1957  srv = *(luaM_checkudata_mt(L, 1, "Service", Service::shared_ptr));
1958  vector<AttributeBase*> props = srv->getValues();
1959 
1960  int key = 1;
1961  lua_createtable(L, props.size(), 0);
1962  for(vector<AttributeBase*>::iterator it = props.begin(); it != props.end(); ++it) {
1963  Attribute_push(L, *it);
1964  lua_rawseti(L, -2, key++);
1965  }
1966 
1967  return 1;
1968 }
1969 
1970 static const struct luaL_Reg Service_f [] = {
1971  { "getName", Service_getName },
1972  { "doc", Service_doc },
1973  { "getProviderNames", Service_getProviderNames },
1974  { "getOperationNames", Service_getOperationNames },
1975  { "hasOperation", Service_hasOperation },
1976  { "getOperationInfo", Service_getOperationInfo },
1977  { "getPortNames", Service_getPortNames },
1978  { "provides", Service_provides },
1979  { "getOperation", Service_getOperation },
1980  { "getPort", Service_getPort },
1981  { "getProperty", Service_getProperty },
1982  { "getProperties", Service_getProperties },
1983  { "getPropertyNames", Service_getPropertyNames },
1984  { "getAttribute", Service_getAttribute },
1985  { "getAttributes", Service_getAttributes },
1986  { "getAttributeNames", Service_getAttributeNames },
1987  { NULL, NULL }
1988 };
1989 
1990 static const struct luaL_Reg Service_m [] = {
1991  { "getName", Service_getName },
1992  { "doc", Service_doc },
1993  { "getProviderNames", Service_getProviderNames },
1994  { "getOperationNames", Service_getOperationNames },
1995  { "hasOperation", Service_hasOperation },
1996  { "getOperationInfo", Service_getOperationInfo },
1997  { "getPortNames", Service_getPortNames },
1998  { "provides", Service_provides },
1999  { "getOperation", Service_getOperation },
2000  { "getPort", Service_getPort },
2001  { "getProperty", Service_getProperty },
2002  { "getProperties", Service_getProperties },
2003  { "getPropertyNames", Service_getPropertyNames },
2004  { "getAttribute", Service_getAttribute },
2005  { "getAttributes", Service_getAttributes },
2006  { "getAttributeNames", Service_getAttributeNames },
2007  { "__gc", GCMethod<Service::shared_ptr> },
2008  { NULL, NULL }
2009 };
2010 
2011 /***************************************************************
2012  * ServiceRequester
2013  ***************************************************************/
2014 
2015 gen_push_bxptr(ServiceRequester_push, "ServiceRequester", ServiceRequester)
2016 
2017 static int ServiceRequester_getRequestName(lua_State *L)
2018 {
2019  ServiceRequester *sr;
2020 
2021  sr = *(luaM_checkudata_bx(L, 1, ServiceRequester));
2022  lua_pushstring(L, sr->getRequestName().c_str());
2023  return 1;
2024 }
2025 
2027 {
2028  ServiceRequester *sr;
2029  sr = *(luaM_checkudata_bx(L, 1, ServiceRequester));
2030  push_vect_str(L, sr->getRequesterNames());
2031  return 1;
2032 }
2033 
2035 {
2036  int ret;
2037  ServiceRequester *sr;
2038  sr = *(luaM_checkudata_bx(L, 1, ServiceRequester));
2039  ret = sr->ready();
2040  lua_pushboolean(L, ret);
2041  return 1;
2042 }
2043 
2045 {
2046  ServiceRequester *sr;
2047  sr = *(luaM_checkudata_bx(L, 1, ServiceRequester));
2048  sr->disconnect();
2049  return 0;
2050 }
2051 
2053 {
2054  int argc, ret, i;
2055  const char* subsr_str;
2056  ServiceRequester *sr;
2058 
2059  sr = *(luaM_checkudata_bx(L, 1, ServiceRequester));
2060  argc = lua_gettop(L);
2061 
2062  /* return "this" if no args given */
2063  if(argc == 1) {
2064  ret = 1;
2065  goto out;
2066  }
2067 
2068  for(i=2; i<=argc; i++) {
2069  subsr_str = luaL_checkstring(L, i);
2070  subsr = sr->requires(subsr_str);
2071  if (subsr == 0)
2072  luaL_error(L, "ServiceRequester: no required subservice %s of service %s",
2073  subsr_str, sr->getRequestName().c_str());
2074  else
2075  ServiceRequester_push(L, subsr.get());
2076  }
2077  ret = argc - 1;
2078 
2079  out:
2080  return ret;
2081 }
2082 
2083 static const struct luaL_Reg ServiceRequester_f [] = {
2084  { "getRequestName", ServiceRequester_getRequestName },
2085  { "getRequesterNames", ServiceRequester_getRequesterNames },
2086  { "ready", ServiceRequester_ready },
2087  { "disconnect", ServiceRequester_disconnect },
2088  { "requires", ServiceRequester_requires },
2089  { NULL, NULL }
2090 };
2091 
2092 static const struct luaL_Reg ServiceRequester_m [] = {
2093  { "getRequestName", ServiceRequester_getRequestName },
2094  { "getRequesterNames", ServiceRequester_getRequesterNames },
2095  { "ready", ServiceRequester_ready },
2096  { "disconnect", ServiceRequester_disconnect },
2097  { "requires", ServiceRequester_requires },
2098  { NULL, NULL }
2099 };
2100 
2101 
2102 /***************************************************************
2103  * TaskContext (boxed)
2104  ***************************************************************/
2105 
2106 gen_push_bxptr(TaskContext_push, "TaskContext", TaskContext)
2107 
2108 static int TaskContext_getName(lua_State *L)
2109 {
2110  const char *s;
2111  TaskContext *tc = *(luaM_checkudata_bx(L, 1, TaskContext));
2112  s = tc->getName().c_str();
2113  lua_pushstring(L, s);
2114  return 1;
2115 }
2116 
2118 {
2119  TaskContext *tc = *(luaM_checkudata_bx(L, 1, TaskContext));
2120  bool b = tc->start();
2121  lua_pushboolean(L, b);
2122  return 1;
2123 }
2124 
2126 {
2127  TaskContext *tc = *(luaM_checkudata_bx(L, 1, TaskContext));
2128  bool b = tc->stop();
2129  lua_pushboolean(L, b);
2130  return 1;
2131 }
2132 
2134 {
2135  TaskContext *tc = *(luaM_checkudata_bx(L, 1, TaskContext));
2136  bool ret = tc->configure();
2137  lua_pushboolean(L, ret);
2138  return 1;
2139 }
2140 
2142 {
2143  TaskContext *tc = *(luaM_checkudata_bx(L, 1, TaskContext));
2144  bool ret = tc->activate();
2145  lua_pushboolean(L, ret);
2146  return 1;
2147 }
2148 
2150 {
2151  TaskContext *tc = *(luaM_checkudata_bx(L, 1, TaskContext));
2152  bool ret = tc->cleanup();
2153  lua_pushboolean(L, ret);
2154  return 1;
2155 }
2156 
2158 {
2159  TaskContext *tc = *(luaM_checkudata_bx(L, 1, TaskContext));
2160  tc->error();
2161  return 0;
2162 }
2163 
2165 {
2166  TaskContext *tc = *(luaM_checkudata_bx(L, 1, TaskContext));
2167  bool ret = tc->recover();
2168  lua_pushboolean(L, ret);
2169  return 1;
2170 }
2171 
2173 {
2176  ts = (*tc)->getTaskState();
2177 
2178  switch(ts) {
2179  case TaskCore::Init: lua_pushstring(L, "Init"); break;
2180  case TaskCore::PreOperational: lua_pushstring(L, "PreOperational"); break;
2181  case TaskCore::FatalError: lua_pushstring(L, "FatalError"); break;
2182  case TaskCore::Exception: lua_pushstring(L, "Exception"); break;
2183  case TaskCore::Stopped: lua_pushstring(L, "Stopped"); break;
2184  case TaskCore::Running: lua_pushstring(L, "Running"); break;
2185  case TaskCore::RunTimeError: lua_pushstring(L, "RunTimeError"); break;
2186  default: lua_pushstring(L, "unknown");
2187  }
2188  return 1;
2189 }
2190 
2191 /* string-table getPeers(TaskContext self)*/
2192 /* should better return array of TC's */
2194 {
2195  TaskContext *tc = *(luaM_checkudata_bx(L, 1, TaskContext));
2196  std::vector<std::string> plist = tc->getPeerList();
2197  push_vect_str(L, plist);
2198  return 1;
2199 }
2200 
2201 /* bool addPeer(TaskContext self, TaskContext peer)*/
2203 {
2204  bool ret;
2205  TaskContext *self = *(luaM_checkudata_bx(L, 1, TaskContext));
2206  TaskContext *peer = *(luaM_checkudata_bx(L, 2, TaskContext));
2207  ret = self->addPeer(peer);
2208  lua_pushboolean(L, ret);
2209  return 1;
2210 }
2211 
2212 /* bool connectPeers(TaskContext self, TaskContext peer)*/
2214 {
2215  bool ret;
2216  TaskContext *self = *(luaM_checkudata_bx(L, 1, TaskContext));
2217  TaskContext *peer = *(luaM_checkudata_bx(L, 2, TaskContext));
2218  ret = self->connectPeers(peer);
2219  lua_pushboolean(L, ret);
2220  return 1;
2221 }
2222 
2223 /* void removePeer(TaskContext self, string peer)*/
2225 {
2226  std::string peer;
2227  TaskContext *self = *(luaM_checkudata_bx(L, 1, TaskContext));
2228  peer = luaL_checkstring(L, 2);
2229  self->removePeer(peer);
2230  return 0;
2231 }
2232 
2233 /* TaskContext getPeer(string name) */
2235 {
2236  std::string strpeer;
2237  TaskContext *peer;
2238  TaskContext *self = *(luaM_checkudata_bx(L, 1, TaskContext));
2239  strpeer = luaL_checkstring(L, 2);
2240  peer = self->getPeer(strpeer);
2241 
2242  if(!peer) {
2243  luaL_error(L, "TaskContext.getPeer: no peer %s", strpeer.c_str());
2244  goto out;
2245  }
2246 
2247  TaskContext_push(L, peer);
2248  out:
2249  return 1;
2250 }
2251 
2253 {
2254  TaskContext *tc = *(luaM_checkudata_bx(L, 1, TaskContext));
2255  std::vector<std::string> plist = tc->ports()->getPortNames();
2256  push_vect_str(L, plist);
2257  return 1;
2258 }
2259 
2261 {
2262  const char* name, *desc;
2263  PortInterface **pi;
2264  int argc = lua_gettop(L);
2265  TaskContext *tc = *(luaM_checkudata_bx(L, 1, TaskContext));
2266 
2267  pi = (PortInterface**) luaL_testudata(L, 2, "InputPort");
2268  if(pi) goto check_name;
2269 
2270  pi = (PortInterface**) luaL_testudata(L, 2, "OutputPort");
2271  if(pi) goto check_name;
2272 
2273  return luaL_error(L, "addPort: invalid argument, not a Port");
2274 
2275  check_name:
2276  if(argc > 2) {
2277  name = luaL_checkstring(L, 3);
2278  (*pi)->setName(name);
2279  }
2280 
2281  if(argc > 3) {
2282  desc = luaL_checkstring(L, 4);
2283  (*pi)->doc(desc);
2284  }
2285 
2286  tc->ports()->addPort(**pi);
2287  return 0;
2288 }
2289 
2291 {
2292  const char* name, *desc;
2293  InputPortInterface **ipi;
2294  int argc = lua_gettop(L);
2295  TaskContext *tc = *(luaM_checkudata_bx(L, 1, TaskContext));
2296 
2297  if((ipi = (InputPortInterface**) luaL_testudata(L, 2, "InputPort")) == NULL)
2298  return luaL_error(L, "addEventPort: invalid argument, not an InputPort");
2299 
2300  if(argc > 2) {
2301  name = luaL_checkstring(L, 3);
2302  (*ipi)->setName(name);
2303  }
2304 
2305  if(argc > 3) {
2306  desc = luaL_checkstring(L, 4);
2307  (*ipi)->doc(desc);
2308  }
2309 
2310  tc->ports()->addEventPort(**ipi);
2311  return 0;
2312 }
2313 
2315 {
2316  const char* name;
2317  PortInterface *pi;
2318  InputPortInterface *ipi;
2319  OutputPortInterface *opi;
2320 
2321  TaskContext *tc = *(luaM_checkudata_bx(L, 1, TaskContext));
2322  name = luaL_checkstring(L, 2);
2323 
2324  pi = tc->getPort(name);
2325  if(!pi)
2326  luaL_error(L, "TaskContext.getPort: no port %s for taskcontext %s",
2327  name, tc->getName().c_str());
2328 
2329  /* input or output? */
2330  if ((ipi = dynamic_cast<InputPortInterface *> (pi)) != NULL)
2331  InputPort_push(L, ipi);
2332  else if ((opi = dynamic_cast<OutputPortInterface *> (pi)) != NULL)
2333  OutputPort_push(L, opi);
2334  else
2335  luaL_error(L, "TaskContext.getPort: unknown port returned");
2336 
2337  return 1;
2338 }
2339 
2341 {
2342  TaskContext *tc = *(luaM_checkudata_bx(L, 1, TaskContext));
2343  const char *port = luaL_checkstring(L, 2);
2344  tc->ports()->removePort(port);
2345  return 0;
2346 }
2347 
2349 {
2350  const char *name, *desc;
2351  int argc = lua_gettop(L);
2352  TaskContext *tc = *(luaM_checkudata_bx(L, 1, TaskContext));
2353  PropertyBase *pb = *(luaM_checkudata_mt_bx(L, 2, "Property", PropertyBase));
2354 
2355  if(argc > 2) {
2356  name = luaL_checkstring(L, 3);
2357  pb->setName(name);
2358  }
2359 
2360  if(argc > 3) {
2361  desc = luaL_checkstring(L, 4);
2362  pb->setDescription(desc);
2363  }
2364 
2365 
2366  if(!tc->addProperty(*pb))
2367  luaL_error(L, "TaskContext.addProperty: failed to add property %s.",
2368  pb->getName().c_str());
2369 
2370  return 0;
2371 }
2372 
2374 {
2375  const char *name;
2376  PropertyBase *prop;
2377 
2378  TaskContext *tc = *(luaM_checkudata_bx(L, 1, TaskContext));
2379  name = luaL_checkstring(L, 2);
2380 
2381  prop = tc->getProperty(name);
2382 
2383  if(!prop)
2384  luaL_error(L, "%s failed. No such property", __FILE__);
2385 
2386  Property_push(L, prop);
2387  return 1;
2388 }
2389 
2390 
2392 {
2393  TaskContext *tc = *(luaM_checkudata_bx(L, 1, TaskContext));
2394  std::vector<std::string> plist = tc->properties()->list();
2395  push_vect_str(L, plist);
2396  return 1;
2397 }
2398 
2400 {
2401  TaskContext *tc = *(luaM_checkudata_bx(L, 1, TaskContext));
2402  vector<PropertyBase*> props = tc->properties()->getProperties();
2403 
2404  int key = 1;
2405  lua_createtable(L, props.size(), 0);
2406  for(vector<PropertyBase*>::iterator it = props.begin(); it != props.end(); ++it) {
2407  Property_push(L, *it);
2408  lua_rawseti(L, -2, key++);
2409  }
2410 
2411  return 1;
2412 }
2413 
2415 {
2416  const char *name;
2417  PropertyBase *prop;
2418 
2419  TaskContext *tc = *(luaM_checkudata_bx(L, 1, TaskContext));
2420  name = luaL_checkstring(L, 2);
2421 
2422  prop = tc->getProperty(name);
2423 
2424  if(!prop)
2425  luaL_error(L, "%s failed. No such property", __FILE__);
2426 
2427  tc->properties()->remove(prop);
2428  return 0;
2429 }
2430 
2432 {
2433  int argc = lua_gettop(L);
2434  TaskContext *tc = *(luaM_checkudata_bx(L, 1, TaskContext));
2435  AttributeBase *pb = *(luaM_checkudata_mt_bx(L, 2, "Attribute", AttributeBase));
2436 
2437  if(argc > 2) {
2438  const char *name = luaL_checkstring(L, 3);
2439  pb->setName(name);
2440  }
2441 
2442  if(!tc->addAttribute(*pb))
2443  luaL_error(L, "TaskContext.addAttribute: failed to add attribute %s.",
2444  pb->getName().c_str());
2445 
2446  return 0;
2447 }
2448 
2450 {
2451  const char *name;
2452  AttributeBase *prop;
2453 
2454  TaskContext *tc = *(luaM_checkudata_bx(L, 1, TaskContext));
2455  name = luaL_checkstring(L, 2);
2456 
2457  prop = tc->getAttribute(name);
2458 
2459  if(!prop)
2460  luaL_error(L, "%s failed. No such Attribute", __FILE__);
2461 
2462  Attribute_push(L, prop);
2463  return 1;
2464 }
2465 
2466 
2468 {
2469  TaskContext *tc = *(luaM_checkudata_bx(L, 1, TaskContext));
2470  std::vector<std::string> plist = tc->attributes()->getAttributeNames();
2471  push_vect_str(L, plist);
2472  return 1;
2473 }
2474 
2476 {
2477  TaskContext *tc = *(luaM_checkudata_bx(L, 1, TaskContext));
2478  vector<AttributeBase*> props = tc->attributes()->getValues();
2479 
2480  int key = 1;
2481  lua_createtable(L, props.size(), 0);
2482  for(vector<AttributeBase*>::iterator it = props.begin(); it != props.end(); ++it) {
2483  Attribute_push(L, *it);
2484  lua_rawseti(L, -2, key++);
2485  }
2486 
2487  return 1;
2488 }
2489 
2491 {
2492  TaskContext *tc = *(luaM_checkudata_bx(L, 1, TaskContext));
2493  const char *name = luaL_checkstring(L, 2);
2494 
2495  if(!tc->attributes()->hasAttribute(name))
2496  luaL_error(L, "%s failed. No such attribute", __FILE__);
2497 
2498  tc->attributes()->removeAttribute(name);
2499 
2500  return 0;
2501 }
2502 
2504 {
2505  TaskContext *tc = *(luaM_checkudata_bx(L, 1, TaskContext));
2506  std::vector<std::string> oplst = tc->operations()->getNames();
2507  push_vect_str(L, oplst);
2508  return 1;
2509 }
2510 
2511 /* returns restype, arity, table-of-arg-descr */
2513 {
2514  int i=1;
2515  TaskContext *tc = *(luaM_checkudata_bx(L, 1, TaskContext));
2516  const char *op = luaL_checkstring(L, 2);
2517  std::vector<ArgumentDescription> args;
2518 
2519  if(!tc->operations()->hasMember(op))
2520  luaL_error(L, "TaskContext.getOpInfo failed: no such operation");
2521 
2522  lua_pushstring(L, tc->operations()->getDescription(op).c_str()); /* description */
2523  lua_pushstring(L, tc->operations()->getResultType(op).c_str()); /* result type */
2524  lua_pushinteger(L, tc->operations()->getArity(op)); /* arity */
2525 
2526  args = tc->operations()->getArgumentList(op);
2527 
2528  lua_newtable(L);
2529 
2530  for (std::vector<ArgumentDescription>::iterator it = args.begin(); it != args.end(); it++) {
2531  lua_newtable(L);
2532  lua_pushstring(L, "name"); lua_pushstring(L, it->name.c_str()); lua_rawset(L, -3);
2533  lua_pushstring(L, "type"); lua_pushstring(L, it->type.c_str()); lua_rawset(L, -3);
2534  lua_pushstring(L, "desc"); lua_pushstring(L, it->description.c_str()); lua_rawset(L, -3);
2535  lua_rawseti(L, -2, i++);
2536  }
2537 
2538  return 4;
2539 }
2540 
2542 {
2543  TaskContext *tc = *(luaM_checkudata_bx(L, 1, TaskContext));
2544  Service::shared_ptr srv = tc->provides();
2545 
2546  if(srv == 0)
2547  luaL_error(L, "TaskContext.provides: no default service");
2548 
2549  /* forward to Serivce.provides */
2550  luaM_pushobject_mt(L, "Service", Service::shared_ptr)(srv);
2551  lua_replace(L, 1);
2552  return Service_provides(L);
2553 }
2554 
2556 {
2557  TaskContext *tc = *(luaM_checkudata_bx(L, 1, TaskContext));
2558  Service::shared_ptr srv = tc->provides();
2559  push_vect_str(L, srv->getProviderNames());
2560  return 1;
2561 }
2562 
2564 {
2566  TaskContext *tc = *(luaM_checkudata_bx(L, 1, TaskContext));
2567  sr = tc->requires();
2568 
2569  if(!sr)
2570  luaL_error(L, "TaskContext.requires returned NULL");
2571 
2572  ServiceRequester_push(L, sr.get());
2573  lua_replace(L, 1);
2574  return ServiceRequester_requires(L);
2575 }
2576 
2578 {
2579  int ret;
2580  TaskContext *tc = *(luaM_checkudata_bx(L, 1, TaskContext));
2581  TaskContext *peer = *(luaM_checkudata_bx(L, 2, TaskContext));
2582  ret = tc->connectServices(peer);
2583  lua_pushboolean(L, ret);
2584  return 1;
2585 }
2586 
2588 {
2589  TaskContext *tc = *(luaM_checkudata_bx(L, 1, TaskContext));
2590  Service::shared_ptr srv = tc->provides();
2591 
2592  if(srv == 0)
2593  luaL_error(L, "TaskContext.provides: no default service");
2594 
2595  /* forward to Serivce.hasOperation */
2596  luaM_pushobject_mt(L, "Service", Service::shared_ptr)(srv);
2597  lua_replace(L, 1);
2598  return Service_hasOperation(L);
2599 }
2600 
2601 
2603 {
2604  TaskContext *tc = *(luaM_checkudata_bx(L, 1, TaskContext));
2605  Service::shared_ptr srv = tc->provides();
2606 
2607  if(srv == 0)
2608  luaL_error(L, "TaskContext.getOperation: no default service");
2609 
2610  /* forward to Serivce.getOperation */
2611  luaM_pushobject_mt(L, "Service", Service::shared_ptr)(srv);
2612  lua_replace(L, 1);
2613  return Service_getOperation(L);
2614 }
2615 
2616 /*
2617  * SendHandle (required for send)
2618  */
2619 
2621 {
2622  switch (ss) {
2623  case SendSuccess: lua_pushstring(L, "SendSuccess"); break;
2624  case SendNotReady: lua_pushstring(L, "SendNotReady"); break;
2625  case SendFailure: lua_pushstring(L, "SendFailure"); break;
2626  default: lua_pushstring(L, "unkown");
2627  }
2628 }
2629 
2630 static int __SendHandle_collect(lua_State *L, bool block)
2631 {
2632  unsigned int coll_argc;
2633  std::vector<DataSourceBase::shared_ptr> coll_args; /* temporarily store args */
2634  SendStatus ss;
2635  const types::TypeInfo *ti;
2637  DataSourceBase::shared_ptr dsb, *dsbp;
2638 
2639  unsigned int argc = lua_gettop(L);
2640  SendHandleC *shc = luaM_checkudata_mt(L, 1, "SendHandle", SendHandleC);
2641 
2642  /* get orp pointer */
2643  oip = shc->getOrp();
2644  coll_argc = oip->collectArity();
2645 
2646  if(block && (argc == 1)) {
2647  // No args supplied, create them.
2648  for(unsigned int i=1; i<=coll_argc; i++) {
2649  ti = oip->getCollectType(i);
2650  dsb = ti->buildValue();
2651  coll_args.push_back(dsb);
2652  shc->arg(dsb);
2653  }
2654  } else if (argc-1 == coll_argc) {
2655  // args supplied, use them.
2656  if (!shc->ready()) {
2657  for(unsigned int arg=2; arg<=argc; arg++) {
2658  if ((dsbp = luaM_testudata_mt(L, arg, "Variable", DataSourceBase::shared_ptr)) != NULL)
2659  dsb = *dsbp;
2660  else
2661  luaL_error(L, "SendHandle.collect: expected Variable argument at position %d", arg-1);
2662  shc->arg(dsb);
2663  }
2664  }
2665  } else {
2666  if (block) {
2667  luaL_error(L, "SendHandle.collect: wrong number of args. expected either 0 or %d, got %d",
2668  coll_argc, argc-1);
2669  } else {
2670  luaL_error(L, "SendHandle.collectIfDone: wrong number of args. expected %d, got %d",
2671  coll_argc, argc-1);
2672  }
2673  }
2674 
2675  if(block) ss = shc->collect();
2676  else ss = shc->collectIfDone();
2677 
2678  SendStatus_push(L, ss);
2679 
2680  if(ss == SendSuccess) {
2681  for (unsigned int i=0; i<coll_args.size(); i++)
2682  Variable_push_coerce(L, coll_args[i]);
2683 
2684  /* SendStatus + collect args */
2685  return coll_args.size() + 1;
2686 
2687  } else {
2688  /* SendStatus only */
2689  return 1;
2690  }
2691 }
2692 
2693 static int SendHandle_collect(lua_State *L) { return __SendHandle_collect(L, true); }
2694 static int SendHandle_collectIfDone(lua_State *L) { return __SendHandle_collect(L, false); }
2695 
2696 static const struct luaL_Reg SendHandle_f [] = {
2697  { "collect", SendHandle_collect },
2698  { "collectIfDone", SendHandle_collectIfDone },
2699  { NULL, NULL }
2700 };
2701 
2702 static const struct luaL_Reg SendHandle_m [] = {
2703  { "collect", SendHandle_collect },
2704  { "collectIfDone", SendHandle_collectIfDone },
2705  { "__gc", GCMethod<SendHandleC> },
2706  { NULL, NULL }
2707 };
2708 
2709 /* only explicit destruction allowed */
2711 {
2712  TaskContext *tc = *(luaM_checkudata_bx(L, 1, TaskContext));
2713  delete tc;
2714 
2715  /* this prevents calling rtt methods which would cause a crash */
2716  luaL_getmetatable(L, "__dead__");
2717  lua_setmetatable(L, -2);
2718  return 0;
2719 }
2720 
2721 static const struct luaL_Reg TaskContext_f [] = {
2722  { "getName", TaskContext_getName },
2723  { "start", TaskContext_start },
2724  { "stop", TaskContext_stop },
2725  { "configure", TaskContext_configure },
2726  { "activate", TaskContext_activate },
2727  { "cleanup", TaskContext_cleanup },
2728  { "error", TaskContext_error },
2729  { "recover", TaskContext_recover },
2730  { "getState", TaskContext_getState },
2731  { "getPeers", TaskContext_getPeers },
2732  { "addPeer", TaskContext_addPeer },
2733  { "removePeer", TaskContext_removePeer },
2734  { "getPeer", TaskContext_getPeer },
2735  { "getPortNames", TaskContext_getPortNames },
2736  { "addPort", TaskContext_addPort },
2737  { "addEventPort", TaskContext_addEventPort },
2738  { "getPort", TaskContext_getPort },
2739  { "removePort", TaskContext_removePort },
2740  { "addProperty", TaskContext_addProperty },
2741  { "getProperty", TaskContext_getProperty },
2742  { "getProperties", TaskContext_getProperties },
2743  { "getPropertyNames", TaskContext_getPropertyNames },
2744  { "removeProperty", TaskContext_removeProperty },
2745  { "addAttribute", TaskContext_addAttribute },
2746  { "getAttribute", TaskContext_getAttribute },
2747  { "getAttributes", TaskContext_getAttributes },
2748  { "getAttributeNames", TaskContext_getAttributeNames },
2749  { "removeAttribute", TaskContext_removeAttribute },
2750  { "getOps", TaskContext_getOps },
2751  { "getOpInfo", TaskContext_getOperationInfo },
2752  { "getOperationInfo", TaskContext_getOperationInfo },
2753  { "hasOperation", TaskContext_hasOperation },
2754  { "provides", TaskContext_provides },
2755  { "getProviderNames", TaskContext_getProviderNames },
2756  { "connectServices", TaskContext_connectServices },
2757  { "getOperation", TaskContext_getOperation },
2758  { "delete", TaskContext_del },
2759  { NULL, NULL}
2760 };
2761 
2762 static const struct luaL_Reg TaskContext_m [] = {
2763  { "getName", TaskContext_getName },
2764  { "start", TaskContext_start },
2765  { "stop", TaskContext_stop },
2766  { "configure", TaskContext_configure },
2767  { "activate", TaskContext_activate },
2768  { "cleanup", TaskContext_cleanup },
2769  { "error", TaskContext_error },
2770  { "recover", TaskContext_recover },
2771  { "getState", TaskContext_getState },
2772  { "getPeers", TaskContext_getPeers },
2773  { "addPeer", TaskContext_addPeer },
2774  { "connectPeers", TaskContext_connectPeers },
2775  { "removePeer", TaskContext_removePeer },
2776  { "getPeer", TaskContext_getPeer },
2777  { "getPortNames", TaskContext_getPortNames },
2778  { "addPort", TaskContext_addPort },
2779  { "addEventPort", TaskContext_addEventPort },
2780  { "getPort", TaskContext_getPort },
2781  { "removePort", TaskContext_removePort },
2782  { "addProperty", TaskContext_addProperty },
2783  { "getProperty", TaskContext_getProperty },
2784  { "getProperties", TaskContext_getProperties },
2785  { "getPropertyNames", TaskContext_getPropertyNames },
2786  { "addAttribute", TaskContext_addAttribute },
2787  { "getAttribute", TaskContext_getAttribute },
2788  { "getAttributes", TaskContext_getAttributes },
2789  { "getAttributeNames", TaskContext_getAttributeNames },
2790  { "removeAttribute", TaskContext_removeAttribute },
2791  { "removeProperty", TaskContext_removeProperty },
2792  { "getOps", TaskContext_getOps },
2793  { "getOpInfo", TaskContext_getOperationInfo },
2794  { "getOperationInfo", TaskContext_getOperationInfo },
2795  { "hasOperation", TaskContext_hasOperation },
2796  { "provides", TaskContext_provides },
2797  { "getProviderNames", TaskContext_getProviderNames },
2798  { "requires", TaskContext_requires },
2799  { "connectServices", TaskContext_connectServices },
2800  { "getOperation", TaskContext_getOperation },
2801  { "delete", TaskContext_del },
2802  // { "__index", TaskContext_index },
2803  /* we don't GC TaskContexts
2804  * { "__gc", GCMethod<TaskContext> }, */
2805  { NULL, NULL}
2806 };
2807 
2808 /*
2809  * Execution engine hook registration
2810  */
2811 
2812 /* executable IF */
2814 {
2815 protected:
2816  std::string func;
2818  TaskContext *tc; /* remember this to be able to print TC name
2819  in error messages */
2820 public:
2821  EEHook(lua_State *_L, std::string _func) { L = _L; func = _func; tc = __getTC(L); }
2822  bool execute() { return call_func(L, func.c_str(), tc, 1, 1); }
2823 };
2824 
2825 static int EEHook_new(lua_State *L)
2826 {
2827  const char *func;
2828  func = luaL_checkstring(L, 1);
2829  luaM_pushobject(L, EEHook)(L, func);
2830  return 1;
2831 }
2832 
2833 static int EEHook_enable(lua_State *L)
2834 {
2835  EEHook *eeh = luaM_checkudata(L, 1, EEHook);
2836  TaskContext *tc = __getTC(L);
2837  lua_pushboolean(L, tc->engine()->runFunction(eeh));
2838  return 1;
2839 }
2840 
2842 { EEHook *eeh = luaM_checkudata(L, 1, EEHook);
2843  TaskContext *tc = __getTC(L);
2844  lua_pushboolean(L, tc->engine()->removeFunction(eeh));
2845  return 1;
2846 }
2847 
2848 #if 0
2849 static int EEHook_gc(lua_State *L)
2850 {
2851  EEHook_disable(L);
2852  lua_settop(L, 1);
2853  reinterpret_cast<EEHook*>(lua_touserdata(L, 1))->~EEHook();
2854  return 0;
2855 }
2856 #endif
2857 
2858 static const struct luaL_Reg EEHook_f [] = {
2859  { "new", EEHook_new },
2860  { "enable", EEHook_enable },
2861  { "disable", EEHook_disable },
2862 };
2863 
2864 
2865 static const struct luaL_Reg EEHook_m [] = {
2866  { "enable", EEHook_enable },
2867  { "disable", EEHook_disable },
2868  /* { "__gc", EEHook_gc }, */
2869 };
2870 
2871 
2872 /*
2873  * Logger and miscellaneous
2874  */
2875 static const char *const loglevels[] = {
2876  "Never", "Fatal", "Critical", "Error", "Warning", "Info", "Debug", "RealTime", NULL
2877 };
2878 
2880 {
2881  Logger::LogLevel ll = (Logger::LogLevel) luaL_checkoption(L, 1, NULL, loglevels);
2882  log().setLogLevel(ll);
2883  return 0;
2884 }
2885 
2887 {
2888  Logger::LogLevel ll = log().getLogLevel();
2889 
2890  switch(ll) {
2891  case Logger::Never: lua_pushstring(L, "Never"); break;
2892  case Logger::Fatal: lua_pushstring(L, "Fatal"); break;
2893  case Logger::Critical: lua_pushstring(L, "Critical"); break;
2894  case Logger::Error: lua_pushstring(L, "Error"); break;
2895  case Logger::Warning: lua_pushstring(L, "Warning"); break;
2896  case Logger::Info: lua_pushstring(L, "Info"); break;
2897  case Logger::Debug: lua_pushstring(L, "Debug"); break;
2898  case Logger::RealTime: lua_pushstring(L, "RealTime"); break;
2899  default:
2900  lua_pushstring(L, "unknown");
2901  }
2902  return 1;
2903 }
2904 
2905 static int Logger_log(lua_State *L)
2906 {
2907  const char *mes;
2908  for(int i=1; i<=lua_gettop(L); i++) {
2909  mes = luaL_checkstring(L, i);
2910  Logger::log() << mes;
2911  }
2912  Logger::log() << endlog();
2913  return 0;
2914 }
2915 
2916 static int Logger_logl(lua_State *L)
2917 {
2918  const char *mes;
2919  Logger::LogLevel ll = (Logger::LogLevel) luaL_checkoption(L, 1, NULL, loglevels);
2920  for(int i=2; i<=lua_gettop(L); i++) {
2921  mes = luaL_checkstring(L, i);
2922  Logger::log(ll) << mes;
2923  }
2924  Logger::log(ll) << endlog();
2925  return 0;
2926 }
2927 
2928 /* misc stuff */
2929 
2930 static int getTime(lua_State *L)
2931 {
2932  unsigned long nsec, sec;
2933  RTT::os::TimeService::nsecs total_nsec = TimeService::Instance()->getNSecs();
2934  sec = total_nsec / 1000000000;
2935  nsec = total_nsec % 1000000000;
2936  lua_pushinteger(L, sec);
2937  lua_pushinteger(L, nsec);
2938  return 2;
2939 }
2940 
2941 static int rtt_sleep(lua_State *L)
2942 {
2943  TIME_SPEC ts;
2944  ts.tv_sec = luaL_checknumber(L, 1);
2945  ts.tv_nsec = luaL_checknumber(L, 2);
2946  rtos_nanosleep(&ts, NULL);
2947  return 0;
2948 }
2949 
2950 static int getTC(lua_State *L)
2951 {
2952  lua_pushstring(L, "this_TC");
2954  return 1;
2955 }
2956 
2958 {
2959  TaskContext *tc;
2960  getTC(L);
2961  tc = *(luaM_checkudata_bx(L, -1, TaskContext));
2962  lua_pop(L, 1);
2963  return tc;
2964 }
2965 
2966 /* access to the globals repository */
2968 {
2969  GlobalsRepository::shared_ptr gr = GlobalsRepository::Instance();
2970  push_vect_str(L, gr->getAttributeNames() );
2971  return 1;
2972 }
2973 
2975 {
2976  GlobalsRepository::shared_ptr gr = GlobalsRepository::Instance();
2977  push_vect_str(L, gr->properties()->list() );
2978  return 1;
2979 }
2980 
2981 static int globals_get(lua_State *L)
2982 {
2983  const char *name;
2984  base::AttributeBase *ab;
2986 
2987  name = luaL_checkstring(L, 1);
2988  GlobalsRepository::shared_ptr gr = GlobalsRepository::Instance();
2989 
2990  ab = gr->getAttribute(name);
2991 
2992  if (ab)
2994  else {
2995  base::PropertyBase *pb = gr->getProperty(name);
2996  if (pb)
2998  else
2999  lua_pushnil(L);
3000  }
3001 
3002  return 1;
3003 }
3004 
3005 /* global service */
3007 {
3008  luaM_pushobject_mt(L, "Service", Service::shared_ptr)(GlobalService::Instance());
3009  lua_insert(L, 1);
3010  return Service_provides(L);
3011 }
3012 
3013 static int rtt_services(lua_State *L)
3014 {
3015  push_vect_str(L, PluginLoader::Instance()->listServices());
3016  return 1;
3017 }
3018 
3019 static int rtt_typekits(lua_State *L)
3020 {
3021  push_vect_str(L, PluginLoader::Instance()->listTypekits());
3022  return 1;
3023 }
3024 
3025 static int rtt_types(lua_State *L)
3026 {
3027  push_vect_str(L, TypeInfoRepository::Instance()->getTypes());
3028  return 1;
3029 }
3030 
3031 static const struct luaL_Reg rtt_f [] = {
3032  {"getTime", getTime },
3033  {"sleep", rtt_sleep },
3034  {"getTC", getTC },
3035  {"globals_getNames", globals_getNames },
3036  {"globals_getProperties", globals_getProperties },
3037  {"globals_get", globals_get },
3038  {"provides", provides_global },
3039  {"services", rtt_services },
3040  {"typekits", rtt_typekits },
3041  {"types", rtt_types },
3042  {"setLogLevel", Logger_setLogLevel },
3043  {"getLogLevel", Logger_getLogLevel },
3044  {"log", Logger_log },
3045  {"logl", Logger_logl },
3046  {NULL, NULL}
3047 };
3048 
3049 extern "C" int luaopen_rtt(lua_State *L);
3050 
3052 {
3053  lua_newtable(L);
3055 
3056  luaL_newmetatable(L, "__dead__");
3057 
3058  /* register MyObj
3059  * 1. line creates metatable MyObj and registers name in registry
3060  * 2. line duplicates metatable
3061  * 3. line sets metatable[__index]=metatable
3062  * (more precisely: table at -2 [__index] = top_of_stack, pops top of stack)
3063  * 4. line register methods in metatable
3064  * 5. line registers free functions in global mystuff.MyObj table
3065  */
3066  luaL_newmetatable(L, "TaskContext");
3067  lua_pushvalue(L, -1); /* duplicates metatable */
3068  lua_setfield(L, -2, "__index");
3069  luaL_register(L, NULL, TaskContext_m);
3070  luaL_register(L, "rtt.TaskContext", TaskContext_f);
3071 
3072  luaL_newmetatable(L, "Operation");
3073  lua_pushvalue(L, -1);
3074  lua_setfield(L, -2, "__index");
3075  luaL_register(L, NULL, Operation_m);
3076  luaL_register(L, "rtt.Operation", Operation_f);
3077 
3078  luaL_newmetatable(L, "Service");
3079  lua_pushvalue(L, -1);
3080  lua_setfield(L, -2, "__index");
3081  luaL_register(L, NULL, Service_m);
3082  luaL_register(L, "rtt.Service", Service_f);
3083 
3084  luaL_newmetatable(L, "ServiceRequester");
3085  lua_pushvalue(L, -1);
3086  lua_setfield(L, -2, "__index");
3087  luaL_register(L, NULL, ServiceRequester_m);
3088  luaL_register(L, "rtt.ServiceRequester", ServiceRequester_f);
3089 
3090  luaL_newmetatable(L, "SendHandle");
3091  lua_pushvalue(L, -1); /* duplicates metatable */
3092  lua_setfield(L, -2, "__index");
3093  luaL_register(L, NULL, SendHandle_m);
3094  luaL_register(L, "rtt.SendHandle", SendHandle_f);
3095 
3096  luaL_newmetatable(L, "InputPort");
3097  lua_pushvalue(L, -1); /* duplicates metatable */
3098  lua_setfield(L, -2, "__index");
3099  luaL_register(L, NULL, InputPort_m);
3100  luaL_register(L, "rtt.InputPort", InputPort_f);
3101 
3102  luaL_newmetatable(L, "OutputPort");
3103  lua_pushvalue(L, -1); /* duplicates metatable */
3104  lua_setfield(L, -2, "__index");
3105  luaL_register(L, NULL, OutputPort_m);
3106  luaL_register(L, "rtt.OutputPort", OutputPort_f);
3107 
3108  luaL_newmetatable(L, "Variable");
3109  lua_pushvalue(L, -1); /* duplicates metatable */
3110  lua_setfield(L, -2, "__index");
3111  luaL_register(L, NULL, Variable_m);
3112  luaL_register(L, "rtt.Variable", Variable_f);
3113 
3114  luaL_newmetatable(L, "Property");
3115  lua_pushvalue(L, -1); /* duplicates metatable */
3116  lua_setfield(L, -2, "__index");
3117  luaL_register(L, NULL, Property_m);
3118  luaL_register(L, "rtt.Property", Property_f);
3119 
3120  luaL_newmetatable(L, "Attribute");
3121  lua_pushvalue(L, -1); /* duplicates metatable */
3122  lua_setfield(L, -2, "__index");
3123  luaL_register(L, NULL, Attribute_m);
3124  luaL_register(L, "rtt.Attribute", Attribute_f);
3125 
3126  luaL_newmetatable(L, "EEHook");
3127  lua_pushvalue(L, -1); /* duplicates metatable */
3128  lua_setfield(L, -2, "__index");
3129  luaL_register(L, NULL, EEHook_m);
3130  luaL_register(L, "rtt.EEHook", EEHook_f);
3131 
3132  /* misc toplevel functions */
3133  luaL_register(L, "rtt", rtt_f);
3134 
3135  return 1;
3136 }
3137 
3138 /* store the TC to be returned by getTC() in registry */
3140 {
3141  TaskContext **new_tc;
3142  lua_pushstring(L, "this_TC");
3143  new_tc = (TaskContext**) lua_newuserdata(L, sizeof(TaskContext*));
3144  *new_tc = (TaskContext*) tc;
3145  luaL_getmetatable(L, "TaskContext");
3146  lua_setmetatable(L, -2);
3148  return 0;
3149 }
3150 
3151 
3152 /* call a zero arity function with a boolean return value
3153  * used to call various hooks */
3154 bool call_func(lua_State *L, const char *fname, TaskContext *tc,
3155  int require_function, int require_result)
3156 {
3157  bool ret = true;
3158  int num_res = (require_result != 0) ? 1 : 0;
3159  lua_getglobal(L, fname);
3160 
3161  if(lua_isnil(L, -1)) {
3162  lua_pop(L, 1);
3163  if(require_function)
3164  luaL_error(L, "%s: no (required) Lua function %s", tc->getName().c_str(), fname);
3165  else
3166  goto out;
3167  }
3168 
3169  if (lua_pcall(L, 0, num_res, 0) != 0) {
3170  Logger::log(Logger::Error) << "LuaComponent '"<< tc->getName() <<"': error calling function "
3171  << fname << ": " << lua_tostring(L, -1) << endlog();
3172  lua_pop(L, 1);
3173  ret = false;
3174  goto out;
3175  }
3176 
3177  if(require_result) {
3178  if (!lua_isboolean(L, -1)) {
3179  Logger::log(Logger::Error) << "LuaComponent '" << tc->getName() << "': " << fname
3180  << " must return a bool but returned a "
3181  << lua_typename(L, lua_type(L, -1)) << endlog();
3182  lua_pop(L, 1);
3183  ret = false;
3184  goto out;
3185  }
3186  ret = lua_toboolean(L, -1);
3187  lua_pop(L, 1); /* pop result */
3188  }
3189  out:
3190  return ret;
3191 }
bool hasAttribute(const std::string &name) const
LUA_API int() lua_pcall(lua_State *L, int nargs, int nresults, int errfunc)
LUA_API int() lua_getmetatable(lua_State *L, int objindex)
static int TaskContext_removeProperty(lua_State *L)
Definition: rtt.cpp:2414
Variable opBinary s not applicable to args
Definition: rtt.cpp:757
static int Logger_logl(lua_State *L)
Definition: rtt.cpp:2916
base::PortInterface & addPort(const std::string &name, base::PortInterface &port)
LUA_API void() lua_replace(lua_State *L, int idx)
static int globals_getProperties(lua_State *L)
Definition: rtt.cpp:2974
LUA_API const char *() lua_typename(lua_State *L, int tp)
Property< T > & addProperty(const std::string &name, T &attr)
LUA_API void() lua_setfield(lua_State *L, int idx, const char *k)
void remove(base::PropertyBase *p)
base::DataSourceBase::shared_ptr buildReference(void *ptr) const
LUA_API void() lua_pushstring(lua_State *L, const char *s)
static TaskContext * __getTC(lua_State *)
Definition: rtt.cpp:2957
virtual result_t get() const =0
static int Service_doc(lua_State *L)
Definition: rtt.cpp:1681
virtual const types::TypeInfo * getTypeInfo() const =0
static int Variable_resize(lua_State *L)
Definition: rtt.cpp:683
static int Variable_getTypeName(lua_State *L)
Definition: rtt.cpp:676
static void Variable_fromlua(lua_State *L, DataSourceBase::shared_ptr &dsb, int valind)
Definition: rtt.cpp:546
static int Service_getOperationInfo(lua_State *L)
Definition: rtt.cpp:1761
static const struct luaL_Reg OutputPort_m[]
Definition: rtt.cpp:1488
LUA_API void() lua_pushinteger(lua_State *L, lua_Integer n)
LUA_API void() lua_pushvalue(lua_State *L, int idx)
base::DataSourceBase::shared_ptr ret_dsb
Definition: rtt.cpp:1516
static int TaskContext_provides(lua_State *L)
Definition: rtt.cpp:2541
virtual bool isLocal() const
static int TaskContext_removeAttribute(lua_State *L)
Definition: rtt.cpp:2490
#define gen_opmet_bool(name, op)
Definition: rtt.cpp:730
static const struct luaL_Reg SendHandle_f[]
Definition: rtt.cpp:2696
static int Variable_tolua(lua_State *L)
Definition: rtt.cpp:355
RequesterNames getRequesterNames() const
static int ServiceRequester_ready(lua_State *L)
Definition: rtt.cpp:2034
static int TaskContext_removePort(lua_State *L)
Definition: rtt.cpp:2340
LUA_API void *() lua_newuserdata(lua_State *L, size_t sz)
base::PortInterface * getPort(const std::string &name) const
OperationInterfacePart * oip
Definition: rtt.cpp:1503
base::AttributeBase * buildAttribute(std::string name, base::DataSourceBase::shared_ptr source=0) const
static int Operation_send(lua_State *L)
Definition: rtt.cpp:1640
OperationInterfacePart * getOrp()
static const struct luaL_Reg OutputPort_f[]
Definition: rtt.cpp:1478
Service::shared_ptr provides()
static const struct luaL_Reg ServiceRequester_f[]
Definition: rtt.cpp:2083
virtual const types::TypeInfo * getArgumentType(unsigned int arg) const =0
static int Service_getPort(lua_State *L)
Definition: rtt.cpp:1858
virtual bool removeFunction(base::ExecutableInterface *f)
static int rtt_types(lua_State *L)
Definition: rtt.cpp:3025
const std::string & getTypeName() const
NoData
static int Variable_unm(lua_State *L)
Definition: rtt.cpp:697
static int Variable_getType(lua_State *L)
Definition: rtt.cpp:662
static int SendHandle_collect(lua_State *L)
Definition: rtt.cpp:2693
void push_vect_str(lua_State *L, const std::vector< std::string > &v)
Definition: rtt.cpp:145
ConfigurationInterface * attributes()
static int __Operation_send(lua_State *L)
Definition: rtt.cpp:1597
static int TaskContext_removePeer(lua_State *L)
Definition: rtt.cpp:2224
virtual bool ready() const
static int TaskContext_activate(lua_State *L)
Definition: rtt.cpp:2141
static int Operation_info(lua_State *L)
Definition: rtt.cpp:1528
#define lua_tostring(L, i)
Definition: lua-repl.h:281
LUA_API void() lua_pushlightuserdata(lua_State *L, void *p)
#define LUA_ENVIRONINDEX
Definition: lua-repl.h:41
virtual FlowStatus read(DataSourceBase::shared_ptr source, bool copy_old_data=true)
int rtos_nanosleep(const TIME_SPEC *rqtp, TIME_SPEC *rmtp)
PortNames getPortNames() const
static void SendStatus_push(lua_State *L, SendStatus ss)
Definition: rtt.cpp:2620
TaskContext * tc
Definition: rtt.cpp:2818
virtual bool stop()
static int Variable_tolightuserdata(lua_State *L)
Definition: rtt.cpp:399
static int Attribute_getRaw(lua_State *L)
Definition: rtt.cpp:1076
std::string getDescription(const std::string &name) const
static int Port_info(lua_State *L)
Definition: rtt.cpp:1196
static const struct luaL_Reg TaskContext_m[]
Definition: rtt.cpp:2762
LUA_API void() lua_insert(lua_State *L, int idx)
Variable opBinary s not applicable to op
Definition: rtt.cpp:757
virtual void set(param_t t)=0
FlowStatus
virtual bool recover()
static int Service_getPropertyNames(lua_State *L)
Definition: rtt.cpp:1903
LUA_API int() lua_type(lua_State *L, int idx)
__int64 int64_t
static int TaskContext_requires(lua_State *L)
Definition: rtt.cpp:2563
const std::string & getRequestName() const
virtual const types::TypeInfo * getCollectType(unsigned int arg) const =0
OperationInterface * operations()
boost::shared_ptr< ServiceRequester > shared_ptr
const std::string & getName() const
static int Attribute_index(lua_State *L)
Definition: rtt.cpp:1145
static int Port_disconnect(lua_State *L)
Definition: rtt.cpp:1271
virtual bool connectPeers(TaskContext *peer)
LUA_API int() lua_rawequal(lua_State *L, int idx1, int idx2)
LUA_NUMBER lua_Number
Definition: lua-repl.h:101
#define luaM_pushobject(L, T)
Definition: rtt.cpp:73
virtual void disconnect()
LUA_API int() lua_toboolean(lua_State *L, int idx)
static int TaskContext_addPort(lua_State *L)
Definition: rtt.cpp:2260
static int __Variable_tolua(lua_State *L, DataSourceBase::shared_ptr dsb)
Definition: rtt.cpp:264
static const struct luaL_Reg rtt_f[]
Definition: rtt.cpp:3031
static int Variable_newindex(lua_State *L)
Definition: rtt.cpp:786
static const struct luaL_Reg EEHook_f[]
Definition: rtt.cpp:2858
static int Property_getRaw(lua_State *L)
Definition: rtt.cpp:928
void list(Names &names) const
virtual std::string getName() const =0
int set_context_tc(TaskContext *tc, lua_State *L)
Definition: rtt.cpp:3139
static int Logger_log(lua_State *L)
Definition: rtt.cpp:2905
static const struct luaL_Reg Operation_m[]
Definition: rtt.cpp:1662
virtual DataSourceBase::shared_ptr getDataSource() const =0
NewData
int OperationGC(lua_State *L)
Definition: rtt.cpp:1520
virtual bool activate()
boost::shared_ptr< OperatorRepository > shared_ptr
static int Attribute_del(lua_State *L)
Definition: rtt.cpp:1128
bool hasMember(const std::string &name) const
virtual std::string description() const =0
virtual WriteStatus write(DataSourceBase::shared_ptr source)
LUA_API void() lua_rawset(lua_State *L, int idx)
virtual bool configure()
static int rtt_services(lua_State *L)
Definition: rtt.cpp:3013
std::string func
Definition: rtt.cpp:2816
base::InputPortInterface & addEventPort(const std::string &name, base::InputPortInterface &port, SlotFunction callback=SlotFunction())
void removeAttribute(const std::string &name)
static int Service_getPortNames(lua_State *L)
Definition: rtt.cpp:1723
LUA_API void() lua_rawget(lua_State *L, int idx)
static const char *const loglevels[]
Definition: rtt.cpp:2875
static int Variable_index(lua_State *L)
Definition: rtt.cpp:770
static int TaskContext_recover(lua_State *L)
Definition: rtt.cpp:2164
static int TaskContext_getAttribute(lua_State *L)
Definition: rtt.cpp:2449
virtual TaskContext * getPeer(const std::string &peer_name) const
SendStatus
static DataSourceBase::shared_ptr lookup_member(lua_State *L, DataSourceBase::shared_ptr parent, const char *mem)
Definition: rtt.cpp:411
virtual void error()
static int TaskContext_getProperties(lua_State *L)
Definition: rtt.cpp:2399
boost::shared_ptr< Service > shared_ptr
virtual unsigned int arity() const =0
static const TypeInfo * ti_lookup(lua_State *L, const char *name)
Definition: rtt.cpp:165
static int Variable_getMemberRaw(lua_State *L)
Definition: rtt.cpp:486
static int Variable_create_ival(lua_State *L, int typeind, int valind)
Definition: rtt.cpp:632
SendHandleC & arg(base::DataSourceBase::shared_ptr a)
LUA_API void() lua_pushnumber(lua_State *L, lua_Number n)
ServiceRequester::shared_ptr requires()
virtual bool evaluate() const =0
LUA_API int() lua_setmetatable(lua_State *L, int objindex)
static int TaskContext_getOperation(lua_State *L)
Definition: rtt.cpp:2602
static int Variable_update(lua_State *L)
Definition: rtt.cpp:500
static int ServiceRequester_disconnect(lua_State *L)
Definition: rtt.cpp:2044
virtual void disconnect()=0
static int TaskContext_addAttribute(lua_State *L)
Definition: rtt.cpp:2431
static int TaskContext_getState(lua_State *L)
Definition: rtt.cpp:2172
static int TaskContext_getAttributes(lua_State *L)
Definition: rtt.cpp:2475
int luaopen_rtt(lua_State *L)
Definition: rtt.cpp:3051
static int Variable_toString(lua_State *L)
Definition: rtt.cpp:655
void * luaL_testudata(lua_State *L, int ud, const char *tname)
Definition: rtt.cpp:120
base::DataSourceBase::shared_ptr call_dsb
Definition: rtt.cpp:1515
static int globals_getNames(lua_State *L)
Definition: rtt.cpp:2967
static int Property_newindex(lua_State *L)
Definition: rtt.cpp:1014
EEHook(lua_State *_L, std::string _func)
Definition: rtt.cpp:2821
DataFlowInterface * ports()
static int SendHandle_collectIfDone(lua_State *L)
Definition: rtt.cpp:2694
static int ServiceRequester_requires(lua_State *L)
Definition: rtt.cpp:2052
#define gen_opmet(name, op)
Definition: rtt.cpp:708
static int Service_provides(lua_State *L)
Definition: rtt.cpp:1730
AttributeObjects const & getValues() const
static int Variable_isbasic(lua_State *L)
Definition: rtt.cpp:252
static const struct luaL_Reg InputPort_f[]
Definition: rtt.cpp:1387
LUA_API void() lua_createtable(lua_State *L, int narr, int nrec)
static int OutputPort_write(lua_State *L)
Definition: rtt.cpp:1439
virtual PeerList getPeerList() const
static int EEHook_enable(lua_State *L)
Definition: rtt.cpp:2833
static const struct luaL_Reg Service_m[]
Definition: rtt.cpp:1990
static int TaskContext_getPort(lua_State *L)
Definition: rtt.cpp:2314
static int TaskContext_getPeer(lua_State *L)
Definition: rtt.cpp:2234
struct lua_State lua_State
Definition: lua-repl.h:54
std::vector< internal::Reference * > args
Definition: rtt.cpp:1514
void removePort(const std::string &name)
LUA_API void() lua_pushboolean(lua_State *L, int b)
#define lua_pop(L, n)
Definition: lua-repl.h:256
#define luaM_checkudata_bx(L, pos, T)
Definition: rtt.cpp:89
static int TaskContext_start(lua_State *L)
Definition: rtt.cpp:2117
static int Variable_getMemberNames(lua_State *L)
Definition: rtt.cpp:392
#define lua_getglobal(L, s)
Definition: lua-repl.h:279
SendFailure
static const struct luaL_Reg InputPort_m[]
Definition: rtt.cpp:1397
const std::string & getDescription() const
OldData
int getArity(const std::string &name) const
static int Property_set(lua_State *L)
Definition: rtt.cpp:935
ServiceRequester::shared_ptr requires()
virtual std::string getType() const =0
base::AttributeBase * getAttribute(const std::string &name) const
#define luaM_checkudata_mt(L, pos, MT, T)
Definition: rtt.cpp:78
static int TaskContext_addProperty(lua_State *L)
Definition: rtt.cpp:2348
OperationCallerC * occ
Definition: rtt.cpp:1504
#define LUA_TSTRING
Definition: lua-repl.h:82
static const struct luaL_Reg TaskContext_f[]
Definition: rtt.cpp:2721
static const struct luaL_Reg Variable_f[]
Definition: rtt.cpp:832
#define CONVERT_TO_NUMBER(CTGT)
Definition: rtt.cpp:536
#define luaM_testudata_mt(L, pos, MT, T)
Definition: rtt.cpp:82
static int Variable_getTypes(lua_State *L)
Definition: rtt.cpp:386
static const struct luaL_Reg Attribute_f[]
Definition: rtt.cpp:1169
static int Logger_getLogLevel(lua_State *L)
Definition: rtt.cpp:2886
static const struct luaL_Reg EEHook_m[]
Definition: rtt.cpp:2865
const std::string & getDescription() const
static int Logger_setLogLevel(lua_State *L)
Definition: rtt.cpp:2879
static int Service_getAttributes(lua_State *L)
Definition: rtt.cpp:1954
#define lua_newtable(L)
Definition: lua-repl.h:258
static int getTC(lua_State *L)
Definition: rtt.cpp:2950
static int TaskContext_error(lua_State *L)
Definition: rtt.cpp:2157
std::vector< base::DataSourceBase::shared_ptr > dsb_store
Definition: rtt.cpp:1513
base::PropertyBase * buildProperty(const std::string &name, const std::string &desc, base::DataSourceBase::shared_ptr source=0) const
static const struct luaL_Reg Property_f[]
Definition: rtt.cpp:1021
bool addAttribute(const std::string &name, T &attr)
#define luaM_checkudata(L, pos, T)
Definition: rtt.cpp:77
Properties & getProperties()
static int Service_getOperation(lua_State *L)
Definition: rtt.cpp:1790
TypeInfoRepository::shared_ptr Types()
LUA_API void() lua_rawseti(lua_State *L, int idx, int n)
static int ServiceRequester_getRequesterNames(lua_State *L)
Definition: rtt.cpp:2026
static int Variable_new(lua_State *L)
Definition: rtt.cpp:642
static int Property_info(lua_State *L)
Definition: rtt.cpp:955
void setName(const std::string &name)
#define LUA_TBOOLEAN
Definition: lua-repl.h:79
LUA_API void() lua_getfield(lua_State *L, int idx, const char *k)
virtual std::string resultType() const =0
static const struct luaL_Reg ServiceRequester_m[]
Definition: rtt.cpp:2092
bool call_func(lua_State *L, const char *fname, TaskContext *tc, int require_function, int require_result)
Definition: rtt.cpp:3154
static int OutputPort_del(lua_State *L)
Definition: rtt.cpp:1467
SendSuccess
static void Variable_coerce(lua_State *L, DataSourceBase::shared_ptr dsb)
Definition: rtt.cpp:366
#define LUA_TNUMBER
Definition: lua-repl.h:81
static int TaskContext_connectServices(lua_State *L)
Definition: rtt.cpp:2577
static int TaskContext_getPropertyNames(lua_State *L)
Definition: rtt.cpp:2391
#define luaM_pushobject_mt(L, MT, T)
Definition: rtt.cpp:74
static int provides_global(lua_State *L)
Definition: rtt.cpp:3006
static int EEHook_new(lua_State *L)
Definition: rtt.cpp:2825
virtual std::vector< ArgumentDescription > getArgumentList() const =0
static int TaskContext_del(lua_State *L)
Definition: rtt.cpp:2710
static const struct luaL_Reg Service_f[]
Definition: rtt.cpp:1970
static int getTime(lua_State *L)
Definition: rtt.cpp:2930
#define LUA_TTABLE
Definition: lua-repl.h:83
static int Service_getName(lua_State *L)
Definition: rtt.cpp:1674
static const struct luaL_Reg Property_m[]
Definition: rtt.cpp:1031
#define luaM_checkudata_mt_bx(L, pos, MT, T)
Definition: rtt.cpp:90
PropertyBag * properties()
static int InputPort_read(lua_State *L)
Definition: rtt.cpp:1337
LUA_API int() lua_gettop(lua_State *L)
static int Service_getProperty(lua_State *L)
Definition: rtt.cpp:1886
std::vector< std::string > getNames() const
static int Service_getOperationNames(lua_State *L)
Definition: rtt.cpp:1705
static int Variable_getTypeIdName(lua_State *L)
Definition: rtt.cpp:669
virtual unsigned int collectArity() const =0
static int Service_getProviderNames(lua_State *L)
Definition: rtt.cpp:1698
static int TaskContext_getOperationInfo(lua_State *L)
Definition: rtt.cpp:2512
static int Property_del(lua_State *L)
Definition: rtt.cpp:980
int GCMethod(lua_State *L)
Definition: rtt.cpp:108
Descriptions getArgumentList(const std::string &name) const
SendNotReady
base::PropertyBase * getProperty(const std::string &name) const
base::OutputPortInterface * outputPort(std::string const &name) const
static const struct luaL_Reg Variable_m[]
Definition: rtt.cpp:861
static int Port_connect(lua_State *L)
Definition: rtt.cpp:1227
static int TaskContext_getProperty(lua_State *L)
Definition: rtt.cpp:2373
AttributeNames getAttributeNames() const
base::DataSourceBase::shared_ptr buildValue() const
static int Service_getAttribute(lua_State *L)
Definition: rtt.cpp:1928
virtual bool connectTo(PortInterface *other, ConnPolicy const &policy)=0
static int TaskContext_connectPeers(lua_State *L)
Definition: rtt.cpp:2213
static int Attribute_newindex(lua_State *L)
Definition: rtt.cpp:1162
static int rtt_sleep(lua_State *L)
Definition: rtt.cpp:2941
const std::string & getName() const
static int TaskContext_addEventPort(lua_State *L)
Definition: rtt.cpp:2290
static void Variable_push_coerce(lua_State *L, DataSourceBase::shared_ptr dsb)
Definition: rtt.cpp:377
LUA_API void *() lua_touserdata(lua_State *L, int idx)
static int __SendHandle_collect(lua_State *L, bool block)
Definition: rtt.cpp:2630
static bool __typenames_cmp(lua_State *L, const types::TypeInfo *ti1, const char *type2)
Definition: rtt.cpp:215
#define lua_isnil(L, n)
Definition: lua-repl.h:269
static int EEHook_disable(lua_State *L)
Definition: rtt.cpp:2841
lua_State * L
Definition: rtt.cpp:2817
bool is_void
Definition: rtt.cpp:1506
static int rtt_typekits(lua_State *L)
Definition: rtt.cpp:3019
static int TaskContext_getProviderNames(lua_State *L)
Definition: rtt.cpp:2555
static int Service_getProperties(lua_State *L)
Definition: rtt.cpp:1912
LUA_API void() lua_pushnil(lua_State *L)
LUA_API void() lua_pushlstring(lua_State *L, const char *s, size_t l)
static int InputPort_del(lua_State *L)
Definition: rtt.cpp:1376
LUA_API const char *() lua_tolstring(lua_State *L, int idx, size_t *len)
int VariableGC(lua_State *L)
Definition: rtt.cpp:823
static int Property_get(lua_State *L)
Definition: rtt.cpp:921
static const struct luaL_Reg Operation_f[]
Definition: rtt.cpp:1654
boost::intrusive_ptr< DataSourceBase > shared_ptr
virtual bool connectServices(TaskContext *peer)
static int Variable_create(lua_State *L)
Definition: rtt.cpp:519
const std::string & getName() const
static int TaskContext_configure(lua_State *L)
Definition: rtt.cpp:2133
PortInterface & doc(const std::string &desc)
bool execute()
Definition: rtt.cpp:2822
static int Property_index(lua_State *L)
Definition: rtt.cpp:997
static int Service_getAttributeNames(lua_State *L)
Definition: rtt.cpp:1945
static int Attribute_info(lua_State *L)
Definition: rtt.cpp:1103
virtual bool connected() const =0
static const struct luaL_Reg SendHandle_m[]
Definition: rtt.cpp:2702
static int Attribute_set(lua_State *L)
Definition: rtt.cpp:1083
static int globals_get(lua_State *L)
Definition: rtt.cpp:2981
virtual bool runFunction(base::ExecutableInterface *f)
#define gen_push_bxptr(name, MT, T)
Definition: rtt.cpp:97
#define LUA_REGISTRYINDEX
Definition: lua-repl.h:40
static int Variable_getMember(lua_State *L)
Definition: rtt.cpp:472
LUA_API void() lua_settop(lua_State *L, int idx)
Definition: rtt.cpp:2813
struct timespec TIME_SPEC
static int Operation_call(lua_State *L)
Definition: rtt.cpp:1627
static int Service_hasOperation(lua_State *L)
Definition: rtt.cpp:1713
static bool __Variable_isbasic(lua_State *L, DataSourceBase::shared_ptr &dsb)
Definition: rtt.cpp:230
LUA_API lua_Number() lua_tonumber(lua_State *L, int idx)
static int TaskContext_hasOperation(lua_State *L)
Definition: rtt.cpp:2587
#define LUA_TLIGHTUSERDATA
Definition: lua-repl.h:80
static int TaskContext_cleanup(lua_State *L)
Definition: rtt.cpp:2149
std::string getResultType(const std::string &name) const
base::InputPortInterface * inputPort(std::string const &name) const
static int __Operation_call(lua_State *L)
Definition: rtt.cpp:1553
#define LUA_TNIL
Definition: lua-repl.h:78
const ExecutionEngine * engine() const
static int TaskContext_getPeers(lua_State *L)
Definition: rtt.cpp:2193
static bool Variable_is_a(lua_State *L, const types::TypeInfo *ti1, const char *type)
Definition: rtt.cpp:223
static int TaskContext_getOps(lua_State *L)
Definition: rtt.cpp:2503
virtual bool start()
virtual bool addPeer(TaskContext *peer, std::string alias="")
unsigned int arity
Definition: rtt.cpp:1505
virtual bool cleanup()
virtual const std::string & getName() const
static int TaskContext_getAttributeNames(lua_State *L)
Definition: rtt.cpp:2467
#define lua_isboolean(L, n)
Definition: lua-repl.h:270
virtual DataSourceBase::shared_ptr getDataSource() const =0
void setName(std::string const &new_name)
static int TaskContext_stop(lua_State *L)
Definition: rtt.cpp:2125
static int Attribute_get(lua_State *L)
Definition: rtt.cpp:1069
static void cache_clear(lua_State *L, DataSourceBase *varptr)
Definition: rtt.cpp:465
static int TaskContext_addPeer(lua_State *L)
Definition: rtt.cpp:2202
void setDescription(const std::string &desc)
static int TaskContext_getPortNames(lua_State *L)
Definition: rtt.cpp:2252
static const struct luaL_Reg Attribute_m[]
Definition: rtt.cpp:1179


ocl
Author(s): OCL Development Team
autogenerated on Mon Mar 23 2020 04:47:19