File: LuaDefs.h

package info (click to toggle)
spring 106.0%2Bdfsg-1
  • links: PTS, VCS
  • area: main
  • in suites: bookworm
  • size: 55,260 kB
  • sloc: cpp: 543,946; ansic: 44,800; python: 12,575; java: 12,201; awk: 5,889; sh: 1,796; asm: 1,546; xml: 655; perl: 405; php: 211; objc: 194; makefile: 76; sed: 2
file content (133 lines) | stat: -rw-r--r-- 4,696 bytes parent folder | download | duplicates (3)
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
/* This file is part of the Spring engine (GPL v2 or later), see LICENSE.html */

#ifndef LUA_DEFS_H
#define LUA_DEFS_H

#include <cassert>
#include <string>

#include "System/UnorderedMap.hpp"

enum DataType {
	INT_TYPE,
	BOOL_TYPE,
	FLOAT_TYPE,
	STRING_TYPE,
	FUNCTION_TYPE,
	READONLY_TYPE,
	ERROR_TYPE
};

struct lua_State;
typedef int (*AccessFunc)(lua_State* L, const void* data);


struct DataElement {
	public:
		DataElement()
		: type(ERROR_TYPE), offset(0), func(NULL), deprecated(true) {}
		DataElement(DataType t)
		: type(t), offset(0), func(NULL), deprecated(false) {}
		DataElement(DataType t, int o)
		: type(t), offset(o), func(NULL), deprecated(false) {}
		DataElement(DataType t, int o, AccessFunc f, bool d=false)
		: type(t), offset(o), func(f), deprecated(d) {}
	public:
		DataType type;
		int offset;
		AccessFunc func;
		bool deprecated;
};


typedef spring::unordered_map<std::string, DataElement> ParamMap;


namespace {
	template<typename T> DataType GetDataType(T) {
		const bool valid_type = false;
		assert(valid_type);
		return ERROR_TYPE;
	}
	template<> inline DataType GetDataType(unsigned)           { return INT_TYPE; }
	template<> inline DataType GetDataType(int)                { return INT_TYPE; }
	template<> inline DataType GetDataType(bool)               { return BOOL_TYPE; }
	template<> inline DataType GetDataType(float)              { return FLOAT_TYPE; }
	template<> inline DataType GetDataType(std::string)        { return STRING_TYPE; }
}

#define ADDRESS(name) ((const char *)&name)

// Requires a "start" address, use ADDRESS()
#define ADD_INT(lua, cpp) \
	paramMap[lua] = DataElement(GetDataType(cpp), ADDRESS(cpp) - start)

#define ADD_BOOL(lua, cpp) \
	paramMap[lua] = DataElement(GetDataType(cpp), ADDRESS(cpp) - start)

#define ADD_FLOAT(lua, cpp) \
	paramMap[lua] = DataElement(GetDataType(cpp), ADDRESS(cpp) - start)

#define ADD_STRING(lua, cpp) \
	paramMap[lua] = DataElement(GetDataType(cpp), ADDRESS(cpp) - start)

#define ADD_FUNCTION(lua, cpp, func) \
	paramMap[lua] = DataElement(FUNCTION_TYPE, ADDRESS(cpp) - start, func)

#define ADD_DEPRECATED_FUNCTION(lua, cpp, func) \
	paramMap[lua] = DataElement(FUNCTION_TYPE, ADDRESS(cpp) - start, func, true)

// keys added through this macro will generate
// (non-fatal) ERROR_TYPE warnings if indexed
#define ADD_DEPRECATED_LUADEF_KEY(lua) \
	paramMap[lua] = DataElement();




#define DECL_LOAD_HANDLER(HandlerType, handlerInst)         \
	bool HandlerType::LoadHandler() {                       \
		std::lock_guard<spring::mutex> lk(m_singleton);     \
                                                            \
		if (handlerInst != nullptr)                         \
			return (handlerInst->IsValid());                \
		if (!HandlerType::CanLoadHandler())                 \
			return false;                                   \
                                                            \
		if (!(handlerInst = new HandlerType())->IsValid())  \
			return false;                                   \
                                                            \
		return (handlerInst->CollectGarbage(true), true);   \
	}

#define DECL_LOAD_SPLIT_HANDLER(HandlerType, handlerInst)             \
	bool HandlerType::LoadHandler(bool onlySynced) {                  \
		std::lock_guard<spring::mutex> lk(m_singleton);               \
                                                                      \
		if (handlerInst != nullptr)                                   \
			return (handlerInst->IsValid());                          \
		if (!HandlerType::CanLoadHandler())                           \
			return false;                                             \
                                                                      \
		if (!(handlerInst = new HandlerType(onlySynced))->IsValid())  \
			return false;                                             \
                                                                      \
		return (handlerInst->CollectGarbage(true), true);             \
	}

#define DECL_FREE_HANDLER(HandlerType, handlerInst)      \
	bool HandlerType::FreeHandler() {                    \
		std::lock_guard<spring::mutex> lk(m_singleton);  \
                                                         \
		if (handlerInst == nullptr)                      \
			return false;                                \
                                                         \
		HandlerType* inst = handlerInst;                 \
		handlerInst = nullptr;                           \
		inst->KillLua(true);                             \
		delete inst;                                     \
		return true;                                     \
	}


#endif // LUA_DEFS_H