UGDK
src/ugdk/script/languages/lua/basegear.h
Go to the documentation of this file.
00001 
00002 #ifndef UGDK_SCRIPT_LUA_BASEGEAR_H_
00003 #define UGDK_SCRIPT_LUA_BASEGEAR_H_
00004 
00005 #include <vector>
00006 #include <ugdk/portable/tr1.h>
00007 #include FROM_TR1(tuple)
00008 
00009 #include <ugdk/script/languages/lua/header.h>
00010 #include <ugdk/script/languages/lua/state.h>
00011 
00012 #include <ugdk/util/uncopyable.h>
00013 
00014 namespace ugdk {
00015 namespace script {
00016 namespace lua {
00017 
00018 class BaseGear {
00019 
00020   public:
00021 
00022     class InternalSafeCall {
00023 
00024       public:
00025 
00026         ~InternalSafeCall() { gear_->settop(old_top_); }
00027 
00028         template <class A1>
00029         InternalSafeCall& Arg(A1 a1) {
00030             gear_->pushprimitive<A1>(a1);
00031             ++arg_num_;
00032             return *this;
00033         }
00034 
00035         template <class R>
00036         R GetResult(const R default_value) {
00037             R result = default_value;
00038             if (gear_.TracedCall(arg_num_,1) == Constant::OK())
00039                 result = gear_->toprimitive<R>(-1);
00040             arg_num_ = 0;
00041             return result;
00042         }
00043 
00044         bool NoResult() {
00045             return gear_.TracedCall(arg_num_,0) == Constant::OK();
00046         }
00047 
00048       private:
00049 
00050         friend class BaseGear;
00051 
00052         InternalSafeCall(BaseGear& gear, lua_CFunction func) :
00053             gear_(gear),
00054             old_top_(gear->gettop()),
00055             arg_num_(1) {
00056             gear_->pushcfunction(func);
00057             gear_->pushudata(&gear_);
00058         }
00059 
00060         InternalSafeCall& operator=(const InternalSafeCall& rhs);
00061 
00062         BaseGear&    gear_;
00063         int          old_top_;
00064         unsigned int arg_num_;
00065 
00066     };
00067 
00068     InternalSafeCall SafeCall(lua_CFunction func) {
00069         return InternalSafeCall(*this, func);
00070     }
00071 
00072     State* operator->() { return &L_; }
00073 
00075 
00080     const Constant TracedCall (int nargs, int nres);
00081 
00082     const Constant Report (const Constant& c);
00083 
00084   protected:
00085 
00086     friend class InternalSafeCall;
00087 
00088     State L_;
00089 
00090     BaseGear(lua_State* L) :
00091         L_(L) {}
00092 
00093     ~BaseGear() {}
00094 
00095     State& L() { return L_; }
00096 };
00097 
00098 #define GETARGPTR(L,i,T,name) \
00099     T* name = static_cast<T*>((L).touserdata(i))
00100 
00101 #define GETARG(L,i,T,name) \
00102     T& name = *(static_cast<T*>((L).touserdata(i)))
00103 
00104 } /* namespace lua */
00105 } /* namespace script */
00106 } /* namespace ugdk */
00107 
00108 #endif /* UGDK_SCRIPT_LUA_BASEGEAR_H_ */
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines