コード例 #1
0
 /** @test simulate a complete command lifecycle with regards to the
  *        storage handling of the command parameters and state memento.
  */
 void
 simulateCmdLifecycle()
   {
     typedef void SIG_do(Tracker<TimeVar>, Tracker<string>, int);
     using Args      = StorageHolder<SIG_do, Tracker<string>>;
     using MemHolder = MementoTie<SIG_do, Tracker<string>>;
     
     Args args;
     CHECK (isnil (args));
     cout << showSizeof(args) << endl;
     
     // store a set of parameter values, later to be used on invocation
     args.storeTuple (
       make_tuple (TTime(randTime()), Tstr("Lumiera rocks"), twoRandomDigits() ));
     CHECK (!isnil (args));
     cout << args << endl;
     
     CHECK (!args.canUndo());
     VERIFY_ERROR(MISSING_MEMENTO,  args.memento() );
     
     MemHolder& memHolder = args.tie(undoIt,captureState);
     CHECK (!memHolder);   // no stored memento....
     CHECK (!args.canUndo());
     
     function<SIG_do> doItFun = doIt;
     function<SIG_do> undoFun = memHolder.tieUndoFunc();
     function<SIG_do> captFun = memHolder.tieCaptureFunc();
     
     typedef function<void()> OpFun;
     
     // now close all the functions with the stored parameter values...
     OpFun bound_doItFun = std::bind (&CmdClosure::invoke, args, CmdFunctor(doItFun));
     OpFun bound_undoFun = std::bind (&CmdClosure::invoke, args, CmdFunctor(undoFun));
     OpFun bound_captFun = std::bind (&CmdClosure::invoke, args, CmdFunctor(captFun));
     
     protocol.seekp(0);
     protocol << "START...";
     
     bound_captFun();
     cout << "captured state: " << args.memento() << endl;
     CHECK (memHolder);
     CHECK (!isnil (*args.memento()));
     CHECK (args.canUndo());
     cout << args << endl;
     
     bound_doItFun();
     cout << protocol.str() << endl;
     bound_undoFun();
     cout << protocol.str() << endl;
     
     // Commands can serve as prototype to be copied....
     Args argsCopy (args);
     bound_captFun();
     protocol.seekp(0);
     protocol << "RESET...";
     
     args.storeTuple (
       make_tuple (TTime(TimeValue(123456)), Tstr("unbelievable"), twoRandomDigits() ));
     cout << "modified: " << args     << endl;
     cout << "copied  : " << argsCopy << endl;    // holds still the old params & memento
     
     bound_undoFun();
     cout << protocol.str() << endl;
   }