Skip to main content
gamedev.net
🔒 Locked

Turning text into a function call

Started by Sfpiano Sep 23, 2005 at 1:59 PM 2 replies 850+ views
Original Post
Sfpiano
Sfpiano
I'm using C++ and am working on a debugging add-on for my program. I have an object, that has an object, that has a couple of classes, that has ... and I need a way to show these things in the debugger in realtime. I was wondering if maybe there was some macro trickery I could utilise; something like: #define TRIFUNCT(x, i, y) ((x->triData.y)) ... char* data = "parentFace"; for(...) int value = TRIFUNCT(obj, i, data); if I wanted to display all of obj->triData.parentFace. I know this won't work, but maybe something like it. Either that or a something similar to how in Visual Studio when you type something in, it brings it up. I'm quite sure how to turn inputted text into a function call.
//------------------------------------------------------------------------------------------------------The great logician Bertrand Russell once claimed that he could prove anything if given that 1+1=1. So one day, some fool asked him, "Ok. Prove that you're the Pope." He thought for a while and proclaimed, "I am one. The Pope is one. Therefore, the Pope and I are one."
flangazor
flangazor
void print (int, int, int) {std::cout << "flan is also known as creme caramel." << std::endl;}typedef void (ternery_function) (int, int, int);std::map<const char*, ternery_function> string_to_ternery_function;string_to_ternery_function.insert("print", print);*(string_to_ternery_function.find("print").second)(1,2,3);

That's from memory. There may be some syntax juggling.
Sfpiano
Sfpiano
I'm trying to insert values into the map, but it keeps giving me an error saying: error C2436: 'second' : member function or nested class in constructor initializer list

I tried:
typedef map<const char*, ternery_function> MYMAP;string_to_ternery_function.insert(MYMAP::value_type("print", print));and:string_to_ternery_function.insert(make_pair("print", print)); 


but both give the same error.
//------------------------------------------------------------------------------------------------------The great logician Bertrand Russell once claimed that he could prove anything if given that 1+1=1. So one day, some fool asked him, "Ok. Prove that you're the Pope." He thought for a while and proclaimed, "I am one. The Pope is one. Therefore, the Pope and I are one."
ZQJ
ZQJ
Try:

typedef void (*ternary_function) (int,int,int);

I have no idea what that does without the * but that is correct for a function pointer. Also, you'll either have to override the comparison operator of the map using:

bool string_compare(const char *s1, const char *s2)
{
return (std::strcmp(s1, s2) > 0);
}

typedef std::map MYMAP;

(For this you have to handle allocation of the strings yourself - if they're in your data segment you'll be fine).

Or use std::string instead:

typedef std::map MYMAP;

One more thing that may come in handy:

#define INSERT_TERNARY_FUNCTION(name) string_to_ternary_function.insert(std::pair(#name, name))

That'll save you typing each function name out twice and avoid mistakes. You'll have to write std::pair(std::string(#name), name) if you use std::string though.

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.