Skip to content

Instantly share code, notes, and snippets.

@mkawserm
Last active January 23, 2023 23:02
Show Gist options
  • Save mkawserm/9259513 to your computer and use it in GitHub Desktop.
Save mkawserm/9259513 to your computer and use it in GitHub Desktop.
The code demonstrates how to use function pointer to call a method via string using c++
/* Program Name : cmdfy
* Developer : kawser
* Developer Blog: http://blog.kawser.org
* Blog Link: http://blog.kawser.org/2014/02/calling-cpp-function-fly.html
*
*
* Objective : This code demonstrates how to use function pointer
* to call a method via string
*
* IDEA : We will define some functions with similar signature and store these
* functions using function pointer into a map with string representation.
* Take input from user and parse the input and according to
* input we will call the mapped method
* Otherwise show a error.
*
*
* Prerequisites :map,vector,sstream
*
* Exampe : 2
*/
#include <map>
#include <utility>
#include <cstdio>
#include <vector>
#include <cstdlib>
#include <cstring>
#include <sstream>
#include <iostream>
using namespace std;
/* we will use the same signature for all functions */
/* signature : return type unsinged int and argument type unsigned int */
/* unsigned int limit: 0 to 65535 */
unsigned int square(unsigned int a){
/* calculate square of a number and returns */
return a*a;
}
unsigned int cube(unsigned int a){
/* calculate cube of a number and returns */
return a*a*a;
}
/*Renaming the signature*/
typedef unsigned int (*sig_ptr)(unsigned int);
void help(void){
cout << "Enter the following commands: without any space" <<endl;
cout <<" 1. square-number : to calcualte the square of the number"<<endl;
cout <<" 2. cube-number : to calcuale the cube of the number"<< endl;
cout <<" 3. -1 : to exit"<< endl<<endl;
}
/*this the the parser of the command*/
vector<string> &split(const string &s, char delim, vector<string> &elems) {
stringstream ss(s);
string item;
while (getline(ss, item, delim)) {
elems.push_back(item);
}
return elems;
}
vector<string> split(const string &s, char delim) {
vector<string> elems;
split(s, delim, elems);
return elems;
}
int main(void){
/* This is the Entry point of the program*/
map < string , sig_ptr> mapped_functions; // defining a map
mapped_functions["square"] = square; // adding square into the map
mapped_functions["cube"] = cube; // adding cube into the map
/*Enter into infinite loop*/
for(;;){
string cmd; //command string
help(); //show the help
cout<<endl;
cout << "Enter a command : ";
cin >> cmd ;
if( cmd != "-1" ) {
vector<string> cmds = split(cmd,'-');// parsing the command
if (cmds.size() == 2){
unsigned int n = atoi( cmds[1].c_str() );//converting the second
//argument into unsigned int
if( mapped_functions.find(cmds[0]) != mapped_functions.end() )
cout <<cmd <<" : " <<mapped_functions[ cmds[0] ](n) <<endl;
}
else{
cout<< "Invalid command"<<endl;
}
}
else{
break;
}
}// End braces of for loop
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment