#include "StdAfx.h"
#include <GPUCVSwitch/Cl_GenSw_FctMngr.h>
namespace GCV{
using namespace std;
using namespace GCV;
#define GPUCV_SWITCH__LICENSE_FILE GetGpuCVSettings()->GetShaderPath() + std::string ("../etc/sed/license.h")
#define GPUCV_SWITCH__TOP_H_FILE GetGpuCVSettings()->GetShaderPath() + std::string ("../etc/sed/cv.h/cv.top.h")
#define GPUCV_SWITCH__TOP_CPP_FILE GetGpuCVSettings()->GetShaderPath() + std::string ("../etc/sed/cv.h/cv.top.cpp")
#define GPUCV_SWITCH__BOTTOM_H_FILE GetGpuCVSettings()->GetShaderPath() + std::string ("../etc/sed/cv.h/cv.bottom.h")
#define GPUCV_SWITCH__BOTTOM_CPP_FILE GetGpuCVSettings()->GetShaderPath() + std::string ("../etc/sed/cv.h/cv.bottom.cpp")
//======================================================
//======================================================
CL_GenSwFn_Mngr::CL_GenSwFn_Mngr(const std::string &_SrcFileName)
: SGE::CL_XML_MNGR<CL_GenSwFn, std::string>(NULL, "SwitchFctDefMngr", "FctDef", "id")
//SGE::CL_XML_BASE_OBJ<std::string>(_name, "FctDef")
,SGE::CL_XML_BASE_OBJ_FILE(_SrcFileName, "SwitchFctFile")
{
}
//======================================================
CL_GenSwFn_Mngr::~CL_GenSwFn_Mngr()
{
}
//======================================================
size_t CL_GenSwFn_Mngr::ParseFile()
{
SGE::CL_BASE_OBJ_FILE::ParseFileName();
//base tests
SG_Assert(GetFullPath()!="", "No input file set");
SG_AssertFile(SGE::CL_BASE_OBJ_FILE::Exists(),GetFullPath(), "File does not exist");
//Open input file
ifstream inputFile;
inputFile.open(GetFullPath().data(), ifstream::in);
std::string line, swfns;
SG_AssertFile(inputFile.is_open(),GetFullPath(), "Could not open file");
//parse file for functions
CL_GenSwFn *LocalFctGen = NULL;
std::string SpareLine;
size_t iPos=0;
do
{
//we check that we do not have several function definition on the same line
if(SpareLine=="")
getline (inputFile,line);
else
{
getline (inputFile,line);
line = SpareLine + line;
SpareLine="";
}
iPos=line.find_first_of(";");
if(iPos==std::string::npos)
{//we grab also next line
std::string nextline ;
getline (inputFile,nextline);
line+=nextline;
}
//get only one function definition
if(iPos!=std::string::npos)
{//we have a ';'
if(iPos!=line.size()-1)
{//it is not the end of the line
line = line.substr(0, iPos);
SpareLine = line.substr(iPos, line.size());
}
}
//=====================
if(line=="")
continue;
if(!line.empty())
{
LocalFctGen = new CL_GenSwFn("");
if(LocalFctGen->ParseLine(line))
{
if(Find(LocalFctGen->GetID()))
{
GPUCV_WARNING("A function has been found twice in the header file: \n "<< LocalFctGen->GetID());
delete LocalFctGen;
continue;
}
else if (LocalFctGen->GetID().substr(0, 2) != "cv")
{
GPUCV_WARNING("A function has not a cv* like name, exiting wrapping: \n "<< LocalFctGen->GetID());
delete LocalFctGen;
continue;
}
AddObj(LocalFctGen);
LocalFctGen->ParseArgsType();
}
else
{
GPUCV_ERROR("Error parsing line: \n=>" << line);
}
}
//if (this->GetCount()>10)
// break;//just for debugging....
}
while (!inputFile.eof());
inputFile.close();
return GetCount();
}
void CL_GenSwFn_Mngr::AppendFileToFnsFile(ofstream & fileswfns, std::string FileName)
{
SG_AssertFile(SGE::CL_BASE_OBJ_FILE::FileExists(FileName),FileName, "File does not exist");
fstream NewFile(FileName.data());
char str[2000];
while (!NewFile.eof())
{
NewFile.getline(str, 2000);
fileswfns << str << std::endl;
}
NewFile.close();
}
void CL_GenSwFn_Mngr::AddObjsToFns_H(std::string Outfilename) //as argument
{
GPUCV_NOTICE("\n\tGenerating "<<Outfilename);
//SG_Assert(CL_GenSwFn::GetFctObjMngr()->GetCount()==0, "CL_GenSwFn::AddObjsToFns_H() must be called after funtcion CL_GenSwFn::AddObjsToFns_CPP()");
ifstream funcfile;
ofstream fileswfns;
string FileOut = Outfilename;
fileswfns.open(FileOut.data());
std::string swfns;
SGE::CL_BASE_OBJ_FILE outFile(Outfilename);
std::string BaseFileName = outFile.GetFileName();
std::string HeaderFileMacro = "__";
HeaderFileMacro += BaseFileName;
HeaderFileMacro += "_H";
HeaderFileMacro = SGE::StringToUpper(HeaderFileMacro );
//Create an output file
AppendFileToFnsFile(fileswfns, GPUCV_SWITCH__LICENSE_FILE);
AppendFileToFnsFile(fileswfns, GPUCV_SWITCH__TOP_H_FILE);
swfns = "#ifndef ";
swfns += HeaderFileMacro;
swfns += "\n#define ";
swfns += HeaderFileMacro;
swfns += "\n";
fileswfns << swfns ;
swfns="";
fileswfns << "\n#include <" << BaseFileName<< "/config.h>\n";
int FctNbr =0;
CL_GenSwFn *LocalFctGen = NULL;
std::string SpareLine;
//export macro
std::string ExportStr = "_";
ExportStr += SGE::StringToUpper(BaseFileName);
ExportStr += "_EXPORT_C ";
std::string ExportStrCPP = "_";
ExportStrCPP += SGE::StringToUpper(BaseFileName);
ExportStrCPP += "_EXPORT ";
int iPos=0;
fileswfns << "#ifdef __cplusplus\n";
fileswfns << ExportStrCPP << " void cvg_"<< BaseFileName<<"_RegisterTracerSingletons(SG_TRC::TTCL_APPLI_TRACER<SG_TRC::SG_TRC_Default_Trc_Type> * _pAppliTracer, SG_TRC::CL_TRACING_EVENT_LIST *_pEventList);\n";
fileswfns << "#endif\n";
//start iterating functions
iterator itFunct;
for(itFunct=GetFirstIter();
itFunct!=GetLastIter();
itFunct++)
{
//if(!itFunct)
// continue;
swfns += ExportStr;
swfns += (*itFunct).second->GenSwFnDeclaration(FILETYPE_H);
}
fileswfns << swfns;
fileswfns << "/*........End Declaration.............*/" << "\n";
AppendFileToFnsFile(fileswfns, GPUCV_SWITCH__BOTTOM_H_FILE);
swfns = "\n#endif //";
swfns += HeaderFileMacro;
fileswfns << swfns;
fileswfns.close();
}
//==============================================================
void CL_GenSwFn_Mngr::AddObjsToFns_H_WRAPPER(std::string Outfilename)
{
GPUCV_NOTICE("\n\tGenerating "<< Outfilename);
//SG_Assert(CL_GenSwFn::GetFctObjMngr()->GetCount()==0, "CL_GenSwFn::AddObjsToFns_H() must be called after funtcion CL_GenSwFn::AddObjsToFns_CPP()");
ifstream funcfile;
ofstream fileswfns;
fileswfns.open(Outfilename.data());
std::string swfns;
SGE::CL_BASE_OBJ_FILE outFile(Outfilename);
std::string BaseFileName = outFile.GetFileName();
std::string HeaderFileMacro = "__";
HeaderFileMacro += BaseFileName;
HeaderFileMacro += "_H";
HeaderFileMacro = SGE::StringToUpper(HeaderFileMacro );
//Create an output file
AppendFileToFnsFile(fileswfns, GPUCV_SWITCH__LICENSE_FILE);
AppendFileToFnsFile(fileswfns, GPUCV_SWITCH__TOP_H_FILE);
int FctNbr =0;
CL_GenSwFn *LocalFctGen = NULL;
std::string SpareLine;
int iPos=0;;
SGE::CL_TEMPLATE_OBJECT_MANAGER<CL_GenSwFn, std::string>::iterator itFunct;
swfns += "#ifndef ";
swfns += HeaderFileMacro;
swfns += "\n#define ";
swfns += HeaderFileMacro;
swfns += "\n";
fileswfns << swfns;
//include file containing the cvgws* functions...
swfns = "\n\n#include <";
swfns += BaseFileName;
swfns += "/";
swfns += BaseFileName;
swfns += ".h";
swfns += ">\n";
fileswfns << swfns;
std::string BaseName;
std::string NewName;
size_t Start=0;
for(itFunct=GetFirstIter();
itFunct!=GetLastIter();
itFunct++)
{
//get function names
BaseName = (*itFunct).second->GetIDStr();
NewName = _GCV_SWITCH_FCT_PREFIX;//"cvgSw";
NewName += (*itFunct).second->GetFnShortName();
//write definitions
//swfns ="\n#ifdef ";
//swfns += BaseName;
//swfns +="\n#undef " ;
//swfns += BaseName;
swfns ="\n#define ";
swfns += BaseName;
swfns +="\t ";
swfns += NewName;
//swfns +="\n;//#endif\n";
fileswfns << swfns;
}
fileswfns << "\n/*........End Declaration.............*/" << "\n";
AppendFileToFnsFile(fileswfns, GPUCV_SWITCH__BOTTOM_H_FILE);
swfns = "\n#endif //";
swfns += HeaderFileMacro;
fileswfns << swfns;
fileswfns.close();
}
//==============================================================
void CL_GenSwFn_Mngr::AddObjsToFns_CPP(std::string Outfilename)
{
GPUCV_NOTICE("\n\tGenerating " <<Outfilename);
GPUCV_NOTICE("\tFrom "<< GetFullName());
//Create an output file
SGE::CL_BASE_OBJ_FILE outFile(Outfilename);
std::string BaseFileName = outFile.GetFileName();
ofstream outputFile;
string FileOut = Outfilename;
outputFile.open(FileOut.data());
SG_AssertFile(outputFile.is_open(), FileOut, "Could not open file for writing");;
//write common parts
AppendFileToFnsFile(outputFile, GPUCV_SWITCH__LICENSE_FILE);
AppendFileToFnsFile(outputFile, GPUCV_SWITCH__TOP_CPP_FILE);
outputFile << "\n#include <" << BaseFileName << "/" << BaseFileName << ".h>\n";
outputFile << "#include <GPUCVSwitch/switch.h>";
//add a first common function to register profiling singletons
outputFile << std::endl << "/*====================================*/" << std::endl;
outputFile << "void cvg_" << BaseFileName << "_RegisterTracerSingletons(SG_TRC::TTCL_APPLI_TRACER<SG_TRC::SG_TRC_Default_Trc_Type> * _pAppliTracer, SG_TRC::CL_TRACING_EVENT_LIST *_pEventList)\n";
outputFile << "{\nSG_TRC::TTCL_APPLI_TRACER<SG_TRC::SG_TRC_Default_Trc_Type>::Instance().RegisterNewSingleton(_pAppliTracer);\n";
outputFile << "SG_TRC::CL_TRACING_EVENT_LIST::Instance().RegisterNewSingleton(_pEventList);\n";
outputFile << "}\n/*====================================*/\n";
//add all functions definitions
int FctNbr =0;
CL_GenSwFn *LocalFctGen = NULL;
std::string SpareLine, swfns;
size_t iPos=0;
iterator iterFct;
CL_GenSwFn * pCurrentFct=NULL;
for (iterFct = GetFirstIter(); iterFct != GetLastIter(); iterFct++)
{
pCurrentFct = (*iterFct).second;
if(!pCurrentFct)
{
GPUCV_ERROR("Empty function object");;
continue;
}
if (pCurrentFct->GetID().substr(0, 2) != "cv")
{
GPUCV_WARNING("A function has not a cv* like name, exiting wrapping: \n "<< pCurrentFct->GetID());
//delete LocalFctGen;//?.?
continue;
}
//call all generate Funct and concat result into a string
swfns = "\n/*====================================*/\n";
swfns += pCurrentFct->GenSwFnDeclaration(FILETYPE_CPP);
swfns += "{\n";
if(pCurrentFct->GetSrc_Arr().empty()
&& pCurrentFct->GetDst_Arr().empty()
&& !pCurrentFct->GetRequireSwitching()
)
{//no input or output images, no need to switch!
GPUCV_NOTICE("Function do not have input image:" << pCurrentFct->GetID());
GPUCV_NOTICE("Function do not have output image:" << pCurrentFct->GetID());
swfns += pCurrentFct->GenNoSwitch();
}
else
{//we switch
swfns += pCurrentFct->GetSwFnType();
swfns += pCurrentFct->GenSwchFnName();
swfns += pCurrentFct->GenSwchInOutArr();
swfns += pCurrentFct->GenStrtRunStop();
}
swfns += "}\n";
outputFile << swfns << "\n";
//if (this->GetCount()>10)
// break;//just for debugging....
}
outputFile << "/*........End Code.............*/" << "\n";
AppendFileToFnsFile(outputFile, GPUCV_SWITCH__BOTTOM_CPP_FILE);
outputFile.close();
}
//======================================================
TiXmlElement* CL_GenSwFn_Mngr::XMLLoad(TiXmlElement* _XML_Root)
{
SG_Assert(_XML_Root, "No XML root");
//todo..add fields
return SGE::CL_XML_MNGR<CL_GenSwFn, std::string>::XMLLoad(_XML_Root);
}
//======================================================
TiXmlElement* CL_GenSwFn_Mngr::XMLSave(TiXmlElement* _XML_Root)
{
SG_Assert(_XML_Root, "No XML root");
//todo..add fields
return SGE::CL_XML_MNGR<CL_GenSwFn, std::string>::XMLSave(_XML_Root);
}
//======================================================
//======================================================
TiXmlElement* CL_GenSwFn_Mngr::XMLLoadFromFile (std::string _fileName)
{
//todo..add fields
return SGE::CL_XML_MNGR<CL_GenSwFn, std::string>::XMLLoadFromFile(_fileName);
}
//======================================================
TiXmlDocument* CL_GenSwFn_Mngr::XMLSaveToFile (std::string _fileName)
{
//todo..add fields
return SGE::CL_XML_MNGR<CL_GenSwFn, std::string>::XMLSaveToFile(_fileName);
}
}//namespace GCV
|