Created
February 12, 2020 23:17
-
-
Save willox/04d63591eebc5c154e019ce36416ece1 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
struct UserData | |
{ | |
void* data; | |
unsigned char type; | |
}; | |
template <class T> | |
struct UserData_Value : UserData | |
{ | |
T value; | |
}; | |
// Creates a new UserData with your own data embedded within it | |
template <class T> | |
inline void PushUserType_Value( const T& val, int iType ) | |
{ | |
// The UserData allocated by CLuaInterface is only guaranteed to have a data alignment of 8 | |
static_assert( std::alignment_of<UserData_Value<T>>::value <= 8, | |
"PushUserType_Value given type with unsupported alignment requirement" ); | |
// Don't give this function objects that can't be trivially destructed | |
// You could ignore this limitation if you implement object destruction in `__gc` | |
static_assert( std::is_trivially_destructible<UserData_Value<T>>::value, | |
"PushUserType_Value given type that is not trivially destructible" ); | |
auto* ud = static_cast<UserData_Value<T>*>( NewUserdata( sizeof( UserData_Value<T> ) ) ); | |
ud->data = new( &ud->value ) T ( val ); | |
ud->type = iType; | |
// Set the metatable | |
if ( PushMetaTable( iType ) ) SetMetaTable( -2 ); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment