25 #include <tqtextstream.h>
27 #include <tqstringlist.h>
36 static int const primes[] =
38 2, 3, 5, 7, 11, 13, 17, 19, 23, 29,
39 31, 37, 41, 43, 47, 53, 59, 61, 67, 71,
40 73, 79, 83, 89, 97, 101, 103, 107, 109, 113,
41 127, 131, 137, 139, 149, 151, 157, 163, 167, 173,
42 179, 181, 191, 193, 197, 199, 211, 223, 227, 229,
43 233, 239, 241, 251, 257, 263, 269, 271, 277, 281,
44 283, 293, 307, 311, 313, 317, 331, 337, 347, 349,
45 353, 359, 367, 373, 379, 383, 389, 397, 401, 409,
46 419, 421, 431, 433, 439, 443, 449, 457, 461, 463,
47 467, 479, 487, 491, 499, 503, 509, 521, 523, 541,
48 547, 557, 563, 569, 571, 577, 587, 593, 599, 601,0
55 Function(
const TQString& t,
const TQString& n,
const TQString&fn,
bool h )
56 : type( t ),
name( n ), fullName( fn ), hidden( h ) {}
67 void generateSkel(
const TQString& idl,
const TQString& filename, TQDomElement de )
69 TQFile skel( filename );
70 if ( !skel.open( IO_WriteOnly ) )
71 tqFatal(
"Could not write to %s", filename.local8Bit().data() );
73 TQTextStream str( &skel );
75 str <<
"/****************************************************************************" <<
endl;
77 str <<
"** DCOP Skeleton generated by dcopidl2cpp from " << idl <<
endl;
79 str <<
"** WARNING! All changes made in this file will be lost!" <<
endl;
81 str <<
"*****************************************************************************/" <<
endl;
84 TQDomElement e = de.firstChild().toElement();
85 if ( e.tagName() ==
"SOURCE" ) {
86 str <<
"#include \"" << e.firstChild().toText().data() <<
"\"" <<
endl <<
endl;
89 for( ; !e.isNull(); e = e.nextSibling().toElement() ) {
90 if ( e.tagName() !=
"CLASS" )
92 TQDomElement n = e.firstChild().toElement();
93 Q_ASSERT( n.tagName() ==
"NAME" );
94 TQString className = n.firstChild().toText().data();
97 TQDomElement s = n.nextSibling().toElement();
98 for( ; !s.isNull(); s = s.nextSibling().toElement() ) {
99 if ( s.tagName() ==
"SUPER" )
100 DCOPParent = s.firstChild().toText().data();
104 TQValueList<Function> functions;
105 s = n.nextSibling().toElement();
106 for( ; !s.isNull(); s = s.nextSibling().toElement() ) {
107 if ( s.tagName() !=
"FUNC" )
109 TQDomElement r = s.firstChild().toElement();
110 Q_ASSERT( r.tagName() ==
"TYPE" );
111 TQString funcType = r.firstChild().toText().data();
112 r = r.nextSibling().toElement();
113 Q_ASSERT ( r.tagName() ==
"NAME" );
114 TQString funcName = r.firstChild().toText().data();
115 TQStringList argtypes;
116 TQStringList argnames;
117 r = r.nextSibling().toElement();
118 for( ; !r.isNull(); r = r.nextSibling().toElement() ) {
119 Q_ASSERT( r.tagName() ==
"ARG" );
120 TQDomElement a = r.firstChild().toElement();
121 Q_ASSERT( a.tagName() ==
"TYPE" );
122 argtypes.append( a.firstChild().toText().data() );
123 a = a.nextSibling().toElement();
125 Q_ASSERT( a.tagName() ==
"NAME" );
126 argnames.append( a.firstChild().toText().data() );
128 argnames.append( TQString::null );
132 TQString fullFuncName = funcName;
134 TQStringList::Iterator ittype = argtypes.begin();
135 TQStringList::Iterator itname = argnames.begin();
136 while ( ittype != argtypes.end() && itname != argnames.end() ) {
143 fullFuncName += *ittype;
144 if ( ! (*itname).isEmpty() ) {
146 fullFuncName += *itname;
153 bool hidden = (s.attribute(
"hidden") ==
"yes");
154 functions.append( Function( funcType, funcName, fullFuncName, hidden ) );
159 int fhash = functions.count() + 1;
160 for (
int i = 0; primes[i]; i++ ) {
161 if ( primes[i] >
static_cast<int>(functions.count()) ) {
167 str <<
"#include <kdatastream.h>" <<
endl;
169 bool useHashing = functions.count() > 7;
171 str <<
"#include <tqasciidict.h>" <<
endl;
174 TQString classNameFull = className;
176 int namespace_count = 0;
177 TQString namespace_tmp = className;
180 int pos = namespace_tmp.find(
"::" );
182 className = namespace_tmp;
185 str <<
"namespace " << namespace_tmp.left( pos ) <<
" {" <<
endl;
187 namespace_tmp = namespace_tmp.mid( pos + 2 );
193 str <<
"static const int " << className <<
"_fhash = " << fhash <<
";" <<
endl;
195 str <<
"static const char* const " << className <<
"_ftable[" << functions.count() + 1 <<
"][3] = {" <<
endl;
196 for( TQValueList<Function>::Iterator it = functions.begin(); it != functions.end(); ++it ){
197 str <<
" { \"" << (*it).type <<
"\", \"" << (*it).name <<
"\", \"" << (*it).fullName <<
"\" }," <<
endl;
199 str <<
" { 0, 0, 0 }" <<
endl;
202 if (functions.count() > 0) {
203 str <<
"static const int " << className <<
"_ftable_hiddens[" << functions.count() <<
"] = {" <<
endl;
204 for( TQValueList<Function>::Iterator it = functions.begin(); it != functions.end(); ++it ){
205 str <<
" " << !!(*it).hidden <<
"," <<
endl;
214 str <<
"bool " << className;
215 str <<
"::process(const TQCString &fun, const TQByteArray &data, TQCString& replyType, TQByteArray &replyData)" <<
endl;
218 str <<
" static TQAsciiDict<int>* fdict = 0;" <<
endl;
220 str <<
" if ( !fdict ) {" <<
endl;
221 str <<
"\tfdict = new TQAsciiDict<int>( " << className <<
"_fhash, true, false );" <<
endl;
222 str <<
"\tfor ( int i = 0; " << className <<
"_ftable[i][1]; i++ )" <<
endl;
223 str <<
"\t fdict->insert( " << className <<
"_ftable[i][1], new int( i ) );" <<
endl;
226 str <<
" int* fp = fdict->find( fun );" <<
endl;
227 str <<
" switch ( fp?*fp:-1) {" <<
endl;
229 s = n.nextSibling().toElement();
231 bool firstFunc =
true;
232 for( ; !s.isNull(); s = s.nextSibling().toElement() ) {
233 if ( s.tagName() !=
"FUNC" )
235 TQDomElement r = s.firstChild().toElement();
236 Q_ASSERT( r.tagName() ==
"TYPE" );
237 TQString funcType = r.firstChild().toText().data();
238 if ( funcType ==
"ASYNC" )
240 r = r.nextSibling().toElement();
241 Q_ASSERT ( r.tagName() ==
"NAME" );
242 TQString funcName = r.firstChild().toText().data();
244 TQStringList argtypes;
245 r = r.nextSibling().toElement();
246 for( ; !r.isNull(); r = r.nextSibling().toElement() ) {
247 Q_ASSERT( r.tagName() ==
"ARG" );
248 TQDomElement a = r.firstChild().toElement();
249 Q_ASSERT( a.tagName() ==
"TYPE" );
250 argtypes.append( a.firstChild().toText().data() );
251 args.append( TQString(
"arg" ) + TQString::number( args.count() ) );
253 TQString plainFuncName = funcName;
256 for( TQStringList::Iterator argtypes_count = argtypes.begin(); argtypes_count != argtypes.end(); ++argtypes_count ){
260 funcName += *argtypes_count;
265 str <<
" case " << fcount <<
": { // " << funcType <<
" " << funcName <<
endl;
268 str <<
" if ( fun == " << className <<
"_ftable[" << fcount <<
"][1] ) { // " << funcType <<
" " << funcName <<
endl;
270 str <<
" else if ( fun == " << className <<
"_ftable[" << fcount <<
"][1] ) { // " << funcType <<
" " << funcName <<
endl;
273 if ( !args.isEmpty() ) {
274 TQStringList::Iterator ittypes = argtypes.begin();
275 TQStringList::Iterator args_count;
276 for( args_count = args.begin(); args_count != args.end(); ++args_count ){
277 str <<
'\t'<< *ittypes <<
" " << *args_count <<
";" <<
endl;
280 str <<
"\tTQDataStream arg( data, IO_ReadOnly );" <<
endl;
281 for( args_count = args.begin(); args_count != args.end(); ++args_count ){
282 str <<
"\tif (arg.atEnd()) return false;" <<
endl;
283 str <<
"\targ >> " << *args_count <<
";" <<
endl;
287 str <<
"\treplyType = " << className <<
"_ftable[" << fcount++ <<
"][0]; " <<
endl;
288 if ( funcType ==
"void" ) {
289 str <<
'\t' << plainFuncName <<
'(';
291 str <<
"\tTQDataStream _replyStream( replyData, IO_WriteOnly );" <<
endl;
292 str <<
"\t_replyStream << " << plainFuncName <<
'(';
296 for ( TQStringList::Iterator args_count = args.begin(); args_count != args.end(); ++args_count ){
302 str <<
" );" <<
endl;
304 str <<
" } break;" <<
endl;
313 str <<
" default: " <<
endl;
315 str <<
" else {" <<
endl;
320 if (!DCOPParent.isEmpty()) {
321 str <<
"\treturn " << DCOPParent <<
"::process( fun, data, replyType, replyData );" <<
endl;
323 str <<
"\treturn false;" <<
endl;
331 str <<
" return true;" <<
endl;
337 str <<
"QCStringList " << className;
338 str <<
"::interfaces()" <<
endl;
340 if (!DCOPParent.isEmpty()) {
341 str <<
" QCStringList ifaces = " << DCOPParent <<
"::interfaces();" <<
endl;
343 str <<
" QCStringList ifaces;" <<
endl;
345 str <<
" ifaces += \"" << classNameFull <<
"\";" <<
endl;
346 str <<
" return ifaces;" <<
endl;
350 str <<
"QCStringList " << className;
351 str <<
"::functions()" <<
endl;
353 if (!DCOPParent.isEmpty()) {
354 str <<
" QCStringList funcs = " << DCOPParent <<
"::functions();" <<
endl;
356 str <<
" QCStringList funcs;" <<
endl;
358 str <<
" for ( int i = 0; " << className <<
"_ftable[i][2]; i++ ) {" <<
endl;
359 if (functions.count() > 0) {
360 str <<
"\tif (" << className <<
"_ftable_hiddens[i])" <<
endl;
361 str <<
"\t continue;" <<
endl;
363 str <<
"\tTQCString func = " << className <<
"_ftable[i][0];" <<
endl;
364 str <<
"\tfunc += ' ';" <<
endl;
365 str <<
"\tfunc += " << className <<
"_ftable[i][2];" <<
endl;
366 str <<
"\tfuncs << func;" <<
endl;
368 str <<
" return funcs;" <<
endl;
372 for(s = e.firstChild().toElement(); !s.isNull(); s = s.nextSibling().toElement() ) {
373 if (s.tagName() !=
"SIGNAL")
375 TQDomElement r = s.firstChild().toElement();
376 TQString result = writeType( str, r );
378 r = r.nextSibling().toElement();
379 Q_ASSERT ( r.tagName() ==
"NAME" );
380 TQString funcName = r.firstChild().toText().data();
381 str << className <<
"::" << funcName <<
"(";
384 TQStringList argtypes;
386 r = r.nextSibling().toElement();
387 for( ; !r.isNull(); r = r.nextSibling().toElement() ) {
393 Q_ASSERT( r.tagName() ==
"ARG" );
394 TQDomElement a = r.firstChild().toElement();
395 TQString type = writeType( str, a );
396 argtypes.append( type );
397 args.append( TQString(
"arg" ) + TQString::number( args.count() ) ) ;
404 if ( s.hasAttribute(
"qual") )
405 str <<
" " << s.attribute(
"qual");
412 for( TQStringList::Iterator it = argtypes.begin(); it != argtypes.end(); ++it ){
420 if ( result !=
"void" )
421 tqFatal(
"Error in DCOP signal %s::%s: DCOP signals can not return values.", className.latin1(), funcName.latin1());
423 str <<
" TQByteArray data;" <<
endl;
424 if ( !args.isEmpty() ) {
425 str <<
" TQDataStream arg( data, IO_WriteOnly );" <<
endl;
426 for( TQStringList::Iterator args_count = args.begin(); args_count != args.end(); ++args_count ){
427 str <<
" arg << " << *args_count <<
";" <<
endl;
431 str <<
" emitDCOPSignal( \"" << funcName <<
"\", data );" <<
endl;
437 for(; namespace_count > 0; --namespace_count )
438 str <<
"} // namespace" <<
endl;
kndbgstream & endl(kndbgstream &s)
TQString name(StdAccel id)