language: C++ 4.7.2 (gcc-4.7.2)
date: 535 days 18 hours ago
link:
visibility: public
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
#include <functional>
#include <iostream>
#include <string>
#include <map>
 
class api {
    //maps containing the different function pointers
    typedef void(*voidfuncptr)();
    typedef int(*stringcrintptr)(std::string, const int&);
 
    std::map<std::string, voidfuncptr> voida;
    std::map<std::string, stringcrintptr> stringcrint;
public:
    //api temp class 
    //given an api and a name, it converts to a function pointer  
    //depending on parameters used
    class apitemp {
        const std::string n;
        const api* p;
    public:
        apitemp(const std::string& name, const api* parent) 
            : n(name), p(parent) {}
        operator voidfuncptr() 
        {return p->voida.find(n)->second;}
        operator stringcrintptr() 
        {return p->stringcrint.find(n)->second;}
    }; 
    //insertion of new functions into appropriate maps
    void insert(const std::string& name, voidfuncptr ptr) 
    {voida[name]=ptr;}
    void insert(const std::string& name, stringcrintptr ptr)
    {stringcrint[name]=ptr;}
    //operator[] for the name gets halfway to the right function
    apitemp operator[](std::string n) const {return apitemp(n, this);}
} myMap;
 
int hello_world(std::string name, const int & number )
{
    name += "!";
    std::cout << "Hello, " << name << std::endl;
    return number;
}
 
int main() {
    myMap.insert("my_method_hello", &hello_world ); 
    int a = myMap["my_method_hello"]("Tim", 25);
}
prog.cpp: In function ‘int main()’:
prog.cpp:46: warning: unused variable ‘a’