2009-07-27 02:38:53 +02:00
|
|
|
/*
|
|
|
|
|
COMMAND-IMPL.hpp - Proc-Layer command implementation (top level)
|
|
|
|
|
|
|
|
|
|
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-impl.hpp
|
|
|
|
|
** Top level of the command implementation. CommandImpl holds together
|
|
|
|
|
** the various data and sub-objects involved into the inner workings of a
|
|
|
|
|
** Proc-Layer command. It serves to implement a "command definition" (prototype)
|
|
|
|
|
** as well as a concrete command instance. It is a data holder with a well defined
|
|
|
|
|
** identity and usually located within the (pooled) storage managed by the
|
|
|
|
|
** CommandRegistry. Client code gets access to a specific CommandImpl through
|
|
|
|
|
** a Command instance, which is a small (refcounting smart-ptr) handle.
|
|
|
|
|
**
|
|
|
|
|
** //TODO
|
|
|
|
|
**
|
|
|
|
|
** @see Command
|
|
|
|
|
** @see ProcDispatcher
|
|
|
|
|
**
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
#ifndef CONTROL_COMMAND_IMPL_H
|
|
|
|
|
#define CONTROL_COMMAND_IMPL_H
|
|
|
|
|
|
|
|
|
|
#include "proc/control/command.hpp"
|
2009-09-21 03:15:06 +02:00
|
|
|
#include "proc/control/command-closure.hpp"
|
2009-08-01 02:18:01 +02:00
|
|
|
#include "proc/control/command-mutation.hpp"
|
2009-10-02 23:12:36 +02:00
|
|
|
#include "lib/typed-allocation-manager.hpp"
|
2009-08-01 02:18:01 +02:00
|
|
|
#include "lib/bool-checkable.hpp"
|
|
|
|
|
|
|
|
|
|
#include <boost/noncopyable.hpp>
|
|
|
|
|
#include <boost/operators.hpp>
|
2009-07-27 02:38:53 +02:00
|
|
|
|
2009-08-03 18:17:41 +02:00
|
|
|
#include <tr1/memory>
|
|
|
|
|
#include <tr1/functional>
|
2009-07-27 02:38:53 +02:00
|
|
|
|
|
|
|
|
|
|
|
|
|
namespace control {
|
|
|
|
|
|
2009-10-02 23:12:36 +02:00
|
|
|
using lib::TypedAllocationManager;
|
2009-08-03 18:17:41 +02:00
|
|
|
using std::tr1::function;
|
|
|
|
|
using std::tr1::shared_ptr;
|
2009-09-21 03:15:06 +02:00
|
|
|
|
2009-07-27 02:38:53 +02:00
|
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
2009-09-21 03:15:06 +02:00
|
|
|
* Proc-Layer Command implementation.
|
|
|
|
|
* Data record holding together the parts necessary for command execution
|
|
|
|
|
* - command operation functor
|
|
|
|
|
* - a functor to UNDO the command effect
|
|
|
|
|
* - closure holding actual parameters and UNDO state
|
2009-07-27 02:38:53 +02:00
|
|
|
*/
|
|
|
|
|
class CommandImpl
|
2009-08-01 02:18:01 +02:00
|
|
|
: public lib::BoolCheckable<CommandImpl
|
|
|
|
|
, boost::noncopyable
|
2009-08-01 23:57:12 +02:00
|
|
|
>
|
2009-07-27 02:38:53 +02:00
|
|
|
{
|
2009-08-01 02:18:01 +02:00
|
|
|
Mutation do_;
|
|
|
|
|
UndoMutation undo_;
|
2009-07-27 02:38:53 +02:00
|
|
|
|
2009-08-03 18:17:41 +02:00
|
|
|
shared_ptr<CmdClosure> pClo_;
|
|
|
|
|
|
2009-08-09 21:55:47 +02:00
|
|
|
HandlingPattern::ID defaultPatt_;
|
|
|
|
|
|
2009-08-03 18:17:41 +02:00
|
|
|
|
|
|
|
|
template<typename ARG>
|
|
|
|
|
struct _Type
|
|
|
|
|
{
|
|
|
|
|
typedef typename ARG::SIG_op SIG_op;
|
|
|
|
|
typedef typename ARG::SIG_cap SIG_cap;
|
|
|
|
|
typedef typename ARG::SIG_undo SIG_undo;
|
|
|
|
|
|
|
|
|
|
typedef function<SIG_op> Func_op;
|
|
|
|
|
typedef function<SIG_cap> Func_cap;
|
|
|
|
|
typedef function<SIG_undo> Func_undo;
|
|
|
|
|
};
|
|
|
|
|
#define _TY(_ID_) typename _Type<ARG>::_ID_
|
|
|
|
|
|
2009-07-27 02:38:53 +02:00
|
|
|
public:
|
2009-08-03 18:17:41 +02:00
|
|
|
/** build a new implementation frame, and do the initial wiring.
|
|
|
|
|
* On the interface the specific type is discarded afterwards.
|
|
|
|
|
* This information is still kept though, as encoded into the vtable
|
|
|
|
|
* of the embedded FunErasure objects holding the command operation
|
|
|
|
|
* and undo functors, and the vtable of the embedded CmdClosure */
|
|
|
|
|
template<typename ARG>
|
|
|
|
|
CommandImpl (shared_ptr<ARG> pArgHolder
|
|
|
|
|
,_TY (Func_op) const& operFunctor
|
|
|
|
|
,_TY (Func_cap) const& captFunctor
|
|
|
|
|
,_TY (Func_undo) const& undoFunctor
|
|
|
|
|
)
|
|
|
|
|
: do_(operFunctor)
|
|
|
|
|
, undo_(pArgHolder->tie (undoFunctor, captFunctor))
|
|
|
|
|
, pClo_(pArgHolder)
|
2009-08-09 21:55:47 +02:00
|
|
|
, defaultPatt_(HandlingPattern::defaultID())
|
2009-08-03 18:17:41 +02:00
|
|
|
{ }
|
|
|
|
|
|
2009-08-09 21:55:47 +02:00
|
|
|
#undef _TY
|
2009-07-27 02:38:53 +02:00
|
|
|
|
|
|
|
|
|
|
|
|
|
~CommandImpl();
|
|
|
|
|
|
|
|
|
|
|
2009-10-09 19:50:16 +02:00
|
|
|
/** TODO: kill kill kill */
|
|
|
|
|
CommandImpl (CommandImpl const& orig, TypedAllocationManager& storageManager)
|
|
|
|
|
: do_(orig.do_)
|
|
|
|
|
, undo_(orig.undo_)
|
2009-10-10 04:24:27 +02:00
|
|
|
// , pClo_(orig.pClo_->createClone(storageManager))
|
2009-10-09 19:50:16 +02:00
|
|
|
, defaultPatt_(orig.defaultPatt_)
|
|
|
|
|
{ }
|
|
|
|
|
|
|
|
|
|
|
2009-08-06 18:00:19 +02:00
|
|
|
/** cloning service for the CommandRegistry:
|
2009-08-09 04:12:46 +02:00
|
|
|
* effectively this is a copy ctor, but as we rely
|
|
|
|
|
* on a argument holder (without knowing the exact type),
|
|
|
|
|
* we need to delegate the cloning of the arguments down
|
|
|
|
|
* while providing a means of allocating storage for the clone */
|
2009-10-10 04:24:27 +02:00
|
|
|
CommandImpl (CommandImpl const& orig
|
|
|
|
|
,UndoMutation const& newUndo
|
|
|
|
|
,shared_ptr<CmdClosure> const& newClosure)
|
2009-08-06 18:00:19 +02:00
|
|
|
: do_(orig.do_)
|
2009-10-09 19:50:16 +02:00
|
|
|
, undo_(newUndo)
|
|
|
|
|
, pClo_(newClosure)
|
2009-08-09 21:55:47 +02:00
|
|
|
, defaultPatt_(orig.defaultPatt_)
|
2009-08-06 18:00:19 +02:00
|
|
|
{ }
|
2009-07-27 02:38:53 +02:00
|
|
|
|
|
|
|
|
|
2009-10-10 04:24:27 +02:00
|
|
|
/** assist with building a clone copy of this CommandImpl.
|
|
|
|
|
* By accepting the clone builder as a visitor and dispatching
|
|
|
|
|
* this visitation down into the concrete closure, the builder
|
|
|
|
|
* can re-gain the fully typed context available on creation
|
|
|
|
|
* of the ComandImpl. Within this context, for the clone
|
|
|
|
|
* to be created, the UndoMutation has to be re-wired,
|
|
|
|
|
* otherwise it would continue to cooperate with
|
|
|
|
|
* original closure.
|
|
|
|
|
*/
|
|
|
|
|
void
|
|
|
|
|
prepareClone (CommandImplCloneBuilder& visitor) const
|
|
|
|
|
{
|
|
|
|
|
ASSERT (pClo_);
|
|
|
|
|
pClo_->accept(visitor);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
2009-08-09 21:55:47 +02:00
|
|
|
void
|
|
|
|
|
setArguments (Arguments& args)
|
2009-08-03 18:17:41 +02:00
|
|
|
{
|
2009-08-04 02:25:50 +02:00
|
|
|
pClo_->bindArguments(args);
|
2009-08-03 18:17:41 +02:00
|
|
|
}
|
2009-08-09 21:55:47 +02:00
|
|
|
|
2009-09-21 03:11:46 +02:00
|
|
|
void invokeOperation() { do_(*pClo_); }
|
|
|
|
|
void invokeCapture() { undo_.captureState(*pClo_); }
|
|
|
|
|
void invokeUndo() { undo_(*pClo_); }
|
|
|
|
|
|
|
|
|
|
|
2009-08-09 21:55:47 +02:00
|
|
|
|
|
|
|
|
typedef HandlingPattern::ID PattID;
|
|
|
|
|
|
|
|
|
|
PattID
|
|
|
|
|
getDefaultHandlingPattern() const
|
|
|
|
|
{
|
|
|
|
|
return defaultPatt_;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/** define a handling pattern to be used by default
|
|
|
|
|
* @return ID of the currently defined default pattern */
|
|
|
|
|
PattID
|
|
|
|
|
setHandlingPattern (PattID newID)
|
|
|
|
|
{
|
|
|
|
|
PattID currID = defaultPatt_;
|
|
|
|
|
defaultPatt_ = newID;
|
|
|
|
|
return currID;
|
|
|
|
|
}
|
|
|
|
|
|
2009-07-27 02:38:53 +02:00
|
|
|
|
|
|
|
|
|
|
|
|
|
/* === diagnostics === */
|
|
|
|
|
|
2009-08-01 02:18:01 +02:00
|
|
|
bool
|
2009-08-11 06:32:29 +02:00
|
|
|
isValid() const ///< validity self-check: is basically usable.
|
2009-08-01 02:18:01 +02:00
|
|
|
{
|
2009-08-11 06:32:29 +02:00
|
|
|
return bool(pClo_)
|
|
|
|
|
&& HandlingPattern::get(defaultPatt_).isValid();
|
2009-08-01 02:18:01 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
bool
|
2009-08-11 06:32:29 +02:00
|
|
|
canExec() const ///< state check: sufficiently defined to be invoked
|
2009-08-01 02:18:01 +02:00
|
|
|
{
|
2009-08-11 06:32:29 +02:00
|
|
|
return isValid()
|
2009-10-05 06:07:21 +02:00
|
|
|
&& pClo_->isValid();
|
2009-08-01 02:18:01 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
bool
|
2009-08-11 06:32:29 +02:00
|
|
|
canUndo() const ///< state check: has undo state been captured?
|
2009-08-01 02:18:01 +02:00
|
|
|
{
|
2009-09-21 03:33:56 +02:00
|
|
|
return isValid() && pClo_->isCaptured();
|
2009-08-01 02:18:01 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
2009-10-05 06:07:21 +02:00
|
|
|
|
2009-10-05 04:29:06 +02:00
|
|
|
friend bool
|
|
|
|
|
operator== (CommandImpl const& ci1, CommandImpl const& ci2)
|
|
|
|
|
{
|
|
|
|
|
return (ci1.do_ == ci2.do_)
|
2009-10-05 06:07:21 +02:00
|
|
|
// && (ci1.undo_ == ci2.undo_) // causes failure regularly, due to the missing equality on boost::function. See Ticket #294
|
2009-10-05 04:29:06 +02:00
|
|
|
&& (ci1.defaultPatt_ == ci2.defaultPatt_)
|
|
|
|
|
&& (ci1.canExec() == ci2.canExec())
|
|
|
|
|
&& (ci1.canUndo() == ci2.canUndo())
|
|
|
|
|
&& (ci1.pClo_->equals(*ci2.pClo_))
|
|
|
|
|
;
|
|
|
|
|
}
|
2009-10-05 06:07:21 +02:00
|
|
|
|
|
|
|
|
friend bool
|
|
|
|
|
operator!= (CommandImpl const& ci1, CommandImpl const& ci2)
|
|
|
|
|
{
|
|
|
|
|
return !(ci1==ci2);
|
|
|
|
|
}
|
2009-07-27 02:38:53 +02:00
|
|
|
};
|
|
|
|
|
|
2009-09-21 03:15:06 +02:00
|
|
|
|
|
|
|
|
|
2009-07-27 02:38:53 +02:00
|
|
|
|
|
|
|
|
|
|
|
|
|
} // namespace control
|
|
|
|
|
#endif
|