saveClassConfig.cpp 13.5 KB
Newer Older
Pierre Aubert's avatar
Pierre Aubert committed
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21

/***************************************
	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){
Pierre Aubert's avatar
Pierre Aubert committed
22 23 24 25 26 27
	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 = '"<<varType<<"', isSimpleType = " << isSimpleType << ", isPtr = " << isPtr << std::endl;
Pierre Aubert's avatar
Pierre Aubert committed
28 29
	
	if(isSimpleType || isPtr){
Pierre Aubert's avatar
Pierre Aubert committed
30
		if(isConst && !isSetter && (!isSimpleType || isPtr)) varTypeName += "const ";
Pierre Aubert's avatar
Pierre Aubert committed
31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345
		varTypeName += varType;
		if(isRef && !isPtr) varTypeName += " &";
	}else{
		if(isConst) varTypeName += "const ";
		varTypeName += varType + " &";
	}
	
// 	if(isPtr){
// 		varTypeName += varType;
// 		if(isRef) varTypeName += " &";
// 	}else{
// 		if(isConst) varTypeName += "const ";
// 		varTypeName += varType + " &";
// 	}
	return varTypeName;
}

///Creates a function decl for setters
/**	@param varType : type of the var to set
 * 	@param varName : name of the var to set
 * 	@param className : name of the class
 * 	@return setters function decl
*/
std::string createSetterDecl(const std::string & varType, const std::string & varName, const std::string & className){
	string functionDecl("void ");
	if(className != "") functionDecl += className + "::";
	functionDecl += "set" + firstToUpper(varName) + "(";
	functionDecl += makeVarType(varType, true, true, false);
	functionDecl += " " + varName + ")";
	return functionDecl;
}

///Creates a function decl for setters
/**	@param varType : type of the var to set
 * 	@param varName : name of the var to set
 * 	@param className : name of the class
 * 	@param isConst : true if the function must be const
 * 	@return setters function decl
*/
std::string createGetterDecl(const std::string & varType, const std::string & varName, const std::string & className, bool isConst){
	string functionDecl("");
	functionDecl += makeVarType(varType, false, isConst, !isConst) + " ";
	if(className != "") functionDecl += className + "::";
	functionDecl += "get" + firstToUpper(varName) + "()";
	return functionDecl;
}

///Creates setters header file
/**	@param[out] fs : header file name
 * 	@param classConfig : class config we want to save
 * 	@return true on success, false otherwise
*/
void saveDeclSetters(std::ofstream & fs, const PClassConfig & classConfig){
	const std::list<PClassAttribute> & listAttr(classConfig.getListAttribute());
	for(std::list<PClassAttribute>::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<PClassAttribute> & listAttr(classConfig.getListAttribute());
	for(std::list<PClassAttribute>::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<std::string>::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<PClassAttribute> & listAttr(classConfig.getListAttribute());
	for(std::list<PClassAttribute>::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<std::string>::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<PClassAttribute> & listAttr(classConfig.getListAttribute());
	for(std::list<PClassAttribute>::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<PClassAttribute> & listAttr(classConfig.getListAttribute());
	for(std::list<PClassAttribute>::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<PClassAttribute> & listAttr(classConfig.getListAttribute());
	for(std::list<PClassAttribute>::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<PClassConfig> & classConfig, const std::string & headerFile, const std::list<std::string> & 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<std::string>::const_iterator it(listInclude.begin()); it != listInclude.end(); ++it){
			fs << "#include " << *it << endl;
		}
		fs << endl;
	}
	for(std::list<PClassConfig>::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<PClassConfig> & 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<PClassConfig>::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<PClassConfig> & classConfig, const std::string & baseFileName, const std::list<std::string> & listInclude){
	if(baseFileName == "") return false;
	if(!saveClassDecl(classConfig, baseFileName + ".h", listInclude)) return false;
	if(!saveClassImpl(classConfig, baseFileName + ".cpp", baseFileName + ".h")) return false;
	return true;
}