Lumiera  0.pre.03
»edityourfreedom«
command-def.hpp
Go to the documentation of this file.
1 /*
2  COMMAND-DEF.hpp - defining and binding a Proc-Layer command
3 
4  Copyright (C) Lumiera.org
5  2009, Hermann Vosseler <Ichthyostega@web.de>
6 
7  This program is free software; you can redistribute it and/or
8  modify it under the terms of the GNU General Public License as
9  published by the Free Software Foundation; either version 2 of
10  the License, or (at your option) any later version.
11 
12  This program is distributed in the hope that it will be useful,
13  but WITHOUT ANY WARRANTY; without even the implied warranty of
14  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15  GNU General Public License for more details.
16 
17  You should have received a copy of the GNU General Public License
18  along with this program; if not, write to the Free Software
19  Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
20 
21 */
22 
23 
53 #ifndef CONTROL_COMMAND_DEF_H
54 #define CONTROL_COMMAND_DEF_H
55 
56 #include "lib/error.hpp"
57 #include "lib/nocopy.hpp"
58 #include "include/logging.h"
59 #include "lib/symbol.hpp"
60 #include "proc/control/command.hpp"
61 #include "proc/control/command-impl.hpp" // note: at command definition site we need to know the full type
66 #include "lib/meta/function.hpp"
67 #include "lib/meta/typelist.hpp"
70 #include "lib/util.hpp"
71 
72 #include <memory>
73 #include <functional>
74 
75 
76 
77 
78 namespace proc {
79 namespace control {
80 
81  using std::shared_ptr;
82  using std::function;
83  using std::bind;
84  using std::placeholders::_1;
85  using std::tuple_size;
86  using lib::Symbol;
87  using util::cStr;
88 
89  using lib::meta::_Fun;
90  using lib::meta::NullType;
91  using lib::meta::Types;
92  using lib::meta::TySeq;
93  using lib::meta::Tuple;
94 
95 
96 
97 
98  namespace stage {
99 
101  using Activation = function<Command&(ImplInstance &&)>;
102 
103 
104 
105 
106  template<typename SIG>
108  : AcceptArgumentBindingRet< Command&, SIG // Return type and Argument Signature of the \c bind(..) function
109  , CompletedDefinition<SIG> // target type (this class) providing the implementation \c bindArg(Tuple<..>)
110  >
111  {
113 
114  using CmdArgs = typename _Fun<SIG>::Args;
115 
116  CompletedDefinition (Command& definedCommand)
117  : prototype_(definedCommand)
118  {
119  REQUIRE (prototype_);
121  TRACE (command_dbg, "Completed definition of %s.", cStr(prototype_));
122  }
123 
124 
126 
130  setHandlingPattern (PattID newID)
131  {
132  prototype_.setHandlingPattern(newID);
133  return *this;
134  }
135 
136 
140  Command&
141  bindArg (Tuple<CmdArgs> const& params)
142  {
143  return prototype_.bindArg(params);
144  }
145 
146 
150  operator Command ()
151  {
152  return prototype_;
153  }
154 
155  private:
160  void
162  {
163  if (0 == tuple_size<Tuple<CmdArgs>>::value )
164  prototype_.bindArg<> (std::tuple<>());
165  }
166  };
167 
168 
169 
170 
171 
172  template<typename SIG, typename MEM>
174  {
179  typedef typename CmdType::CmdArgs CmdArgs;
180 
181  typedef function<CommandOperationSig> OperFunc;
182  typedef function<UndoOperationSig> UndoFunc;
183  typedef function<UndoCaptureSig> CaptFunc;
184 
186  OperFunc operFunctor_;
187  CaptFunc captFunctor_;
188  UndoFunc undoFunctor_;
189 
190 
191  UndoDefinition (Activation const& whenComplete,
192  OperFunc const& commandOperation,
193  CaptFunc const& undoCapOperation)
194  : activatePrototype_(whenComplete)
195  , operFunctor_(commandOperation)
196  , captFunctor_(undoCapOperation)
197  , undoFunctor_()
198  { }
199 
200 
202  undoOperation (UndoOperationSig how_to_Undo)
203  {
204  undoFunctor_ = UndoFunc (how_to_Undo);
205  REQUIRE (operFunctor_);
206  REQUIRE (undoFunctor_);
207  REQUIRE (captFunctor_);
208 
210  ImplInstance completedDef = registry.newCommandImpl(operFunctor_
211  ,captFunctor_
212  ,undoFunctor_);
213  return CompletedDefinition<SIG> {activatePrototype_(move (completedDef))};
214  }
215  };
216 
217 
218 
219 
222  template<typename U_SIG>
224  {
226  };
227 
228 
229 
230 
231 
232 
233  template<typename SIG>
235  {
237  function<SIG> operation_;
238 
239  BasicDefinition(Activation const& whenComplete, function<SIG> const& operation)
240  : callback_(whenComplete)
241  , operation_(operation)
242  { }
243 
244 
245  template<typename FUN2>
246  auto
247  captureUndo (FUN2 how_to_capture_UndoState)
248  {
249  using Sig2 = typename _Fun<FUN2>::Sig;
250  using UndoCapSig = typename UndoSignature<Sig2>::CaptureSig;
251  using SpecificUndoDefinition = typename BuildUndoDefType<UndoSignature<Sig2>>::Type;
252 
253  function<UndoCapSig> captureOperation (how_to_capture_UndoState);
254  return SpecificUndoDefinition (callback_, operation_, captureOperation);
255  }
256  };
257 
258  } // (END) namespace stage (definition process)
259 
260 
280  {
283 
286 
287  public:
289  : id_(cmdID)
290  , prototype_(Command::fetchDef(cmdID))
291  {
292  TRACE (command_dbg, "starting CommandDef('%s')...", cmdID.c() );
293  }
294 
295  ~CommandDef();
296 
297 
298 
299  template<typename FUN>
300  auto
301  operation (FUN operation_to_define)
302  {
303  using Sig = typename _Fun<FUN>::Sig;
304 
305  function<Sig> opera1 (operation_to_define);
306  Activation callback_when_defined = bind (&CommandDef::activate, this, _1);
307 
308  return stage::BasicDefinition<Sig>(callback_when_defined, opera1);
309  }
310 
311 
312  explicit operator bool() const { return isValid(); }
313 
314  bool isValid() const;
315 
316 
317  private:
321  Command& activate (PImpl && completedDef)
322  {
323  prototype_.activate (move (completedDef), id_);
324  ENSURE (prototype_);
325  return prototype_;
326  }
327  };
328 
329 
330 
331 
332 
333 }} // namespace proc::control
334 #endif
ChPredicate isValid(is_alnum() or is_any_of("-_.+$()@"))
characters to be retained
auto operation(FUN operation_to_define)
A template metaprogramming technique for manipulating collections of types.
Core of a Proc-Layer command: functor containing the operation to be executed.
type re-binding helper: create a suitable UndoDefinition type, based on the UndoSignature template in...
CompletedDefinition(Command &definedCommand)
Mixin-templates providing arbitrary function call operators and argument binding functions.
Any copy and copy construction prohibited.
Definition: nocopy.hpp:46
Top level of the command implementation.
typename FunctionTypedef< MEM, Args >::Sig CaptureSig
Managing command definitions and the storage of individual command objects.
typename FunctionTypedef< void, Args >::Sig OperateSig
function< UndoCaptureSig > CaptFunc
function< Command &(ImplInstance &&)> Activation
Helper for uniform access to function signature types.
Definition: function.hpp:108
temporary workaround: alternative definition of "type sequence", already using variadic template para...
Variation of AcceptArgumentBinding, allowing to control the return type of the generated bind(...
shared_ptr< CommandImpl > ImplInstance
< helpers for building up a command definition
This header is for including and configuring NoBug.
Command & activate(PImpl &&completedDef)
callback from completed command definition stage: "arm up" the command handle object and register it ...
const char * c() const
Definition: symbol.hpp:92
HandlingPattern::ID setHandlingPattern(HandlingPattern::ID)
define a handling pattern to be used by default
Definition: command.cpp:479
Helper class used solely for defining a Command-Object.
typename BuildTupleType< TYPES >::Type Tuple
Build a std::tuple from types given as type sequence.
function< CommandOperationSig > OperFunc
const char * cStr(string const &org)
convenience shortcut: conversion to c-String via string.
Definition: util.hpp:397
Metaprogramming with tuples-of-types and the std::tuple record.
CmdType::OperateSig CommandOperationSig
Registry managing command implementation objects (Singleton).
Token or Atom with distinct identity.
Definition: symbol.hpp:116
CompletedDefinition< SIG > undoOperation(UndoOperationSig how_to_Undo)
shared_ptr< CommandImpl > newCommandImpl(function< SIG_OPER > &operFunctor, function< SIG_CAPT > &captFunctor, function< SIG_UNDO > &undoFunctor)
set up a new command implementation frame
Mix-Ins to allow or prohibit various degrees of copying and cloning.
BasicDefinition(Activation const &whenComplete, function< SIG > const &operation)
Metaprogramming tools for transforming functor types.
Marker types to indicate a literal string and a Symbol.
void activate(shared_ptr< CommandImpl > &&, Symbol cmdID=0)
Definition: command.cpp:169
auto captureUndo(FUN2 how_to_capture_UndoState)
Tiny helper functions and shortcuts to be used everywhere Consider this header to be effectively incl...
typename Case< Ret, Args >::CaptureSig CaptureSig
stage::Activation Activation
Proc-Layer command frontend.
CommandSignature< SIG, MEM > CmdType
Lumiera error handling (C++ interface).
TRACE(test, "inserted %d", data)
function< UndoOperationSig > UndoFunc
typename FunctionTypedef< void, ExtendedArgs >::Sig UndoOp_Sig
UndoDefinition(Activation const &whenComplete, OperFunc const &commandOperation, CaptFunc const &undoCapOperation)
static lib::Depend< CommandRegistry > instance
storage for the singleton factory used to access CommandRegistry
CompletedDefinition setHandlingPattern(PattID newID)
allow for defining the default execution pattern, which is used by Command::operator() ...
Handle object representing a single Command instance to be used by client code.
Definition: command.hpp:125
Metaprogramming helpers for deriving the precise function signatures necessary to implement a given c...
Command & bindArg(Tuple< CmdArgs > const &params)
allow to bind immediately to a set of arguments.
Proc-Layer implementation namespace root.
Definition: id-scheme.hpp:63
Command & bindArg(std::tuple< TYPES... > const &)
Definition: command.hpp:254
Metaprogramming: Helpers for manipulating lists-of-types.
ENSURE(r==&pq)
Metaprogramming helper for building Command function signatures.
typename _Fun< SIG >::Args CmdArgs
void maybeArm_if_zero_parameters()
Helper: automatically "bind" and make executable a command, for the case when the command operation t...