2009-06-08 21:29:09 +02:00
|
|
|
/*
|
|
|
|
|
CommandBasic(Test) - checking simple ProcDispatcher command definition and execution
|
2010-12-17 23:28:49 +01:00
|
|
|
|
2009-06-08 21:29:09 +02:00
|
|
|
Copyright (C) Lumiera.org
|
|
|
|
|
2009, Hermann Vosseler <Ichthyostega@web.de>
|
2010-12-17 23:28:49 +01:00
|
|
|
|
2009-06-08 21:29:09 +02:00
|
|
|
This program is free software; you can redistribute it and/or
|
|
|
|
|
modify it under the terms of the GNU General Public License as
|
2010-12-17 23:28:49 +01:00
|
|
|
published by the Free Software Foundation; either version 2 of
|
|
|
|
|
the License, or (at your option) any later version.
|
|
|
|
|
|
2009-06-08 21:29:09 +02:00
|
|
|
This program is distributed in the hope that it will be useful,
|
|
|
|
|
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
|
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
|
GNU General Public License for more details.
|
2010-12-17 23:28:49 +01:00
|
|
|
|
2009-06-08 21:29:09 +02:00
|
|
|
You should have received a copy of the GNU General Public License
|
|
|
|
|
along with this program; if not, write to the Free Software
|
|
|
|
|
Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
|
2010-12-17 23:28:49 +01:00
|
|
|
|
2009-06-08 21:29:09 +02:00
|
|
|
* *****************************************************/
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
#include "lib/test/run.hpp"
|
|
|
|
|
#include "proc/control/command-def.hpp"
|
2009-10-06 04:08:47 +02:00
|
|
|
#include "lib/p.hpp"
|
2009-06-08 21:29:09 +02:00
|
|
|
|
2009-06-24 06:01:14 +02:00
|
|
|
#include <cstdlib>
|
2009-06-08 21:29:09 +02:00
|
|
|
|
2009-06-24 06:01:14 +02:00
|
|
|
using std::rand;
|
2009-06-08 21:29:09 +02:00
|
|
|
|
|
|
|
|
|
|
|
|
|
namespace control {
|
|
|
|
|
namespace test {
|
2009-06-10 03:42:23 +02:00
|
|
|
|
2009-10-06 04:08:47 +02:00
|
|
|
using lumiera::P;
|
2009-06-10 03:42:23 +02:00
|
|
|
|
2009-06-12 20:10:27 +02:00
|
|
|
|
|
|
|
|
|
2009-06-19 05:57:06 +02:00
|
|
|
|
|
|
|
|
|
2009-10-06 04:08:47 +02:00
|
|
|
namespace { // functions to be invoked through the command system
|
2009-10-06 05:11:30 +02:00
|
|
|
|
2009-06-10 03:42:23 +02:00
|
|
|
void
|
2009-06-10 05:00:01 +02:00
|
|
|
operate (P<Time> dummyObj, int randVal)
|
2009-06-10 03:42:23 +02:00
|
|
|
{
|
2009-06-10 05:00:01 +02:00
|
|
|
*dummyObj += Time(randVal);
|
2009-06-10 03:42:23 +02:00
|
|
|
}
|
|
|
|
|
|
2009-06-10 05:00:01 +02:00
|
|
|
Time
|
|
|
|
|
capture (P<Time> dummyObj, int)
|
2009-06-10 03:42:23 +02:00
|
|
|
{
|
|
|
|
|
return *dummyObj;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void
|
2009-06-10 05:00:01 +02:00
|
|
|
undoIt (P<Time> dummyObj, int, Time oldVal)
|
2009-06-10 03:42:23 +02:00
|
|
|
{
|
|
|
|
|
*dummyObj = oldVal;
|
|
|
|
|
}
|
2009-06-08 21:29:09 +02:00
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/***************************************************************************
|
|
|
|
|
* @test basic usage of the Proc-Layer command dispatch system.
|
2009-10-06 04:08:47 +02:00
|
|
|
* Shows how to define a simple command inline and how to
|
|
|
|
|
* trigger execution and UNDO. Verifies the command action
|
|
|
|
|
* takes place and is reverted again by the UNDO function.
|
2009-10-06 05:11:30 +02:00
|
|
|
*
|
|
|
|
|
* This is a simplified demonstration. Usually, commands would be defined
|
|
|
|
|
* in bulk and without specifying parameters. Later, typically client code
|
|
|
|
|
* accesses a handle by ID, binds to the concrete argument and dispatches
|
|
|
|
|
* the invocation. Note in this example that by using a smart-ptr as
|
|
|
|
|
* argument allows accessing an object by reference and late binding.
|
2009-10-06 04:08:47 +02:00
|
|
|
*
|
2009-06-08 21:29:09 +02:00
|
|
|
* @see control::Command
|
|
|
|
|
* @see control::CommandDef
|
|
|
|
|
* @see mobject::ProcDispatcher
|
|
|
|
|
*/
|
|
|
|
|
class CommandBasic_test : public Test
|
|
|
|
|
{
|
|
|
|
|
|
|
|
|
|
virtual void
|
|
|
|
|
run (Arg)
|
2009-06-10 03:42:23 +02:00
|
|
|
{
|
2009-06-15 04:57:23 +02:00
|
|
|
P<Time> obj (new Time(5));
|
|
|
|
|
int randVal ((rand() % 10) - 5);
|
|
|
|
|
|
2009-06-19 05:57:06 +02:00
|
|
|
CommandDef ("test.command1")
|
2009-07-23 03:47:27 +02:00
|
|
|
.operation (operate)
|
|
|
|
|
.captureUndo (capture)
|
|
|
|
|
.undoOperation (undoIt)
|
2009-06-15 04:57:23 +02:00
|
|
|
.bind (obj, randVal)
|
2009-06-10 03:42:23 +02:00
|
|
|
;
|
2009-06-19 05:57:06 +02:00
|
|
|
|
|
|
|
|
|
2009-08-01 23:57:12 +02:00
|
|
|
Command ourCmd = Command::get("test.command1");
|
2009-06-19 05:57:06 +02:00
|
|
|
|
|
|
|
|
// invoke the command
|
2010-12-10 02:55:40 +01:00
|
|
|
CHECK (*obj == Time(5));
|
2009-06-19 05:57:06 +02:00
|
|
|
ourCmd();
|
2010-12-10 02:55:40 +01:00
|
|
|
CHECK (*obj == Time(5) + Time(randVal));
|
2009-06-19 05:57:06 +02:00
|
|
|
|
|
|
|
|
// undo the effect of the command
|
|
|
|
|
ourCmd.undo();
|
2010-12-10 02:55:40 +01:00
|
|
|
CHECK (*obj == Time(5));
|
2009-06-08 21:29:09 +02:00
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/** Register this test class... */
|
|
|
|
|
LAUNCHER (CommandBasic_test, "unit controller");
|
2009-10-06 05:11:30 +02:00
|
|
|
|
|
|
|
|
|
2009-06-08 21:29:09 +02:00
|
|
|
}} // namespace control::test
|