2009-06-08 04:46:07 +02:00
|
|
|
/*
|
|
|
|
|
HANDLILNG-PATTERN.hpp - A skeleton for executing commands, including standard implementations
|
2010-12-17 23:28:49 +01:00
|
|
|
|
2009-06-08 04:46:07 +02:00
|
|
|
Copyright (C) Lumiera.org
|
|
|
|
|
2009, Hermann Vosseler <Ichthyostega@web.de>
|
2010-12-17 23:28:49 +01:00
|
|
|
|
2009-06-08 04:46:07 +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 04:46:07 +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 04:46:07 +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 04:46:07 +02:00
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/** @file handling-pattern.hpp
|
2009-10-01 13:29:53 +02:00
|
|
|
** Pre-defined command execution skeletons.
|
2009-09-21 03:11:46 +02:00
|
|
|
** Any command can be configured to use a specific handling pattern
|
|
|
|
|
** on invocation. Moreover, there is a default handling pattern for commands.
|
|
|
|
|
** These patterns define the steps necessary for getting the command actually
|
|
|
|
|
** invoked (template method pattern). A pattern may cause the command to be
|
|
|
|
|
** enqueued, registered for UNDO or dispatched into a background thread.
|
|
|
|
|
** To carry out the work, HandlingPattern implementations are allowed to
|
|
|
|
|
** invoke the CommandImpl API directly.
|
|
|
|
|
**
|
|
|
|
|
** @todo it is not clear what's the difference between "throw" and "no-throw" pattern
|
|
|
|
|
** @todo any integration with the (yet undefined as of 9/09) ProcDispatcher is missing.
|
2009-06-08 04:46:07 +02:00
|
|
|
**
|
|
|
|
|
** @see ProcDispatcher
|
|
|
|
|
** @see Session
|
|
|
|
|
**
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
#ifndef CONTROL_HANDLING_PATTERN_H
|
|
|
|
|
#define CONTROL_HANDLING_PATTERN_H
|
|
|
|
|
|
2009-07-26 02:00:47 +02:00
|
|
|
#include "lib/error.hpp"
|
2009-09-24 23:02:40 +02:00
|
|
|
#include "lib/symbol.hpp"
|
2009-10-01 13:29:53 +02:00
|
|
|
#include "lib/bool-checkable.hpp"
|
2009-06-08 04:46:07 +02:00
|
|
|
|
2009-08-02 18:00:03 +02:00
|
|
|
#include <string>
|
2009-06-08 04:46:07 +02:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2011-12-02 17:50:44 +01:00
|
|
|
namespace proc {
|
2009-06-08 04:46:07 +02:00
|
|
|
namespace control {
|
|
|
|
|
|
2009-08-02 18:00:03 +02:00
|
|
|
using std::string;
|
2009-09-24 23:02:40 +02:00
|
|
|
using lib::Symbol;
|
2009-06-08 04:46:07 +02:00
|
|
|
|
|
|
|
|
|
2009-09-21 03:11:46 +02:00
|
|
|
class CommandImpl;
|
2009-06-08 04:46:07 +02:00
|
|
|
|
|
|
|
|
|
2009-08-02 18:00:03 +02:00
|
|
|
/**
|
2009-10-01 13:29:53 +02:00
|
|
|
* Result (Status) of command execution.
|
|
|
|
|
* It is returned when invoking a HandlingPattern
|
|
|
|
|
* and can be used to check for success and/or re-throw
|
|
|
|
|
* any Exception encountered during the command execution.
|
2011-06-14 05:41:02 +02:00
|
|
|
* @todo couldn't that be replaced by a lib::Result<void> instance??
|
2009-08-02 18:00:03 +02:00
|
|
|
*/
|
|
|
|
|
class ExecResult
|
|
|
|
|
: public lib::BoolCheckable<ExecResult>
|
|
|
|
|
{
|
|
|
|
|
const string log_;
|
|
|
|
|
|
|
|
|
|
public:
|
|
|
|
|
bool isValid() const;
|
|
|
|
|
void maybeThrow() const;
|
|
|
|
|
|
|
|
|
|
protected:
|
|
|
|
|
ExecResult () { } ///< default: command executed successfully
|
|
|
|
|
ExecResult (lumiera::Error const&); ///< this result marks a failed execution
|
|
|
|
|
|
|
|
|
|
friend class HandlingPattern;
|
|
|
|
|
};
|
|
|
|
|
|
2009-10-01 13:29:53 +02:00
|
|
|
|
|
|
|
|
|
2009-06-08 04:46:07 +02:00
|
|
|
/**
|
2009-10-01 13:29:53 +02:00
|
|
|
* Operation Skeleton how to invoke or undo a command.
|
|
|
|
|
* Concrete implementations may be retrieved by ID;
|
|
|
|
|
* they range from just invoking the command operations
|
|
|
|
|
* straight forward to dispatching with the ProcDispatcher
|
|
|
|
|
* or running the command asynchronously in a background thread.
|
|
|
|
|
* A HandlingPattern first of all describes how to invoke the
|
|
|
|
|
* command operation, but for each pattern it is possible to
|
|
|
|
|
* get a special "undo pattern", which, on activation, will
|
|
|
|
|
* reverse the effect of the basic pattern.
|
2009-06-08 04:46:07 +02:00
|
|
|
*/
|
|
|
|
|
class HandlingPattern
|
2009-08-11 06:32:29 +02:00
|
|
|
: public lib::BoolCheckable<HandlingPattern>
|
2009-06-08 04:46:07 +02:00
|
|
|
{
|
|
|
|
|
|
|
|
|
|
public:
|
|
|
|
|
|
2009-07-26 02:00:47 +02:00
|
|
|
enum ID
|
|
|
|
|
{ SYNC
|
|
|
|
|
, SYNC_THROW
|
|
|
|
|
, ASYNC
|
2009-09-29 18:09:34 +02:00
|
|
|
, DUMMY
|
2009-08-09 21:55:47 +02:00
|
|
|
|
|
|
|
|
, NUM_IDS
|
2009-07-26 02:00:47 +02:00
|
|
|
};
|
|
|
|
|
|
2009-10-01 14:32:26 +02:00
|
|
|
static ID defaultID() { return DUMMY; } ///////////TODO: should be ID::SYNC Ticket #211
|
2009-08-09 21:55:47 +02:00
|
|
|
|
|
|
|
|
|
2009-07-26 02:00:47 +02:00
|
|
|
static HandlingPattern const& get (ID id);
|
|
|
|
|
|
|
|
|
|
|
2009-07-24 17:50:14 +02:00
|
|
|
virtual ~HandlingPattern() {}
|
|
|
|
|
|
2009-08-02 18:00:03 +02:00
|
|
|
/** main functionality: invoke a command, detect errors.
|
|
|
|
|
* @return ExecResult object, which might later be used to
|
|
|
|
|
* detect errors on execution */
|
2009-09-21 03:11:46 +02:00
|
|
|
ExecResult invoke (CommandImpl& command, Symbol name) const;
|
2009-08-02 18:00:03 +02:00
|
|
|
|
2009-10-01 13:29:53 +02:00
|
|
|
/** @return HandlingPattern describing how the UNDO operation is to be performed */
|
|
|
|
|
HandlingPattern const& howtoUNDO() const { return getUndoPatt(); }
|
2009-08-02 18:00:03 +02:00
|
|
|
|
|
|
|
|
|
2009-08-11 06:32:29 +02:00
|
|
|
virtual bool isValid() const =0;
|
2009-08-02 18:00:03 +02:00
|
|
|
|
|
|
|
|
protected:
|
|
|
|
|
|
2009-10-01 13:29:53 +02:00
|
|
|
virtual HandlingPattern const& getUndoPatt() const =0;
|
|
|
|
|
virtual void perform (CommandImpl& command) const =0;
|
2009-08-02 18:00:03 +02:00
|
|
|
|
2009-10-01 13:29:53 +02:00
|
|
|
virtual void exec (CommandImpl& command) const =0;
|
|
|
|
|
virtual void undo (CommandImpl& command) const =0;
|
2009-07-24 17:50:14 +02:00
|
|
|
|
|
|
|
|
|
2009-08-11 06:32:29 +02:00
|
|
|
|
2009-06-08 04:46:07 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2011-12-02 17:50:44 +01:00
|
|
|
}} // namespace proc::control
|
2009-06-08 04:46:07 +02:00
|
|
|
#endif
|