From 49102ff18fc565af701dc829728843aa592606c2 Mon Sep 17 00:00:00 2001 From: Ichthyostega Date: Fri, 31 Mar 2017 04:14:45 +0200 Subject: [PATCH] Commands: define typical standard usage of CommandSetup --- src/proc/control/command-setup.cpp | 2 +- src/proc/control/command-setup.hpp | 17 ++++--- .../core/proc/control/command-setup-test.cpp | 48 ++++++++++++++++++- 3 files changed, 59 insertions(+), 8 deletions(-) diff --git a/src/proc/control/command-setup.cpp b/src/proc/control/command-setup.cpp index 47dd9df15..e02adf56e 100644 --- a/src/proc/control/command-setup.cpp +++ b/src/proc/control/command-setup.cpp @@ -80,7 +80,7 @@ namespace control { CommandSetup::~CommandSetup() { } /** Start a command setup for defining a Proc-Layer command with the given cmdID */ - CommandSetup::CommandSetup(Literal cmdID) + CommandSetup::CommandSetup(Symbol cmdID) : cmdID_(cmdID) { } diff --git a/src/proc/control/command-setup.hpp b/src/proc/control/command-setup.hpp index 522ee01f8..4d0e86f5b 100644 --- a/src/proc/control/command-setup.hpp +++ b/src/proc/control/command-setup.hpp @@ -69,7 +69,7 @@ namespace control { using std::string; // using lib::Symbol; - using lib::Literal; + using lib::Symbol; //using std::shared_ptr; class CommandDef; @@ -83,16 +83,16 @@ namespace control { */ class CommandSetup { - Literal cmdID_; + Symbol cmdID_; public: ~CommandSetup(); - CommandSetup (Literal cmdID); + CommandSetup (Symbol cmdID); CommandSetup (CommandSetup const&) = default; CommandSetup (CommandSetup &&) = default; CommandSetup& operator= (CommandSetup const&) = delete; - operator Literal const&() const + operator Symbol const&() const { return cmdID_; } @@ -125,8 +125,13 @@ namespace control { - /** */ - + /** + * Macro to write command definitions in a compact form. + * On expansion, a variable of type CommandSetup will be defined in the current scope, + * and immediately be assigned by a lambda, whose body is what follows the macro invocation + */ + #define COMMAND_DEFINITION(_NAME_) \ + CommandSetup _NAME_ = CommandSetup{STRINGIFY(_NAME_)} = [&](CommandDef& def) diff --git a/tests/core/proc/control/command-setup-test.cpp b/tests/core/proc/control/command-setup-test.cpp index 1d98faf9f..a2eca56c8 100644 --- a/tests/core/proc/control/command-setup-test.cpp +++ b/tests/core/proc/control/command-setup-test.cpp @@ -91,6 +91,16 @@ namespace test { { testString = oldVal; } + + + /* ==== prepare a dummy command definition ==== */ + + COMMAND_DEFINITION (test_CommandSetup_test) + { + def.operation(operate) + .captureUndo(capture) + .undoOperation(undoIt); + }; } @@ -234,7 +244,43 @@ namespace test { void verify_standardUsage() { - UNIMPLEMENTED ("cover standard usage of command definitions"); + Command{test_CommandSetup_test} + .storeDef("c1") + .storeDef("c2"); + + Command c1{"c1"}, c2{"c2"}; + CHECK (not c1.canExec()); + CHECK (not c2.canExec()); + + c1.bind (string{"wuz.*"}, string{"the Devonian"}); + c2.bind (string{"\\s*\\w+$"}, string{""}); + CHECK (c1.canExec()); + CHECK (c2.canExec()); + CHECK (not Command::canExec(test_CommandSetup_test)); + + CHECK (testString == "Ichthyostega wuz here"); + + c1(); + CHECK (testString == "Ichthyostega the Devonian"); + + c2(); + CHECK (testString == "Ichthyostega the"); + + c2(); + CHECK (testString == "Ichthyostega"); + + c2(); + CHECK (testString == ""); + + c1.undo(); + CHECK (testString == "Ichthyostega wuz here"); + + Command::remove("c1"); + Command::remove("c2"); + + CHECK (not Command::defined("c1")); + CHECK (not Command::defined("c2")); + CHECK (Command::defined(test_CommandSetup_test)); } };