2012-11-03 14:46:06 +00:00
|
|
|
// GtkSharp.Generation.Parameters.cs - The Parameters Generation Class.
|
|
|
|
//
|
|
|
|
// Author: Mike Kestner <mkestner@speakeasy.net>
|
|
|
|
//
|
|
|
|
// Copyright (c) 2001-2003 Mike Kestner
|
|
|
|
// Copyright (c) 2004 Novell, Inc.
|
|
|
|
//
|
|
|
|
// This program is free software; you can redistribute it and/or
|
|
|
|
// modify it under the terms of version 2 of the GNU General Public
|
|
|
|
// License as published by the Free Software Foundation.
|
|
|
|
//
|
|
|
|
// This program is distributed in the hope that it will be useful,
|
|
|
|
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
|
|
|
// General Public License for more details.
|
|
|
|
//
|
|
|
|
// You should have received a copy of the GNU General Public
|
|
|
|
// License along with this program; if not, write to the
|
|
|
|
// Free Software Foundation, Inc., 59 Temple Place - Suite 330,
|
|
|
|
// Boston, MA 02111-1307, USA.
|
|
|
|
|
|
|
|
|
|
|
|
namespace GtkSharp.Generation {
|
|
|
|
|
|
|
|
using System;
|
2012-11-04 15:19:05 +00:00
|
|
|
using System.Collections.Generic;
|
2012-11-03 14:46:06 +00:00
|
|
|
using System.Xml;
|
|
|
|
|
|
|
|
public class ArrayParameter : Parameter {
|
|
|
|
|
|
|
|
bool null_terminated;
|
|
|
|
|
|
|
|
public ArrayParameter (XmlElement elem) : base (elem)
|
|
|
|
{
|
|
|
|
null_terminated = elem.GetAttributeAsBoolean ("null_term_array");
|
2014-03-17 21:39:14 +00:00
|
|
|
if (elem.HasAttribute ("array_len"))
|
|
|
|
FixedArrayLength = Int32.Parse (elem.GetAttribute ("array_len"));
|
2012-11-03 14:46:06 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
public override string MarshalType {
|
|
|
|
get {
|
|
|
|
if (Generatable is StructBase)
|
|
|
|
return CSType;
|
|
|
|
else
|
|
|
|
return base.MarshalType;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
bool NullTerminated {
|
|
|
|
get {
|
|
|
|
return null_terminated;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-03-17 21:39:14 +00:00
|
|
|
public int? FixedArrayLength { get; private set; }
|
|
|
|
|
2012-11-03 14:46:06 +00:00
|
|
|
public override string[] Prepare {
|
|
|
|
get {
|
2014-03-17 21:39:14 +00:00
|
|
|
if (CSType == MarshalType && !FixedArrayLength.HasValue)
|
2012-11-03 14:46:06 +00:00
|
|
|
return new string [0];
|
|
|
|
|
2012-11-04 15:19:05 +00:00
|
|
|
var result = new List<string> ();
|
2014-03-17 21:39:14 +00:00
|
|
|
|
|
|
|
if (FixedArrayLength.HasValue) {
|
|
|
|
result.Add (String.Format ("{0} = new {1}[{2}];", Name, MarshalType.TrimEnd ('[', ']'), FixedArrayLength));
|
|
|
|
return result.ToArray ();
|
|
|
|
}
|
2012-11-03 14:46:06 +00:00
|
|
|
result.Add (String.Format ("int cnt_{0} = {0} == null ? 0 : {0}.Length;", CallName));
|
|
|
|
result.Add (String.Format ("{0}[] native_{1} = new {0} [cnt_{1}" + (NullTerminated ? " + 1" : "") + "];", MarshalType.TrimEnd('[', ']'), CallName));
|
|
|
|
result.Add (String.Format ("for (int i = 0; i < cnt_{0}; i++)", CallName));
|
|
|
|
IGeneratable gen = Generatable;
|
|
|
|
if (gen is IManualMarshaler)
|
|
|
|
result.Add (String.Format ("\tnative_{0} [i] = {1};", CallName, (gen as IManualMarshaler).AllocNative (CallName + "[i]")));
|
|
|
|
else
|
|
|
|
result.Add (String.Format ("\tnative_{0} [i] = {1};", CallName, gen.CallByName (CallName + "[i]")));
|
|
|
|
|
|
|
|
if (NullTerminated)
|
|
|
|
result.Add (String.Format ("native_{0} [cnt_{0}] = IntPtr.Zero;", CallName));
|
2012-11-04 15:19:05 +00:00
|
|
|
return result.ToArray ();
|
2012-11-03 14:46:06 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
public override string CallString {
|
|
|
|
get {
|
|
|
|
if (CSType != MarshalType)
|
|
|
|
return "native_" + CallName;
|
2014-03-17 21:39:14 +00:00
|
|
|
else if (FixedArrayLength.HasValue)
|
|
|
|
return base.CallString;
|
2012-11-03 14:46:06 +00:00
|
|
|
else
|
|
|
|
return CallName;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
public override string[] Finish {
|
|
|
|
get {
|
|
|
|
if (CSType == MarshalType)
|
|
|
|
return new string [0];
|
|
|
|
|
|
|
|
IGeneratable gen = Generatable;
|
|
|
|
if (gen is IManualMarshaler) {
|
|
|
|
string [] result = new string [4];
|
|
|
|
result [0] = "for (int i = 0; i < native_" + CallName + ".Length" + (NullTerminated ? " - 1" : "") + "; i++) {";
|
|
|
|
result [1] = "\t" + CallName + " [i] = " + Generatable.FromNative ("native_" + CallName + "[i]") + ";";
|
|
|
|
result [2] = "\t" + (gen as IManualMarshaler).ReleaseNative ("native_" + CallName + "[i]") + ";";
|
|
|
|
result [3] = "}";
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
return new string [0];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
public class ArrayCountPair : ArrayParameter {
|
|
|
|
|
|
|
|
XmlElement count_elem;
|
|
|
|
bool invert;
|
|
|
|
|
|
|
|
public ArrayCountPair (XmlElement array_elem, XmlElement count_elem, bool invert) : base (array_elem)
|
|
|
|
{
|
|
|
|
this.count_elem = count_elem;
|
|
|
|
this.invert = invert;
|
|
|
|
}
|
|
|
|
|
|
|
|
string CountNativeType {
|
|
|
|
get {
|
|
|
|
return SymbolTable.Table.GetMarshalType(count_elem.GetAttribute("type"));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
string CountType {
|
|
|
|
get {
|
|
|
|
return SymbolTable.Table.GetCSType(count_elem.GetAttribute("type"));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
string CountCast {
|
|
|
|
get {
|
|
|
|
if (CountType == "int")
|
|
|
|
return String.Empty;
|
|
|
|
else
|
|
|
|
return "(" + CountType + ") ";
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
string CountName {
|
|
|
|
get {
|
|
|
|
return SymbolTable.Table.MangleName (count_elem.GetAttribute("name"));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
string CallCount (string name)
|
|
|
|
{
|
|
|
|
string result = CountCast + "(" + name + " == null ? 0 : " + name + ".Length)";
|
|
|
|
IGeneratable gen = SymbolTable.Table[count_elem.GetAttribute("type")];
|
|
|
|
return gen.CallByName (result);
|
|
|
|
}
|
|
|
|
|
|
|
|
public override string CallString {
|
|
|
|
get {
|
|
|
|
if (invert)
|
|
|
|
return CallCount (CallName) + ", " + base.CallString;
|
|
|
|
else
|
|
|
|
return base.CallString + ", " + CallCount (CallName);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
public override string NativeSignature {
|
|
|
|
get {
|
|
|
|
if (invert)
|
|
|
|
return CountNativeType + " " + CountName + ", " + MarshalType + " " + Name;
|
|
|
|
else
|
|
|
|
return MarshalType + " " + Name + ", " + CountNativeType + " " + CountName;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|