UGDK  0.5.0
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros
basegear.h
Go to the documentation of this file.
1 
2 #ifndef UGDK_SCRIPT_LUA_BASEGEAR_H_
3 #define UGDK_SCRIPT_LUA_BASEGEAR_H_
4 
5 #include <vector>
6 
7 #include <tuple>
8 
11 
12 #include <ugdk/util/uncopyable.h>
13 
14 namespace ugdk {
15 namespace script {
16 namespace lua {
17 
18 class BaseGear {
19 
20  public:
21 
23 
24  public:
25 
26  ~InternalSafeCall() { gear_->settop(old_top_); }
27 
28  template <class A1>
30  gear_->pushprimitive<A1>(a1);
31  ++arg_num_;
32  return *this;
33  }
34 
35  template <class R>
36  R GetResult(const R default_value) {
37  R result = default_value;
38  if (gear_.TracedCall(arg_num_,1) == Constant::OK())
39  result = gear_->toprimitive<R>(-1);
40  arg_num_ = 0;
41  return result;
42  }
43 
44  bool NoResult() {
45  return gear_.TracedCall(arg_num_,0) == Constant::OK();
46  }
47 
48  private:
49 
50  friend class BaseGear;
51 
53  gear_(gear),
54  old_top_(gear->gettop()),
55  arg_num_(1) {
56  gear_->pushcfunction(func);
57  gear_->pushudata(&gear_);
58  }
59 
60  InternalSafeCall& operator=(const InternalSafeCall& rhs);
61 
62  BaseGear& gear_;
63  int old_top_;
64  unsigned int arg_num_;
65 
66  };
67 
69  return InternalSafeCall(*this, func);
70  }
71 
72  State* operator->() { return &L_; }
73 
75 
80  const Constant TracedCall (int nargs, int nres);
81 
82  const Constant Report (const Constant& c);
83 
84  protected:
85 
86  friend class InternalSafeCall;
87 
89 
91  L_(L) {}
92 
93  ~BaseGear() {}
94 
95  State& L() { return L_; }
96 };
97 
98 #define GETARGPTR(L,i,T,name) \
99  T* name = static_cast<T*>((L).touserdata(i))
100 
101 #define GETARG(L,i,T,name) \
102  T& name = *(static_cast<T*>((L).touserdata(i)))
103 
104 } /* namespace lua */
105 } /* namespace script */
106 } /* namespace ugdk */
107 
108 #endif /* UGDK_SCRIPT_LUA_BASEGEAR_H_ */
void pushcfunction(lua_CFunction func, int n=0)
Definition: state.h:44
InternalSafeCall SafeCall(lua_CFunction func)
Definition: basegear.h:68
~BaseGear()
Definition: basegear.h:93
State & L()
Definition: basegear.h:95
InternalSafeCall & Arg(A1 a1)
Definition: basegear.h:29
const Constant Report(const Constant &c)
void settop(int index)
Definition: state.h:35
void pushudata(UData ptr)
Definition: state.h:42
Definition: animation.h:11
T toprimitive(int n) const
Definition: state.h:85
void pushprimitive(T value)
Definition: state.h:50
BaseGear(lua_State *L)
Definition: basegear.h:90
Represents a constant value from the Lua library.
Definition: header.h:25
bool NoResult()
Definition: basegear.h:44
State * operator->()
Definition: basegear.h:72
struct lua_State lua_State
Definition: defs.h:8
~InternalSafeCall()
Definition: basegear.h:26
int(* lua_CFunction)(lua_State *)
Definition: defs.h:9
const Constant TracedCall(int nargs, int nres)
Makes a traced call to a function.
Definition: basegear.h:18
friend class InternalSafeCall
Definition: basegear.h:86
State L_
Definition: basegear.h:88
R GetResult(const R default_value)
Definition: basegear.h:36
Definition: state.h:18