#include<stdio.h>
#include<stdlib.h>
#include<string.h>
struct templateStructure
{
signed int mcsi_row;
signed int mcsi_col;
signed int mcsi_type;
char *mcpc_ptr;
//void (*init)(templateStructure *,const char*,const int,const int);
void (*mcfn_get)(struct templateStructure *,const int ,const int,void *);
void (*mcfn_set)(struct templateStructure *,const int,const int,const void *);
};
typedef struct templateStructure template_t;
typedef template_t* template_p;
void fnG_set(template_p StL_obj,const int iL_row,const int iL_col,const void *val)
{
char *pvL_tmpPtr;
pvL_tmpPtr =StL_obj->mcpc_ptr + (((StL_obj->mcsi_col*StL_obj->mcsi_type)*iL_row)+(iL_col*StL_obj->mcsi_type));
memcpy(pvL_tmpPtr,val,StL_obj->mcsi_type);
}
void fnG_get(template_p StL_obj,const int iL_row,const int iL_col,void *val)
{
char *pvL_tmpPtr;
pvL_tmpPtr =StL_obj->mcpc_ptr + ((((StL_obj->mcsi_col)*(StL_obj->mcsi_type))*(iL_row))+((iL_col)*(StL_obj->mcsi_type)));
memcpy(val,pvL_tmpPtr,StL_obj->mcsi_type);
}
int fnG_initTemplate(template_p StL_obj,const char *pcL_type,const int iL_row,const int iL_col)
{
StL_obj->mcsi_row=iL_row;
StL_obj->mcsi_col=iL_col;
if(strcmp(pcL_type,"int")==0)
{
StL_obj->mcsi_type=sizeof(int);
StL_obj->mcpc_ptr=(char *)malloc((sizeof(int)*iL_row)*iL_col);
}
else if(strcmp(pcL_type,"char")==0)
{
StL_obj->mcsi_type=sizeof(char);
StL_obj->mcpc_ptr=(char *)malloc((sizeof(char)*iL_row)*iL_col);
}
else if(strcmp(pcL_type,"float")==0)
{
StL_obj->mcsi_type=sizeof(float);
StL_obj->mcpc_ptr=(char *)malloc((sizeof(float)*iL_row)*iL_col);
}
StL_obj->mcfn_get=fnG_get;
StL_obj->mcfn_set=fnG_set;
}
int main(int argc,char* argv[],char* envp[])
{
char val='a';
char val1='z';
// int val=1000;
// int val1=999;
template_t myObj;
printf("val=%c and val1=%c\n",val,val1);
//printf("val=%d and val1=%d\n",val,val1);
fnG_initTemplate(&myObj,"char",4,3);//use to define different type at run time
//fnG_initTemplate(&myObj,"int",4,3);//use to define different type at run time
myObj.mcfn_set(&myObj,0,3,&val1);
printf("val=%c and val1=%c \n",val,val1);
myObj.mcfn_get(&myObj,0,3,&val);
printf("val=%c and val1=%c \n",val,val1);
//printf("val=%d and val1=%d\n",val,val1);
return 0;
}
Wednesday, 1 May 2013
A Template Class In C
Subscribe to:
Post Comments (Atom)
No comments:
Post a Comment