#include <string>
#include <sstream>
#include <iostream>
+#include <functional>
#include "parameter.h"
#include "callable.h"
: Parameter(description), Callable<ParameterType>(function) {
}
+ Argument(const std::string & description, std::function<void(ParameterType)> function)
+ : Parameter(description), Callable<ParameterType>(function) {
+ }
+
/**
*
*/
#define __COMMAND_CALLABLE_H
#include <string>
+#include <functional>
namespace command {
/**
/**
* Function handling user Arguments
*/
- void (*func)(ParameterType);
+// void (*func)(ParameterType);
+ std::function<void(ParameterType)> func;
public:
/**
*
* @param function Function that will be invoked
*/
- Callable(void (*function)(ParameterType))
+// Callable(void (*function)(ParameterType))
+// : func(function) {
+// }
+
+ Callable(std::function<void(ParameterType)> function)
: func(function) {
}
/**
* Function handling user Arguments
*/
- void (*func)(void);
+// void (*func)(void);
+ std::function<void(void)> func;
public:
/**
*
* @param function Function that will be invoked
*/
- Callable(void (*function)(void))
+// Callable(void (*function)(void))
+// : func(function) {
+// }
+
+ Callable(std::function<void(void)> function)
: func(function) {
}
+
virtual ~Callable() { }
protected:
descriptive/holds_data.test \
callable/invokes_provided_function.test \
callable/invokes_void_function.test \
+ callable/invokes_class_method.test \
parameter/is_descriptive.test \
parameter/should_be_non_required.test \
argument/handles_string_value.test \
callable_invokes_provided_function_test_SOURCES = callable/invokes_provided_function.cpp
callable_invokes_void_function_test_SOURCES = callable/invokes_void_function.cpp
+callable_invokes_class_method_test_SOURCES = callable/invokes_class_method.cpp
parameter_is_descriptive_test_SOURCES = parameter/is_descriptive.cpp
parameter_should_be_non_required_test_SOURCES = parameter/should_be_non_required.cpp
ArgumentType test;
-void function(ArgumentType value) {
+void _function(ArgumentType value) {
test = value;
}
int main() {
- Argument<ArgumentType> argument("Argument as boolean", function);
+ Argument<ArgumentType> argument("Argument as boolean", _function);
if (argument.understand(FALSE)) {
argument.handle();
cout << "Argument class handles boolean (FALSE) values\n";
}
- Argument<ArgumentType> argument2("Argument as boolean", function);
+ Argument<ArgumentType> argument2("Argument as boolean", _function);
if (argument2.understand(TRUE)) {
argument2.handle();
}
ArgumentType test;
-void function(ArgumentType value) {
+void _function(ArgumentType value) {
test = value;
}
int main() {
- Argument<ArgumentType> argument("Argument as float", function);
+ Argument<ArgumentType> argument("Argument as float", _function);
if (argument.understand(VALUE)) {
argument.handle();
ArgumentType test;
-void function(ArgumentType value) {
+void _function(ArgumentType value) {
test = value;
}
int main() {
- Argument<ArgumentType> argument("Argument as int", function);
+ Argument<ArgumentType> argument("Argument as int", _function);
if (argument.understand(VALUE)) {
argument.handle();
ArgumentType test;
-void function(ArgumentType value) {
+void _function(ArgumentType value) {
test = value;
}
int main() {
- Argument<ArgumentType> argument("Argument as negative float", function);
+ Argument<ArgumentType> argument("Argument as negative float", _function);
if (argument.understand(VALUE)) {
argument.handle();
ArgumentType test;
-void function(ArgumentType value) {
+void _function(ArgumentType value) {
test = value;
}
int main() {
- Argument<ArgumentType> argument("Argument as negative int", function);
+ Argument<ArgumentType> argument("Argument as negative int", _function);
if (argument.understand(VALUE)) {
argument.handle();
ArgumentType test;
-void function(ArgumentType value) {
+void _function(ArgumentType value) {
test = value;
}
int main() {
- Argument<ArgumentType> argument("Argument as string", function);
+ Argument<ArgumentType> argument("Argument as string", _function);
if (argument.understand(VALUE)) {
argument.handle();
: Callable<ArgumentType>(function) {
}
+ TestCallable(std::function<void(ArgumentType)> function)
+ : Callable<ArgumentType>(function) {
+ }
+
void callFunction(ArgumentType test) {
this->call(test);
}
: Callable<void>(function) {
}
+ TestCallable(std::function<void(void)> function)
+ : Callable<void>(function) {
+ }
+
void callFunction() {
this->call();
}
--- /dev/null
+#include <cstring>
+#include <iostream>
+
+#include <functional> // std::bind, _1
+
+#include "TestCallable.h"
+#include "callable.h"
+
+using namespace std;
+using namespace command;
+
+bool test = false;
+
+class Class {
+public:
+ void method(void) {
+ test = true;
+ };
+};
+
+int main() {
+ Class c;
+ TestCallable<void> callable(std::bind(&Class::method, &c));
+ callable.callFunction();
+
+ if (test == true) {
+ cout << "Callable class calls provided class member method\n";
+ return 0;
+ }
+
+ cout << "Callable class does not call provided member method\n";
+
+ return 1;
+}
+
bool test = false;
-void function(bool val) {
+void _function(bool val) {
test = val;
};
int main() {
- TestCallable<bool> callable(function);
+ TestCallable<bool> callable(_function);
callable.callFunction(true);
if (test == true) {
bool test = false;
-void function(void) {
+void _function(void) {
test = true;
};
int main() {
- TestCallable<void> callable(function);
+ TestCallable<void> callable(_function);
callable.callFunction();
if (test == true) {
std::vector<ArgumentType> input;
-void function(ArgumentType value) {
+void _function(ArgumentType value) {
input.push_back(value);
cout << "Catched value: " << value << "\n";
}
int main() {
- Parameter * argument = new MultiValue(",", new Argument<ArgumentType>("Argument as multiValue int", function));
+ Parameter * argument = new MultiValue(",", new Argument<ArgumentType>("Argument as multiValue int", _function));
try {
if (argument->understand(VALUE)) {
std::vector<OptionType> input;
-void function(OptionType value) {
+void _function(OptionType value) {
input.push_back(value);
cout << "Catched value: " << value << "\n";
}
int main() {
- Parameter * option = new MultiValue(",", new Option<OptionType>(NAME, "Option as multiValue int", function));
+ Parameter * option = new MultiValue(",", new Option<OptionType>(NAME, "Option as multiValue int", _function));
try {
if (option->understand(OPTION)) {
OptionType test;
-void function(OptionType value) {
+void _function(OptionType value) {
test = value;
}
int main() {
- Option<OptionType> option(NAME, "Option with boolean value", function);
+ Option<OptionType> option(NAME, "Option with boolean value", _function);
if (option.understand(OPTION1)) {
option.handle();
cout << option.describe() << " handles " << FALSE << " values\n";
}
- Option<OptionType> option2(NAME, "Option with boolean value", function);
+ Option<OptionType> option2(NAME, "Option with boolean value", _function);
if (option2.understand(OPTION2)) {
option2.handle();
OptionType test;
-void function(OptionType value) {
+void _function(OptionType value) {
test = value;
}
int main() {
- Option<OptionType> option(NAME, "Option with float value", function);
+ Option<OptionType> option(NAME, "Option with float value", _function);
if (option.understand(OPTION)) {
option.handle();
OptionType test;
-void function(OptionType value) {
+void _function(OptionType value) {
test = value;
}
int main() {
- Option<OptionType> option(NAME, "Option as int", function);
+ Option<OptionType> option(NAME, "Option as int", _function);
if (option.understand(OPTION)) {
option.handle();
OptionType test;
-void function(OptionType value) {
+void _function(OptionType value) {
test = value;
}
int main() {
- Option<OptionType> option(NAME, "Option with negative float value", function);
+ Option<OptionType> option(NAME, "Option with negative float value", _function);
if (option.understand(OPTION)) {
option.handle();
OptionType test;
-void function(OptionType value) {
+void _function(OptionType value) {
test = value;
}
int main() {
- Option<OptionType> option(NAME, "Option as negative int", function);
+ Option<OptionType> option(NAME, "Option as negative int", _function);
if (option.understand(OPTION)) {
option.handle();
OptionType test;
-void function(OptionType value) {
+void _function(OptionType value) {
test = value;
}
int main() {
- Option<OptionType> option(NAME, "Option with string value", function);
+ Option<OptionType> option(NAME, "Option with string value", _function);
if (option.understand(OPTION)) {
option.handle();
bool test = false;
-void function() {
+void _function() {
test = true;
}
int main() {
- Option<OptionType> option(NAME, "Option with void value", function);
+ Option<OptionType> option(NAME, "Option with void value", _function);
if (option.understand(NAME)) {
option.handle();
using namespace std;
using namespace command;
-void function(void) { }
+void _function(void) { }
int main() {
std::vector<std::string> badOptions;
badOptions.push_back("te");
badOptions.push_back("t");
- Option<void> option(NAME, "Option should match only exact name", function);
+ Option<void> option(NAME, "Option should match only exact name", _function);
for (std::string bad : badOptions) {
if (option.understand(bad)) {
std::cout << option.describe() << " but '" << NAME << "' was matched as same to '" << bad << "'\n";
using namespace std;
using namespace command;
-void function(int) { }
+void _function(int) { }
int main() {
- Option<int> option(NAME, "Option should throw exception on missing value", function);
+ Option<int> option(NAME, "Option should throw exception on missing value", _function);
try {
if (option.understand(BAD_OPTION)) {
typedef int Type;
-void function(Type val) { }
+void _function(Type val) { }
int main() {
- Parameter * requiredOption = new Required(new Option<Type>(NAME, "Required Option", function));
- Parameter * requiredArgument = new Required(new Argument<Type>("Required Argument", function));
+ Parameter * requiredOption = new Required(new Option<Type>(NAME, "Required Option", _function));
+ Parameter * requiredArgument = new Required(new Argument<Type>("Required Argument", _function));
if (!requiredOption->isRequired()) {
cout << requiredOption->describe() << " should be treated as required but it is not\n";