summaryrefslogtreecommitdiffstats
path: root/dcop/dcopidl2cpp/stubimpl.cpp
blob: 1e42eea37899b2ade532074ecb195f4944f0b3d9 (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
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
/*****************************************************************
Copyright (c) 1999 Torben Weis <weis@kde.org>
Copyright (c) 2000 Matthias Ettrich <ettrich@kde.org>

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL THE
X CONSORTIUM BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN
AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

******************************************************************/
#include <tqdom.h>
#include <tqfile.h>
#include <tqtextstream.h>
#include <tqstring.h>
#include <tqstringlist.h>

#include <string.h>
#include <stdlib.h>
#include <stdio.h>
#include <unistd.h>
#include "main.h"
#include "type.h"

static bool isIntType( const TQString& t )
{
  return ((t == "int")
       || (t == "signed int")
       || (t == "unsigned int")
       || (t == "uint")
       || (t == "unsigned")
       || (t == "signed short int")
       || (t == "signed short")
       || (t == "short int")
       || (t == "short")
       || (t == "unsigned short int")
       || (t == "unsigned short")
       || (t == "ushort")
       || (t == "long int")
       || (t == "signed long int")
       || (t == "long")
       || (t == "signed long")
       || (t == "unsigned long int")
       || (t == "unsigned long")
       || (t == "ulong")
       || (t == "char")
       || (t == "signed char")
       || (t == "unsigned char"));
}

/*
 * Writes the stub implementation
 */
void generateStubImpl( const TQString& idl, const TQString& header, const TQString& /*headerBase*/, const TQString& filename, TQDomElement de )
{
    TQFile impl( filename );
    if ( !impl.open( IO_WriteOnly ) )
	tqFatal("Could not write to %s", filename.latin1() );

    TQTextStream str( &impl );

    str << "/****************************************************************************" << endl;
    str << "**" << endl;
    str << "** DCOP Stub Implementation created by dcopidl2cpp from " << idl << endl;
    str << "**" << endl;
    str << "** WARNING! All changes made in this file will be lost!" << endl;
    str << "**" << endl;
    str << "*****************************************************************************/" << endl;
    str << endl;

    str << "#include \"" << header << "\"" << endl;
    str << "#include <dcopclient.h>" << endl << endl;
    str << "#include <kdatastream.h>" << endl;

    TQDomElement e = de.firstChild().toElement();
    for( ; !e.isNull(); e = e.nextSibling().toElement() ) {
	if ( e.tagName() != "CLASS" )
	    continue;
	TQDomElement n = e.firstChild().toElement();
	Q_ASSERT( n.tagName() == "NAME" );
	TQString classNameBase = n.firstChild().toText().data();
	TQString className_stub = classNameBase + "_stub";
    
	TQString classNameFull = className_stub; // class name with possible namespaces prepended
					   // namespaces will be removed from className now
	int namespace_count = 0;
	TQString namespace_tmp = className_stub;
	str << endl;
	for(;;) {
	    int pos = namespace_tmp.find( "::" );
	    if( pos < 0 ) {
		className_stub = namespace_tmp;
		break;
	    }
	    str << "namespace " << namespace_tmp.left( pos ) << " {" << endl;
	    ++namespace_count;
	    namespace_tmp = namespace_tmp.mid( pos + 2 );
	}

	str << endl;

	// Write constructors
	str << className_stub << "::" << className_stub << "( const TQCString& app, const TQCString& obj )" << endl;
	str << "  : ";

	// Always explicitly call DCOPStub constructor, because it's virtual base class.           
	// Calling other ones doesn't matter, as they don't do anything important.
	str << "DCOPStub( app, obj )" << endl;

	str << "{" << endl;
	str << "}" << endl << endl;

	str << className_stub << "::" << className_stub << "( DCOPClient* client, const TQCString& app, const TQCString& obj )" << endl;
	str << "  : ";
    
	str << "DCOPStub( client, app, obj )" << endl;

	str << "{" << endl;
	str << "}" << endl << endl;

	str << className_stub << "::" << className_stub << "( const DCOPRef& ref )" << endl;
	str << "  : ";
    
	str << "DCOPStub( ref )" << endl;

	str << "{" << endl;
	str << "}" << endl << endl;

	// Write marshalling code
	TQDomElement s = e.firstChild().toElement();
	for( ; !s.isNull(); s = s.nextSibling().toElement() ) {
	    if (s.tagName() != "FUNC")
		continue;
	    TQDomElement r = s.firstChild().toElement();
	    Q_ASSERT( r.tagName() == "TYPE" );
	    TQString result = r.firstChild().toText().data();
	    bool async = result == "ASYNC";
	    if ( async) {
		result = "void";
		str << result << " ";
	    } else
		result = writeType( str, r );

	    r = r.nextSibling().toElement();
	    Q_ASSERT ( r.tagName() == "NAME" );
	    TQString funcName = r.firstChild().toText().data();
	    str << className_stub << "::" << funcName << "(";

	    TQStringList args;
	    TQStringList argtypes;
	    bool first = true;
	    r = r.nextSibling().toElement();
	    for( ; !r.isNull(); r = r.nextSibling().toElement() ) {
		if ( !first )
		    str << ", ";
		else
		    str << " ";
		first = false;
		Q_ASSERT( r.tagName() == "ARG" );
		TQDomElement a = r.firstChild().toElement();
		TQString type = writeType( str, a );
		argtypes.append( type );
		args.append( TQString("arg" ) + TQString::number( args.count() ) ) ;
		str << args.last();
	    }
	    if ( !first )
		str << " ";
	    str << ")";

	    //const methods in a stub can't compile, they need to call setStatus()
	    //if ( s.hasAttribute("qual") ) 
       //   str << " " << s.attribute("qual");
	    str << endl;
	
	    str << "{" << endl ;

	
	    funcName += "(";
	    first = true;
	    for( TQStringList::Iterator it = argtypes.begin(); it != argtypes.end(); ++it ){
		if ( !first )
		    funcName += ",";
		first = false;
		funcName += *it;
	    }
	    funcName += ")";
	
	    if ( async ) {

		str << "    if ( !dcopClient()  ) {"<< endl;
		str << "\tsetStatus( CallFailed );" << endl;
		str << "\treturn;" << endl;
		str << "    }" << endl;
	
		str << "    TQByteArray data;" << endl;
		if ( !args.isEmpty() ) {
		    str << "    TQDataStream arg( data, IO_WriteOnly );" << endl;
		    for( TQStringList::Iterator args_count = args.begin(); args_count != args.end(); ++args_count ){
			str << "    arg << " << *args_count << ";" << endl;
		    }
		}

		str << "    dcopClient()->send( app(), obj(), \"" << funcName << "\", data );" << endl;
		str << "    setStatus( CallSucceeded );" << endl;

	    } else {

		if ( result != "void" ) {
		    str << "    " << result << " result";
		    if (isIntType( result ))
			str << " = 0";
		    else if (result == "float" || result == "double")
			str << " = 0.0";
		    else if ( result == "bool" )
			str << " = false";

		    str << ";" << endl;
		}

		str << "    if ( !dcopClient()  ) {"<< endl;
		str << "\tsetStatus( CallFailed );" << endl;
		if ( result != "void" )
		    str << "\treturn result;" << endl;
		else
		    str << "\treturn;" << endl;
		str << "    }" << endl;

		str << "    TQByteArray data, replyData;" << endl;
		str << "    TQCString replyType;" << endl;
	
		if ( !args.isEmpty() ) {
		    str << "    TQDataStream arg( data, IO_WriteOnly );" << endl;
		    for( TQStringList::Iterator args_count = args.begin(); args_count != args.end(); ++args_count ){
			str << "    arg << " << *args_count << ";" << endl;
		    }
		}
		str << "    if ( dcopClient()->call( app(), obj(), \"" << funcName << "\",";
		str << " data, replyType, replyData ) ) {" << endl;
		if ( result != "void" ) {
		    str << "\tif ( replyType == \"" << result << "\" ) {" << endl;
		    str << "\t    TQDataStream _reply_stream( replyData, IO_ReadOnly );"  << endl;
		    str << "\t    _reply_stream >> result;" << endl;
		    str << "\t    setStatus( CallSucceeded );" << endl;
		    str << "\t} else {" << endl;
		    str << "\t    callFailed();" << endl;
		    str << "\t}" << endl;
		} else {
		    str << "\tsetStatus( CallSucceeded );" << endl;
		}
		str << "    } else { " << endl;
		str << "\tcallFailed();" << endl;
		str << "    }" << endl;
		if ( result != "void" )
		    str << "    return result;" << endl;
	    }
	    str << "}" << endl << endl;
	}

	for(; namespace_count > 0; --namespace_count )
	    str << "} // namespace" << endl;
	str << endl;
    }
    impl.close();
}

// :set expandtab!<RETURN>:set ts=8<RETURN>:set sts=4<RETURN>:set sw=4<RETURN>