Thread: how to implement a template AVAILABLE OUTSIDE of the module !! (??)

Threaded View

Previous Post Previous Post   Next Post Next Post
  1. #1
    Alessio Stella
    Join Date
    May 2008
    Location
    Italy, Bologna
    Posts
    251

    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);
    }
    Last edited by mynickmynick; 08-20-2008 at 11:53 AM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Screwy Linker Error - VC2005
    By Tonto in forum C++ Programming
    Replies: 5
    Last Post: 06-19-2007, 02:39 PM
  2. error: template with C linkage
    By michaels-r in forum C++ Programming
    Replies: 3
    Last Post: 05-17-2006, 08:11 AM
  3. include question
    By Wanted420 in forum C++ Programming
    Replies: 8
    Last Post: 10-17-2003, 03:49 AM
  4. oh me oh my hash maps up the wazoo
    By DarkDays in forum C++ Programming
    Replies: 5
    Last Post: 11-30-2001, 12:54 PM
  5. Function basics
    By sjleonard in forum C++ Programming
    Replies: 15
    Last Post: 11-21-2001, 12:02 PM