File: LuaDefs.h

package info (click to toggle)
spring 88.0%2Bdfsg1-1.1
  • links: PTS, VCS
  • area: main
  • in suites: wheezy
  • size: 41,524 kB
  • sloc: cpp: 343,114; ansic: 38,414; python: 12,257; java: 12,203; awk: 5,748; sh: 1,204; xml: 997; perl: 405; objc: 192; makefile: 181; php: 134; sed: 2
file content (95 lines) | stat: -rwxr-xr-x 2,258 bytes parent folder | download
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
/* This file is part of the Spring engine (GPL v2 or later), see LICENSE.html */

#ifndef LUA_DEFS_H
#define LUA_DEFS_H

#include <map>
#include <string>


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


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


struct DataElement {
	public:
		DataElement() : type(ERROR_TYPE), offset(0), func(NULL) {}
		DataElement(DataType t)
		: type(t), offset(0), func(NULL) {}
		DataElement(DataType t, int o)
		: type(t), offset(o), func(NULL) {}
		DataElement(DataType t, int o, AccessFunc f)
		: type(t), offset(o), func(f) {}
	public:
		DataType type;
		int offset;
		AccessFunc func;
};


typedef std::map<std::string, DataElement> ParamMap;


/* This is unused and does not compile on GCC 4.3 -- tvo 9/9/2007
   "error: explicit template specialization cannot have a storage class"

template<typename T> static DataType GetDataType(T) {
	const bool valid_type = false;
	assert(valid_type);
	return ERROR_TYPE;
}
template<> static DataType GetDataType(int)    { return INT_TYPE; }
template<> static DataType GetDataType(bool)   { return BOOL_TYPE; }
template<> static DataType GetDataType(float)  { return FLOAT_TYPE; }
template<> static DataType GetDataType(string) { return STRING_TYPE; }
*/


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

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

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

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

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

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

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




#if (LUA_VERSION_NUM < 500)
#  define LUA_OPEN_LIB(L, lib) lib(L)
#else
#  define LUA_OPEN_LIB(L, lib) \
     lua_pushcfunction((L), lib); \
     lua_pcall((L), 0, 0, 0);
#endif




#endif // LUA_DEFS_H