dcop Library API Documentation

stub.cpp

00001 /*****************************************************************
00002 Copyright (c) 1999 Torben Weis <weis@kde.org>
00003 Copyright (c) 2000 Matthias Ettrich <ettrich@kde.org>
00004 
00005 Permission is hereby granted, free of charge, to any person obtaining a copy
00006 of this software and associated documentation files (the "Software"), to deal
00007 in the Software without restriction, including without limitation the rights
00008 to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
00009 copies of the Software, and to permit persons to whom the Software is
00010 furnished to do so, subject to the following conditions:
00011 
00012 The above copyright notice and this permission notice shall be included in
00013 all copies or substantial portions of the Software.
00014 
00015 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
00016 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
00017 FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL THE
00018 X CONSORTIUM BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN
00019 AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
00020 CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
00021 
00022 ******************************************************************/
00023 #include <qdom.h>
00024 #include <qfile.h>
00025 #include <qtextstream.h>
00026 #include <qstring.h>
00027 #include <qstringlist.h>
00028 
00029 #include <string.h>
00030 #include <stdlib.h>
00031 #include <stdio.h>
00032 #include <unistd.h>
00033 #include "main.h"
00034 
00035 
00039 void generateStub( const QString& idl, const QString& filename, QDomElement de)
00040 {
00041     QFile stub( filename );
00042     if ( !stub.open( IO_WriteOnly ) )
00043         qFatal("Could not write to %s", filename.local8Bit().data() );
00044         
00045     QTextStream str( &stub );
00046 
00047     str << "/****************************************************************************" << endl;
00048     str << "**" << endl;
00049     str << "** DCOP Stub Definition created by dcopidl2cpp from " << idl << endl;
00050     str << "**" << endl;
00051     str << "** WARNING! All changes made in this file will be lost!" << endl;
00052     str << "**" << endl;
00053     str << "*****************************************************************************/" << endl;
00054     str << endl;
00055 
00056     QString ifdefstring = idl.upper();
00057     int pos = idl.findRev( '.' );
00058     if ( pos != -1 )
00059         ifdefstring = ifdefstring.left( pos );
00060 
00061     QString ifdefsuffix = "_STUB__";
00062     str << "#ifndef __" << ifdefstring << ifdefsuffix << endl;
00063     str << "#define __" << ifdefstring << ifdefsuffix << endl << endl;
00064 
00065     str << "#include <dcopstub.h>" << endl;
00066 
00067     QStringList includeslist, all_includes;
00068     QDomElement e = de.firstChild().toElement();
00069     for( ; !e.isNull(); e = e.nextSibling().toElement() ) {
00070         if ( e.tagName() == "INCLUDE" ) {
00071             // dcopidl lists the includes in reversed order because of the used yacc/bison gramatic
00072             // so let's reverse it back, as the order may be important
00073             includeslist.prepend( e.firstChild().toText().data());
00074             continue;
00075         }
00076         if( !includeslist.empty()) {
00077             for( QStringList::ConstIterator it = includeslist.begin();
00078                  it != includeslist.end();
00079                  ++it ) {
00080                 str << "#include <" << ( *it ) << ">" << endl;
00081                 all_includes.append( *it );
00082             }
00083             includeslist.clear();
00084         }
00085         if ( e.tagName() == "CLASS" ) {
00086             str << endl;
00087         
00088             QDomElement n = e.firstChild().toElement();
00089             Q_ASSERT( n.tagName() == "NAME" );
00090             QString className = n.firstChild().toText().data() 
00091                          + ( "_stub" );
00092         
00093             // find dcop parent ( rightmost super class )
00094             QString DCOPParent;
00095             QDomElement s = n.nextSibling().toElement();
00096             for( ; !s.isNull(); s = s.nextSibling().toElement() ) {
00097                 if ( s.tagName() == "SUPER" )
00098                     DCOPParent = s.firstChild().toText().data();
00099             }
00100             
00101             if( DCOPParent != "DCOPObject" ) { // we need to include the .h file for the base stub
00102                 if( all_includes.contains( DCOPParent + ".h" ))
00103                     str << "#include <" << DCOPParent << "_stub.h>" << endl;
00104                 else if( all_includes.contains( DCOPParent.lower() + ".h" ))
00105                     str << "#include <" << DCOPParent.lower() << "_stub.h>" << endl;
00106                 else {// damn ... let's assume it's the last include
00107                     QString stub_h = all_includes.last();
00108                     unsigned int pos = stub_h.find( ".h" );
00109                     if( pos > 0 ) {
00110                         stub_h = stub_h.remove( pos, 100000 );
00111                         str << "#include <" << stub_h << "_stub.h>" << endl;
00112                     }
00113                     else
00114                         str << "#include <" << stub_h << ">" << endl;
00115                 }
00116             }
00117 
00118             QString classNameFull = className; // class name with possible namespaces prepended
00119                                                // namespaces will be removed from className now
00120             int namespace_count = 0;
00121             QString namespace_tmp = className;
00122             for(;;) {
00123                 int pos = namespace_tmp.find( "::" );
00124                 if( pos < 0 )
00125                     {
00126                     className = namespace_tmp;
00127                     break;
00128                     }
00129                 str << "namespace " << namespace_tmp.left( pos ) << " {" << endl;
00130                 ++namespace_count;
00131                 namespace_tmp = namespace_tmp.mid( pos + 2 );
00132             }
00133 
00134             str << endl;
00135 
00136             // Stub class definition
00137             str << "class " << className;
00138 
00139             // Parent : inherited interface stub or dcopstub
00140             if ( !DCOPParent.isEmpty() && DCOPParent != "DCOPObject" ) {
00141                str << " : ";
00142                str << "virtual public " << DCOPParent << "_stub";
00143             } else {
00144                str << " : virtual public DCOPStub";
00145             }
00146 
00147             str << endl;
00148             str << "{" << endl;
00149             str << "public:" << endl;
00150         
00151             // Constructors
00152             str << "    " << className << "( const QCString& app, const QCString& id );" << endl;
00153             str << "    " << className << "( DCOPClient* client, const QCString& app, const QCString& id );" << endl;
00154 
00155             s = e.firstChild().toElement();
00156             for( ; !s.isNull(); s = s.nextSibling().toElement() ) {
00157                 if (s.tagName() == "FUNC") {
00158                     QDomElement r = s.firstChild().toElement();
00159                     Q_ASSERT( r.tagName() == "TYPE" );
00160                     str << "    virtual "; // KDE4 - I really don't think these need to be virtual
00161                     if ( r.hasAttribute( "qleft" ) )
00162                         str << r.attribute("qleft") << " ";
00163                     str << r.firstChild().toText().data();
00164                     if ( r.hasAttribute( "qright" ) )
00165                         str << r.attribute("qright") << " ";
00166                     else
00167                         str << " ";
00168 
00169                     r = r.nextSibling().toElement();
00170                     Q_ASSERT ( r.tagName() == "NAME" );
00171                     str << r.firstChild().toText().data() << "(";
00172 
00173                     bool first = TRUE;
00174                     r = r.nextSibling().toElement();
00175                     for( ; !r.isNull(); r = r.nextSibling().toElement() ) {
00176                         if ( !first )
00177                             str << ", ";
00178                         else
00179                             str << " ";
00180                         first = FALSE;
00181                         Q_ASSERT( r.tagName() == "ARG" );
00182                         QDomElement a = r.firstChild().toElement();
00183                         Q_ASSERT( a.tagName() == "TYPE" );
00184                         if ( a.hasAttribute( "qleft" ) )
00185                             str << a.attribute("qleft") << " ";
00186                         str << a.firstChild().toText().data();
00187                         if ( a.hasAttribute( "qright" ) )
00188                             str << a.attribute("qright") << " ";
00189                         else
00190                             str << " ";
00191                         a = a.nextSibling().toElement();
00192                         if ( a.tagName() == "NAME" )
00193                             str << a.firstChild().toText().data();
00194                     }
00195                     if ( !first )
00196                         str << " ";
00197                     str << ")";
00198 
00199                     if ( s.hasAttribute("qual") )
00200                         str << " " << s.attribute("qual");
00201                     str << ";" << endl;
00202                 }
00203             }
00204 
00205             // needed for inherited stubs
00206             str << "protected:" << endl;
00207             str << "    " << className << "() : DCOPStub( never_use ) {};" << endl;
00208 
00209             str << "};" << endl;
00210             str << endl;
00211 
00212             for(;
00213                  namespace_count > 0;
00214                  --namespace_count )
00215                 str << "} // namespace" << endl;
00216             str << endl;
00217 
00218         }
00219     }
00220 
00221     str << "#endif" << endl;
00222     stub.close();
00223 }
00224 
KDE Logo
This file is part of the documentation for kdelibs Version 3.1.5.
Documentation copyright © 1996-2002 the KDE developers.
Generated on Wed Jan 28 12:42:42 2004 by doxygen 1.3.4 written by Dimitri van Heesch, © 1997-2001