]> err.no Git - sope/blob - libFoundation/doc/signature-test.pl
Add libxml2-dev to libsope-xml4.7-dev deps
[sope] / libFoundation / doc / signature-test.pl
1 #! /usr/local/bin/perl
2
3 @types = (
4     'char', 
5     'short',
6     'int',
7     'long',
8     'float',
9     'double',
10     'void*',
11     'Struct1',
12     'Struct2',
13     'Struct3',
14     'Struct4',
15     'Struct5',
16     'Struct6',
17     'Struct7',
18     'Struct8'
19 );
20
21 %type_encoding = (
22     'char' => 'c',
23     'short' => 's',
24     'int' => 'i',
25     'long' => 'l',
26     'float' => 'f',
27     'double' => 'd',
28     'void*' => '^v',
29     'Struct1' => '{?=c}',
30     'Struct2' => '{?=s}',
31     'Struct3' => '{?=sc}',
32     'Struct4' => '{?=i}',
33     'Struct5' => '{?=ic}',
34     'Struct6' => '{?=is}',
35     'Struct7' => '{?=isc}',
36     'Struct8' => '{?=ii}'
37 );
38
39 %type_method_name = (
40     'char' => 'c',
41     'short' => 's',
42     'int' => 'i',
43     'long' => 'l',
44     'float' => 'f',
45     'double' => 'd',
46     'void*' => 'p',
47     'Struct1' => 's1',
48     'Struct2' => 's2',
49     'Struct3' => 's3',
50     'Struct4' => 's4',
51     'Struct5' => 's5',
52     'Struct6' => 's6',
53     'Struct7' => 's7',
54     'Struct8' => 's8'
55 );
56
57 # The maximum number of arguments a method should have
58 $no_of_args = 2;
59
60 sub generate_methods_file {
61     local ($file,
62             $file_prologue, $file_epilogue,
63             $method_prefix, $method_code,
64             $initial_acc_args
65     ) = @_;
66
67     $FILE = "> $file";
68     #$FILE = ">-";
69     open(FILE) or die "cannot open file: $!\n";
70     print FILE $file_prologue;
71
72     # The number of methods generated
73     $methods_generated = 0;
74
75     gen_method($no_of_args, $method_prefix, $method_code, $initial_acc_args);
76     print FILE $file_epilogue;
77
78     print "$methods_generated methods generated in file $file.\n";
79 }
80
81 sub gen_method {
82     local ($mth_no_of_args,
83             $method_prefix, $method_code,
84             $initial_acc_args
85     ) = @_;
86
87     gen_method1(0, $mth_no_of_args, "", $initial_acc_args,
88                             $method_prefix, $method_code);
89 }
90
91 sub gen_method1 {
92     local (
93             $curr_type_no, $curr_arg_no,
94             $acc_mth_name, $acc_args,
95             $method_prefix, $method_code
96     ) = @_;
97
98     if ($curr_arg_no == 0) {
99         print FILE "$method_prefix";
100         if ($acc_mth_name ne "") {
101                 print FILE "${acc_mth_name}${acc_args}";
102         }
103         print FILE $method_code;
104         $methods_generated++;
105         print "$methods_generated\r";
106         return;
107     }
108
109     # Determine the preceding types. We not generate the methods that have more
110     # than 2 identical types in consecutive positions.
111     local $prev_type = "";
112     if ($acc_mth_name =~ /.*_(.*)$/) {
113             $prev_type = $acc_mth_name;
114             $prev_type =~ s/.*_(.*)$/$1/;
115     }
116
117     for (local $curr_type_no = 0; $curr_type_no <= $#types; $curr_type_no++) {
118         local $label = $type_method_name{$types[$curr_type_no]};
119
120         if(!($label eq $prev_type)) {
121             gen_method1($curr_type_no, $curr_arg_no - 1,
122                     "${acc_mth_name}_$label",
123                     &$show_acc_args($acc_args, $curr_type_no, $curr_arg_no),
124                     $method_prefix, $method_code);
125         }
126     }
127 }
128
129 $source_file = __FILE__;
130
131 $show_acc_args = sub {
132     local ($acc_args, $curr_type_no, $curr_arg_no) = @_;
133     local $number = $no_of_args - $curr_arg_no;
134     local $label = $type_method_name{$types[$curr_type_no]};
135
136     return "$acc_args:($types[$curr_type_no])${label}${number} ";
137 };
138
139 generate_methods_file("SignatureTest.m",
140 "/* Do not modify! Generated automatically from $source_file. */
141
142 #include <objc/Object.h>
143
144 typedef struct {
145 char c;
146 } Struct1;
147
148 typedef struct {
149 short s;
150 } Struct2;
151
152 typedef struct {
153 short s;
154 char c;
155 } Struct3;
156
157 typedef struct {
158 int i;
159 } Struct4;
160
161 typedef struct {
162 int i;
163 char c;
164 } Struct5;
165
166 typedef struct {
167 int i;
168 short s;
169 } Struct6;
170
171 typedef struct {
172 int i;
173 short s;
174 char c;
175 } Struct7;
176
177 typedef struct {
178 int i;
179 int j;
180 } Struct8;
181
182 \@interface SignatureTest : Object
183 \@end
184
185 \@implementation SignatureTest
186
187 ", "
188 \@end /* SignatureTest */
189 ", "- (void)method",
190 "{}
191 ",
192 "");
193
194
195 $show_acc_args = sub {
196     local ($acc_args, $curr_type_no, $curr_arg_no) = @_;
197
198     return ":${acc_args}";
199 };
200
201 generate_methods_file("test.m",
202 "/* Do not modify! Generated automatically from $source_file. */
203 #include <stdio.h>
204 #include <objc/objc.h>
205 #include <objc/objc-api.h>
206 #include \"SignatureTest.m\"
207
208 void main()
209 {
210     id signtest = [[SignatureTest alloc] init];
211     struct objc_method* mth;
212
213 ", "
214 }
215 ",
216 "    mth = class_get_instance_method([signtest class], \@selector(method",
217 "));
218     printf(\"'%s' is '%s'\\n\", sel_get_name(mth->method_name), mth->method_types);
219 ",
220 "");