Possibility to pass class method reference
[command.git] / include / callable.h
1 #ifndef __COMMAND_CALLABLE_H
2 #define __COMMAND_CALLABLE_H
3
4 #include <string>
5 #include <functional>
6
7 namespace command {
8     /**
9      * Callable behaviour class.
10      */
11     template<typename ParameterType>
12     class Callable {
13     protected:
14         /**
15          * Function handling user Arguments
16          */
17 //         void (*func)(ParameterType);
18         std::function<void(ParameterType)> func;
19
20     public:
21         /**
22          * Default constructor.
23          *
24          * @param function Function that will be invoked
25          */
26 //         Callable(void (*function)(ParameterType))
27 //             : func(function) {
28 //         }
29
30         Callable(std::function<void(ParameterType)> function)
31             : func(function) {
32         }
33
34         virtual ~Callable() { }
35
36     protected:
37         /**
38          * Executes command binded with argument
39          *
40          * @param value Value passed to program argument
41          */
42         void call(ParameterType value) {
43             this->func(value);
44         }
45     };
46
47     /**
48      * Template specialization of Callable behaviour class.
49      * Allows passing functions with void argument
50      */
51     template<>
52     class Callable<void> {
53     protected:
54         /**
55          * Function handling user Arguments
56          */
57 //         void (*func)(void);
58         std::function<void(void)> func;
59
60     public:
61         /**
62          * Default constructor.
63          *
64          * @param function Function that will be invoked
65          */
66 //         Callable(void (*function)(void))
67 //             : func(function) {
68 //         }
69
70         Callable(std::function<void(void)> function)
71             : func(function) {
72         }
73
74         virtual ~Callable() { }
75
76     protected:
77         /**
78          * Executes command
79          */
80         void call() {
81             this->func();
82         }
83     };
84 }
85
86 #endif /* __COMMAND_DESCRIPTIVE_H */