2009-06-08 04:46:07 +02:00
|
|
|
/*
|
|
|
|
|
COMMAND.hpp - Key abstraction for proc/edit operations and UNDO management
|
|
|
|
|
|
|
|
|
|
Copyright (C) Lumiera.org
|
|
|
|
|
2009, Hermann Vosseler <Ichthyostega@web.de>
|
|
|
|
|
|
|
|
|
|
This program is free software; you can redistribute it and/or
|
|
|
|
|
modify it under the terms of the GNU General Public License as
|
|
|
|
|
published by the Free Software Foundation; either version 2 of the
|
|
|
|
|
License, or (at your option) any later version.
|
|
|
|
|
|
|
|
|
|
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.
|
|
|
|
|
|
|
|
|
|
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.
|
|
|
|
|
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/** @file command.hpp
|
2009-08-10 01:32:33 +02:00
|
|
|
** Proc-Layer command frontend.
|
|
|
|
|
** A \b command is a functor, which can be invoked according to a pre-defined HandlingPattern.
|
|
|
|
|
** Most notably, command invocation can be scheduled and logged with the serialiser, and the effect
|
|
|
|
|
** of any command invocation can be \em undone later on by invoking the "undo operation" defined
|
|
|
|
|
** alongside with the command's operation. The command operation is defined through a C/C++ function
|
|
|
|
|
** and may receive an arbitrary number and type of arguments. After setting up such a CommandDef ,
|
|
|
|
|
** it can be referred for use through a symbolic ID. Before being able to invoke the command, concrete
|
|
|
|
|
** function arguments need to be provided (this is called "binding" or "closing the function arguments").
|
|
|
|
|
** These function arguments are stored within the command definition and remain opaque to the client code
|
|
|
|
|
** actually invoking the command. Behind the scenes, there is a CommandRegistry, holding an index of the
|
|
|
|
|
** registered commands and managing the storage for command definitions and arguments. The actual
|
|
|
|
|
** Command object used by client code is a small, copyable and ref-counting handle to this
|
|
|
|
|
** stored definition backend.
|
|
|
|
|
**
|
|
|
|
|
** //TODO maybe summarise how UNDO works?
|
|
|
|
|
**
|
|
|
|
|
** @see command-def.hpp
|
|
|
|
|
** @see command-use1-test.cpp
|
|
|
|
|
** @see command-use2-test.cpp
|
|
|
|
|
** @see command-use3-test.cpp
|
2009-06-08 04:46:07 +02:00
|
|
|
** @see ProcDispatcher
|
|
|
|
|
** @see Session
|
|
|
|
|
**
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
#ifndef CONTROL_COMMAND_H
|
|
|
|
|
#define CONTROL_COMMAND_H
|
|
|
|
|
|
2009-07-25 19:21:50 +02:00
|
|
|
#include "pre.hpp"
|
2009-08-10 01:32:33 +02:00
|
|
|
#include "lib/error.hpp"
|
2009-09-24 23:02:40 +02:00
|
|
|
#include "lib/symbol.hpp"
|
2009-07-25 19:21:50 +02:00
|
|
|
#include "proc/control/command-binding.hpp"
|
2009-08-10 01:32:33 +02:00
|
|
|
#include "proc/control/argument-erasure.hpp"
|
2009-08-02 18:00:03 +02:00
|
|
|
#include "proc/control/handling-pattern.hpp"
|
2009-07-24 17:50:14 +02:00
|
|
|
#include "lib/bool-checkable.hpp"
|
2009-08-10 01:32:33 +02:00
|
|
|
#include "lib/meta/tuple.hpp"
|
2009-07-27 02:38:53 +02:00
|
|
|
#include "lib/handle.hpp"
|
2009-06-08 04:46:07 +02:00
|
|
|
|
2009-08-01 17:14:27 +02:00
|
|
|
#include <string>
|
2009-06-08 04:46:07 +02:00
|
|
|
|
|
|
|
|
///////////////////////////////////////////TODO: define an C-API representation here, make the header multilingual!
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
namespace control {
|
|
|
|
|
|
2009-08-01 17:14:27 +02:00
|
|
|
using std::string;
|
2009-09-24 23:02:40 +02:00
|
|
|
using lib::Symbol;
|
2009-08-09 03:48:19 +02:00
|
|
|
using std::tr1::shared_ptr;
|
2009-08-10 01:32:33 +02:00
|
|
|
using lumiera::typelist::Tuple;
|
|
|
|
|
|
2009-06-08 04:46:07 +02:00
|
|
|
|
2009-07-25 19:21:50 +02:00
|
|
|
LUMIERA_ERROR_DECLARE (UNBOUND_ARGUMENTS); ///< Command functor not yet usable, because arguments aren't bound
|
|
|
|
|
LUMIERA_ERROR_DECLARE (INVALID_COMMAND); ///< Unknown or insufficiently defined command
|
2009-08-01 17:14:27 +02:00
|
|
|
LUMIERA_ERROR_DECLARE (DUPLICATE_COMMAND); ///< Attempt to redefine an already existing command definition
|
2009-07-25 19:21:50 +02:00
|
|
|
LUMIERA_ERROR_DECLARE (INVALID_ARGUMENTS); ///< Arguments provided for binding doesn't match stored command function parameters
|
|
|
|
|
|
2009-08-10 01:32:33 +02:00
|
|
|
|
|
|
|
|
|
2009-08-02 20:57:04 +02:00
|
|
|
typedef void* FuncPtr;
|
2009-06-08 04:46:07 +02:00
|
|
|
|
2009-07-26 02:00:47 +02:00
|
|
|
class CommandDef;
|
2009-08-01 23:57:12 +02:00
|
|
|
class CommandImpl;
|
2009-07-24 17:50:14 +02:00
|
|
|
|
|
|
|
|
|
2009-06-08 04:46:07 +02:00
|
|
|
/**
|
2009-08-10 01:32:33 +02:00
|
|
|
* Handle object representing a single Command instance to be used by client code.
|
|
|
|
|
* Commands are accessed \link #get through a symbolic ID \endlink; there need to be
|
|
|
|
|
* a CommandDef somewhere to specify the actual operation and to define, how the
|
|
|
|
|
* effect of the command can be undone. Moreover, the command's definition
|
|
|
|
|
* refers to a HandlingPattern, which describes how the command is actually
|
|
|
|
|
* to be executed (the default is to schedule it within the ProcDispatcher)
|
|
|
|
|
*
|
|
|
|
|
* Client code usually just
|
|
|
|
|
* - creates a command instance by referring to a command ID
|
|
|
|
|
* - maybe binds to concrete arguments (e.g. a target object)
|
|
|
|
|
* - triggers command execution through operator()
|
|
|
|
|
* - maybe checks the return value for errors
|
2009-06-08 04:46:07 +02:00
|
|
|
*/
|
|
|
|
|
class Command
|
2009-07-29 16:55:15 +02:00
|
|
|
: public com::ArgumentBinder<Command // accepts arbitrary bind(..) calls (with runtime check)
|
2009-08-01 23:57:12 +02:00
|
|
|
, lib::Handle<CommandImpl> // actually implemented as ref counting Handle
|
2009-08-10 01:32:33 +02:00
|
|
|
> //
|
2009-06-08 04:46:07 +02:00
|
|
|
{
|
2009-08-01 02:18:01 +02:00
|
|
|
typedef lib::Handle<CommandImpl> _Handle;
|
2009-06-08 04:46:07 +02:00
|
|
|
|
|
|
|
|
public:
|
2009-07-24 17:50:14 +02:00
|
|
|
/* === command registry === */
|
2009-08-01 17:14:27 +02:00
|
|
|
static Command get (Symbol cmdID);
|
2009-08-02 20:57:04 +02:00
|
|
|
static Command get (FuncPtr func);
|
2009-08-01 17:14:27 +02:00
|
|
|
static bool remove (Symbol cmdID);
|
2009-07-24 17:50:14 +02:00
|
|
|
|
2009-09-25 16:36:56 +02:00
|
|
|
/** create a clone definition */
|
|
|
|
|
Command storeDef (Symbol newCmdID) const;
|
2009-07-24 17:50:14 +02:00
|
|
|
|
2009-07-30 03:57:19 +02:00
|
|
|
Command() { } ///< undefined command
|
2009-07-24 17:50:14 +02:00
|
|
|
~Command();
|
|
|
|
|
|
2009-09-26 02:14:07 +02:00
|
|
|
|
|
|
|
|
|
|
|
|
|
/* === command lifecycle === */
|
|
|
|
|
|
|
|
|
|
void activate (shared_ptr<CommandImpl> const&);
|
|
|
|
|
|
|
|
|
|
template<typename TYPES>
|
|
|
|
|
Command& bindArg (Tuple<TYPES> const&);
|
|
|
|
|
|
|
|
|
|
|
2009-08-02 20:55:43 +02:00
|
|
|
ExecResult operator() () ;
|
|
|
|
|
ExecResult undo () ;
|
2009-07-24 17:50:14 +02:00
|
|
|
|
2009-06-19 19:11:33 +02:00
|
|
|
|
2009-07-24 17:50:14 +02:00
|
|
|
/** core operation: invoke the command
|
|
|
|
|
* @param execPattern describes the individual steps
|
|
|
|
|
* necessary to get this command invoked properly
|
|
|
|
|
*/
|
2009-08-02 20:55:43 +02:00
|
|
|
ExecResult exec (HandlingPattern const& execPattern);
|
|
|
|
|
ExecResult exec (HandlingPattern::ID);
|
2009-06-19 19:11:33 +02:00
|
|
|
|
2009-09-21 03:11:46 +02:00
|
|
|
/** invoke using a default "synchronous" execution pattern */
|
2009-08-02 20:55:43 +02:00
|
|
|
ExecResult execSync ();
|
2009-07-26 02:00:47 +02:00
|
|
|
|
2009-08-02 18:00:03 +02:00
|
|
|
/** @return ID of the execution pattern used by operator() */
|
|
|
|
|
HandlingPattern::ID getDefaultHandlingPattern() const;
|
|
|
|
|
|
|
|
|
|
/** define a handling pattern to be used by default
|
|
|
|
|
* @return ID of the currently defined default pattern */
|
|
|
|
|
HandlingPattern::ID setHandlingPattern (HandlingPattern::ID);
|
2009-06-08 04:46:07 +02:00
|
|
|
|
2009-07-24 17:50:14 +02:00
|
|
|
|
2009-08-01 17:14:27 +02:00
|
|
|
|
2009-07-24 17:50:14 +02:00
|
|
|
/* === diagnostics === */
|
|
|
|
|
|
|
|
|
|
static size_t definition_count();
|
|
|
|
|
static size_t instance_count();
|
|
|
|
|
|
|
|
|
|
bool canExec() const;
|
|
|
|
|
bool canUndo() const;
|
2009-07-26 02:00:47 +02:00
|
|
|
|
2009-08-01 17:14:27 +02:00
|
|
|
operator string() const;
|
2009-08-01 02:18:01 +02:00
|
|
|
friend bool operator== (Command const&, Command const&);
|
2009-09-26 04:19:57 +02:00
|
|
|
friend bool operator< (Command const&, Command const&);
|
2009-08-01 02:18:01 +02:00
|
|
|
|
|
|
|
|
|
2009-07-26 02:00:47 +02:00
|
|
|
protected:
|
2009-09-26 02:14:07 +02:00
|
|
|
static Command fetchDef (Symbol cmdID);
|
2009-07-29 16:55:15 +02:00
|
|
|
|
|
|
|
|
friend class CommandDef;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
private:
|
2009-08-04 02:25:50 +02:00
|
|
|
void setArguments (Arguments&);
|
2009-06-08 04:46:07 +02:00
|
|
|
};
|
|
|
|
|
|
2009-08-09 21:55:47 +02:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2009-08-02 20:55:43 +02:00
|
|
|
inline ExecResult
|
2009-07-24 17:50:14 +02:00
|
|
|
Command::operator() ()
|
|
|
|
|
{
|
2009-08-02 20:55:43 +02:00
|
|
|
return exec (getDefaultHandlingPattern());
|
2009-07-24 17:50:14 +02:00
|
|
|
}
|
2009-08-01 02:18:01 +02:00
|
|
|
|
|
|
|
|
|
2009-08-02 20:57:04 +02:00
|
|
|
template<typename TYPES>
|
|
|
|
|
inline Command&
|
2009-08-04 02:25:50 +02:00
|
|
|
Command::bindArg (Tuple<TYPES> const& tuple)
|
2009-08-02 20:57:04 +02:00
|
|
|
{
|
2009-08-04 02:25:50 +02:00
|
|
|
TypedArguments<Tuple<TYPES> > args(tuple);
|
|
|
|
|
this->setArguments (args);
|
2009-08-02 20:57:04 +02:00
|
|
|
return *this;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2009-08-01 23:57:12 +02:00
|
|
|
inline bool
|
2009-08-01 02:18:01 +02:00
|
|
|
operator== (Command const& c1, Command const& c2)
|
2009-09-24 19:38:11 +02:00
|
|
|
{
|
|
|
|
|
return (!c1 && !c2)
|
|
|
|
|
|| ( c1 && c2 && (&c1.impl() == &c2.impl()));
|
|
|
|
|
}
|
2009-08-01 02:18:01 +02:00
|
|
|
|
2009-08-01 23:57:12 +02:00
|
|
|
inline bool
|
2009-08-01 02:18:01 +02:00
|
|
|
operator!= (Command const& c1, Command const& c2)
|
2009-09-24 19:38:11 +02:00
|
|
|
{
|
|
|
|
|
return ! (c1 == c2);
|
|
|
|
|
}
|
2009-08-01 02:18:01 +02:00
|
|
|
|
2009-09-26 04:19:57 +02:00
|
|
|
/** allow for sets and associative containers */
|
|
|
|
|
inline bool
|
|
|
|
|
operator< (Command const& c1, Command const& c2)
|
|
|
|
|
{
|
|
|
|
|
return ( c1 && c2 && (&c1.impl() < &c2.impl()));
|
|
|
|
|
}
|
|
|
|
|
|
2009-08-10 01:32:33 +02:00
|
|
|
|
2009-06-08 04:46:07 +02:00
|
|
|
|
|
|
|
|
|
|
|
|
|
} // namespace control
|
|
|
|
|
#endif
|