VAPoR  3.0.0
command.h
Go to the documentation of this file.
1 //************************************************************************
2 // *
3 // Copyright (C) 2004 *
4 // University Corporation for Atmospheric Research *
5 // All Rights Reserved *
6 // *
7 //************************************************************************/
8 //
9 // File: command.h
10 //
11 // Author: Alan Norton
12 // National Center for Atmospheric Research
13 // PO 3000, Boulder, Colorado
14 //
15 // Date: October 2004
16 //
17 //
18 
19 #ifndef COMMAND_H
20 #define COMMAND_H
21 
22 #define MAX_HISTORY 1000
23 
24 #include "params.h"
25 #include "vapor/ParamNode.h"
26 
27 namespace VAPoR{
28 class Params;
29 
49 typedef void (*UndoRedoHelpCB_T) (bool isUndo, int instance, VAPoR::Params* beforeP, VAPoR::Params* afterP, VAPoR::Params* auxBP, VAPoR::Params* auxAP);
51  public:
52 
56  Command(Params*, const char* descr, UndoRedoHelpCB_T helper = 0, Params* auxParams = 0);
57  virtual ~Command( ) {
58  if (_nextRoot) delete _nextRoot;
59  if (_prevRoot) delete _prevRoot;
60  if (_nextAuxRoot) delete _nextAuxRoot;
61  if (_prevAuxRoot) delete _prevAuxRoot;
62  _tag.clear();
63  _auxTag.clear();
64  }
65 
69  static Params* BackupQueue();
70 
74  static Params* AdvanceQueue();
75 
77  static void resetCommandQueue();
78 
79 
81  //sa blockCapture, unblockCapture
82  // \return bool indicates whether or not recording is enabled.
83  static bool isRecording() {return (_recordingCount == 0);}
88  static Command* CurrentCommand(int offset) {
89  int posn = _currentQueuePos - offset;
90  while (posn < 0) posn += MAX_HISTORY;
91  return _commandQueue[posn%MAX_HISTORY];
92  }
96  void setDescription(const char* str){
97  _description = str;
98  }
101  string getDescription(){return _description;}
102 
115  static Command* CaptureStart(Params* prevParams, const char* desc, UndoRedoHelpCB_T helper = 0, Params* prevAuxParams = 0){
116  if (!isRecording()) return 0;
117  Command* cmd = new Command(prevParams, desc, helper, prevAuxParams);
118  blockCapture();
119  return cmd;
120  }
127  static void CaptureEnd(Command* pCom, Params *nextParams, Params* nextAuxParams = 0){
128  if (!pCom) return;
129  if (nextParams){
130  pCom->_nextRoot = nextParams->GetRootNode()->deepCopy();
131  if (!pCom->_prevRoot){ //If the first Params was null, get the tag, etc. from the next params:
132  pCom->_tag = nextParams->GetName();
133  pCom->_instance = nextParams->GetInstanceIndex();
134  pCom->_winnum = nextParams->GetVizNum();
135  }
136  }
137  if (nextAuxParams){
138  pCom->_nextAuxRoot = nextAuxParams->GetRootNode()->deepCopy();
139  if (!pCom->_prevAuxRoot){ //If the first AuxParams was null, get the tag, etc. from the next params:
140  pCom->_auxTag = nextAuxParams->GetName();
141  pCom->_auxInstance = nextAuxParams->GetInstanceIndex();
142  pCom->_auxWinnum = nextAuxParams->GetVizNum();
143  }
144  }
145 
146  unblockCapture();
147  AddToHistory(pCom);
148  }
149 
150 
155  Params* CopyParams(ParamNode* rootNode);
160  Params* CopyAuxParams(ParamNode* rootNode);
161 
162 #ifndef DOXYGEN_SKIP_THIS
163  //Anytime it is necessary to stop inserting into the _commandQueue, call blockCapture,
164  //then unblock when they are done. No recording happens until all blockers has stopped
165  //blocking it. This is not currently regarded as part of the public API.
166  //
169  //sa unblockCapture, isRecording
170  static void blockCapture() {_recordingCount++;}
173  //sa blockCapture, isRecording
174  static void unblockCapture() {_recordingCount--; assert(_recordingCount>=0);}
175 private:
176 
177  void applyHelpers(bool isUndo, int instance, Params* prev, Params* next);
183  static int AddToHistory(Command* cmd, bool ignoreBlocking = false);
184  static Command* CurrentUndoCommand() {return CurrentCommand(0);}
185  static Command* CurrentRedoCommand() {return CurrentCommand(-1);}
189  static Params* reDo();
193  static Params* unDo();
194 
195  UndoRedoHelpCB_T _undoRedoHelper;
196  string _description;
197  ParamNode* _prevRoot;
198  ParamNode* _nextRoot;
199  ParamNode* _prevAuxRoot;
200  ParamNode* _nextAuxRoot;
201  string _tag;
202  string _auxTag;
203  int _instance;
204  int _auxInstance;
205  int _winnum;
206  int _auxWinnum;
207  //Statics indicate state of queue:
208  static Command* _commandQueue[MAX_HISTORY];
209  static bool _first;
210  static int _currentQueuePos;
211  static int _endQueuePos;
212  static int _startQueuePos;
213  static int _recordingCount;
214 #endif //DOXYGEN_SKIP_THIS
215 };
216 
217 };
218 #endif
219 
void setDescription(const char *str)
Definition: command.h:96
ParamNode * GetRootNode() const
Definition: ParamsBase.h:177
A pure virtual class for managing parameters used in visualization.
Definition: params.h:129
virtual int GetInstanceIndex()
static Command * CaptureStart(Params *prevParams, const char *desc, UndoRedoHelpCB_T helper=0, Params *prevAuxParams=0)
Definition: command.h:115
string getDescription()
Definition: command.h:101
static Command * CurrentCommand(int offset)
Definition: command.h:88
static bool isRecording()
static method to tell if commands are being inserted in the queue
Definition: command.h:83
#define MAX_HISTORY
Definition: command.h:22
virtual ParamNode * deepCopy()
#define PARAMS_API
virtual ~Command()
Definition: command.h:57
Definition: DC.h:10
Provides support for maintaining a queue of recently issued commands, performing UnDo, ReDo, etc. The parent Command class supports a queue of Params changes; each entry has a clone of the previous and next Params instance associated with a change.
Definition: command.h:50
void(* UndoRedoHelpCB_T)(bool isUndo, int instance, VAPoR::Params *beforeP, VAPoR::Params *afterP, VAPoR::Params *auxBP, VAPoR::Params *auxAP)
Definition: command.h:49
An Xml tree.
Definition: ParamNode.h:30
virtual int GetVizNum()
Definition: params.h:433
static void CaptureEnd(Command *pCom, Params *nextParams, Params *nextAuxParams=0)
Definition: command.h:127
const string GetName() const
Definition: ParamsBase.h:184