forked from stepmania/stepmania
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathEnumHelper.h
201 lines (186 loc) · 6.18 KB
/
EnumHelper.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
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
#ifndef ENUM_HELPER_H
#define ENUM_HELPER_H
#include "LuaReference.h"
#include "RageUtil.h"
#include "RageString.hpp"
#include <memory>
extern "C"
{
#include "../extern/lua-5.1/src/lua.h"
}
/** @brief A general foreach loop for enumerators. */
#define FOREACH_ENUM( e, var ) for( e var=(e)0; var<NUM_##e; enum_add<e>( var, +1 ) )
int CheckEnum(lua_State *L,
LuaReference &table,
int iPos,
int iInvalid,
const char *szType,
bool bAllowInvalid,
bool bAllowAnything= false);
template<typename T>
struct EnumTraits
{
static LuaReference StringToEnum;
static LuaReference EnumToString;
static T Invalid;
static const char *szName;
};
template<typename T> LuaReference EnumTraits<T>::StringToEnum;
template<typename T> LuaReference EnumTraits<T>::EnumToString;
/** @brief Lua helpers for Enumerators. */
namespace Enum
{
template<typename T>
static T Check( lua_State *L, int iPos, bool bAllowInvalid = false, bool bAllowAnything= false )
{
return (T) CheckEnum(L,
EnumTraits<T>::StringToEnum,
iPos,
EnumTraits<T>::Invalid,
EnumTraits<T>::szName,
bAllowInvalid,
bAllowAnything);
}
template<typename T>
static void Push( lua_State *L, T iVal )
{
/* Enum_Invalid values are nil in Lua. */
if( iVal == EnumTraits<T>::Invalid )
{
lua_pushnil( L );
return;
}
/* Look up the string value. */
EnumTraits<T>::EnumToString.PushSelf( L );
lua_rawgeti( L, -1, iVal + 1 );
lua_remove( L, -2 );
}
void SetMetatable( lua_State *L, LuaReference &EnumTable, LuaReference &EnumIndexTable, const char *szName );
};
std::string const EnumToString( int iVal, int iMax, const char **szNameArray, std::unique_ptr<std::string> *pNameCache ); // XToString helper
#define XToString(X) \
std::string const X##ToString(X x); \
COMPILE_ASSERT( NUM_##X == ARRAYLEN(X##Names) ); \
std::string const X##ToString( X x ) \
{ \
static std::unique_ptr<std::string> as_##X##Name[NUM_##X+2]; \
return EnumToString( x, NUM_##X, X##Names, as_##X##Name ); \
} \
namespace StringConversion { template<> std::string ToString<X>( const X &value ) { return X##ToString(value); } }
#define XToLocalizedString(X) \
std::string const X##ToLocalizedString(X x); \
std::string const X##ToLocalizedString( X x ) \
{ \
static std::unique_ptr<LocalizedString> g_##X##Name[NUM_##X]; \
if( g_##X##Name[0].get() == nullptr ) { \
for( unsigned i = 0; i < NUM_##X; ++i ) \
{ \
std::unique_ptr<LocalizedString> ap( new LocalizedString(#X, X##ToString((X)i)) ); \
g_##X##Name[i] = std::move(ap); \
} \
} \
return g_##X##Name[x]->GetValue(); \
}
#define StringToX(X) \
X StringTo##X(const std::string&); \
X StringTo##X( const std::string& s ) \
{ \
Rage::ci_ascii_string target{(s).c_str()}; \
for( unsigned i = 0; i < ARRAYLEN(X##Names); ++i ) \
{ \
if ( target == (X##Names[i]) ) \
return (X)i; \
} \
return X##_Invalid; \
} \
namespace StringConversion \
{ \
template<> bool FromString<X>( const std::string &sValue, X &out ) \
{ \
out = StringTo##X(sValue); \
return out != X##_Invalid; \
} \
}
// currently unused
#define LuaDeclareType(X)
#define LuaXType(X) \
template struct EnumTraits<X>; \
static void Lua##X(lua_State* L) \
{ \
/* Create the EnumToString table: { "UnlockEntry_ArcadePoints", "UnlockEntry_DancePoints" } */ \
lua_newtable( L ); \
FOREACH_ENUM( X, i ) \
{ \
std::string s = X##ToString( i ); \
lua_pushstring( L, ((#X "_")+s).c_str() ); \
lua_rawseti( L, -2, i+1 ); /* 1-based */ \
} \
EnumTraits<X>::EnumToString.SetFromStack( L ); \
EnumTraits<X>::EnumToString.PushSelf( L ); \
lua_setglobal( L, #X ); \
/* Create the StringToEnum table: { "UnlockEntry_ArcadePoints" = 0, "UnlockEntry_DancePoints" = 1 } */ \
lua_newtable( L ); \
FOREACH_ENUM( X, i ) \
{ \
std::string s = X##ToString( i ); \
lua_pushstring( L, ((#X "_")+s).c_str() ); \
lua_pushnumber( L, i ); /* 0-based */ \
lua_rawset( L, -3 ); \
/* Compatibility with old, case-insensitive values */ \
s = Rage::make_lower(s); \
lua_pushstring( L, (s).c_str() ); \
lua_pushnumber( L, i ); /* 0-based */ \
lua_rawset( L, -3 ); \
/* Compatibility with old, raw values */ \
lua_pushnumber( L, i ); \
lua_rawseti( L, -2, i ); \
} \
EnumTraits<X>::StringToEnum.SetFromStack( L ); \
EnumTraits<X>::StringToEnum.PushSelf( L ); \
Enum::SetMetatable( L, EnumTraits<X>::EnumToString, EnumTraits<X>::StringToEnum, #X ); \
} \
REGISTER_WITH_LUA_FUNCTION( Lua##X ); \
template<> X EnumTraits<X>::Invalid = X##_Invalid; \
template<> const char *EnumTraits<X>::szName = #X; \
namespace LuaHelpers \
{ \
template<> bool FromStack<X>( lua_State *L, X &Object, int iOffset ) \
{ \
Object = Enum::Check<X>( L, iOffset, true ); \
return Object != EnumTraits<X>::Invalid; \
} \
} \
namespace LuaHelpers \
{ \
template<> void Push<X>( lua_State *L, const X &Object ) \
{ \
Enum::Push<X>( L, Object ); \
} \
}
#endif
/**
* @file
* @author Chris Danford, Glenn Maynard (c) 2004-2006
* @section LICENSE
* All rights reserved.
*
* Permission is hereby granted, free of charge, to any person obtaining a
* copy of this software and associated documentation files (the
* "Software"), to deal in the Software without restriction, including
* without limitation the rights to use, copy, modify, merge, publish,
* distribute, and/or sell copies of the Software, and to permit persons to
* whom the Software is furnished to do so, provided that the above
* copyright notice(s) and this permission notice appear in all copies of
* the Software and that both the above copyright notice(s) and this
* permission notice appear in supporting documentation.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
* OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT OF
* THIRD PARTY RIGHTS. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR HOLDERS
* INCLUDED IN THIS NOTICE BE LIABLE FOR ANY CLAIM, OR ANY SPECIAL INDIRECT
* OR CONSEQUENTIAL DAMAGES, OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS
* OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR
* OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
* PERFORMANCE OF THIS SOFTWARE.
*/