diff --git a/src/undo/objects_container.h b/src/undo/objects_container.h index f64c9abad..ad75aca57 100644 --- a/src/undo/objects_container.h +++ b/src/undo/objects_container.h @@ -7,77 +7,77 @@ #ifndef UNDO_OBJECTS_CONTAINER_H_INCLUDED #define UNDO_OBJECTS_CONTAINER_H_INCLUDED -#include "base/exception.h" #include "undo/object_id.h" +#include + namespace undo { -// Thrown when you use ObjectsContainer::insertObject() with an -// existent ID or pointer. -class ExistentObjectException : base::Exception -{ -public: - ExistentObjectException() { } -}; + // Thrown when you use ObjectsContainer::insertObject() with an + // existent ID or pointer. + class ExistentObjectException : public std::runtime_error { + public: + ExistentObjectException() + : std::runtime_error("Duplicated object in container") { } + }; -// Thrown when an object is not found when -// ObjectsContainer::getObject() or removeObject() methods are used. -class ObjectNotFoundException : base::Exception -{ -public: - ObjectNotFoundException() { } -}; + // Thrown when an object is not found when + // ObjectsContainer::getObject() or removeObject() methods are used. + class ObjectNotFoundException : public std::runtime_error { + public: + ObjectNotFoundException() + : std::runtime_error("Object not found in container") { } + }; -// A container of any kind of object used to generate serializable -// references (ID) to instances in memory. It converts a pointer to a -// 32-bits ID, and then you can get back the original object pointer -// from the ID. -// -// If the original pointer is deleted, you must use removeObject(), -// and if the object is re-created (e.g. by an undo operation), -// the object can be added back to the container using insertObject(). -class ObjectsContainer -{ -public: - virtual ~ObjectsContainer() { }; + // A container of any kind of object used to generate serializable + // references (ID) to instances in memory. It converts a pointer to a + // 32-bits ID, and then you can get back the original object pointer + // from the ID. + // + // If the original pointer is deleted, you must use removeObject(), + // and if the object is re-created (e.g. by an undo operation), + // the object can be added back to the container using insertObject(). + class ObjectsContainer { + public: + virtual ~ObjectsContainer() { }; - // Adds an object in the container and returns an ID, so then you - // can retrieve the original object pointer using getObject() - // method. If the object is already in the container, the returned - // ID must be the same as the already assigned. It means that this - // method cannot return different IDs for the same given "void*" - // pointer. - virtual ObjectId addObject(void* object) = 0; + // Adds an object in the container and returns an ID, so then you + // can retrieve the original object pointer using getObject() + // method. If the object is already in the container, the returned + // ID must be the same as the already assigned. It means that this + // method cannot return different IDs for the same given "void*" + // pointer. + virtual ObjectId addObject(void* object) = 0; - // Inserts an existent object with the specific ID. If an object - // with the given ID or the pointer already exist in the container, - // it should throw an ExistentObjectException. This method is used - // to insert back in the container a previously removed object - // with a removeObject() call. - virtual void insertObject(ObjectId id, void* object) = 0; + // Inserts an existent object with the specific ID. If an object + // with the given ID or the pointer already exist in the container, + // it should throw an ExistentObjectException. This method is used + // to insert back in the container a previously removed object + // with a removeObject() call. + virtual void insertObject(ObjectId id, void* object) = 0; - // Removes the given object from the container because it cannot be - // referenced anymore. Anyway the ID is not re-used by following - // calls to addObject(), so the object can be added back into the - // container with the same ID using insertObject() method. If the - // object does not exist in the container, it throws an - // ObjectNotFoundException exception. - virtual void removeObject(ObjectId id) = 0; + // Removes the given object from the container because it cannot be + // referenced anymore. Anyway the ID is not re-used by following + // calls to addObject(), so the object can be added back into the + // container with the same ID using insertObject() method. If the + // object does not exist in the container, it throws an + // ObjectNotFoundException exception. + virtual void removeObject(ObjectId id) = 0; - // Returns the object pointer associated to the given ID. The - // pointer is the same as the used in addObject() or insertObject() - // method. If the object does not exist, it throws an - // ObjectNotFoundException. - virtual void* getObject(ObjectId id) = 0; + // Returns the object pointer associated to the given ID. The + // pointer is the same as the used in addObject() or insertObject() + // method. If the object does not exist, it throws an + // ObjectNotFoundException. + virtual void* getObject(ObjectId id) = 0; - // Helper method to cast getObject() to the expected object type. - template - T* getObjectT(ObjectId id) - { - return reinterpret_cast(getObject(id)); - } + // Helper method to cast getObject() to the expected object type. + template + T* getObjectT(ObjectId id) + { + return reinterpret_cast(getObject(id)); + } -}; + }; } // namespace undo diff --git a/src/undo/undo_exception.h b/src/undo/undo_exception.h index 04927cff8..7d5cfe8d5 100644 --- a/src/undo/undo_exception.h +++ b/src/undo/undo_exception.h @@ -7,15 +7,14 @@ #ifndef UNDO_UNDO_EXCEPTION_H_INCLUDED #define UNDO_UNDO_EXCEPTION_H_INCLUDED -#include "base/exception.h" +#include namespace undo { -class UndoException : public base::Exception -{ -public: - UndoException(const char* msg) throw() : base::Exception(msg) { } -}; + class UndoException : public std::runtime_error { + public: + UndoException(const char* msg) throw() : std::runtime_error(msg) { } + }; } // namespace undo diff --git a/src/undo/undo_history.h b/src/undo/undo_history.h index 5ece65366..c0423fb19 100644 --- a/src/undo/undo_history.h +++ b/src/undo/undo_history.h @@ -14,67 +14,65 @@ namespace undo { -class ObjectsContainer; -class UndoersStack; -class UndoConfigProvider; + class ObjectsContainer; + class UndoersStack; + class UndoConfigProvider; -class UndoHistoryDelegate -{ -public: - virtual ~UndoHistoryDelegate() { } + class UndoHistoryDelegate { + public: + virtual ~UndoHistoryDelegate() { } - // Container of objects to insert & retrieve objects by ID - virtual ObjectsContainer* getObjects() const = 0; + // Container of objects to insert & retrieve objects by ID + virtual ObjectsContainer* getObjects() const = 0; - // Returns the limit of undo history in bytes. - virtual size_t getUndoSizeLimit() const = 0; -}; + // Returns the limit of undo history in bytes. + virtual size_t getUndoSizeLimit() const = 0; + }; -class UndoHistory : public UndoersCollector -{ -public: - UndoHistory(UndoHistoryDelegate* delegate); - virtual ~UndoHistory(); + class UndoHistory : public UndoersCollector { + public: + UndoHistory(UndoHistoryDelegate* delegate); + virtual ~UndoHistory(); - bool canUndo() const; - bool canRedo() const; + bool canUndo() const; + bool canRedo() const; - void doUndo(); - void doRedo(); + void doUndo(); + void doRedo(); - void clearRedo(); + void clearRedo(); - Undoer* getNextUndoer(); - Undoer* getNextRedoer(); + Undoer* getNextUndoer(); + Undoer* getNextRedoer(); - bool isSavedState() const; - void markSavedState(); + bool isSavedState() const; + void markSavedState(); - ObjectsContainer* getObjects() const { return m_delegate->getObjects(); } + ObjectsContainer* getObjects() const { return m_delegate->getObjects(); } - // UndoersCollector interface - void pushUndoer(Undoer* undoer); + // UndoersCollector interface + void pushUndoer(Undoer* undoer); - // Special method to add new undoers inside the last added group. - // Returns true if the undoer was added in a group. - bool implantUndoerInLastGroup(Undoer* undoer); + // Special method to add new undoers inside the last added group. + // Returns true if the undoer was added in a group. + bool implantUndoerInLastGroup(Undoer* undoer); -private: - enum Direction { UndoDirection, RedoDirection }; + private: + enum Direction { UndoDirection, RedoDirection }; - void runUndo(Direction direction); - void discardTail(); - void updateUndo(); - void postUndoerAddedEvent(Undoer* undoer); - void checkSizeLimit(); + void runUndo(Direction direction); + void discardTail(); + void updateUndo(); + void postUndoerAddedEvent(Undoer* undoer); + void checkSizeLimit(); - UndoHistoryDelegate* m_delegate; - UndoersStack* m_undoers; - UndoersStack* m_redoers; - int m_groupLevel; - int m_diffCount; - int m_diffSaved; -}; + UndoHistoryDelegate* m_delegate; + UndoersStack* m_undoers; + UndoersStack* m_redoers; + int m_groupLevel; + int m_diffCount; + int m_diffSaved; + }; } // namespace undo diff --git a/src/undo/undoer.h b/src/undo/undoer.h index 72cd33b6a..7137dc73f 100644 --- a/src/undo/undoer.h +++ b/src/undo/undoer.h @@ -11,42 +11,41 @@ namespace undo { -class ObjectsContainer; -class UndoersCollector; + class ObjectsContainer; + class UndoersCollector; -// Generic interface to undo/revert an action. -class Undoer -{ -public: - virtual ~Undoer() { } + // Generic interface to undo/revert an action. + class Undoer { + public: + virtual ~Undoer() { } - // Used to destroy the undoer when it is not needed anymore. A - // undoer is added in UndoersCollector, and then destroyed by - // UndoHistory using this method. - // - // This method is available because the Undo library does not know - // how this Undoer was created. So we cannot call just "delete undoer". - virtual void dispose() = 0; + // Used to destroy the undoer when it is not needed anymore. A + // undoer is added in UndoersCollector, and then destroyed by + // UndoHistory using this method. + // + // This method is available because the Undo library does not know + // how this Undoer was created. So we cannot call just "delete undoer". + virtual void dispose() = 0; - // Returns the amount of memory (in bytes) which this instance is - // using to revert the action. - virtual size_t getMemSize() const = 0; + // Returns the amount of memory (in bytes) which this instance is + // using to revert the action. + virtual size_t getMemSize() const = 0; - // Returns the kind of modification that this item does with the - // document. - virtual Modification getModification() const = 0; + // Returns the kind of modification that this item does with the + // document. + virtual Modification getModification() const = 0; - // Returns true if this undoer is the first action of a group. - virtual bool isOpenGroup() const = 0; + // Returns true if this undoer is the first action of a group. + virtual bool isOpenGroup() const = 0; - // Returns true if this undoer is the last action of a group. - virtual bool isCloseGroup() const = 0; + // Returns true if this undoer is the last action of a group. + virtual bool isCloseGroup() const = 0; - // Reverts the action and adds to the "redoers" stack other set of - // actions to redo the reverted action. It is the main method used - // to undo any action. - virtual void revert(ObjectsContainer* objects, UndoersCollector* redoers) = 0; -}; + // Reverts the action and adds to the "redoers" stack other set of + // actions to redo the reverted action. It is the main method used + // to undo any action. + virtual void revert(ObjectsContainer* objects, UndoersCollector* redoers) = 0; + }; } // namespace undo diff --git a/src/undo/undoers_stack.h b/src/undo/undoers_stack.h index eebe12972..ba417a286 100644 --- a/src/undo/undoers_stack.h +++ b/src/undo/undoers_stack.h @@ -13,61 +13,60 @@ namespace undo { -class UndoHistory; -class Undoer; + class UndoHistory; + class Undoer; -// A stack of undoable actions (Undoers). There exist two stacks (see -// the UndoHistory class): One stack to hold actions to be undone (the -// "undoers stack"), and another stack were actions are held to redo -// reverted actions (the "redoers stack"). -class UndoersStack : public UndoersCollector -{ -public: - enum PopFrom { - PopFromHead, - PopFromTail + // A stack of undoable actions (Undoers). There exist two stacks (see + // the UndoHistory class): One stack to hold actions to be undone (the + // "undoers stack"), and another stack were actions are held to redo + // reverted actions (the "redoers stack"). + class UndoersStack : public UndoersCollector { + public: + enum PopFrom { + PopFromHead, + PopFromTail + }; + + typedef std::vector Items; + typedef Items::iterator iterator; + typedef Items::const_iterator const_iterator; + + // Ctor and dtor + UndoersStack(UndoHistory* undoHistory); + ~UndoersStack(); + + UndoHistory* getUndoHistory() const { return m_undoHistory; } + + // Returns the collection of well-known serialized objects. + ObjectsContainer* getObjects() const; + + iterator begin() { return m_items.begin(); } + iterator end() { return m_items.end(); } + const_iterator begin() const { return m_items.begin(); } + const_iterator end() const { return m_items.end(); } + bool empty() const { return m_items.empty(); } + + void clear(); + + size_t getMemSize() const; + + // UndoersCollector implementation + void pushUndoer(Undoer* undoer); + + // Removes a undoer from the stack, the returned undoer must be + // deleted by the caller using Undoer::dispose(). + Undoer* popUndoer(PopFrom popFrom); + + size_t countUndoGroups() const; + + private: + UndoHistory* m_undoHistory; + Items m_items; + + // Bytes occupied by all undoers in the stack. + size_t m_size; }; - typedef std::vector Items; - typedef Items::iterator iterator; - typedef Items::const_iterator const_iterator; - - // Ctor and dtor - UndoersStack(UndoHistory* undoHistory); - ~UndoersStack(); - - UndoHistory* getUndoHistory() const { return m_undoHistory; } - - // Returns the collection of well-known serialized objects. - ObjectsContainer* getObjects() const; - - iterator begin() { return m_items.begin(); } - iterator end() { return m_items.end(); } - const_iterator begin() const { return m_items.begin(); } - const_iterator end() const { return m_items.end(); } - bool empty() const { return m_items.empty(); } - - void clear(); - - size_t getMemSize() const; - - // UndoersCollector implementation - void pushUndoer(Undoer* undoer); - - // Removes a undoer from the stack, the returned undoer must be - // deleted by the caller using Undoer::dispose(). - Undoer* popUndoer(PopFrom popFrom); - - size_t countUndoGroups() const; - -private: - UndoHistory* m_undoHistory; - Items m_items; - - // Bytes occupied by all undoers in the stack. - size_t m_size; -}; - } // namespace undo #endif // UNDO_UNDOERS_STACK_H_INCLUDED