C++ turning "APP.QUIT()" (a string) into actual code
#1 Members - Reputation: 433
Posted 17 February 2012 - 05:19 PM
"app.quit()"
and in game make the game quit. I started now by writing
if(ClassDepth[0]=="app")
{
if(ClassDepth[1]=="quit"){app.quit();}
}
but it will take a long time to re write everything. is there any way to make this easier?
#2 Senior Moderators - Reputation: 3113
Posted 17 February 2012 - 05:28 PM
In time the project grows, the ignorance of its devs it shows, with many a convoluted function, it plunges into deep compunction, the price of failure is high, Washu's mirth is nigh.
ScapeCode - Blog | SlimDX
#5 Members - Reputation: 3696
Posted 17 February 2012 - 08:06 PM
The voices in my head may not be real, but they have some good ideas!
#6 Members - Reputation: 2042
Posted 18 February 2012 - 12:45 AM
struct action : uncopyable
{
action() { }
virtual ~action() { }
virtual void invoke() = 0;
};
struct quit_action : action
{
quit_action(Application &app) : app(app) { }
virtual void invoke() { app.quit(); }
Application &app;
};
// ...
std::map<std::string, shared_ptr<action> > actions;
actions["quit"].reset(new quit_action(the_app));
// ...
bool do_action(const std::string &name)
{
std::map<std::string, shared_ptr<action> >::iterator f = actions.find(name);
if (f != actions.end())
{
(*f)->invoke();
return true;
}
return false;
}
If your actions need parameters, you might pass those via a stack, once parsed out of the command string, and each action's invoke() method would attempt to pop them as needed before use (with appropriate checking for underflow).
I'd also agree that Lua is a good idea in principle, but it might actually be more work to do it that way, especially to someone that's relatively new to C++. On the other hand, using a proper scripting language opens up your code to all kinds of other possibilities...
#7 Moderator* - Reputation: 5362
Posted 18 February 2012 - 02:40 AM
[edit]
Google's protocol buffers (protobuf) apparently does RPC too.
#9 Moderator* - Reputation: 5362
Posted 18 February 2012 - 03:02 PM
You can't. C++ doesn't support reflection.You could use reflection. It turns strings into real code in runtime. Don´t know how to do this in c++. Java has its own class for this.
#10 Members - Reputation: 391
Posted 20 February 2012 - 08:56 AM
You can't. C++ doesn't support reflection.
You could use reflection. It turns strings into real code in runtime. Don´t know how to do this in c++. Java has its own class for this.
Didnt knew that
Oh, i forgot java/c# gets interpreted and c++ is an executeable.
#11 Members - Reputation: 2369
Posted 20 February 2012 - 09:18 AM
The distinction isn't all that useful, both Java and C# VMs run mostly native code, same as C++ binary. They just defer the compilation until code runs. And even then it's possible to force most compilation upfront.Oh, i forgot java/c# gets interpreted and c++ is an executeable.
On Android, Java is statically compiled, exactly the same way as C++.
On most modern x86 CPUs even native code is "interpreted", at very least the instructions that CPU executes are defined via microcode rather than being implemented directly.
Didnt knew that
Is there no way to do this?
Of course there is, it's just a bit of work. Structure data must either be manually defined or somehow generated by compiler or some other tool.
C++ doesn't offer reflection out-of-box due to optimization. A compiler is free to remove anything it deems redundant, including structures and functions. Full reflection would require preserving even unused parts. Templates in particular cause unacceptable overhead without such optimization.
#12 Members - Reputation: 391
Posted 21 February 2012 - 02:21 PM
The distinction isn't all that useful, both Java and C# VMs run mostly native code, same as C++ binary. They just defer the compilation until code runs. And even then it's possible to force most compilation upfront.
Oh, i forgot java/c# gets interpreted and c++ is an executeable.
On Android, Java is statically compiled, exactly the same way as C++.
On most modern x86 CPUs even native code is "interpreted", at very least the instructions that CPU executes are defined via microcode rather than being implemented directly.Didnt knew that
Is there no way to do this?
Of course there is, it's just a bit of work. Structure data must either be manually defined or somehow generated by compiler or some other tool.
C++ doesn't offer reflection out-of-box due to optimization. A compiler is free to remove anything it deems redundant, including structures and functions. Full reflection would require preserving even unused parts. Templates in particular cause unacceptable overhead without such optimization.
Really usefull knowledge
Thank you.
#13 Members - Reputation: 1149
Posted 21 February 2012 - 02:30 PM
What do you mean "exactly the same way as C++" ? On Android Java is compiled exactly same as for desktop - to bytecode. During runtime bytecodes gets interpreted and hotspots are JITed to native machine code. So that is not exactly same as C++, which gets compiled completely to machine code on development host.On Android, Java is statically compiled, exactly the same way as C++.
#14 Members - Reputation: 2369
Posted 21 February 2012 - 05:18 PM
My bad, I was thinking of something else.What do you mean "exactly the same way as C++" ? On Android Java is compiled exactly same as for desktop - to bytecode. During runtime bytecodes gets interpreted and hotspots are JITed to native machine code. So that is not exactly same as C++, which gets compiled completely to machine code on development host.On Android, Java is statically compiled, exactly the same way as C++.






