Tools and libraries to glue C/C++ APIs to high-level languages
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 

49 lines
2.2 KiB

//
// Mono.VisualC.Interop.CppField.cs: Represents a field in a native C++ object
//
// Author:
// Alexander Corrado (alexander.corrado@gmail.com)
//
// Copyright (C) 2010 Alexander Corrado
//
using System;
using System.Runtime.InteropServices;
namespace Mono.VisualC.Interop {
public class CppField<T> {
private int fieldOffset;
public CppField (int fieldOffset)
{
this.fieldOffset = fieldOffset;
}
public T this [CppInstancePtr ip] {
get {
Type retType = typeof (T);
object retVal;
if (retType.Equals (typeof (Byte)))
retVal = Marshal.ReadByte (ip.Native, fieldOffset);
else if (retType.Equals (typeof (Int16)))
retVal = Marshal.ReadInt16 (ip.Native, fieldOffset);
else if (retType.Equals (typeof (Int32)))
retVal = Marshal.ReadInt32 (ip.Native, fieldOffset);
else throw new NotImplementedException ("Cannot read C++ fields of type " + retType.Name);
return (T)retVal;
}
set {
Type setType = typeof (T);
object setVal = value;
if (setType.Equals (typeof (Byte)))
Marshal.WriteByte (ip.Native, fieldOffset, (byte)setVal);
else if (setType.Equals (typeof (Int16)))
Marshal.WriteInt16 (ip.Native, fieldOffset, (Int16)setVal);
else if (setType.Equals (typeof (Int32)))
Marshal.WriteInt32 (ip.Native, fieldOffset, (Int32)setVal);
else throw new NotImplementedException ("Cannot write C++ fields of type " + setType.Name);
}
}
}
}