umbrello API Documentation

cppheadercodeoperation.cpp

00001 /***************************************************************************
00002  *                                                                         *
00003  *   This program is free software; you can redistribute it and/or modify  *
00004  *   it under the terms of the GNU General Public License as published by  *
00005  *   the Free Software Foundation; either version 2 of the License, or     *
00006  *   (at your option) any later version.                                   *
00007  *                                                                         *
00008  *   copyright (C) 2004-2006                                               *
00009  *   Umbrello UML Modeller Authors <uml-devel@uml.sf.net>                  *
00010  ***************************************************************************/
00011 
00012 /*  This code generated by:
00013  *      Author : thomas
00014  *      Date   : Mon Sep 1 2003
00015  */
00016 
00017 #include "cppheadercodeoperation.h"
00018 
00019 #include "cppcodegenerator.h"
00020 #include "cppcodegenerationpolicy.h"
00021 #include "cppheadercodedocument.h"
00022 #include "cppcodedocumentation.h"
00023 #include "../uml.h"
00024 
00025 // Constructors/Destructors
00026 //
00027 
00028 CPPHeaderCodeOperation::CPPHeaderCodeOperation
00029  ( CPPHeaderCodeDocument * doc, UMLOperation *parent, const QString & body, const QString & comment )
00030         : CodeOperation (doc, parent, body, comment)
00031 {
00032     // lets not go with the default comment and instead use
00033     // full-blown cpp documentation object instead
00034     setComment(new CPPCodeDocumentation(doc));
00035 
00036     // these things never change..
00037     setOverallIndentationLevel(1);
00038 
00039     setText("");
00040     setStartMethodText("");
00041     setEndMethodText("");
00042 
00043     updateMethodDeclaration();
00044     updateContent();
00045 
00046 }
00047 
00048 CPPHeaderCodeOperation::~CPPHeaderCodeOperation ( ) { }
00049 
00050 // Other methods
00051 //
00052 
00053 // we basically just want to know whether or not to print out
00054 // the body of the operation.
00055 // In C++ if the operations are inline, then we DO print out
00056 // the body text.
00057 void CPPHeaderCodeOperation::updateContent( )
00058 {
00059     CodeGenPolicyExt *pe = UMLApp::app()->getPolicyExt();
00060     CPPCodeGenerationPolicy * policy = dynamic_cast<CPPCodeGenerationPolicy*>(pe);
00061     bool isInlineMethod = policy->getOperationsAreInline( );
00062 
00063     if(isInlineMethod)
00064         setText(""); // change whatever it is to "";
00065 }
00066 
00067 // we basically want to update the doc and start text of this method
00068 void CPPHeaderCodeOperation::updateMethodDeclaration()
00069 {
00070     ClassifierCodeDocument *ccd = dynamic_cast<ClassifierCodeDocument*>(getParentDocument());
00071     bool isInterface = ccd->parentIsInterface();
00072     UMLOperation * o = getParentOperation();
00073 
00074     CodeGenPolicyExt *pe = UMLApp::app()->getPolicyExt();
00075     CPPCodeGenerationPolicy * policy = dynamic_cast<CPPCodeGenerationPolicy*>(pe);
00076     bool isInlineMethod = policy->getOperationsAreInline( );
00077 
00078     QString endLine = getNewLineEndingChars();
00079 
00080     // first, the comment on the operation, IF its autogenerated/empty
00081     QString comment = o->getDoc();
00082     if(comment.isEmpty() && getContentType() == CodeBlock::AutoGenerated)
00083     {
00084         UMLAttributeList parameters = o->getParmList();
00085         for(UMLAttributeListIt iterator(parameters); iterator.current(); ++iterator) {
00086             comment += endLine + "@param " + iterator.current()->getName() + ' ';
00087             comment += iterator.current()->getDoc();
00088         }
00089         getComment()->setText(comment);
00090     }
00091 
00092     // no return type for constructors
00093     QString methodReturnType = o->getTypeName();
00094     QString methodName = o->getName();
00095     QString paramStr = QString("");
00096 
00097     // assemble parameters
00098     UMLAttributeList list = getParentOperation()->getParmList();
00099     int nrofParam = list.count();
00100     int paramNum = 0;
00101     for(UMLAttribute* parm = list.first(); parm; parm=list.next())
00102     {
00103         QString rType = parm->getTypeName();
00104         QString paramName = parm->getName();
00105         QString initialValue = parm->getInitialValue();
00106         paramStr += rType + ' ' + paramName;
00107         if(!initialValue.isEmpty())
00108             paramStr += '=' + initialValue;
00109 
00110         paramNum++;
00111 
00112         if (paramNum != nrofParam )
00113             paramStr  += ", ";
00114     }
00115 
00116     // if an operation isn't a constructor or a destructor and it has no return type
00117     if (o->isLifeOperation())         // constructor/destructor has no type
00118         methodReturnType = "";
00119     else if (methodReturnType.isEmpty())  // this operation should be 'void'
00120         methodReturnType = QString("void");
00121 
00122     // set start/end method text
00123     QString prototype = methodReturnType+' '+methodName+" ("+paramStr+')';
00124 
00125     QString startText;
00126     QString endText;
00127 
00128     applyStereotypes (prototype, o, isInlineMethod, isInterface, startText, endText);
00129 
00130     setStartMethodText(prototype+startText);
00131     setEndMethodText(endText);
00132 }
00133 
00134 int CPPHeaderCodeOperation::lastEditableLine() {
00135     ClassifierCodeDocument * doc = dynamic_cast<ClassifierCodeDocument*>(getParentDocument());
00136     UMLOperation * o = getParentOperation();
00137     if(doc->parentIsInterface() || o->getAbstract())
00138         return -1; // very last line is NOT editable as its a one-line declaration w/ no body in
00139     // an interface.
00140     return 0;
00141 }
00142 
00143 void CPPHeaderCodeOperation::applyStereotypes (QString& prototype, UMLOperation * pOp,
00144                                                bool inlinePolicy, bool interface,
00145                                                QString& start, QString& end)
00146 {
00147     // if the class is an interface, all methods will be declared as pure
00148     // virtual functions
00149     start = (inlinePolicy ? " {" : ";");
00150     end = (inlinePolicy ? "}" : "");
00151     if (pOp->getConst())
00152         prototype += " const";
00153     if (interface || pOp->getAbstract()) {
00154        // constructor can't be virtual or abstract
00155        if (!pOp->isLifeOperation()) {
00156            prototype = "virtual " + prototype + " = 0";
00157            if (inlinePolicy) {
00158                start = ";";
00159                end = "";
00160            }
00161        }
00162     } // constructors could not be declared as static
00163     else if (pOp->getStatic() && !pOp->isLifeOperation()) {
00164        prototype = "static " + prototype;
00165     }
00166     // apply the stereotypes
00167     if (!pOp->getStereotype().isEmpty()) {
00168         if ((pOp->getStereotype() == "friend") || (pOp->getStereotype(false) == "virtual")) {
00169             if (!pOp->isLifeOperation() && !(interface || pOp->getAbstract()) && !pOp->getStatic())
00170                 prototype = pOp->getStereotype() + ' ' + prototype;
00171         }
00172     }
00173 }
00174 
00175 #include "cppheadercodeoperation.moc"
KDE Logo
This file is part of the documentation for umbrello Version 3.1.0.
Documentation copyright © 1996-2004 the KDE developers.
Generated on Tue Jun 26 08:07:56 2007 by doxygen 1.4.1 written by Dimitri van Heesch, © 1997-2003