Possibility to pass class method reference
[command.git] / include / callable.h
index 7e6e503c436e23f086306d7c0dd33f1267795eff..f4d9e084cabe385dfcee6cfbfb0adcf87016e551 100644 (file)
@@ -2,18 +2,20 @@
 #define __COMMAND_CALLABLE_H
 
 #include <string>
+#include <functional>
 
 namespace command {
     /**
      * Callable behaviour class.
      */
-    template<typename ArgumentType>
+    template<typename ParameterType>
     class Callable {
     protected:
         /**
          * Function handling user Arguments
          */
-        void (*func)(ArgumentType);
+//         void (*func)(ParameterType);
+        std::function<void(ParameterType)> func;
 
     public:
         /**
@@ -21,9 +23,14 @@ namespace command {
          *
          * @param function Function that will be invoked
          */
-        Callable(void (*function)(ArgumentType))
+//         Callable(void (*function)(ParameterType))
+//             : func(function) {
+//         }
+
+        Callable(std::function<void(ParameterType)> function)
             : func(function) {
         }
+
         virtual ~Callable() { }
 
     protected:
@@ -32,10 +39,48 @@ namespace command {
          *
          * @param value Value passed to program argument
          */
-        void call(ArgumentType value) {
+        void call(ParameterType value) {
             this->func(value);
         }
     };
+
+    /**
+     * Template specialization of Callable behaviour class.
+     * Allows passing functions with void argument
+     */
+    template<>
+    class Callable<void> {
+    protected:
+        /**
+         * Function handling user Arguments
+         */
+//         void (*func)(void);
+        std::function<void(void)> func;
+
+    public:
+        /**
+         * Default constructor.
+         *
+         * @param function Function that will be invoked
+         */
+//         Callable(void (*function)(void))
+//             : func(function) {
+//         }
+
+        Callable(std::function<void(void)> function)
+            : func(function) {
+        }
+
+        virtual ~Callable() { }
+
+    protected:
+        /**
+         * Executes command
+         */
+        void call() {
+            this->func();
+        }
+    };
 }
 
 #endif /* __COMMAND_DESCRIPTIVE_H */