Jump to content
  • Advertisement
Sign in to follow this  
Sfpiano

Turning text into a function call

This topic is 4657 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

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.

Share this post


Link to post
Share on other sites
Advertisement

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.

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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<const char*, ternary_function, string_compare> 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<std::string, ternary_function> 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.

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

Participate in the game development conversation and more when you create an account on GameDev.net!

Sign me up!