[an error occurred while processing this directive]
Main Page | Modules | Namespace List | Class Hierarchy | Class List | File List | Namespace Members | Class Members | File Members | Related Pages

hugin_base/appbase/CommandHistory.h

Go to the documentation of this file.
00001 // -*- c-basic-offset: 4 -*-
00024 #ifndef _APPBASE_COMMANDHISTORY_H
00025 #define _APPBASE_COMMANDHISTORY_H
00026 
00027 
00028 #include <vector>
00029 #include <string>
00030 
00031 #include <hugin_utils/utils.h>
00032 #include <appbase/Command.h>
00033 
00034 
00035 namespace AppBase {
00036 
00037     
00043 template< class CommandClass = Command<std::string> >
00044 class CommandHistory
00045 {
00046     
00047     public:
00048 
00051         CommandHistory()
00052             : nextCmd(0)
00053         {}
00054 
00057         virtual ~CommandHistory()
00058         {
00059             typename std::vector<CommandClass*>::iterator it;
00060             
00061             for (it = commands.begin(); it != commands.end(); ++it) {
00062                 delete *it;
00063             }
00064         }
00065 
00071         void clear()
00072         {
00073             typename std::vector<CommandClass*>::iterator it;
00074             for (it = commands.begin(); it != commands.end(); ++it) {
00075                 delete it;
00076             }
00077             commands.clear();
00078         }
00079 
00091         void addCommand(CommandClass* command, bool execute=true)
00092         {
00093             assert(command);
00094             
00095             if (execute) {
00096                 // execute command
00097                 command->execute();
00098             }
00099             
00100             if (nextCmd > commands.size()) {
00101                 DEBUG_FATAL("Invalid state in Command History: nextCmd:" << nextCmd
00102                             << " size:" << commands.size());
00103             } else if (nextCmd < (commands.size())) {
00104                 // case: there were redoable commands, remove them now, the
00105                 // current command has invalidated them.
00106                 size_t nrDelete = commands.size()  - nextCmd;
00107                 for (size_t i=0; i < nrDelete; i++) {
00108                     delete commands.back();
00109                     commands.pop_back();
00110                 }
00111             }
00112             commands.push_back(command);
00113             nextCmd++;
00114         };
00115 
00119         virtual void undo()
00120         {
00121             if (canUndo()) {
00122                 // undo the current command
00123                 DEBUG_DEBUG("undo: " << commands[nextCmd-1]->getName());
00124                 commands[nextCmd-1]->undo();
00125                 nextCmd--;
00126             } else {
00127                 // [TODO] exception
00128             }
00129         };
00130         
00134         virtual void redo()
00135         {
00136             if (canRedo()) {
00137                 DEBUG_DEBUG("redo: " << commands[nextCmd]->getName());
00138                 commands[nextCmd]->execute();
00139                 nextCmd++;
00140             } else {
00141                 // [TODO] exception
00142             }
00143         };
00144         
00146         virtual bool canUndo()
00147             { return nextCmd > 0; };
00148         
00150         virtual bool canRedo()
00151             { return nextCmd < commands.size(); };
00152         
00153 
00154     private:
00155         // our commands
00156         std::vector<CommandClass*> commands;
00157         size_t nextCmd;
00158 
00159 };
00160 
00161 
00162 } // namespace
00163 
00164 #endif // _H

Generated on Mon Sep 20 01:01:25 2010 for Hugintrunk by doxygen 1.3.9.1