0

For my project I'm working on I want to display to the user from the command prompt all available function that are available to be used, now is there anyway of displaying these functions other than typing out each one

example:

Functions I want displayed

bool status();
double getTime();
void setTime(int h, int min, int sec);
double getDate();
void setDate(int d,int m,int y);
void setAuto();
void turnOn();
void turnOff();

These are declared in a class i've called Audio_Visual.h

//Have a small welcome text
cout << "Welcome to this Home Automation System" << endl;
cout << "Here Are a list of functions, please type what you want to do" << endl;
// DISPLAY FUNCTIONS

//display current function such as lights.setAuto();
string input = "";

//How to get a string/sentence with spaces
cout << "Please enter a valid function" << endl;
getline(cin, input);
cout << "You entered: " << input << endl << endl;

How I've written the command line stuff

Kara
  • 6,115
  • 16
  • 50
  • 57
Tomsta
  • 131
  • 1
  • 2
  • 5

4 Answers4

1

You can use a map to map function name to function pointer

typedef void ( Audio_Visual::* fv)();
std::map<std::string, fv> void_functions;

or

std::map<std::string, std::function<void(void)> > void_functions;

You need distinct map for distinct signatures: i.e.

typedef double ( Audio_Visual::* fd)();
std::map<std::string, fd> double_functions;

or

std::map<std::string, std::function<double(void)> > double_functions;

usage:

Audio_Visual a;
void_functions[ "turnOn"] = &Audio_Visual::turnOn;
std::cout << "Now we are turning on...";
(a.(*void_functions[ "turnOn"]))();

Here you can find more on the second option: Using generic std::function objects with member functions in one class

Community
  • 1
  • 1
4pie0
  • 29,204
  • 9
  • 82
  • 118
0

It is not easy to do this in C++ as it does not implement Reflection.

Such constructs exist in other languages (Java and C# for example).

Bathsheba
  • 231,907
  • 34
  • 361
  • 483
  • Well, he could use some freaking C macros, such as the `#` operator to stringfy the name of the function. But of course there is no way to iterate over the member functions stringfying them. – Manu343726 Apr 09 '14 at 13:11
  • I'd give serious thought to building a JNI. – Bathsheba Apr 09 '14 at 13:13
0

I suggest you make a table of <function name, function pointer>. This is commonly known as a look up table.

Search for the function name, then dereference the function pointer at the same location.

The drawback is that all the functions must have the same signature.

Another alternative is to use the Factory Design Pattern.

Thomas Matthews
  • 56,849
  • 17
  • 98
  • 154
  • So would a lookup table be used just to display the functions from the class? or could it be used to check to see whether the user has inputted a valid function – Tomsta Apr 09 '14 at 13:16
  • You would have to manually enter the function names into the table. And yes, if the User's text is not in the table, the function name is not valid. – Thomas Matthews Apr 09 '14 at 13:42
0

C++ doesn't support reflection, so there is no way to iterate over the member functions of a class.

However I suggest you to split the design in two things: "Actions" and the implementation itself.

Different actions do different things (i.e. call different functions of your implementation side). So an action is something which takes the necessary user input and calls the proper implementation function. Some simple example:

void get_status_action()
{
    //Just propmts the status:
    std::cout << impl.status();
}

void set_time_action()
{
    int hour , minute , second;

    //Get user input:
    std::cout << "Please enter the time: ";
    std::cin >> hour >> minute >> second;

    //Call the implementation:
    impl.steTime( hour , minute , second );
}

Now what you want is to store actions in a way the user could select them easily. Use a map from strings (The name of the action) to the actions. Note that in my example actions are just functions using a global implementation instance. You could write some complex action implementations, like functors, classes, etc).

int main()
{
    std::unordered_map<std::string,std::function<void()>> actions;

    actions["Set status"] = set_status_action;
    actions["Set current time"] = set_time_action;

    while( true )
    {
        //Propmt the actions:

        std::cout << "Select one of the following actions:" << std::endl;
        
        for( auto& pair : actions )
            std::cout << pair.first << std::endl;

        //Read the selection and execute the proper action:

        std::string selection;
        std::getline( std::cin , selection );

        actions[selection]();
    }
}
marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
Manu343726
  • 13,969
  • 4
  • 40
  • 75