LPCGame
A Simple 2d Game Engine
 All Classes Namespaces Functions Variables Enumerations Enumerator Pages
luacudata.h
1 #ifndef LUACUDATA_H
2 #define LUACUDATA_H
3 
4 #include <string>
5 #include <lua.hpp>
6 #include "luacscript.h"
7 
9 
13 namespace LuaC {
18  template<class T>
19  class UdataLib {
20  public:
27  static T* Allocate(lua_State *l){
28  void *block = lua_newuserdata(l, sizeof(T));
29  T *obj = new(block) T();
30  AddMetaTable(l, -1);
31  return obj;
32  }
38  static void Push(lua_State *l, const T *obj){
39  T *o = Allocate(l);
40  *o = *obj;
41  }
48  static void Copy(lua_State *from, int idx, lua_State *too){
49  T *obj = Check(from, idx);
50  Push(too, obj);
51  }
58  static T* Check(lua_State *l, int i){
59  return (T*)luaL_checkudata(l, i, mMetaTable.c_str());
60  }
61 
62  private:
68  static void AddMetaTable(lua_State *l, int i){
69  LuaScriptLib::Add(l, i, mMetaTable);
70  }
71 
72  private:
74  static const std::string mMetaTable;
75  };
76  template<class T>
77  const std::string UdataLib<T>::mMetaTable = "";
78 }
79 
80 #endif