-
Notifications
You must be signed in to change notification settings - Fork 0
/
shared.h
135 lines (112 loc) · 4.09 KB
/
shared.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
#ifndef SHARED_H
#define SHARED_H
#include <QOpenGLFunctions_4_4_Core>
#include <QFile>
#include <exception>
#include <memory>
#include <type_traits>
#include "state.h"
#include "library.h"
typedef QOpenGLFunctions_4_4_Core GL_t;
class GameWindow;
enum {
KILOBYTE = 1024 * 1,
MEGABYTE = 1024 * KILOBYTE,
GIGABYTE = 1024 * MEGABYTE
};
namespace LuaApi {
typedef Lua::lua_exception gl_error;
typedef Lua::lua_exception al_error;
template <typename T>
class RefCounted {
std::shared_ptr<T> m_data;
public:
RefCounted() {}
RefCounted(RefCounted const&) =default;
RefCounted(RefCounted &&) =default;
RefCounted& operator= (RefCounted const&) =default;
RefCounted& operator= (RefCounted &&) =default;
template <typename ... Args>
void Init(Args&& ... args)
{
m_data = std::make_shared<T>(std::forward<Args>(args)...);
}
bool IsValid() const {
return m_data != nullptr;
}
void SoftRelease() {
m_data.reset();
}
T& Get() const {
if(m_data == nullptr)
throw std::runtime_error(std::string("Usage of invalid entity: ") + MetatableDescriptor<T>::name());
return *m_data;
}
T* operator -> () const {
if(m_data == nullptr)
throw std::runtime_error(std::string("Usage of invalid entity: ") + MetatableDescriptor<T>::name());
return m_data.get();
}
T& operator * () const {
return Get();
}
//Problem is around here.
//I'm unable to correctly pass lua_State*, most likely.
int RunFunction(lua_State* state, Lua::ClassMemberFunctor<T> fnc) {
return fnc(Get(),state);
}
int IsValidFnc(lua_State* state, Lua::ClassMemberFunctor<T> fnc) {
if(!IsValid())
{
lua_pushboolean(state,0);
return 1;
}
return fnc(Get(),state);
}
};
}
template <typename T> struct MetatableDescriptor<LuaApi::RefCounted<T>> {
static char const* name() { return MetatableDescriptor<T>::name(); }
static char const* luaname() { return MetatableDescriptor<T>::luaname(); }
static char const* constructor() { return MetatableDescriptor<T>::constructor(); }
typedef decltype(MetatableDescriptor<T>::construct(std::declval<T*>())) valid_subclass;
struct bad {};
static bool construct(typename std::conditional<std::is_default_constructible<T>::value, LuaApi::RefCounted<T>, bad>::type *location) {
if(!Lua::DefaultConstructor(location))
return false;
location->Init();
return location->IsValid();
}
static bool construct(typename std::conditional<!std::is_default_constructible<T>::value, LuaApi::RefCounted<T>, bad>::type*) {
return false;
}
/*static bool construct(LuaApi::RefCounted<T>* location) {
if(!Lua::DefaultConstructor(location))
return false;
location->Init();
return location->IsValid();
}*/
static void metatable(Lua::member_function_storage<LuaApi::RefCounted<T>>& mt) {
Lua::member_function_storage<T> fnc;
MetatableDescriptor<T>::metatable(fnc);
for(auto it = fnc.begin(); it != fnc.end(); ++it)
{
std::string const& name = it->first;
Lua::ClassMemberFunctor<T> const& fnc = it->second;
mt[name] = Lua::ToFnc([=](LuaApi::RefCounted<T>& m, lua_State* s) -> int {
return m.RunFunction(s,fnc);
});
}
auto it = fnc.find("IsValid");
if(it != fnc.end())
{
Lua::ClassMemberFunctor<T> const& fnc = it->second;
mt["IsValid"] = Lua::ToFnc([=](LuaApi::RefCounted<T>& m, lua_State* s) -> int {
return m.IsValidFnc(s,fnc);
});
}
else
mt["IsValid"] = Lua::Transform(&LuaApi::RefCounted<T>::IsValid);
}
};
#endif // SHARED_H