backend.cpp 22.8 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
///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;
}

Pierre Aubert's avatar
Pierre Aubert committed
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
///Save the header of the given PTable
/**	@param table : PTable to be used
 * 	@return corresponding string
*/
std::string ph5_backendTableHeaderSetter(const PTable & table){
	std::string body("");
	const PVecAttribute & vecAttriute = table.getVecAttribute();
	for(PVecAttribute::const_iterator it(vecAttriute.begin()); it != vecAttriute.end(); ++it){
		std::string type(it->getType());
		if(ph5_attributeIsTensor(*it)){
			body += "\t\tvoid set" + firstToUpper(it->getName()) + "(size_t i, const "+type+" * tabVal);\n";
		}else{
			body += "\t\tvoid set" + firstToUpper(it->getName()) + "(size_t i, "+type+" val);\n";
		}
	}
	return body;
}

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


Pierre Aubert's avatar
Pierre Aubert committed
65 66 67 68 69
///Save the header of the given PTable
/**	@param table : PTable to be used
 * 	@return corresponding string
*/
std::string ph5_backendTableHeaderOffset(const PTable & table){
Pierre Aubert's avatar
Pierre Aubert committed
70
	std::string body("");
Pierre Aubert's avatar
Pierre Aubert committed
71 72 73 74 75 76
	const PVecAttribute & vecAttriute = table.getVecAttribute();
	for(PVecAttribute::const_iterator it(vecAttriute.begin()); it != vecAttriute.end(); ++it){
		body += "\t\tstatic size_t getOffset" + firstToUpper(it->getName()) + "();\n";
	}
	return body;
}
Pierre Aubert's avatar
Pierre Aubert committed
77 78 79 80 81
///Save the header of the given PTable
/**	@param table : PTable to be used
 * 	@return corresponding string
*/
std::string ph5_backendTableHeaderCompType(const PTable & table){
Pierre Aubert's avatar
Pierre Aubert committed
82
	std::string body("\t\tstatic H5::CompType getCompTypeAll();\n");
Pierre Aubert's avatar
Pierre Aubert committed
83 84
	const PVecAttribute & vecAttriute = table.getVecAttribute();
	for(PVecAttribute::const_iterator it(vecAttriute.begin()); it != vecAttriute.end(); ++it){
Pierre Aubert's avatar
Pierre Aubert committed
85 86 87 88 89 90 91 92 93 94 95 96 97 98
		body += "\t\tstatic H5::CompType getCompType" + 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_backendTableHeaderDataType(const PTable & table){
	std::string body("");
	const PVecAttribute & vecAttriute = table.getVecAttribute();
	for(PVecAttribute::const_iterator it(vecAttriute.begin()); it != vecAttriute.end(); ++it){
		body += "\t\tstatic H5::DataType getType" + firstToUpper(it->getName()) + "();\n";
Pierre Aubert's avatar
Pierre Aubert committed
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
	}
	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";
	
Pierre Aubert's avatar
Pierre Aubert committed
143 144 145
	body += ph5_backendTableHeaderSetter(table);
	body += ph5_backendTableHeaderGetter(table);
	body += "\n";
Pierre Aubert's avatar
Pierre Aubert committed
146
	body += ph5_backendTableHeaderOffset(table);
Pierre Aubert's avatar
Pierre Aubert committed
147
	body += ph5_backendTableHeaderCompType(table);
Pierre Aubert's avatar
Pierre Aubert committed
148
	body += ph5_backendTableHeaderDataType(table);
Pierre Aubert's avatar
Pierre Aubert committed
149 150
	
	body += "\tprivate:\n";
Pierre Aubert's avatar
Pierre Aubert committed
151 152
	body += "\t\tvoid readDataSet(const H5::DataSet & dataset);\n";
	body += "\t\tvoid writeDataSet(H5::DataSet & dataset) const;\n";
Pierre Aubert's avatar
Pierre Aubert committed
153
	body += "\t\tvoid allocate(size_t nbRow);\n";
Pierre Aubert's avatar
Pierre Aubert committed
154 155 156 157 158 159 160 161 162 163 164 165 166 167
	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());
Pierre Aubert's avatar
Pierre Aubert committed
168
	std::string baseMacro("__" + strToUpper(eraseExtension(source.getName())) + "_H__");
Pierre Aubert's avatar
Pierre Aubert committed
169 170 171 172 173 174 175 176 177 178 179 180
	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;
}

Pierre Aubert's avatar
Pierre Aubert committed
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
///Save the source of the given PTable
/**	@param table : PTable to be used
 * 	@return corresponding string
*/
std::string ph5_backendTableSourceResize(const PTable & table){
	std::string body(""), name(table.getName());
	body += "///Resize the table " + name + "\n";
	body += "/*\t@param nbRow : new number of rows of the Table\n";
	body += "*/\n";
	body += "void " + name + "::resize(size_t nbRow){\n";
	body += "\tif(nbRow == p_nbRow){return;}	//Nothing to do\n";
	body += "\tclear()\n";
	body += "\tallocate(nbRow);\n";
	body += "}\n\n";
	
	return body;
}

///Save the source of the given PTable
/**	@param table : PTable to be used
 * 	@return corresponding string
*/
std::string ph5_backendTableSourceClear(const PTable & table){
	std::string body(""), name(table.getName());
	body += "///Clear the table " + name + " (delete all column)\n";
	body += "void " + name + "::clear(){\n";
	const PVecAttribute & vecAttriute = table.getVecAttribute();
	for(PVecAttribute::const_iterator it(vecAttriute.begin()); it != vecAttriute.end(); ++it){
		body += "\tif(p_"+it->getName()+" != NULL){delete [] p_"+it->getName()+";p_"+it->getName()+" = NULL;}\n";
	}
	body += "}\n\n";
	
	return body;
}

Pierre Aubert's avatar
Pierre Aubert committed
216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238
///Save the source of the given PTable
/**	@param table : PTable to be used
 * 	@return corresponding string
*/
std::string ph5_backendTableSourceRead(const PTable & table){
	std::string body(""), name(table.getName());
	body += "///Read the table " + name + " from given file\n";
	body += "/**\t@param file : HDF5 file to be used\n";
	body += "*/\n";
	body += "void " + name + "::read(const H5::H5File & file){\n";
	body += "\tH5::DataSet dataset = file.openDataSet(\""+table.getDataSetName()+"\");\n";
	body += "\treadDataSet(dataset);\n";
	body += "}\n\n";
	
	body += "///Read the table " + name + " from given group\n";
	body += "/**\t@param group : HDF5 group to be used\n";
	body += "*/\n";
	body += "void " + name + "::read(const H5::Group & group){\n";
	body += "\tH5::DataSet dataset = group.openDataSet(\""+table.getDataSetName()+"\");\n";
	body += "\treadDataSet(dataset);\n";
	body += "}\n\n";
	return body;
}
Pierre Aubert's avatar
Pierre Aubert committed
239

Pierre Aubert's avatar
Pierre Aubert committed
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
///Save the source of the given PTable
/**	@param table : PTable to be used
 * 	@return corresponding string
*/
std::string ph5_backendTableSourceWrite(const PTable & table){
	std::string body(""), name(table.getName());
	body += "///Create and write the table " + name + " in given file\n";
	body += "/**\t@param file : HDF5 file to be used\n";
	body += "*/\n";
	body += "void " + name + "::write(const H5::H5File & file) const{\n";
	body += "\thsize_t dim[1];\n";
	body += "\tdim[0] = p_nbRow;\n";
	body += "\tH5::DataSpace space(1, dim);\n";
	body += "\tH5::DataSet dataset = file.createDataSet(\""+table.getDataSetName()+"\", " + name + "::getCompTypeAll(), space);\n";
	body += "\twriteDataSet(dataset);\n";
	body += "}\n\n";
	
	body += "///Create and write the table " + name + " in given file\n";
	body += "/**\t@param group : HDF5 group to be used\n";
	body += "*/\n";
	body += "void " + name + "::write(const H5::Group & group) const{\n";
	body += "\thsize_t dim[1];\n";
	body += "\tdim[0] = p_nbRow;\n";
	body += "\tH5::DataSpace space(1, dim);\n";
	body += "\tH5::DataSet dataset = group.createDataSet(\""+table.getDataSetName()+"\", " + name + "::getCompTypeAll(), space);\n";
	body += "\twriteDataSet(dataset);\n";
	body += "}\n\n";
	return body;
}
Pierre Aubert's avatar
Pierre Aubert committed
269

Pierre Aubert's avatar
Pierre Aubert committed
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
///Save the header of the given PTable
/**	@param table : PTable to be used
 * 	@return corresponding string
*/
std::string ph5_backendTableSourceSetter(const PTable & table){
	std::string body(""), name(table.getName());
	const PVecAttribute & vecAttriute = table.getVecAttribute();
	for(PVecAttribute::const_iterator it(vecAttriute.begin()); it != vecAttriute.end(); ++it){
		std::string type(it->getType());
		body += "///Set the attribute "+it->getName()+" (column "+it->getColName()+")\n";
		if(ph5_attributeIsTensor(*it)){
			body += "/**\t@param i : index of the row to be used\n";
			body += " * \t@param tabVal : table of value to be copied\n";
			body += "*/\n";
			body += "void " + name + "::set" + firstToUpper(it->getName()) + "(size_t i, const "+type+" * tabVal){\n";
			body += "\tsize_t sizeRow("+ph5_getFullAttributeElement(*it)+");\n";
			body += "\tmemcpy(p_"+it->getName()+" + i*sizeRow, tabVal, sizeRow);\n";
			body += "}\n\n";
		}else{
			body += "/**\t@param i : index of the row to be used\n";
			body += " * \t@param val : value to be copied\n";
			body += "*/\n";
			body += "void " + name + "::set" + firstToUpper(it->getName()) + "(size_t i, "+type+" val){\n";
			body += "\tp_"+it->getName()+"[i] = val;\n";
			body += "}\n\n";
		}
	}
	return body;
}

///Save the header of the given PTable
/**	@param table : PTable to be used
 * 	@return corresponding string
*/
std::string ph5_backendTableSourceGetter(const PTable & table){
	std::string body(""), name(table.getName());
	const PVecAttribute & vecAttriute = table.getVecAttribute();
	for(PVecAttribute::const_iterator it(vecAttriute.begin()); it != vecAttriute.end(); ++it){
		std::string type(it->getType());
		if(ph5_attributeIsTensor(*it)){
			std::string docString("");
			docString += "///Get the tensor i of the attribute "+it->getName()+" (column "+it->getColName()+")\n";
			docString += "/**\t@param i : index of the row to be used\n";
			docString += " * \t@return pointer to the corresponding tensor\n";
			docString += "*/\n";
			body += docString;
			std::string sizeElement(ph5_getFullAttributeElement(*it));
			body += "const "+type+" * " + name + "::get" + firstToUpper(it->getName()) + "(size_t i) const{\n";
			body += "return p_"+it->getName()+" + i*"+sizeElement+";\n";
			body += "}\n\n";
			body += docString;
			body += type+" * " + name + "::get" + firstToUpper(it->getName()) + "(size_t i){\n";
			body += "return p_"+it->getName()+" + i**"+sizeElement+";\n";
			body += "}\n\n";
		}else{
			std::string docString("");
			docString += "///Get the value i of the attribute "+it->getName()+" (column "+it->getColName()+")\n";
			docString += "/**\t@param i : index of the row to be used\n";
			docString += " * \t@return corresponding value\n";
			docString += "*/\n";
			body += docString;
			body += type+" " + name + "::get" + firstToUpper(it->getName()) + "(size_t i) const{\n";
			body += "\treturn p_"+it->getName()+"[i];\n";
			body += "}\n\n";
			body += docString;
			body += type+" & " + name + "::get" + firstToUpper(it->getName()) + "(size_t i){\n";
			body += "\treturn p_"+it->getName()+"[i];\n";
			body += "}\n\n";
		}
	}
	return body;
}

Pierre Aubert's avatar
Pierre Aubert committed
343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392
///Get the size of a column
/**	@param attr : PAttribute to be used
 * 	@return corresponding string
*/
std::string ph5_getSizeOfCol(const PAttribute & attr){
	std::string body("sizeof("+attr.getType()+")");
	const PVecDim & vecDim = attr.getVecDim();
	for(PVecDim::const_iterator it(vecDim.begin()); it != vecDim.end(); ++it){
		body += "*" + ph5_dimentionToStr(*it);
	}
	return body;
}

///Save the header of the given PTable
/**	@param table : PTable to be used
 * 	@return corresponding string
*/
std::string ph5_backendTableSourceOffset(const PTable & table){
	std::string body(""), name(table.getName()), prevAttribute(""), prevAttributeSize("");
	const PVecAttribute & vecAttriute = table.getVecAttribute();
	for(PVecAttribute::const_iterator it(vecAttriute.begin()); it != vecAttriute.end(); ++it){
		body += "///Get the offset of the attribute " + it->getName() + "\n";
		body += "/**\t@return offset of the attribute in bytes\n";
		body += "*/\n";
		body += "size_t " + name + "::getOffset" + firstToUpper(it->getName()) + "(){\n" ;
		if(prevAttribute == "" && prevAttributeSize == ""){
			body += "\treturn 0lu;\n";
		}else{
			body += "\treturn " + name + "::getOffset" + firstToUpper(prevAttribute) + "() + " + prevAttributeSize + ";\n";
		}
		body += "}\n\n";
		prevAttribute = it->getName();
		prevAttributeSize = ph5_getSizeOfCol(*it);
	}
	return body;
}

///Save the header of the given PTable
/**	@param table : PTable to be used
 * 	@return corresponding string
*/
std::string ph5_backendTableSourceCompType(const PTable & table){
	std::string body(""), name(table.getName());
	const PVecAttribute & vecAttriute = table.getVecAttribute();
	body += "H5::CompType "+name+"::getCompTypeAll(){\n";
	body += "\tsize_t sizeAll(";
	bool addPlus(false);
	for(PVecAttribute::const_iterator it(vecAttriute.begin()); it != vecAttriute.end(); ++it){
		if(addPlus){body += " + ";}
		body += ph5_getSizeOfCol(*it);
Pierre Aubert's avatar
Pierre Aubert committed
393
		addPlus = true;
Pierre Aubert's avatar
Pierre Aubert committed
394 395 396 397
	}
	body += ");\n";
	body += "\tH5::CompType typeCol(sizeAll);\n";
	for(PVecAttribute::const_iterator it(vecAttriute.begin()); it != vecAttriute.end(); ++it){
Pierre Aubert's avatar
Pierre Aubert committed
398
		body += "\ttypeCol.insertMember(\""+it->getColName()+"\", " + name + "::getOffset" + firstToUpper(it->getName()) + "(), "+
Pierre Aubert's avatar
Pierre Aubert committed
399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445
				name+"::getType" + firstToUpper(it->getName()) + "());\n";
	}
	body += "\treturn typeCol;\n";
	body += "}\n\n";
	
	for(PVecAttribute::const_iterator it(vecAttriute.begin()); it != vecAttriute.end(); ++it){
		body += "///Get DataType of attribute "+it->getName() +" (column " + it->getColName() + ")\n";
		body += "/**\t@return DataType of the attribute "+it->getName() +"\n";
		body += "*/\n";
		body += "H5::CompType "+name+"::getCompType" + firstToUpper(it->getName()) + "(){\n";
		body += "\tH5::CompType typeCol("+ph5_getSizeOfCol(*it)+");\n";
		body += "\tdatasetTypeX.insertMember(\""+it->getColName()+"\", 0, "+name+"::getType" + firstToUpper(it->getName()) + "());\n";
		body += "\treturn typeCol;\n";
		body += "}\n\n";
	}
	return body;
}

///Convert a C type into an HDF5 type
/**	@param type : C type to be converted
 * 	@return HDF5 type
*/
std::string ph5_cTypeToHDF5(const std::string & type){
	if(type == "char"){return "H5::PredType::NATIVE_INT8";}
	else if(type == "unsinged char"){return "H5::PredType::NATIVE_UINT8";}
	else if(type == "short"){return "H5::PredType::NATIVE_INT16";}
	else if(type == "unsigned short"){return "H5::PredType::NATIVE_UINT16";}
	else if(type == "int"){return "H5::PredType::NATIVE_INT";}
	else if(type == "unsigned int"){return "H5::PredType::NATIVE_UINT";}
	else if(type == "float"){return "H5::PredType::NATIVE_FLOAT";}
	else if(type == "double"){return "H5::PredType::NATIVE_DOUBLE";}
	return "";
}

///Get the corresponding HDF5 type of the given PAttribute
/**	@param attr : PAttribute to be used
 * 	@return corresponding HDF5 type
*/
std::string ph5_getTypeHDF5(const PAttribute & attr){
	std::string body("");
	if(ph5_attributeIsTensor(attr)){
		const PVecDim & vecDim = attr.getVecDim();
		body += "\thsize_t dims["+convertToString(vecDim.size())+"];\n";
		size_t i(0lu);
		for(PVecDim::const_iterator it(vecDim.begin()); it != vecDim.end(); ++it){
			body += "\tdims["+convertToString(i)+"] = "+ ph5_dimentionToStr(*it) +";\n";
		}
Pierre Aubert's avatar
Pierre Aubert committed
446 447
		body += "\tH5::ArrayType arrayType("+ph5_cTypeToHDF5(attr.getType())+", "+convertToString(vecDim.size())+", dims);\n";
		body += "\treturn arrayType;\n";
Pierre Aubert's avatar
Pierre Aubert committed
448
	}else{
Pierre Aubert's avatar
Pierre Aubert committed
449
		body += "\treturn " + ph5_cTypeToHDF5(attr.getType()) + ";\n";
Pierre Aubert's avatar
Pierre Aubert committed
450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471
	}
	return body;
}

///Save the header of the given PTable
/**	@param table : PTable to be used
 * 	@return corresponding string
*/
std::string ph5_backendTableSourceDataType(const PTable & table){
	std::string body(""), name(table.getName());
	const PVecAttribute & vecAttriute = table.getVecAttribute();
	for(PVecAttribute::const_iterator it(vecAttriute.begin()); it != vecAttriute.end(); ++it){
		body += "///Get DataType of attribute "+it->getName() +" (column " + it->getColName() + ")\n";
		body += "/**\t@return DataType of the attribute "+it->getName() +"\n";
		body += "*/\n";
		body += "H5::DataType "+name+"::getType" + firstToUpper(it->getName()) + "(){\n";
		body += ph5_getTypeHDF5(*it);
		body += "}\n\n";
	}
	return body;
}

Pierre Aubert's avatar
Pierre Aubert committed
472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510
///Save the header of the given PTable
/**	@param table : PTable to be used
 * 	@return corresponding string
*/
std::string ph5_backendTableSourceReadDataSet(const PTable & table){
	std::string body(""), name(table.getName());
	body += "///Read the given DataSet and fill the Table with it\n";
	body += "/**\t@param dataset : dataset to be used\n";
	body += "*/\n";
	body += "void "+name+"::readDataSet(const H5::DataSet & dataset){\n";
	body += "\tH5::DataSpace dataSpace = dataset.getSpace();\n";
	body += "\tsize_t nbEntries(dataSpace.getSimpleExtentNpoints());\n";
	body += "\tresize(nbEntries);\n";
	const PVecAttribute & vecAttriute = table.getVecAttribute();
	for(PVecAttribute::const_iterator it(vecAttriute.begin()); it != vecAttriute.end(); ++it){
		body += "\tdataset.read(p_"+it->getName()+", "+name+"::getCompType" + firstToUpper(it->getName()) + "());\n";
	}
	body += "}\n\n";
	return body;
}

///Save the header of the given PTable
/**	@param table : PTable to be used
 * 	@return corresponding string
*/
std::string ph5_backendTableSourceWriteDataSet(const PTable & table){
	std::string body(""), name(table.getName());
	body += "///Write the given DataSet and fill the Table with it\n";
	body += "/**\t@param[out] dataset : dataset to be modified\n";
	body += "*/\n";
	body += "void "+name+"::writeDataSet(H5::DataSet & dataset) const{\n";
	const PVecAttribute & vecAttriute = table.getVecAttribute();
	for(PVecAttribute::const_iterator it(vecAttriute.begin()); it != vecAttriute.end(); ++it){
		body += "\tdataset.write(p_"+it->getName()+", "+name+"::getCompType" + firstToUpper(it->getName()) + "());\n";
	}
	body += "}\n\n";
	return body;
}

Pierre Aubert's avatar
Pierre Aubert committed
511 512 513 514 515 516 517 518 519 520 521 522 523
///Save the source of the given PTable
/**	@param table : PTable to be used
 * 	@return corresponding string
*/
std::string ph5_backendTableSourceAllocate(const PTable & table){
	std::string body(""), name(table.getName());
	body += "///Allocate the table " + name + " (delete all column)\n";
	body += "/*\t@param nbRow : new number of rows of the Table\n";
	body += "*/\n";
	body += "void " + name + "::allocate(size_t nbRow){\n";
	body += "\tp_nbRow = nbRow;\n";
	const PVecAttribute & vecAttriute = table.getVecAttribute();
	for(PVecAttribute::const_iterator it(vecAttriute.begin()); it != vecAttriute.end(); ++it){
Pierre Aubert's avatar
Pierre Aubert committed
524
		body += "\tp_"+it->getName()+" = new "+it->getType()+"[p_nbRow];\n";
Pierre Aubert's avatar
Pierre Aubert committed
525 526 527 528 529
	}
	body += "}\n\n";
	return body;
}

Pierre Aubert's avatar
Pierre Aubert committed
530 531 532 533 534 535 536 537 538
///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";
Pierre Aubert's avatar
Pierre Aubert committed
539 540 541 542
	const PVecAttribute & vecAttriute = table.getVecAttribute();
	for(PVecAttribute::const_iterator it(vecAttriute.begin()); it != vecAttriute.end(); ++it){
		body += "\tp_"+it->getName()+" = NULL;\n";
	}
Pierre Aubert's avatar
Pierre Aubert committed
543 544 545
	body += "}\n\n";
	body += "///Destructor of the class " + name + "\n";
	body += name + "::~" + name + "(){\n";
Pierre Aubert's avatar
Pierre Aubert committed
546
	body += "\tclear();\n";
Pierre Aubert's avatar
Pierre Aubert committed
547 548
	body += "}\n\n";
	
Pierre Aubert's avatar
Pierre Aubert committed
549 550 551
	body += ph5_backendTableSourceResize(table);
	body += ph5_backendTableSourceClear(table);
	
Pierre Aubert's avatar
Pierre Aubert committed
552 553 554
	body += ph5_backendTableSourceRead(table);
	body += ph5_backendTableSourceWrite(table);
	
Pierre Aubert's avatar
Pierre Aubert committed
555 556 557
	body += ph5_backendTableSourceSetter(table);
	body += ph5_backendTableSourceGetter(table);
	
Pierre Aubert's avatar
Pierre Aubert committed
558 559 560
	body += ph5_backendTableSourceOffset(table);
	body += ph5_backendTableSourceCompType(table);
	body += ph5_backendTableSourceDataType(table);
Pierre Aubert's avatar
Pierre Aubert committed
561
	
Pierre Aubert's avatar
Pierre Aubert committed
562 563 564
	body += ph5_backendTableSourceReadDataSet(table);
	body += ph5_backendTableSourceWriteDataSet(table);
	
Pierre Aubert's avatar
Pierre Aubert committed
565
	body += ph5_backendTableSourceAllocate(table);
Pierre Aubert's avatar
Pierre Aubert committed
566 567
	return body;
}
Pierre Aubert's avatar
Pierre Aubert committed
568

Pierre Aubert's avatar
Pierre Aubert committed
569 570 571 572 573 574
///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());
Pierre Aubert's avatar
Pierre Aubert committed
575
	body += "#include <string.h>\n";
Pierre Aubert's avatar
Pierre Aubert committed
576
	body += "#include \""+eraseExtension(source.getName())+".h\"\n\n";
Pierre Aubert's avatar
Pierre Aubert committed
577 578 579 580 581 582
	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
583

Pierre Aubert's avatar
Pierre Aubert committed
584 585 586 587 588 589 590 591
///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));
Pierre Aubert's avatar
Pierre Aubert committed
592 593 594 595
	std::string baseFileName(eraseExtension(source.getName()));
	std::string outputHeaderFile(outputDir + "/" + baseFileName + ".h");
	std::string outputSourceFile(outputDir + "/" + baseFileName + ".cpp");
	if(!saveFileContent(outputHeaderFile, headerSrc)){
Pierre Aubert's avatar
Pierre Aubert committed
596 597 598
		std::cerr << "ph5_backend : cannot save header file '"<<outputHeaderFile<<"'" << std::endl;
		return false;
	}
Pierre Aubert's avatar
Pierre Aubert committed
599
	if(!saveFileContent(outputSourceFile, sourceSrc)){
Pierre Aubert's avatar
Pierre Aubert committed
600 601 602 603 604
		std::cerr << "ph5_backend : cannot save source file '"<<outputSourceFile<<"'" << std::endl;
		return false;
	}
	return true;
}
Pierre Aubert's avatar
Pierre Aubert committed
605

Pierre Aubert's avatar
Pierre Aubert committed
606 607 608 609 610 611 612 613 614 615 616 617
///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
618