/*************************************** Auteur : Pierre Aubert Mail : aubertp7@gmail.com Licence : CeCILL-C ****************************************/ #include "string_utils.h" #include "header_generator.h" #include "saveClassConfig.h" using namespace std; ///Makes the var type by taking account of the type /** @param varType : variable type name * @param isSetter : true if the type is made for a setter * @param isConst : true if the type must be const * @param isRef : true if the variable is a reference * @return var type by taking account of the type */ std::string makeVarType(const std::string & varType, bool isSetter, bool isConst, bool isRef){ std::string varTypeName(""); std::string restVarName(eraseCharsInStr(replaceStrInStr(varType, "unsigned", ""), " \n\t*&")); bool isPtr(findInString(varType, '*')); bool isSimpleType = (restVarName == "char" || restVarName == "short" || restVarName == "int" || restVarName == "float" || restVarName == "double" || restVarName == "bool" || restVarName == "long unsigned int" || restVarName == "size_t" || restVarName == "ssize_t"); std::cerr << "makeVarType : varType = '"< & listAttr(classConfig.getListAttribute()); for(std::list::const_iterator it(listAttr.begin()); it != listAttr.end(); ++it){ fs << "\t\t" << createSetterDecl(it->getType(), it->getName(), "") << ";" << endl; } } ///Creates getters header file /** @param[out] fs : header file name * @param classConfig : class config we want to save * @return true on success, false otherwise */ void saveDeclGetters(std::ofstream & fs, const PClassConfig & classConfig){ const std::list & listAttr(classConfig.getListAttribute()); for(std::list::const_iterator it(listAttr.begin()); it != listAttr.end(); ++it){ fs << "\t\t" << createGetterDecl(it->getType(), it->getName(), "", true) << " const;" << endl; fs << "\t\t" << createGetterDecl(it->getType(), it->getName(), "", false) << ";" << endl; } } ///Creates header file /** @param[out] fs : header file name * @param classConfig : class config we want to save * @return true on success, false otherwise */ void saveClassDecl(std::ofstream & fs, const PClassConfig & classConfig){ if(classConfig.getClassDocumentation() != "") fs << classConfig.getClassDocumentation() << endl; string name(classConfig.getName()); fs << "class " << name; if(classConfig.getListParentClass().size() != 0lu){ std::list::const_iterator it(classConfig.getListParentClass().begin()); fs << " : public " << *it; if(classConfig.getListParentClass().size() > 1lu){ while(it != classConfig.getListParentClass().end()){ fs << ", " << *it; ++it; } } } fs << "{" << endl; fs << "\tpublic:" << endl; fs << "\t\t" << name << "();" << endl; fs << "\t\t" << name << "(const " << name << " & other);" << endl; fs << "\t\tvirtual ~" << name << "();" << endl; fs << "\t\t" << name << " & operator = (const " << name << " & other);" << endl; saveDeclSetters(fs, classConfig); saveDeclGetters(fs, classConfig); fs << "\tprotected:" << endl; fs << "\t\tvoid copy" << name << "(const " << name << " & other);" << endl; fs << "\tprivate:" << endl; const std::list & listAttr(classConfig.getListAttribute()); for(std::list::const_iterator it(listAttr.begin()); it != listAttr.end(); ++it){ if(it->getDocumentation() != "") fs << "\t\t" << it->getDocumentation() << endl; fs << "\t\t" << it->getType() << " p_" << it->getName() << ";" << endl; } fs << "};" << endl << endl; } ///Saves constructor of the class /** @param[out] fs : header file name * @param classConfig : PClassConfig we vant to save */ void saveClassConstructorImpl(std::ofstream & fs, const PClassConfig & classConfig){ string name(classConfig.getName()); fs << "///Constructor of class " << name << endl; fs << name << "::" << name << "()"; if(classConfig.getListParentClass().size() != 0lu){ std::list::const_iterator it(classConfig.getListParentClass().begin()); fs << endl << "\t: " << *it << "()"; if(classConfig.getListParentClass().size() > 1lu){ while(it != classConfig.getListParentClass().end()){ fs << ", " << *it << "()"; ++it; } } fs << endl; } fs << "{" << endl; fs << "\t" << endl; fs << "}" << endl << endl; } ///Saves copy constructor of the class /** @param[out] fs : header file name * @param classConfig : PClassConfig we vant to save */ void saveClassCopyConstructorImpl(std::ofstream & fs, const PClassConfig & classConfig){ string name(classConfig.getName()); fs << "///Copy Constructor of class " << name << endl; fs << "/**\t@param other : " << name << " we want ot copy" << endl; fs << "*/" << endl; fs << name << "::" << name << "(const " << name << " & other){" << endl; fs << "\tcopy" << name << "(other);" << endl; fs << "}" << endl << endl; } ///Saves destructor of the class /** @param[out] fs : header file name * @param classConfig : PClassConfig we vant to save */ void saveClassDestructorImpl(std::ofstream & fs, const PClassConfig & classConfig){ string name(classConfig.getName()); fs << "///Destructor of class " << name << endl; fs << name << "::~" << name << "(){" << endl; fs << "\t" << endl; fs << "}" << endl << endl; } ///Saves = operator of the class /** @param[out] fs : header file name * @param classConfig : PClassConfig we vant to save */ void saveClassEqualOperatorImpl(std::ofstream & fs, const PClassConfig & classConfig){ string name(classConfig.getName()); fs << "///Operator = of class " << name << endl; fs << "/**\t@param other : " << name << " we want ot copy" << endl; fs << " * \t@return copied class " << name << endl; fs << "*/" << endl; fs << name << " & " << name << "::operator =" << " (const " << name << " & other){" << endl; fs << "\tcopy" << name << "(other);" << endl; fs << "\treturn *this;" << endl; fs << "}" << endl << endl; } ///Saves the class setters implementation /** @param[out] fs : header file name * @param classConfig : PClassConfig we vant to save */ void saveClassSettersImpl(std::ofstream & fs, const PClassConfig & classConfig){ const std::list & listAttr(classConfig.getListAttribute()); for(std::list::const_iterator it(listAttr.begin()); it != listAttr.end(); ++it){ fs << "///Sets the " << it->getName() << " of the " << classConfig.getName() << endl; fs << "/**\t@param " << it->getName() << " : " << it->getName() << " of the " << classConfig.getName() << endl; fs << "*/" << endl; fs << createSetterDecl(it->getType(), it->getName(), classConfig.getName()) << "{" << endl; fs << "\tp_" << it->getName() << " = " << it->getName() << ";" << endl; fs << "}" << endl << endl; } } ///Saves the class getters implementation /** @param[out] fs : header file name * @param classConfig : PClassConfig we vant to save */ void saveClassGettersImpl(std::ofstream & fs, const PClassConfig & classConfig){ const std::list & listAttr(classConfig.getListAttribute()); for(std::list::const_iterator it(listAttr.begin()); it != listAttr.end(); ++it){ fs << "///Gets the " << it->getName() << " of the " << classConfig.getName() << endl; fs << "/**\t@return " << it->getName() << " of the " << classConfig.getName() << endl; fs << "*/" << endl; fs << createGetterDecl(it->getType(), it->getName(), classConfig.getName(), true) << " const{" << endl; fs << "\treturn p_" << it->getName() << ";" << endl; fs << "}" << endl << endl; fs << "///Gets the " << it->getName() << " of the " << classConfig.getName() << endl; fs << "/**\t@return " << it->getName() << " of the " << classConfig.getName() << endl; fs << "*/" << endl; fs << createGetterDecl(it->getType(), it->getName(), classConfig.getName(), false) << "{" << endl; fs << "\treturn p_" << it->getName() << ";" << endl; fs << "}" << endl << endl; } } ///Saves the copy function of a class /** @param[out] fs : header file name * @param classConfig : PClassConfig we vant to save */ void saveClassCopyFunctionImpl(std::ofstream & fs, const PClassConfig & classConfig){ string name(classConfig.getName()); fs << "///Copy Function of class " << name << endl; fs << "/**\t@param other : " << name << " we want ot copy" << endl; fs << "*/" << endl; fs << "void " << name << "::copy" << name << "(const " << name << " & other){" << endl; const std::list & listAttr(classConfig.getListAttribute()); for(std::list::const_iterator it(listAttr.begin()); it != listAttr.end(); ++it){ fs << "\tp_" << it->getName() << " = other.p_" << it->getName() << ";" << endl; } fs << "}" << endl << endl; } ///Creates header file /** @param[out] fs : header file name * @param classConfig : PClassConfig we vant to save */ void saveClassImpl(std::ofstream & fs, const PClassConfig & classConfig){ saveClassConstructorImpl(fs, classConfig); saveClassCopyConstructorImpl(fs, classConfig); saveClassDestructorImpl(fs, classConfig); saveClassEqualOperatorImpl(fs, classConfig); saveClassSettersImpl(fs, classConfig); saveClassGettersImpl(fs, classConfig); saveClassCopyFunctionImpl(fs, classConfig); } ///Creates header file /** @param classConfig : class config we want to save * @param headerFile : header file name * @param listInclude : list of the include files * @return true on success, false otherwise */ bool saveClassDecl(const std::list & classConfig, const std::string & headerFile, const std::list & listInclude){ if(headerFile == "") return false; std::ofstream fs; fs.open(headerFile.c_str()); if(!fs.is_open()){ std::cerr << "saveClassDecl : can't open file '" << headerFile << "'" << std::endl; return false; } licenceSave(fs); string macroDef(makeMultiIncludeDefineMacro(headerFile)); fs << "#ifndef " << macroDef << endl; fs << "#define " << macroDef << endl << endl; if(listInclude.size() != 0lu){ for(std::list::const_iterator it(listInclude.begin()); it != listInclude.end(); ++it){ fs << "#include " << *it << endl; } fs << endl; } for(std::list::const_iterator it(classConfig.begin()); it != classConfig.end(); ++it){ saveClassDecl(fs, *it); } fs << endl << endl << "#endif" << endl << endl; fs.close(); return true; } ///Creates source file /** @param classConfig : class config we want to save * @param sourceFile : source file name * @param headerFile : header file name * @return true on success, false otherwise */ bool saveClassImpl(const std::list & classConfig, const std::string & sourceFile, const std::string & headerFile){ if(sourceFile == "" || headerFile == "") return false; std::ofstream fs; fs.open(sourceFile.c_str()); if(!fs.is_open()){ std::cerr << "saveClassImpl : can't open file '" << sourceFile << "'" << std::endl; return true; } licenceSave(fs); fs << endl << "#include \"" << headerFile << "\"" << endl << endl; for(std::list::const_iterator it(classConfig.begin()); it != classConfig.end(); ++it){ saveClassImpl(fs, *it); } fs.close(); return true; } ///Creates header file /** @param classConfig : class config we want to save * @param baseFileName : base file name for header or source file * @param listInclude : list of the include files * @return true on success, false otherwise */ bool saveClassImplDecl(const std::list & classConfig, const std::string & baseFileName, const std::list & listInclude){ if(baseFileName == "") return false; if(!saveClassDecl(classConfig, baseFileName + ".h", listInclude)) return false; if(!saveClassImpl(classConfig, baseFileName + ".cpp", baseFileName + ".h")) return false; return true; }