Free Electron
LuaComponent.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_LuaComponent_h__
8 #define __lua_LuaComponent_h__
9 
10 namespace fe
11 {
12 namespace ext
13 {
14 
15 typedef LuaObject<sp<Component>, LuaContext::e_component> t_lua_component;
16 
17 /** Component to Lua binding object */
18 class LuaComponentObject : public t_lua_component
19 {
20  public:
22  LuaContext *pContext, const sp<Component> &data)
23  : t_lua_component(pContext, data) {}
24 virtual ~LuaComponentObject(void){}
25 static int index(lua_State *pLuaState);
26 static int newindex(lua_State *pLuaState);
27 static int call(lua_State *pLuaState);
28 static int name(lua_State *pLuaState);
29 static int setName(lua_State *pLuaState);
30 static int adjoin(lua_State *pLuaState);
31 
32  void setMethod(const String &name, lua_CFunction f)
33  {
34  m_methods[name] = f;
35  }
36  bool pushMethod(String &name)
37  {
38  std::map<String, lua_CFunction>::iterator i_methods =
39  m_methods.find(name);
40  if(i_methods == m_methods.end()) { return false; }
41  lua_State *pLuaState = context()->state();
42  lua_pushstring(pLuaState, name.c_str());
43  lua_pushcclosure(context()->state(), i_methods->second, 1);
44  return true;
45  }
46  void populateMethods(void);
47  template<class T>
48  bool is(void)
49  {
50  return data().is<T>();
51  }
52 static void pushComponent(lua_State *pLuaState, sp<Component> spC)
53  {
54  lua_getglobal(pLuaState, "fe_context");
55  LuaContext *pContext = (LuaContext *)(lua_touserdata(pLuaState,-1));
56  lua_pop(pLuaState, 1);
57 
58  void *pBlock = lua_newuserdata(pLuaState,
59  sizeof(LuaComponentObject));
60 
62  new(pBlock)LuaComponentObject(pContext,spC);
63 
64  pC->populateMethods();
65  }
66  private:
67  std::map<String, lua_CFunction> m_methods;
68 };
69 
70 typedef LuaType<sp<Component>, LuaComponentObject> t_lua_component_t;
71 class LuaComponentType : public t_lua_component_t
72 {
73  public:
74  LuaComponentType(LuaContext *pContext,const String &mtn);
75 virtual ~LuaComponentType(void){}
76 virtual void pushNewMetaTable(void);
77 };
78 
79 template<class T>
80 T *to(lua_State *pLuaState, int index, int tid)
81 {
82  void *pV = lua_touserdata(pLuaState, index);
83  if(!pV)
84  {
85  String s;
86  s.sPrintf("type mismatch on arg %d : did you use '.' instead of ':' for method?", index);
87  LuaContext::error(pLuaState, s.c_str());
88  assert(0);
89  }
90 
91  LuaBaseObject *pB = (LuaBaseObject *)pV;
92  if(pB->type() != pB->context()->lookup(tid))
93  {
94  String s;
95  s.sPrintf("type mismatch on argument %d", index);
96  assert(0);
97  LuaContext::error(pLuaState, s.c_str());
98  }
99 
100  return static_cast<T *>(pB);
101 }
102 
103 template<class Obj, class T>
104 sp<T> counted(lua_State *pLuaState, int index, int tid)
105 {
106  return to<Obj>(pLuaState,index,tid)->data();
107 }
108 
109 template<class T>
110 sp<T> component(lua_State *pLuaState, int index)
111 {
112  LuaComponentObject *pC =
113  to<LuaComponentObject>(pLuaState, index, LuaContext::e_component);
114 
115  sp<T> spT(pC->data());
116 
117  if(!spT.isValid())
118  {
119  String s;
120  s.sPrintf("component type mismatch on argument %d", index);
121  LuaContext::error(pLuaState, s.c_str());
122  }
123 
124  return spT;
125 }
126 
127 template<class T>
128 LuaComponentObject *validate(lua_State *pLuaState, int index)
129 {
130  sp<T> spT = component<T>(pLuaState, index);
131  LuaComponentObject *pC = to<LuaComponentObject>(pLuaState, index,
132  LuaContext::e_component);
133 
134  return pC;
135 }
136 
137 Record record(lua_State *pLuaState, int index);
138 
139 sp<RecordGroup> recordgroup(lua_State *pLuaState, int index);
140 
141 } /* namespace ext */
142 } /* namespace fe */
143 
144 #endif /* __lua_LuaComponent_h__ */
const FESTRING_I8 * c_str(void) const
Return the contents of the 8-bit buffer cast as signed bytes.
Definition: String.h:352
kernel
Definition: namespace.dox:3
void populateMethods(void)
This is where you directly bind interfaces to lua.
Definition: LuaComponent.cc:793
String & sPrintf(const char *fmt,...)
Populate the string in the manner of sprintf().
Definition: String.cc:529
Automatically reference-counted string container.
Definition: String.h:128
Reference to an instance of a Layout.
Definition: RecordSB.h:35
Component to Lua binding object.
Definition: LuaComponent.h:18
Intrusive Smart Pointer.
Definition: src/core/ptr.h:53