c++ - Get list of functions in a namespace at runtime? -


is possible list of functions in namespace or functions in program @ runtime?

i have function pointer map , need add commands on own it, thought: why not create namespace , let program work @ runtime?

something like(pseudocode):

typedef bool (*command)(void); namespace commands {     bool start(void)     {         return true;     }     bool end(void)     {         return true;     } }; std::map<std::string,command> commandmap; main() {     for(each function in namespace commands)     {         commandmap[std::string(function_name)] = function;     }     commandmap["start"]();     commandmap["end"]();     return 0; } 

instead of

std::map<std::string,command> commandmap; main() {     commandmap["start"] = commands::start;     commandmap["end"] = commands::end;     //list of thousands of other commands......     commandmap["start"]();     commandmap["end"]();     return 0; } 

is possible achieve in c++ or c++11? or alternatives goal?

consider like:

class commandcollection {    ...    void register_command(command*, string);    map<string, command*> m_command_map; }  class command {    ...    virtual do_command(...) = 0; }  class eachcommand : public command {    eachcommand() { commandcollection::instance().register_command(this, my_name); }    ...    virtual do_command(...); }  eachcommand each_command_inst; 

the command base class has virtual command. each derived type implements command (you try overloading () operator make them more functions). each derived command registers commandcollection, can known in central location. if want associate commands string (seems if user typing them in), key in map.


Comments

Popular posts from this blog

c# - Operator '==' incompatible with operand types 'Guid' and 'Guid' using DynamicExpression.ParseLambda<T, bool> -