summaryrefslogtreecommitdiff
path: root/source4/pidl/lib/Parse/Pidl/Samba4/COM/Header.pm
blob: bc592e7f803eaa182d061e8ffcbe08f2cb84d05e (plain)
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
# COM Header generation
# (C) 2005 Jelmer Vernooij <jelmer@samba.org>

package Parse::Pidl::Samba4::COM::Header;

use Parse::Pidl::Typelist qw(mapTypeName);
use Parse::Pidl::Util qw(has_property is_constant);

use vars qw($VERSION);
$VERSION = '0.01';

use strict;

sub GetArgumentProtoList($)
{
	my $f = shift;
	my $res = "";

	foreach my $a (@{$f->{ELEMENTS}}) {

		$res .= ", " . mapTypeName($a->{TYPE}) . " ";

		my $l = $a->{POINTERS};
		$l-- if (Parse::Pidl::Typelist::scalar_is_reference($a->{TYPE}));
		foreach my $i (1..$l) {
			$res .= "*";
		}

		if (defined $a->{ARRAY_LEN}[0] && !is_constant($a->{ARRAY_LEN}[0]) &&
		!$a->{POINTERS}) {
			$res .= "*";
		}
		$res .= $a->{NAME};
		if (defined $a->{ARRAY_LEN}[0] && is_constant($a->{ARRAY_LEN}[0])) {
			$res .= "[$a->{ARRAY_LEN}[0]]";
		}
	}

	return $res;
}

sub GetArgumentList($)
{
	my $f = shift;
	my $res = "";

	foreach (@{$f->{ELEMENTS}}) { $res .= ", $_->{NAME}"; }

	return $res;
}

#####################################################################
# generate vtable structure for COM interface
sub HeaderVTable($)
{
	my $interface = shift;
	my $res;
	$res .= "#define " . uc($interface->{NAME}) . "_METHODS \\\n";
	if (defined($interface->{BASE})) {
		$res .= "\t" . uc($interface->{BASE} . "_METHODS") . "\\\n";
	}

	my $data = $interface->{DATA};
	foreach my $d (@{$data}) {
		$res .= "\t" . mapTypeName($d->{RETURN_TYPE}) . " (*$d->{NAME}) (struct $interface->{NAME} *d, TALLOC_CTX *mem_ctx" . GetArgumentProtoList($d) . ");\\\n" if ($d->{TYPE} eq "FUNCTION");
	}
	$res .= "\n";
	$res .= "struct $interface->{NAME}_vtable {\n";
	$res .= "\tstruct GUID iid;\n";
	$res .= "\t" . uc($interface->{NAME}) . "_METHODS\n";
	$res .= "};\n\n";

	return $res;
}

sub ParseInterface($)
{
	my $if = shift;
	my $res;

	$res .= "\n#ifndef _$if->{NAME}_\n";
	$res .= "#define _$if->{NAME}_\n";

	$res .="\n\n/* $if->{NAME} */\n";

	$res .="#define COM_" . uc($if->{NAME}) . "_UUID $if->{PROPERTIES}->{uuid}\n\n";

	$res .="struct $if->{NAME}_vtable;\n\n";

	$res .="struct $if->{NAME} {
	struct com_context *ctx;
	struct $if->{NAME}_vtable *vtable;
	void *object_data;
};\n\n";

	$res.=HeaderVTable($if);

	foreach my $d (@{$if->{DATA}}) {
		next if ($d->{TYPE} ne "FUNCTION");

		$res .= "#define $if->{NAME}_$d->{NAME}(interface, mem_ctx" . GetArgumentList($d) . ") ";

		$res .= "((interface)->vtable->$d->{NAME}(interface, mem_ctx" . GetArgumentList($d) . "))";

		$res .="\n";
	}

	$res .= "#endif\n";

	return $res;
}

sub ParseCoClass($)
{
	my ($c) = @_;
	my $res = "";
	$res .= "#define CLSID_" . uc($c->{NAME}) . " $c->{PROPERTIES}->{uuid}\n";
	if (has_property($c, "progid")) {
		$res .= "#define PROGID_" . uc($c->{NAME}) . " $c->{PROPERTIES}->{progid}\n";
	}
	$res .= "\n";
	return $res;
}

sub Parse($$)
{
	my ($idl,$ndr_header) = @_;
	my $res = "";

	$res .= "#include \"librpc/gen_ndr/orpc.h\"\n" . 
			"#include \"$ndr_header\"\n\n";

	foreach (@{$idl})
	{
		if ($_->{TYPE} eq "INTERFACE" && has_property($_, "object")) {
			$res.=ParseInterface($_);
		} 

		if ($_->{TYPE} eq "COCLASS") {
			$res.=ParseCoClass($_);
		}
	}

	return $res;
}

1;