backend.cpp 5.5 KB
Newer Older
Pierre Aubert's avatar
Pierre Aubert committed
1 2 3 4 5 6
/***************************************
	Auteur : Pierre Aubert
	Mail : aubertp7@gmail.com
	Licence : CeCILL-C
****************************************/

Pierre Aubert's avatar
Pierre Aubert committed
7
#include "string_utils.h"
Pierre Aubert's avatar
Pierre Aubert committed
8 9
#include "backend.h"

Pierre Aubert's avatar
Pierre Aubert committed
10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 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
///Get the licence in string
/**	@return licence in string
*/
std::string ph5_licenceSaveStr(){
	std::string body("");
	body += "/***************************************\n";
	body += "\tAuteur : Pierre Aubert\n";
	body += "\tMail : aubertp7@gmail.com\n";
	body += "\tLicence : CeCILL-C\n";
	body += "****************************************/\n\n\n";
	
	body += "//Warning : this file has been generated automatically by the phoenix_hdf5 program\n";
	body += "//Do NOT modify it\n\n\n";
	return body;
}


///Save the header of the given PTable
/**	@param table : PTable to be used
 * 	@return corresponding string
*/
std::string ph5_backendTableHeaderCompType(const PTable & table){
	std::string body("\t\tstatic H5::CompType getTypeAll();\n");
	const PVecAttribute & vecAttriute = table.getVecAttribute();
	for(PVecAttribute::const_iterator it(vecAttriute.begin()); it != vecAttriute.end(); ++it){
		body += "\t\tstatic H5::CompType getType" + firstToUpper(it->getName()) + "();\n";
	}
	return body;
}

///Save the header of the given PTable
/**	@param table : PTable to be used
 * 	@return corresponding string
*/
std::string ph5_backendTableHeaderAttribute(const PTable & table){
	std::string body("");
	const PVecAttribute & vecAttriute = table.getVecAttribute();
	for(PVecAttribute::const_iterator it(vecAttriute.begin()); it != vecAttriute.end(); ++it){
		if(it->getDocString() != ""){
			body += "\t\t///" + it->getDocString() + "\n";
		}
		body += "\t\t"+it->getType()+" * p_"+it->getName()+";\n";
	}
	return body;
}

///Save the header of the given PTable
/**	@param table : PTable to be used
 * 	@return corresponding string
*/
std::string ph5_backendTableHeader(const PTable & table){
	std::string body("");
	if(table.getDocString() != ""){
		body += "///" + table.getDocString() + "\n";
	}
	std::string name(table.getName());
	body += "class " + name + "{\n";
	body += "\tpublic:\n";
	body += "\t\t" + name + "();\n";
	body += "\t\tvirtual ~" + name + "();\n";
	
	body += "\t\tvoid resize(size_t nbRow);\n";
	body += "\t\tvoid clear();\n\n";
	
	body += "\t\tvoid read(const H5::H5File & file);\n";
	body += "\t\tvoid read(const H5::Group & group);\n\n";
	
	body += "\t\tvoid write(H5::H5File & file) const;\n";
	body += "\t\tvoid write(H5::Group & group) const;\n\n";
	
	body += ph5_backendTableHeaderCompType(table);
	
	body += "\tprivate:\n";
	body += "\t\t///Number of rows in the table "+name+"\n";
	body += "\t\tsize_t p_nbRow;\n";
	body += ph5_backendTableHeaderAttribute(table);
	
	
	body += "};\n\n";
	return body;
}

///Save the header of the given PSource
/**	@param source : PSource to be used
 * 	@return corresponding string
*/
std::string ph5_backendHeader(const PSource & source){
	std::string body(ph5_licenceSaveStr());
	std::string baseMacro("__" + strToUpper(source.getName()) + "_H__");
	body += "#ifndef " + baseMacro + "\n";
	body += "#define " + baseMacro + "\n\n";
	body += "#include \"H5Cpp.h\"\n\n";
	
	const PVecTable & vecTable = source.getVecTable();
	for(PVecTable::const_iterator it(vecTable.begin()); it != vecTable.end(); ++it){
		body += ph5_backendTableHeader(*it);
	}
	body += "#endif\n\n";
	return body;
}

///Save the source of the given PTable
/**	@param table : PTable to be used
 * 	@return corresponding string
*/
std::string ph5_backendTableSource(const PTable & table){
	std::string body(""), name(table.getName());
	body += "///Constructor of the class " + name + "\n";
	body += name + "::" + name + "(){\n";
	body += "\tp_nbRow = 0lu;\n";
	body += "}\n\n";
	body += "///Destructor of the class " + name + "\n";
	body += name + "::~" + name + "(){\n";
	body += "\t\n";
	body += "}\n\n";
	
	return body;
}
Pierre Aubert's avatar
Pierre Aubert committed
128

Pierre Aubert's avatar
Pierre Aubert committed
129 130 131 132 133 134 135 136 137 138 139 140 141
///Save the source of the given PSource
/**	@param source : PSource to be used
 * 	@return corresponding string
*/
std::string ph5_backendSource(const PSource & source){
	std::string body(ph5_licenceSaveStr());
	body += "#include \""+source.getName()+".h\"\n\n";
	const PVecTable & vecTable = source.getVecTable();
	for(PVecTable::const_iterator it(vecTable.begin()); it != vecTable.end(); ++it){
		body += ph5_backendTableSource(*it);
	}
	return body;
}
Pierre Aubert's avatar
Pierre Aubert committed
142

Pierre Aubert's avatar
Pierre Aubert committed
143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163
///Save a vector of PSource in the output directory
/**	@param source : source to be saved
 * 	@param outputDir : output directory where to save PSource
 * 	@return true on success, false otherwise
*/
bool ph5_backend(const PSource & source, const std::string & outputDir){
	std::string headerSrc(ph5_backendHeader(source));
	std::string sourceSrc(ph5_backendSource(source));
	
	std::string outputHeaderFile(outputDir + "/" + source.getName() + ".h");
	std::string outputSourceFile(outputDir + "/" + source.getName() + ".cpp");
	if(saveFileContent(outputHeaderFile, headerSrc)){
		std::cerr << "ph5_backend : cannot save header file '"<<outputHeaderFile<<"'" << std::endl;
		return false;
	}
	if(saveFileContent(outputSourceFile, sourceSrc)){
		std::cerr << "ph5_backend : cannot save source file '"<<outputSourceFile<<"'" << std::endl;
		return false;
	}
	return true;
}
Pierre Aubert's avatar
Pierre Aubert committed
164

Pierre Aubert's avatar
Pierre Aubert committed
165 166 167 168 169 170 171 172 173 174 175 176
///Save a vector of PSource in the output directory
/**	@param vecSource : vector of source
 * 	@param outputDir : output directory where to save PSource
 * 	@return true on success, false otherwise
*/
bool ph5_backend(const PVecSource & vecSource, const std::string & outputDir){
	bool b(true);
	for(PVecSource::const_iterator it(vecSource.begin()); it != vecSource.end(); ++it){
		b &= ph5_backend(*it, outputDir);
	}
	return b;
}
Pierre Aubert's avatar
Pierre Aubert committed
177