/** * Write the header file for this classifier. */ void CppWriter::writeHeaderFile (UMLClassifier *c, QFile &file) { // open stream for writing QTextStream h (&file); // up the indent level to one m_indentLevel = 1; // write header blurb QString str = getHeadingFile(".h"); if (!str.isEmpty()) { str.replace(QRegExp("%filename%"), fileName_ + ".h"); str.replace(QRegExp("%filepath%"), file.fileName()); h << str<< m_endl; } // Write the hash define stuff to prevent multiple parsing/inclusion of header QString hashDefine = className_.toUpper().simplified().replace(QRegExp(" "), "_"); writeBlankLine(h); h << "#ifndef "<< hashDefine + "_H" << m_endl; h << "#define "<< hashDefine + "_H" << m_endl; writeClassDecl(c, h); // last thing..close our hashdefine h << m_endl << "#endif // " << hashDefine + "_H" << m_endl; }
/** * Call this method to generate d code for a UMLClassifier. * @param c the class to generate code for */ void DWriter::writeClass(UMLClassifier *c) { if (!c) { uDebug() << "Cannot write class of NULL concept!"; return; } isInterface = c->isInterface(); QString fileName = cleanName(c->name().toLower()); //find an appropriate name for our file fileName = findFileName(c, QLatin1String(".d")); if (fileName.isEmpty()) { emit codeGenerated(c, false); return; } // check that we may open that file for writing QFile file; if (!openFile(file, fileName)) { emit codeGenerated(c, false); return; } // open text stream to file QTextStream d(&file); //try to find a heading file (license, coments, etc) QString str; str = getHeadingFile(QLatin1String(".d")); if (!str.isEmpty()) { str.replace(QRegExp(QLatin1String("%filename%")), fileName); str.replace(QRegExp(QLatin1String("%filepath%")), file.fileName()); d<<str<<m_endl; } // source file begins with the module declaration writeModuleDecl(c, d); // imports writeModuleImports(c, d); // write the opening declaration for the class incl any documentation, // interfaces and/or inheritence issues we have writeClassDecl(c, d); // start body of class d << " {" << m_endl; // Preparations // // sort attributes by Scope UMLAttributeList atl; UMLAttributeList atpub, atprot, atpriv, atpkg, atexport; UMLAttributeList final_atpub, final_atprot, final_atpriv, final_atpkg, final_atexport; if (!isInterface) { UMLAttributeList atl = c->getAttributeList(); foreach (UMLAttribute* at, atl) { switch(at->visibility()) { case Uml::Visibility::Public: if (at->isStatic()) final_atpub.append(at); else atpub.append(at); break; case Uml::Visibility::Protected: if (at->isStatic()) final_atprot.append(at); else atprot.append(at); break; case Uml::Visibility::Private: if (at->isStatic()) final_atpriv.append(at); else atpriv.append(at); break;/* TODO: requires support from the gui & other structures case Uml::Visibility::Package: if (at->getStatic()) final_atpkg.append(at); else atpkg.append(at); break; case Uml::Visibility::Export: if (at->getStatic()) final_atexport.append(at); else atexport.append(at); break;*/ default: break; } } }
/** * Write the source code body file for this classifier. */ void CppWriter::writeSourceFile(UMLClassifier *c, QFile &file) { // open stream for writing QTextStream cpp (&file); // set the starting indentation at zero m_indentLevel = 0; //try to find a heading file (license, coments, etc) QString str; str = getHeadingFile(".cpp"); if (!str.isEmpty()) { str.replace(QRegExp("%filename%"), fileName_ + ".cpp"); str.replace(QRegExp("%filepath%"), file.fileName()); cpp << str << m_endl; } // IMPORT statements // Q: Why all utils? Isnt just List and Vector the only classes we are using? // Our import *should* also look at operations, and check that objects being // used arent in another package (and thus need to be explicitly imported here). cpp << "#include \"" << className_ << ".h\"" << m_endl; writeBlankLine(cpp); if (c->visibility() == Uml::Visibility::Implementation) { writeClassDecl(c, cpp); } // Start body of class // Constructors: anything we more we need to do here ? // if (!c->isInterface()) writeConstructorMethods(c, cpp); // METHODS // // write comment for section IF needed QString indnt = indent(); if (forceDoc() || c->hasAccessorMethods() || c->hasOperationMethods()) { writeComment(" ", indnt, cpp); writeComment("Methods", indnt, cpp); writeComment(" ", indnt, cpp); writeBlankLine(cpp); writeBlankLine(cpp); } // write comment for sub-section IF needed if (forceDoc() || c->hasAccessorMethods() ) { writeComment("Accessor methods", indnt, cpp); writeComment(" ", indnt, cpp); writeBlankLine(cpp); } // Accessor methods for attributes const bool bInlineAccessors = policyExt()->getAccessorsAreInline(); if (!bInlineAccessors && c->hasAttributes()) { writeAttributeMethods(c->getAttributeListStatic(Uml::Visibility::Public), Uml::Visibility::Public, false, true, !bInlineAccessors, cpp); writeAttributeMethods(c->getAttributeList(Uml::Visibility::Public), Uml::Visibility::Public, false, false, !bInlineAccessors, cpp); writeAttributeMethods(c->getAttributeListStatic(Uml::Visibility::Protected), Uml::Visibility::Protected, false, true, !bInlineAccessors, cpp); writeAttributeMethods(c->getAttributeList(Uml::Visibility::Protected), Uml::Visibility::Protected, false, false, !bInlineAccessors, cpp); writeAttributeMethods(c->getAttributeListStatic(Uml::Visibility::Private), Uml::Visibility::Private, false, true, !bInlineAccessors, cpp); writeAttributeMethods(c->getAttributeList(Uml::Visibility::Private), Uml::Visibility::Private, false, false, !bInlineAccessors, cpp); } // accessor methods for associations // public writeAssociationMethods(c->getSpecificAssocs(Uml::at_Association), Uml::Visibility::Public, false, !INLINE_ASSOCIATION_METHODS, true, c->id(), cpp); writeAssociationMethods(c->getUniAssociationToBeImplemented(), Uml::Visibility::Public, false, !INLINE_ASSOCIATION_METHODS, true, c->id(), cpp); writeAssociationMethods(c->getAggregations(), Uml::Visibility::Public, false, !INLINE_ASSOCIATION_METHODS, true, c->id(), cpp); writeAssociationMethods(c->getCompositions(), Uml::Visibility::Public, false, !INLINE_ASSOCIATION_METHODS, true, c->id(), cpp); // protected writeAssociationMethods(c->getSpecificAssocs(Uml::at_Association), Uml::Visibility::Protected, false, !INLINE_ASSOCIATION_METHODS, true, c->id(), cpp); writeAssociationMethods(c->getUniAssociationToBeImplemented(), Uml::Visibility::Protected, false, !INLINE_ASSOCIATION_METHODS, true, c->id(), cpp); writeAssociationMethods(c->getAggregations(), Uml::Visibility::Protected, false, !INLINE_ASSOCIATION_METHODS, true, c->id(), cpp); writeAssociationMethods(c->getCompositions(), Uml::Visibility::Protected, false, !INLINE_ASSOCIATION_METHODS, true, c->id(), cpp); // private writeAssociationMethods(c->getSpecificAssocs(Uml::at_Association), Uml::Visibility::Private, false, !INLINE_ASSOCIATION_METHODS, true, c->id(), cpp); writeAssociationMethods(c->getUniAssociationToBeImplemented(), Uml::Visibility::Private, false, !INLINE_ASSOCIATION_METHODS, true, c->id(), cpp); writeAssociationMethods(c->getAggregations(), Uml::Visibility::Private, false, !INLINE_ASSOCIATION_METHODS, true, c->id(), cpp); writeAssociationMethods(c->getCompositions(), Uml::Visibility::Private, false, !INLINE_ASSOCIATION_METHODS, true, c->id(), cpp); writeBlankLine(cpp); // Other operation methods -- all other operations are now written // // write comment for sub-section IF needed if (forceDoc() || c->hasOperationMethods()) { writeComment("Other methods", indnt, cpp); writeComment(" ", indnt, cpp); writeBlankLine(cpp); } if (!policyExt()->getOperationsAreInline()) { writeOperations(c,false,Uml::Visibility::Public,cpp); writeOperations(c,false,Uml::Visibility::Protected,cpp); writeOperations(c,false,Uml::Visibility::Private,cpp); } // Yep, bringing up the back of the bus, our initialization method for attributes writeInitAttributeMethod(c, cpp); writeBlankLine(cpp); }