Documenti di Didattica
Documenti di Professioni
Documenti di Cultura
Randy Gaul
Reflection
Binding Lua to C++ is a chore
Functions and objects
Reflection automates a lot of boilerplate
Much of this talk is about reflection
Prerequisites
Here are some must-haves in order to accomplish
Lua binding like I have set up:
MetaData type info object
Lookup by string
RefVariant
Size
Goal:
Call any function with call operator
Essential for generic code (Lua bindings)
VariantFunction – Actual Fn Ptr
Actual function pointer can be large
Up to like 20+ bytes on some compilers
Why so large?
Multiple inheritance + vtables
Use a strange union to contain pointer
Let compiler deal with actual max bounded size
VariantFunction – Actual Fn Ptr
Strange Union
VariantFunction – Actual Fn Ptr
Stranger contents
Thanks to Treb Connell for providing this
Not sure where he got it, maybe Boost
VariantFunction – Call Helper
Clever trick for calling the function pointer
Store pointer to helper
Helper is template
Template parameter is function pointer to call
This preserves the function pointer type
Acts as layer of indirection
Will likely get compiled away
VariantFunction – Call Helper
Templated call helper
Upon construction of VariantFunction
Store pointer to template instance of this
VariantFunction – Call Helper
Pass in:
Binding
This pointer as RefVariant
Return RefVariant
Copy return value to here
Return value likely to require heap memory
Array of RefVariants
Allocate these on stack, alloca
To pass in arguments
MultiFnPtr union
VariantFunction – Call Helper
You will need many call helpers
One per argument count
Template overloads, le sigh
Both member function and global functions
Const/non const member funcs
Void/non void
If you’re fancy you can try MS demo compiler
Variadic templates, no overloads required
It’s a demo compiler though…
VariantFunction – Call Helper
One last cavaet
Callingmember function pointer
ptr->*Call( params );
Macro to help:
Used like:
VariantFunction – Call Helper
Example of CALL_PTR in action:
VariantFunction - Checkpoint
Signature
Argument types
This pointer type
Return value
VariantFunction
Actual function pointer
Call helper to store function as template param
RefVariant
FunctionSignature
VariantFunction
Lua Primer – A quick and dirty one
Lua is embedded
Bunch of C code
Communicate with Lua by a stack
Defaulted to max 20 element size
Not thread-safe!
Lua Primer – A quick and dirty one
Lua Stack
Push things onto stack
Pop things from stack
Retrieving upvalue:
Any questions?
MetaData Magic
You may have noticed some black boxes…
What do?
Everyone that uses reflection now needs to know Lua
Bad.
Generic To/From Lua
Require generic to/from default
Canbe overridden if user does know Lua!
Good example is custom vector binding
Lua is cool
Call a Lua Function from C++
Will require more overloads…
Call a Lua Function from C++
Can study code after
Checkpoint
VariantFunction
Call any C++ function within C++
Lua binding
Call C++ from Lua
Pass objects to/from Lua