2009-06-08 04:46:07 +02:00
|
|
|
/*
|
2009-06-19 14:47:37 +02:00
|
|
|
COMMAND-MUTATION.hpp - functor encapsulating the actual operation of proc-Command
|
2009-06-08 04:46:07 +02:00
|
|
|
|
|
|
|
|
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.
|
|
|
|
|
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
|
2009-06-19 14:47:37 +02:00
|
|
|
/** @file command-mutation.hpp
|
|
|
|
|
** The core of a Proc-Layer command: functor containing the actual operation to be executed.
|
2009-06-08 04:46:07 +02:00
|
|
|
** //TODO
|
|
|
|
|
**
|
|
|
|
|
** @see Command
|
|
|
|
|
** @see ProcDispatcher
|
|
|
|
|
**
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2009-06-19 19:11:33 +02:00
|
|
|
#ifndef CONTROL_COMMAND_MUTATION_H
|
|
|
|
|
#define CONTROL_COMMAND_MUTATION_H
|
2009-06-08 04:46:07 +02:00
|
|
|
|
|
|
|
|
//#include "pre.hpp"
|
2009-06-19 05:57:06 +02:00
|
|
|
#include "lib/error.hpp"
|
2009-07-06 05:26:31 +02:00
|
|
|
#include "lib/bool-checkable.hpp"
|
2009-06-19 19:11:33 +02:00
|
|
|
#include "proc/control/command-closure.hpp"
|
2009-06-28 15:27:27 +02:00
|
|
|
#include "proc/control/memento-tie.hpp"
|
2009-06-08 04:46:07 +02:00
|
|
|
|
|
|
|
|
//#include <tr1/memory>
|
2009-06-19 19:11:33 +02:00
|
|
|
#include <boost/scoped_ptr.hpp>
|
|
|
|
|
#include <tr1/functional>
|
|
|
|
|
#include <iostream>
|
|
|
|
|
#include <string>
|
2009-06-08 04:46:07 +02:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
namespace control {
|
|
|
|
|
|
|
|
|
|
// using lumiera::Symbol;
|
|
|
|
|
// using std::tr1::shared_ptr;
|
2009-06-19 19:11:33 +02:00
|
|
|
using boost::scoped_ptr;
|
|
|
|
|
using std::tr1::function;
|
|
|
|
|
using std::ostream;
|
|
|
|
|
using std::string;
|
2009-06-08 04:46:07 +02:00
|
|
|
|
|
|
|
|
|
2009-06-24 06:38:33 +02:00
|
|
|
LUMIERA_ERROR_DECLARE (UNBOUND_ARGUMENTS); ///< Mutation functor not yet usable, because arguments aren't bound
|
|
|
|
|
LUMIERA_ERROR_DECLARE (MISSING_MEMENTO); ///< Undo functor not yet usable, because no undo state has been captured
|
|
|
|
|
|
|
|
|
|
|
2009-06-08 04:46:07 +02:00
|
|
|
/**
|
|
|
|
|
* @todo Type-comment
|
|
|
|
|
*/
|
|
|
|
|
class Mutation
|
2009-07-06 05:26:31 +02:00
|
|
|
: public lib::BoolCheckable<Mutation>
|
2009-06-08 04:46:07 +02:00
|
|
|
{
|
2009-06-20 07:13:20 +02:00
|
|
|
CmdFunctor func_;
|
2009-06-24 06:38:33 +02:00
|
|
|
CmdClosure* clo_;
|
2009-06-08 04:46:07 +02:00
|
|
|
|
|
|
|
|
public:
|
2009-06-19 05:57:06 +02:00
|
|
|
template<typename SIG>
|
|
|
|
|
Mutation (function<SIG> const& func)
|
2009-06-23 05:54:54 +02:00
|
|
|
: func_(func),
|
|
|
|
|
clo_(0)
|
2009-06-20 07:13:20 +02:00
|
|
|
{ }
|
2009-06-08 04:46:07 +02:00
|
|
|
|
2009-06-19 05:57:06 +02:00
|
|
|
virtual ~Mutation() {}
|
|
|
|
|
|
2009-06-19 19:11:33 +02:00
|
|
|
|
2009-06-19 05:57:06 +02:00
|
|
|
virtual Mutation&
|
2009-06-24 06:38:33 +02:00
|
|
|
close (CmdClosure& cmdClosure)
|
2009-06-19 05:57:06 +02:00
|
|
|
{
|
2009-06-23 05:54:54 +02:00
|
|
|
REQUIRE (!clo_, "Lifecycle error: already closed over the arguments");
|
2009-06-26 19:04:22 +02:00
|
|
|
REQUIRE (func_, "Param error: not bound to a valid function");
|
2009-06-24 06:38:33 +02:00
|
|
|
func_ = cmdClosure.bindArguments(func_);
|
2009-06-23 05:54:54 +02:00
|
|
|
clo_ = &cmdClosure;
|
2009-06-19 19:11:33 +02:00
|
|
|
return *this;
|
2009-06-19 05:57:06 +02:00
|
|
|
}
|
|
|
|
|
|
2009-06-19 19:11:33 +02:00
|
|
|
|
2009-06-19 05:57:06 +02:00
|
|
|
void
|
|
|
|
|
operator() ()
|
|
|
|
|
{
|
2009-06-23 05:54:54 +02:00
|
|
|
if (!clo_)
|
|
|
|
|
throw lumiera::error::State ("Lifecycle error: function arguments not yet provided",
|
|
|
|
|
LUMIERA_ERROR_UNBOUND_ARGUMENTS);
|
|
|
|
|
invoke (func_);
|
2009-06-19 05:57:06 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
2009-07-06 05:26:31 +02:00
|
|
|
/** diagnostics */
|
2009-06-19 05:57:06 +02:00
|
|
|
operator string() const
|
|
|
|
|
{
|
2009-06-19 19:11:33 +02:00
|
|
|
return isValid()? string (*clo_) : "Mutation(state missing)";
|
2009-06-19 05:57:06 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
virtual bool
|
2009-06-19 19:11:33 +02:00
|
|
|
isValid () const
|
2009-06-19 05:57:06 +02:00
|
|
|
{
|
2009-06-26 19:04:22 +02:00
|
|
|
return func_ && clo_;
|
2009-06-23 05:54:54 +02:00
|
|
|
}
|
|
|
|
|
|
2009-07-06 05:26:31 +02:00
|
|
|
protected:
|
2009-06-23 05:54:54 +02:00
|
|
|
void
|
|
|
|
|
invoke (CmdFunctor & closedFunction)
|
|
|
|
|
{
|
|
|
|
|
closedFunction.getFun<void()>() ();
|
|
|
|
|
}
|
2009-06-08 04:46:07 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
2009-06-28 15:27:27 +02:00
|
|
|
inline ostream& operator<< (ostream& os, Mutation const& muta) { return os << string(muta); }
|
2009-06-19 05:57:06 +02:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2009-06-08 04:46:07 +02:00
|
|
|
/**
|
|
|
|
|
* @todo Type-comment
|
|
|
|
|
*/
|
|
|
|
|
class UndoMutation
|
|
|
|
|
: public Mutation
|
|
|
|
|
{
|
2009-07-06 05:26:31 +02:00
|
|
|
Mutation captureMemento_;
|
2009-06-08 04:46:07 +02:00
|
|
|
|
|
|
|
|
public:
|
2009-07-06 05:26:31 +02:00
|
|
|
template<typename TIE>
|
|
|
|
|
UndoMutation (TIE & mementoHolder)
|
|
|
|
|
: Mutation (mementoHolder.tieUndoFunc())
|
|
|
|
|
, captureMemento_(mementoHolder.tieCaptureFunc())
|
2009-06-19 19:11:33 +02:00
|
|
|
{ }
|
|
|
|
|
|
|
|
|
|
|
2009-06-24 05:51:02 +02:00
|
|
|
virtual Mutation&
|
2009-06-24 06:38:33 +02:00
|
|
|
close (CmdClosure& cmdClosure)
|
2009-06-24 05:51:02 +02:00
|
|
|
{
|
2009-07-06 05:26:31 +02:00
|
|
|
Mutation::close(cmdClosure);
|
|
|
|
|
captureMemento_.close(cmdClosure);
|
2009-06-24 05:51:02 +02:00
|
|
|
return *this;
|
|
|
|
|
}
|
2009-07-06 05:26:31 +02:00
|
|
|
|
2009-06-24 05:51:02 +02:00
|
|
|
|
2009-06-19 05:57:06 +02:00
|
|
|
Mutation&
|
|
|
|
|
captureState ()
|
|
|
|
|
{
|
2009-06-23 05:54:54 +02:00
|
|
|
if (!Mutation::isValid())
|
|
|
|
|
throw lumiera::error::State ("need to bind function arguments prior to capturing undo state",
|
|
|
|
|
LUMIERA_ERROR_UNBOUND_ARGUMENTS);
|
|
|
|
|
|
2009-07-06 05:26:31 +02:00
|
|
|
captureMemento_();
|
2009-06-23 05:54:54 +02:00
|
|
|
return *this;
|
2009-06-19 05:57:06 +02:00
|
|
|
}
|
|
|
|
|
|
2009-06-23 05:54:54 +02:00
|
|
|
|
|
|
|
|
private:
|
|
|
|
|
virtual bool
|
|
|
|
|
isValid () const
|
|
|
|
|
{
|
2009-07-06 05:26:31 +02:00
|
|
|
return Mutation::isValid() && memento_;
|
2009-06-19 05:57:06 +02:00
|
|
|
}
|
2009-06-08 04:46:07 +02:00
|
|
|
|
2009-06-23 05:54:54 +02:00
|
|
|
|
2009-06-08 04:46:07 +02:00
|
|
|
};
|
|
|
|
|
////////////////TODO currently just fleshing out the API....
|
|
|
|
|
|
2009-06-19 19:11:33 +02:00
|
|
|
|
2009-06-08 04:46:07 +02:00
|
|
|
|
|
|
|
|
|
|
|
|
|
} // namespace control
|
|
|
|
|
#endif
|