From ae6743a2a2c69b7a927f64ff1d3abf38a5e7d4bc Mon Sep 17 00:00:00 2001 From: =?utf8?q?Rafa=C5=82=20D=C5=82ugo=C5=82=C4=99cki?= Date: Mon, 8 Feb 2016 01:38:16 +0100 Subject: [PATCH] Possibility to pass class method reference --- include/argument.h | 5 +++ include/callable.h | 20 ++++++++--- tests/Makefile.am | 2 ++ tests/argument/handles_boolean_value.cpp | 6 ++-- tests/argument/handles_float_value.cpp | 4 +-- tests/argument/handles_int_value.cpp | 4 +-- .../argument/handles_negative_float_value.cpp | 4 +-- tests/argument/handles_negative_int_value.cpp | 4 +-- tests/argument/handles_string_value.cpp | 4 +-- tests/callable/TestCallable.h | 8 +++++ tests/callable/invokes_class_method.cpp | 35 +++++++++++++++++++ tests/callable/invokes_provided_function.cpp | 4 +-- tests/callable/invokes_void_function.cpp | 4 +-- .../should_extract_arguments_by_separator.cpp | 4 +-- .../should_extract_options_by_separator.cpp | 4 +-- tests/option/handles_boolean_value.cpp | 6 ++-- tests/option/handles_float_value.cpp | 4 +-- tests/option/handles_int_value.cpp | 4 +-- tests/option/handles_negative_float_value.cpp | 4 +-- tests/option/handles_negative_int_value.cpp | 4 +-- tests/option/handles_string_value.cpp | 4 +-- tests/option/handles_void_value.cpp | 4 +-- tests/option/should_match_exact_name.cpp | 4 +-- ...hould_throw_exception_on_missing_value.cpp | 4 +-- tests/required/should_be_required.cpp | 6 ++-- 25 files changed, 109 insertions(+), 47 deletions(-) create mode 100644 tests/callable/invokes_class_method.cpp diff --git a/include/argument.h b/include/argument.h index 81a7cd4..2cd3c30 100644 --- a/include/argument.h +++ b/include/argument.h @@ -4,6 +4,7 @@ #include #include #include +#include #include "parameter.h" #include "callable.h" @@ -35,6 +36,10 @@ namespace command { : Parameter(description), Callable(function) { } + Argument(const std::string & description, std::function function) + : Parameter(description), Callable(function) { + } + /** * */ diff --git a/include/callable.h b/include/callable.h index 4ac1bd9..f4d9e08 100644 --- a/include/callable.h +++ b/include/callable.h @@ -2,6 +2,7 @@ #define __COMMAND_CALLABLE_H #include +#include namespace command { /** @@ -13,7 +14,8 @@ namespace command { /** * Function handling user Arguments */ - void (*func)(ParameterType); +// void (*func)(ParameterType); + std::function func; public: /** @@ -21,7 +23,11 @@ namespace command { * * @param function Function that will be invoked */ - Callable(void (*function)(ParameterType)) +// Callable(void (*function)(ParameterType)) +// : func(function) { +// } + + Callable(std::function function) : func(function) { } @@ -48,7 +54,8 @@ namespace command { /** * Function handling user Arguments */ - void (*func)(void); +// void (*func)(void); + std::function func; public: /** @@ -56,9 +63,14 @@ namespace command { * * @param function Function that will be invoked */ - Callable(void (*function)(void)) +// Callable(void (*function)(void)) +// : func(function) { +// } + + Callable(std::function function) : func(function) { } + virtual ~Callable() { } protected: diff --git a/tests/Makefile.am b/tests/Makefile.am index 4df147e..e17e256 100644 --- a/tests/Makefile.am +++ b/tests/Makefile.am @@ -4,6 +4,7 @@ TEST_PROGS = \ 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 \ @@ -51,6 +52,7 @@ descriptive_holds_data_test_SOURCES = descriptive/holds_data.cpp 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 diff --git a/tests/argument/handles_boolean_value.cpp b/tests/argument/handles_boolean_value.cpp index b41106f..5fb944e 100644 --- a/tests/argument/handles_boolean_value.cpp +++ b/tests/argument/handles_boolean_value.cpp @@ -12,12 +12,12 @@ typedef bool ArgumentType; ArgumentType test; -void function(ArgumentType value) { +void _function(ArgumentType value) { test = value; } int main() { - Argument argument("Argument as boolean", function); + Argument argument("Argument as boolean", _function); if (argument.understand(FALSE)) { argument.handle(); @@ -31,7 +31,7 @@ int main() { cout << "Argument class handles boolean (FALSE) values\n"; } - Argument argument2("Argument as boolean", function); + Argument argument2("Argument as boolean", _function); if (argument2.understand(TRUE)) { argument2.handle(); } diff --git a/tests/argument/handles_float_value.cpp b/tests/argument/handles_float_value.cpp index 9e53dc8..f757a84 100644 --- a/tests/argument/handles_float_value.cpp +++ b/tests/argument/handles_float_value.cpp @@ -11,12 +11,12 @@ typedef float ArgumentType; ArgumentType test; -void function(ArgumentType value) { +void _function(ArgumentType value) { test = value; } int main() { - Argument argument("Argument as float", function); + Argument argument("Argument as float", _function); if (argument.understand(VALUE)) { argument.handle(); diff --git a/tests/argument/handles_int_value.cpp b/tests/argument/handles_int_value.cpp index ffe130c..4295286 100644 --- a/tests/argument/handles_int_value.cpp +++ b/tests/argument/handles_int_value.cpp @@ -12,12 +12,12 @@ typedef int ArgumentType; ArgumentType test; -void function(ArgumentType value) { +void _function(ArgumentType value) { test = value; } int main() { - Argument argument("Argument as int", function); + Argument argument("Argument as int", _function); if (argument.understand(VALUE)) { argument.handle(); diff --git a/tests/argument/handles_negative_float_value.cpp b/tests/argument/handles_negative_float_value.cpp index 7c97467..c9e1e8f 100644 --- a/tests/argument/handles_negative_float_value.cpp +++ b/tests/argument/handles_negative_float_value.cpp @@ -11,12 +11,12 @@ typedef float ArgumentType; ArgumentType test; -void function(ArgumentType value) { +void _function(ArgumentType value) { test = value; } int main() { - Argument argument("Argument as negative float", function); + Argument argument("Argument as negative float", _function); if (argument.understand(VALUE)) { argument.handle(); diff --git a/tests/argument/handles_negative_int_value.cpp b/tests/argument/handles_negative_int_value.cpp index f9c222f..3116526 100644 --- a/tests/argument/handles_negative_int_value.cpp +++ b/tests/argument/handles_negative_int_value.cpp @@ -11,12 +11,12 @@ typedef int ArgumentType; ArgumentType test; -void function(ArgumentType value) { +void _function(ArgumentType value) { test = value; } int main() { - Argument argument("Argument as negative int", function); + Argument argument("Argument as negative int", _function); if (argument.understand(VALUE)) { argument.handle(); diff --git a/tests/argument/handles_string_value.cpp b/tests/argument/handles_string_value.cpp index 38e6efa..6f7309e 100644 --- a/tests/argument/handles_string_value.cpp +++ b/tests/argument/handles_string_value.cpp @@ -12,12 +12,12 @@ typedef std::string ArgumentType; ArgumentType test; -void function(ArgumentType value) { +void _function(ArgumentType value) { test = value; } int main() { - Argument argument("Argument as string", function); + Argument argument("Argument as string", _function); if (argument.understand(VALUE)) { argument.handle(); diff --git a/tests/callable/TestCallable.h b/tests/callable/TestCallable.h index 7fea870..a4185c5 100644 --- a/tests/callable/TestCallable.h +++ b/tests/callable/TestCallable.h @@ -9,6 +9,10 @@ public: : Callable(function) { } + TestCallable(std::function function) + : Callable(function) { + } + void callFunction(ArgumentType test) { this->call(test); } @@ -21,6 +25,10 @@ public: : Callable(function) { } + TestCallable(std::function function) + : Callable(function) { + } + void callFunction() { this->call(); } diff --git a/tests/callable/invokes_class_method.cpp b/tests/callable/invokes_class_method.cpp new file mode 100644 index 0000000..3b0de6b --- /dev/null +++ b/tests/callable/invokes_class_method.cpp @@ -0,0 +1,35 @@ +#include +#include + +#include // 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 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; +} + diff --git a/tests/callable/invokes_provided_function.cpp b/tests/callable/invokes_provided_function.cpp index 5defb08..b722fbe 100644 --- a/tests/callable/invokes_provided_function.cpp +++ b/tests/callable/invokes_provided_function.cpp @@ -9,12 +9,12 @@ using namespace command; bool test = false; -void function(bool val) { +void _function(bool val) { test = val; }; int main() { - TestCallable callable(function); + TestCallable callable(_function); callable.callFunction(true); if (test == true) { diff --git a/tests/callable/invokes_void_function.cpp b/tests/callable/invokes_void_function.cpp index f0e4671..bf881d0 100644 --- a/tests/callable/invokes_void_function.cpp +++ b/tests/callable/invokes_void_function.cpp @@ -9,12 +9,12 @@ using namespace command; bool test = false; -void function(void) { +void _function(void) { test = true; }; int main() { - TestCallable callable(function); + TestCallable callable(_function); callable.callFunction(); if (test == true) { diff --git a/tests/multiValue/should_extract_arguments_by_separator.cpp b/tests/multiValue/should_extract_arguments_by_separator.cpp index 8b5e5e3..4b0c1ec 100644 --- a/tests/multiValue/should_extract_arguments_by_separator.cpp +++ b/tests/multiValue/should_extract_arguments_by_separator.cpp @@ -13,13 +13,13 @@ typedef int ArgumentType; std::vector 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("Argument as multiValue int", function)); + Parameter * argument = new MultiValue(",", new Argument("Argument as multiValue int", _function)); try { if (argument->understand(VALUE)) { diff --git a/tests/multiValue/should_extract_options_by_separator.cpp b/tests/multiValue/should_extract_options_by_separator.cpp index 1f19354..dbbc38a 100644 --- a/tests/multiValue/should_extract_options_by_separator.cpp +++ b/tests/multiValue/should_extract_options_by_separator.cpp @@ -16,13 +16,13 @@ typedef int OptionType; std::vector 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(NAME, "Option as multiValue int", function)); + Parameter * option = new MultiValue(",", new Option(NAME, "Option as multiValue int", _function)); try { if (option->understand(OPTION)) { diff --git a/tests/option/handles_boolean_value.cpp b/tests/option/handles_boolean_value.cpp index 473a6b7..7e3193a 100644 --- a/tests/option/handles_boolean_value.cpp +++ b/tests/option/handles_boolean_value.cpp @@ -16,12 +16,12 @@ typedef bool OptionType; OptionType test; -void function(OptionType value) { +void _function(OptionType value) { test = value; } int main() { - Option option(NAME, "Option with boolean value", function); + Option option(NAME, "Option with boolean value", _function); if (option.understand(OPTION1)) { option.handle(); @@ -35,7 +35,7 @@ int main() { cout << option.describe() << " handles " << FALSE << " values\n"; } - Option option2(NAME, "Option with boolean value", function); + Option option2(NAME, "Option with boolean value", _function); if (option2.understand(OPTION2)) { option2.handle(); diff --git a/tests/option/handles_float_value.cpp b/tests/option/handles_float_value.cpp index 6bbc5ed..e04b622 100644 --- a/tests/option/handles_float_value.cpp +++ b/tests/option/handles_float_value.cpp @@ -15,12 +15,12 @@ typedef float OptionType; OptionType test; -void function(OptionType value) { +void _function(OptionType value) { test = value; } int main() { - Option option(NAME, "Option with float value", function); + Option option(NAME, "Option with float value", _function); if (option.understand(OPTION)) { option.handle(); diff --git a/tests/option/handles_int_value.cpp b/tests/option/handles_int_value.cpp index a6d9c46..a386c26 100644 --- a/tests/option/handles_int_value.cpp +++ b/tests/option/handles_int_value.cpp @@ -15,12 +15,12 @@ typedef int OptionType; OptionType test; -void function(OptionType value) { +void _function(OptionType value) { test = value; } int main() { - Option option(NAME, "Option as int", function); + Option option(NAME, "Option as int", _function); if (option.understand(OPTION)) { option.handle(); diff --git a/tests/option/handles_negative_float_value.cpp b/tests/option/handles_negative_float_value.cpp index 02bc7e0..6e03113 100644 --- a/tests/option/handles_negative_float_value.cpp +++ b/tests/option/handles_negative_float_value.cpp @@ -15,12 +15,12 @@ typedef float OptionType; OptionType test; -void function(OptionType value) { +void _function(OptionType value) { test = value; } int main() { - Option option(NAME, "Option with negative float value", function); + Option option(NAME, "Option with negative float value", _function); if (option.understand(OPTION)) { option.handle(); diff --git a/tests/option/handles_negative_int_value.cpp b/tests/option/handles_negative_int_value.cpp index 816e8da..2e2ea82 100644 --- a/tests/option/handles_negative_int_value.cpp +++ b/tests/option/handles_negative_int_value.cpp @@ -15,12 +15,12 @@ typedef int OptionType; OptionType test; -void function(OptionType value) { +void _function(OptionType value) { test = value; } int main() { - Option option(NAME, "Option as negative int", function); + Option option(NAME, "Option as negative int", _function); if (option.understand(OPTION)) { option.handle(); diff --git a/tests/option/handles_string_value.cpp b/tests/option/handles_string_value.cpp index 7f9099d..c318351 100644 --- a/tests/option/handles_string_value.cpp +++ b/tests/option/handles_string_value.cpp @@ -15,12 +15,12 @@ typedef std::string OptionType; OptionType test; -void function(OptionType value) { +void _function(OptionType value) { test = value; } int main() { - Option option(NAME, "Option with string value", function); + Option option(NAME, "Option with string value", _function); if (option.understand(OPTION)) { option.handle(); diff --git a/tests/option/handles_void_value.cpp b/tests/option/handles_void_value.cpp index b6528e3..4157bc4 100644 --- a/tests/option/handles_void_value.cpp +++ b/tests/option/handles_void_value.cpp @@ -11,12 +11,12 @@ typedef void OptionType; bool test = false; -void function() { +void _function() { test = true; } int main() { - Option option(NAME, "Option with void value", function); + Option option(NAME, "Option with void value", _function); if (option.understand(NAME)) { option.handle(); diff --git a/tests/option/should_match_exact_name.cpp b/tests/option/should_match_exact_name.cpp index 81f3778..b5580f1 100644 --- a/tests/option/should_match_exact_name.cpp +++ b/tests/option/should_match_exact_name.cpp @@ -8,7 +8,7 @@ using namespace std; using namespace command; -void function(void) { } +void _function(void) { } int main() { std::vector badOptions; @@ -18,7 +18,7 @@ int main() { badOptions.push_back("te"); badOptions.push_back("t"); - Option option(NAME, "Option should match only exact name", function); + Option 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"; diff --git a/tests/option/should_throw_exception_on_missing_value.cpp b/tests/option/should_throw_exception_on_missing_value.cpp index f872ed1..4b901fb 100644 --- a/tests/option/should_throw_exception_on_missing_value.cpp +++ b/tests/option/should_throw_exception_on_missing_value.cpp @@ -9,10 +9,10 @@ using namespace std; using namespace command; -void function(int) { } +void _function(int) { } int main() { - Option option(NAME, "Option should throw exception on missing value", function); + Option option(NAME, "Option should throw exception on missing value", _function); try { if (option.understand(BAD_OPTION)) { diff --git a/tests/required/should_be_required.cpp b/tests/required/should_be_required.cpp index b59db3d..ee57160 100644 --- a/tests/required/should_be_required.cpp +++ b/tests/required/should_be_required.cpp @@ -14,11 +14,11 @@ using namespace command; typedef int Type; -void function(Type val) { } +void _function(Type val) { } int main() { - Parameter * requiredOption = new Required(new Option(NAME, "Required Option", function)); - Parameter * requiredArgument = new Required(new Argument("Required Argument", function)); + Parameter * requiredOption = new Required(new Option(NAME, "Required Option", _function)); + Parameter * requiredArgument = new Required(new Argument("Required Argument", _function)); if (!requiredOption->isRequired()) { cout << requiredOption->describe() << " should be treated as required but it is not\n"; -- 2.30.2