~ubuntu-branches/ubuntu/maverick/docky/maverick

« back to all changes in this revision

Viewing changes to lib/gio-sharp/generator/StructField.cs

  • Committer: Bazaar Package Importer
  • Author(s): Christopher James Halse Rogers
  • Date: 2010-02-17 15:10:07 UTC
  • Revision ID: james.westby@ubuntu.com-20100217151007-msxpd0lsj300ndde
Tags: upstream-2.0.0
ImportĀ upstreamĀ versionĀ 2.0.0

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
// GtkSharp.Generation.StructField.cs - The Structure Field generation
 
2
// Class.
 
3
//
 
4
// Author: Mike Kestner <mkestner@ximian.com>
 
5
//
 
6
// Copyright (c) 2004-2005 Novell, Inc.
 
7
//
 
8
// This program is free software; you can redistribute it and/or
 
9
// modify it under the terms of version 2 of the GNU General Public
 
10
// License as published by the Free Software Foundation.
 
11
//
 
12
// This program is distributed in the hope that it will be useful,
 
13
// but WITHOUT ANY WARRANTY; without even the implied warranty of
 
14
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 
15
// General Public License for more details.
 
16
//
 
17
// You should have received a copy of the GNU General Public
 
18
// License along with this program; if not, write to the
 
19
// Free Software Foundation, Inc., 59 Temple Place - Suite 330,
 
20
// Boston, MA 02111-1307, USA.
 
21
 
 
22
 
 
23
namespace GtkSharp.Generation {
 
24
 
 
25
        using System;
 
26
        using System.IO;
 
27
        using System.Xml;
 
28
 
 
29
        public class StructField : FieldBase {
 
30
 
 
31
                public static int bitfields;
 
32
 
 
33
                public StructField (XmlElement elem, ClassBase container_type) : base (elem, container_type) {}
 
34
 
 
35
                protected override string DefaultAccess {
 
36
                        get {
 
37
                                if (IsPadding)
 
38
                                        return "private";
 
39
 
 
40
                                return "public";
 
41
                        }
 
42
                }
 
43
 
 
44
                int ArrayLength {
 
45
                        get {
 
46
                                if (!IsArray)
 
47
                                        return 0;
 
48
                                
 
49
                                int result;
 
50
                                try {
 
51
                                        result = Int32.Parse (elem.GetAttribute("array_len"));
 
52
                                } catch (Exception) {
 
53
                                        Console.Write ("Non-numeric array_len: " + elem.GetAttribute("array_len"));
 
54
                                        Console.WriteLine (" warning: array field {0} incorrectly generated", Name);
 
55
                                        result = 0;
 
56
                                }
 
57
                                return result;
 
58
                        }
 
59
                }
 
60
 
 
61
                public new string CSType {
 
62
                        get {
 
63
                                string type = base.CSType;
 
64
                                if (IsArray)
 
65
                                        type += "[]";
 
66
                                else if ((IsPointer || SymbolTable.Table.IsOpaque (CType)) && type != "string")
 
67
                                        type = "IntPtr";
 
68
 
 
69
                                return type;
 
70
                        }
 
71
                }
 
72
 
 
73
                bool IsPadding {
 
74
                        get {
 
75
                                return (CName.StartsWith ("dummy") || CName.StartsWith ("padding"));
 
76
                        }
 
77
                }
 
78
 
 
79
                public bool IsPointer {
 
80
                        get {
 
81
                                return (CType.EndsWith ("*") || CType.EndsWith ("pointer"));
 
82
                        }
 
83
                }
 
84
 
 
85
                public new string Name {
 
86
                        get {
 
87
                                string result = "";
 
88
                                if ((IsPointer || SymbolTable.Table.IsOpaque (CType)) && CSType != "string")
 
89
                                        result = "_";
 
90
                                result += SymbolTable.Table.MangleName (CName);
 
91
 
 
92
                                return result;
 
93
                        }
 
94
                }
 
95
 
 
96
                string StudlyName {
 
97
                        get {
 
98
                                string studly = base.Name;
 
99
                                if (studly == "")
 
100
                                        throw new Exception ("API file must be regenerated with a current version of the GAPI parser. It is incompatible with this version of the GAPI code generator.");
 
101
 
 
102
                                return studly;
 
103
                        }
 
104
                }
 
105
 
 
106
                public override void Generate (GenerationInfo gen_info, string indent)
 
107
                {
 
108
                        if (Hidden)
 
109
                                return;
 
110
 
 
111
                        StreamWriter sw = gen_info.Writer;
 
112
                        SymbolTable table = SymbolTable.Table;
 
113
 
 
114
                        string wrapped = table.GetCSType (CType);
 
115
                        string wrapped_name = SymbolTable.Table.MangleName (CName);
 
116
                        IGeneratable gen = table [CType];
 
117
 
 
118
                        if (IsArray) {
 
119
                                sw.WriteLine (indent + "[MarshalAs (UnmanagedType.ByValArray, SizeConst=" + ArrayLength + ")]");
 
120
                                sw.WriteLine (indent + "{0} {1} {2};", Access, CSType, StudlyName);
 
121
                        } else if (IsBitfield) {
 
122
                                base.Generate (gen_info, indent);
 
123
                        } else if (gen is IAccessor) {
 
124
                                sw.WriteLine (indent + "private {0} {1};", gen.MarshalType, Name);
 
125
 
 
126
                                if (Access != "private") {
 
127
                                        IAccessor acc = table [CType] as IAccessor;
 
128
                                        sw.WriteLine (indent + Access + " " + wrapped + " " + StudlyName + " {");
 
129
                                        acc.WriteAccessors (sw, indent + "\t", Name);
 
130
                                        sw.WriteLine (indent + "}");
 
131
                                }
 
132
                        } else if (IsPointer && (gen is StructGen || gen is BoxedGen)) {
 
133
                                sw.WriteLine (indent + "private {0} {1};", CSType, Name);
 
134
                                sw.WriteLine ();
 
135
                                if (Access != "private") {
 
136
                                        sw.WriteLine (indent + Access + " " + wrapped + " " + wrapped_name + " {");
 
137
                                        sw.WriteLine (indent + "\tget { return " + table.FromNativeReturn (CType, Name) + "; }");
 
138
                                        sw.WriteLine (indent + "}");
 
139
                                }
 
140
                        } else if (IsPointer && CSType != "string") {
 
141
                                // FIXME: probably some fields here which should be visible.
 
142
                                sw.WriteLine (indent + "private {0} {1};", CSType, Name);
 
143
                        } else {
 
144
                                sw.WriteLine (indent + "{0} {1} {2};", Access, CSType, Access == "public" ? StudlyName : Name);
 
145
                        }
 
146
                }
 
147
        }
 
148
}
 
149