how to implement a template AVAILABLE OUTSIDE of the module !! (??)
In the following (still to be tested) I guess I have implemented a template AVAILABLE OUTSIDE of the module ?! That is a way to turn around the fact that C++ does not support it?
Please let me know what you think about it!! Mistakes, drawbacks, etc.
(of course i have cut out code which is not relevant for the discussion)
Well for sure it is not nice that I have to put in the header a different declaration for every needed argument type
Code:
Recipes.h:
EXTERN int LoadRecipe_Par(FILE * RecipeFile, etc., int * RecipePar);
EXTERN int LoadRecipe_Par(FILE * RecipeFile, etc., unsigned int * RecipePar);
EXTERN int LoadRecipe_Par(FILE * RecipeFile, etc., float * RecipePar);
Recipes.cpp:
int Cabled_sscanf(char * LoadString, char * VarName, int * RecipePar)
{
return(sscanf(LoadString,"%s \t# \t%d",VarName, RecipePar));
}
int Cabled_sscanf(char * LoadString, char * VarName,unsigned int * RecipePar)
{
return(sscanf(LoadString,"%s \t# \t%u",VarName, RecipePar));
}
int Cabled_sscanf(char * LoadString, char * VarName, float * RecipePar)
{
return(sscanf(LoadString,"%s \t# \t%f",VarName, RecipePar));
}
template <class Type_Par> int LoadRecipePar(FILE * RecipeFile, int * BreakAll,int BreakAllAllowed, char * LoadString, char * VarName, Type_Par * RecipePar)
{
int res;
if ( GetNextRecipeLine(RecipeFile, BreakAll, BreakAllAllowed, LoadString) )
{
fclose(RecipeFile);
return -1; //fine file inaspettata
}
if ( (strstr(LoadString, "#HMI")==NULL) && (strstr(LoadString, "# HMI")==NULL) && (strstr(LoadString, "# HMI")==NULL) )
{
res=Cabled_sscanf(LoadString, VarName, RecipePar);
if(res!=2)
{
fclose(RecipeFile);
printf("Problems reading Recipe at or after %hs\n",VarName);
return -1;
}
}
return(0);
}
//This way I basically have a template available outside (which is not supported by standard C++)
int LoadRecipe_Par(FILE * RecipeFile, int * BreakAll,int BreakAllAllowed, char * LoadString, char * VarName, int * RecipePar)
{
LoadRecipePar(RecipeFile, BreakAll, BreakAllAllowed, LoadString, VarName, RecipePar);
}
int LoadRecipe_Par(FILE * RecipeFile, int * BreakAll,int BreakAllAllowed, char * LoadString, char * VarName, unsigned int * RecipePar)
{
LoadRecipePar(RecipeFile, BreakAll, BreakAllAllowed, LoadString, VarName, RecipePar);
}
int LoadRecipe_Par(FILE * RecipeFile, int * BreakAll,int BreakAllAllowed, char * LoadString, char * VarName, float * RecipePar)
{
LoadRecipePar(RecipeFile, BreakAll, BreakAllAllowed, LoadString, VarName, RecipePar);
}