LPCGame
A Simple 2d Game Engine
 All Classes Namespaces Functions Variables Enumerations Enumerator Pages
luacudataparam.h
1 #ifndef LUAUDATAPARAM_H
2 #define LUAUDATAPARAM_H
3 
4 #include <functional>
5 #include <string>
6 #include <memory>
7 #include <lua.hpp>
8 #include "luacparam.h"
9 #include "luacvector2f.h"
10 #include "luacrectf.h"
11 #include "luacphysics.h"
12 #include "luacentity.h"
13 #include "luacdebug.h"
14 #include "luaccolor.h"
15 #include "luactimer.h"
16 #include "luacinput.h"
17 #include "luacstate.h"
18 #include "luaccamera.h"
19 #include "luacmath.h"
20 #include "luacimage.h"
21 #include "luacwindow.h"
22 #include "luacanimatedimage.h"
23 #include "luactext.h"
24 
26 
30 namespace LuaC {
35  template<class T>
36  class LuaUdataParam : public LuaParam {
37  public:
42  LuaUdataParam(T* obj) : mObj(obj) {}
47  void Push(lua_State *l) override {
48  mPusher(l, mObj);
49  }
55  void Push(lua_State *l, std::string name) override {
56  Push(l);
57  lua_setglobal(l, name.c_str());
58  }
64  static T* Retrieve(lua_State *l) {
65  return mRetriever(l, -1);
66  }
67 
68  private:
69  const T *mObj;
70  const static std::function<void(lua_State*, const T*)> mPusher;
71  const static std::function<T*(lua_State*, int)> mRetriever;
72  };
73  //Is there a way i can enable doing this? I'd need to somehow deduce the lib
74  //from the type T, i guess i could use auto and decltype for returns and then do
75  //typedefs based on the lib name. hmm
76  /*
77  template<class T>
78  const std::function<void(lua_State*, const T*)>
79  LuaUdataParam<T>::mPusher = &T::Push;
80  template<class T>
81  const std::function<T*(lua_State*, int)>
82  LuaUdataParam<T>::mRetriever = &T::Check;
83  */
84  //Pusher function specializations & shorthand typedefs
85  //Animated Image
86  typedef LuaUdataParam<std::shared_ptr<AnimatedImage>> AnimImgParam;
87  template<>
88  const std::function<void(lua_State*, const std::shared_ptr<AnimatedImage>*)>
89  AnimImgParam::mPusher = &AnimatedImageLib::Push;
90  template<>
91  const std::function<std::shared_ptr<AnimatedImage>*(lua_State*, int)>
92  AnimImgParam::mRetriever = &AnimatedImageLib::Check;
93 
94  //Camera
95  typedef LuaUdataParam<std::weak_ptr<Camera>> CameraParam;
96  template<>
97  const std::function<void(lua_State*, const std::weak_ptr<Camera>*)>
98  CameraParam::mPusher = &CameraLib::Push;
99  template<>
100  const std::function<std::weak_ptr<Camera>*(lua_State*, int)>
101  CameraParam::mRetriever = &CameraLib::Check;
102 
103  //Color
104  typedef LuaUdataParam<Color> ColorParam;
105  template<>
106  const std::function<void(lua_State*, const Color*)>
107  ColorParam::mPusher = &ColorLib::Push;
108  template<>
109  const std::function<Color*(lua_State*, int)>
110  ColorParam::mRetriever = &ColorLib::Check;
111 
112  //Entity
113  typedef LuaUdataParam<std::shared_ptr<Entity>> EntityParam;
114  template<>
115  const std::function<void(lua_State*, const std::shared_ptr<Entity>*)>
116  EntityParam::mPusher = &EntityLib::Push;
117  template<>
118  const std::function<std::shared_ptr<Entity>*(lua_State*, int)>
119  EntityParam::mRetriever = &EntityLib::Check;
120 
121  //Image
122  typedef LuaUdataParam<std::shared_ptr<Image>> ImageParam;
123  template<>
124  const std::function<void(lua_State*, const std::shared_ptr<Image>*)>
125  ImageParam::mPusher = &ImageLib::Push;
126  template<>
127  const std::function<std::shared_ptr<Image>*(lua_State*, int)>
128  ImageParam::mRetriever = &ImageLib::Check;
129 
130  //Physics
131  typedef LuaUdataParam<std::weak_ptr<Physics>> PhysicsParam;
132  template<>
133  const std::function<void(lua_State*, const std::weak_ptr<Physics>*)>
134  PhysicsParam::mPusher = &PhysicsLib::Push;
135  template<>
136  const std::function<std::weak_ptr<Physics>*(lua_State*, int)>
137  PhysicsParam::mRetriever = &PhysicsLib::Check;
138 
139  //Rectf
140  typedef LuaUdataParam<Rectf> RectfParam;
141  template<>
142  const std::function<void(lua_State*, const Rectf*)>
143  RectfParam::mPusher = &RectfLib::Push;
144  template<>
145  const std::function<Rectf*(lua_State*, int)>
146  RectfParam::mRetriever = &RectfLib::Check;
147 
148  //Text
149  typedef LuaUdataParam<std::shared_ptr<Text>> TextParam;
150  template<>
151  const std::function<void(lua_State*, const std::shared_ptr<Text>*)>
152  TextParam::mPusher = &TextLib::Push;
153  template<>
154  const std::function<std::shared_ptr<Text>*(lua_State*, int)>
155  TextParam::mRetriever = &TextLib::Check;
156 
157  //Timer
158  typedef LuaUdataParam<Timer> TimerParam;
159  template<>
160  const std::function<void(lua_State*, const Timer*)>
161  TimerParam::mPusher = &TimerLib::Push;
162  template<>
163  const std::function<Timer*(lua_State*, int)>
164  TimerParam::mRetriever = &TimerLib::Check;
165 
166  //Vector2f
167  typedef LuaUdataParam<Vector2f> Vector2fParam;
168  template<>
169  const std::function<void(lua_State*, const Vector2f*)>
170  Vector2fParam::mPusher = &Vector2fLib::Push;
171  template<>
172  const std::function<Vector2f*(lua_State*, int)>
173  Vector2fParam::mRetriever = &Vector2fLib::Check;
174 }
175 #endif