1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305
|
// 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;
using System.Collections;
using System.Collections.Generic;
using System.Xml;
public class Parameters : IEnumerable<Parameter> {
IList<Parameter> param_list = new List<Parameter> ();
XmlElement elem;
bool first_is_instance;
public Parameters (XmlElement elem) : this (elem, false) { }
public Parameters (XmlElement elem, bool first_is_instance)
{
if (elem == null)
valid = true;
this.elem = elem;
this.first_is_instance = first_is_instance;
if (first_is_instance)
is_static = false;
}
public int Count {
get { return param_list.Count; }
}
// gapi assumes GError** parameters to be error parameters in version 1 and 2
private bool throws = false;
public bool Throws {
get {
if (Parser.GetVersion (elem.OwnerDocument.DocumentElement) <= 2)
return true;
if (!throws && elem.HasAttribute ("throws"))
throws = elem.GetAttributeAsBoolean ("throws");
return throws;
}
}
public int VisibleCount {
get {
int visible = 0;
foreach (Parameter p in this) {
if (!IsHidden (p))
visible++;
}
return visible;
}
}
public Parameter this [int idx] {
get {
return param_list [idx];
}
}
public bool IsHidden (Parameter p)
{
int idx = param_list.IndexOf (p);
if (idx > 0 && p.IsLength && p.PassAs == String.Empty && this [idx - 1].IsString)
return true;
if (p.IsCount)
return true;
if (p.IsHidden)
return true;
if (p.CType == "GError**" && Throws)
return true;
if (HasCB || HideData) {
if (Parser.GetVersion (elem.OwnerDocument.DocumentElement) >= 3) {
foreach (Parameter param in param_list) {
if (param.Closure == idx)
return true;
if (param.DestroyNotify == idx)
return true;
}
} else {
if (p.IsUserData && (idx == Count - 1))
return true;
if (p.IsUserData && (idx == Count - 2) && this [Count - 1] is ErrorParameter)
return true;
if (p.IsUserData && idx > 0 && this [idx - 1].Generatable is CallbackGen)
return true;
if (p.IsDestroyNotify && (idx == Count - 1) && this [idx - 1].IsUserData)
return true;
}
}
return false;
}
bool has_cb;
public bool HasCB {
get { return has_cb; }
set { has_cb = value; }
}
public bool HasOutParam {
get {
foreach (Parameter p in this)
if (p.PassAs == "out")
return true;
return false;
}
}
bool hide_data;
public bool HideData {
get { return hide_data; }
set { hide_data = value; }
}
bool is_static;
public bool Static {
get { return is_static; }
set { is_static = value; }
}
bool has_optional;
internal bool HasOptional {
get { return has_optional;}
}
public Parameter GetCountParameter (string param_name)
{
foreach (Parameter p in this)
if (p.Name == param_name) {
p.IsCount = true;
return p;
}
return null;
}
void Clear ()
{
elem = null;
param_list.Clear ();
param_list = null;
}
public IEnumerator<Parameter> GetEnumerator ()
{
return param_list.GetEnumerator ();
}
IEnumerator IEnumerable.GetEnumerator ()
{
return GetEnumerator ();
}
bool valid = false;
public bool Validate (LogWriter log)
{
if (valid)
return true;
if (elem == null)
return false;
for (int i = first_is_instance ? 1 : 0; i < elem.ChildNodes.Count; i++) {
XmlElement parm = elem.ChildNodes [i] as XmlElement;
if (parm == null || parm.Name != "parameter")
continue;
Parameter p = new Parameter (parm);
if (p.IsEllipsis) {
log.Warn ("Ellipsis parameter: hide and bind manually if no alternative exists. ");
Clear ();
return false;
}
if ((p.CSType == "") || (p.Name == "") ||
(p.MarshalType == "") || (SymbolTable.Table.CallByName(p.CType, p.Name) == "")) {
log.Warn ("Unknown type {1} on parameter {0}", p.Name, p.CType);
Clear ();
return false;
}
if (p.IsOptional && p.PassAs == String.Empty)
has_optional = true;
IGeneratable gen = p.Generatable;
if (p.IsArray) {
p = new ArrayParameter (parm);
if (i < elem.ChildNodes.Count - 1) {
XmlElement next = elem.ChildNodes [i + 1] as XmlElement;
if (next != null || next.Name == "parameter") {
Parameter c = new Parameter (next);
if (c.IsCount) {
p = new ArrayCountPair (parm, next, false);
i++;
}
}
}
} else if (p.IsCount) {
p.IsCount = false;
if (i < elem.ChildNodes.Count - 1) {
XmlElement next = elem.ChildNodes [i + 1] as XmlElement;
if (next != null || next.Name == "parameter") {
Parameter a = new Parameter (next);
if (a.IsArray) {
p = new ArrayCountPair (next, parm, true);
i++;
}
}
}
} else if (p.CType == "GError**" && Throws)
p = new ErrorParameter (parm);
else if (gen is StructBase || gen is ByRefGen) {
p = new StructParameter (parm);
} else if (gen is CallbackGen) {
has_cb = true;
}
param_list.Add (p);
}
if (Parser.GetVersion (elem.OwnerDocument.DocumentElement) < 3 &&
has_cb && Count > 2 && this [Count - 3].Generatable is CallbackGen && this [Count - 2].IsUserData && this [Count - 1].IsDestroyNotify)
this [Count - 3].Scope = "notified";
valid = true;
return true;
}
public bool IsAccessor {
get {
return VisibleCount == 1 && AccessorParam.PassAs == "out";
}
}
public Parameter AccessorParam {
get {
foreach (Parameter p in this) {
if (!IsHidden (p))
return p;
}
return null;
}
}
public string AccessorReturnType {
get {
Parameter p = AccessorParam;
if (p != null)
return p.CSType;
else
return null;
}
}
public string AccessorName {
get {
Parameter p = AccessorParam;
if (p != null)
return p.Name;
else
return null;
}
}
public string ImportSignature {
get {
if (Count == 0)
return String.Empty;
string[] result = new string [Count];
for (int i = 0; i < Count; i++)
result [i] = this [i].NativeSignature;
return String.Join (", ", result);
}
}
}
}
|