Free Electron
LuaType.h
Go to the documentation of this file.
1 /* Copyright (C) 2003-2021 Free Electron Organization
2  Any use of this software requires a license. If a valid license
3  was not distributed with this file, visit freeelectron.org. */
4 
5 /** @file */
6 
7 #ifndef __lua_LuaType_h__
8 #define __lua_LuaType_h__
9 
10 namespace fe
11 {
12 namespace ext
13 {
14 
15 class LuaBaseType : public Counted
16 {
17  public:
18  LuaBaseType(void) {}
19 virtual ~LuaBaseType(void) {}
20 
21 virtual void putMetaTable(void) = 0;
22 virtual void pushMetaTable(void) = 0;
23 
24  lua_State *state(void) { return m_pContext->state(); }
25  LuaContext *context(void) { return m_pContext; }
26 
27  void setMethod(const String &name, lua_CFunction f)
28  {
29  m_methods[name] = f;
30  }
31  bool pushMethod(String &name)
32  {
33  std::map<String, lua_CFunction>::iterator i_methods =
34  m_methods.find(name);
35  if(i_methods == m_methods.end()) { return false; }
36  lua_pushcfunction(state(), i_methods->second);
37  return true;
38  }
39 
40  //sp<BaseType> baseType(void) { return m_spBT; }
41 
42 const String& name(void) const { return m_mtname; }
43 const String verboseName(void) const;
44 
45  protected:
46  //sp<BaseType> m_spBT;
47  LuaContext *m_pContext;
48  String m_mtname;
49  std::map<String, lua_CFunction> m_methods;
50 };
51 
52 inline const String LuaBaseType::verboseName(void) const
53 {
54  return "LuaBaseType " + name();
55 }
56 
57 template<class T, class Obj>
58 class LuaType : public LuaBaseType
59 {
60  public:
61  LuaType(LuaContext *pContext, const String &mtname)
62  {
63  m_mtname = mtname;
64  m_pContext = pContext;
65  //m_spBT = pContext->typeMaster()->
66  // lookupType(TypeInfo(typeid(T)));
67  }
68 virtual ~LuaType(void) {}
69 virtual void pushMetaTable(void)
70  {
71  lua_getglobal(m_pContext->state(), m_mtname.c_str());
72  }
73 virtual void putMetaTable(void)
74  {
75  lua_rawgeti(m_pContext->state(),
76  LUA_REGISTRYINDEX, LUA_RIDX_GLOBALS);
77  lua_pushstring(m_pContext->state(), m_mtname.c_str());
78  pushNewMetaTable();
79  lua_settable(m_pContext->state(), -3);
80  lua_pop(m_pContext->state(), 1);
81  }
82 virtual void pushNewMetaTable(void)
83  {
84  lua_State *pLuaState = m_pContext->state();
85 
86 // lua_newtable(pLuaState);
87  luaL_newmetatable(pLuaState,m_mtname.c_str());
88 
89  lua_pushstring(pLuaState, "__gc");
90  lua_pushcfunction(pLuaState, Obj::destroy);
91  lua_settable(pLuaState, -3);
92 
93 
94  lua_pushstring(pLuaState, "__index");
95  lua_pushcfunction(pLuaState, Obj::index);
96  lua_settable(pLuaState, -3);
97 
98  }
99  private:
100 };
101 
102 } /* namespace ext */
103 } /* namespace fe */
104 
105 #endif /* __lua_LuaType_h__ */
106 
kernel
Definition: namespace.dox:3