Skip to content

Instantly share code, notes, and snippets.

%include "typemaps.i"
%define %TypeOutParam(TYPE)
%apply TYPE& OUTPUT { TYPE& };
%enddef
%TypeOutParam(bool)
%TypeOutParam(int)
%TypeOutParam(float)
...
SWIG_THIS Matrix& Identity(); // SWIG_THIS doesn't actually exist.
Matrix4 wrappedMatrix = new Matrix4();
return wrappedMatrix.Identity();
class Matrix
{
Matrix();
Matrix& Identity();
}
%typemap(out) const Vector3& %{ $result = new Vector3(*$1); %}
#if !defined(SWIG)
#define MYNEW ...
#define MYDELETE ...
#endif
public static T CastTo<t>(YourBaseRTTIObject self) where T : YourBaseRTTIObject
{
if (self == null)
return null;
// Check if the object is actually the type we're trying to cast to.
if (self.IsKindOf<t>())
{
Type type = typeof(T);
private static Dictionary<Type, Func<IntPtr, bool, object>> constructorCache =
new Dictionary<Type, Func<IntPtr, bool, object>>();
private static Func<IntPtr, bool, object> CreateConstructorDelegate<T>()
{
// We need to first grab the reflection information for the more derrived type we're casting to.
// SWIG has protected constructors we'll be able to call that take 2 parameters, the native pointer
// to the type and whether or not SWIG owns the memory
ConstructorInfo ctor = typeof(T).GetConstructor(
BindingFlags.Instance | BindingFlags.NonPublic, null,
%typemap(cscode) YourBaseRTTIObject
%{
internal YourBaseRTTIObject m_castedSource;
public bool IsKindOf<T>()
{
// Use your C++ RTTI system to test if this wrapped C# object's
// native object is a 'typeof(T).Name'
}
%}
void main()
{
gl_FragColor = gl_FrontFacing ? vec4(0,0,1,1) : vec4(1,0,0,1);
}