I have something like below which will get repeated many times based on the function that get called
for eg
acceptfunction()
{
inserter["quantity"] = sdd.getfloat(quantity);
inserter["prodtype"] = sdd.getstring(prodtype);
:
:
so on
}
Like accept above there are 20 more functions(reject,cancel etc) which will do the similar thing.But the parameteres they insert can differ based on function called.
How can I automate this kind of code.So that I dont need to write new function from scratch.Basically what I need is if i provide parametres like ("quantity",prodtype) through some text file or xml, it should generate the required function with the input parametres.
Is this task can be handled through C++ tempalte Meta programming or someother code generation tool will help me to do this?
It's ugly, but you can use a preprocessor macro:
#define FUNCTION_MACRO(NAME, ATTRIB1, ATTRIB2)\
void NAME()\
{\
inserter[#ATTRIB1] = sdd.getfloat(ATTRIB1);\
inserter[#ATTRIB2] = sdd.getstring(ATTRIB2);\
}
And then to create a function you just need to do:
FUNCTION_MACRO(accept_function, quantity, prodtype)
Well, when it comes down do it, you almost certainly could but it would require implementing an XML or text parser in TMP. Would be quite a feat.
That's not how things would normally be done, but you've not specified enough details to go into it further.
Macros are generally to be avoided, but this is a case where they are still useful. Something like
#define GET_FUNCTION(type, name) \
do \
{ \
inserter[#name] = sdd.get ## type(name); \
} \
while (0)
will let you say
void acceptfunction()
{
GET_FUNCTION(float, quantity);
GET_FUNCTION(string, prodtype);
// etc...
}
(The reason for the odd do-while construct is to guarantee that the result is a single statement.)
Define different macros for rejectfunction(), etc.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.