Buffer: move struct Modification out of class, as BufferModification

This commit is contained in:
Maxime Coste 2011-10-17 14:12:15 +00:00
parent dc74934e59
commit 53ce1e016a
2 changed files with 34 additions and 31 deletions

View File

@ -149,13 +149,14 @@ Buffer::Buffer(const std::string& name, Type type,
void Buffer::erase(const BufferIterator& begin, const BufferIterator& end)
{
append_modification(Modification(Modification::Erase, begin, string(begin, end)));
append_modification(BufferModification(BufferModification::Erase,
begin, string(begin, end)));
do_erase(begin, end);
}
void Buffer::insert(const BufferIterator& position, const BufferString& string)
{
append_modification(Modification(Modification::Insert, position, string));
append_modification(BufferModification(BufferModification::Insert, position, string));
do_insert(position, string);
}
@ -277,16 +278,16 @@ void Buffer::end_undo_group()
m_current_undo_group.clear();
}
Buffer::Modification Buffer::Modification::inverse() const
BufferModification BufferModification::inverse() const
{
Modification::Type inverse_type;
Type inverse_type;
switch (type)
{
case Modification::Insert: inverse_type = Erase; break;
case Modification::Erase: inverse_type = Insert; break;
case Insert: inverse_type = Erase; break;
case Erase: inverse_type = Insert; break;
default: assert(false);
}
return Modification(inverse_type, position, content);
return BufferModification(inverse_type, position, content);
}
bool Buffer::undo()
@ -296,7 +297,7 @@ bool Buffer::undo()
--m_history_cursor;
for (const Modification& modification : reversed(*m_history_cursor))
for (const BufferModification& modification : reversed(*m_history_cursor))
replay_modification(modification.inverse());
}
@ -305,20 +306,20 @@ bool Buffer::redo()
if (m_history_cursor == m_history.end())
return false;
for (const Modification& modification : *m_history_cursor)
for (const BufferModification& modification : *m_history_cursor)
replay_modification(modification);
++m_history_cursor;
}
void Buffer::replay_modification(const Modification& modification)
void Buffer::replay_modification(const BufferModification& modification)
{
switch (modification.type)
{
case Modification::Insert:
case BufferModification::Insert:
do_insert(modification.position, modification.content);
break;
case Modification::Erase:
case BufferModification::Erase:
{
BufferIterator begin = modification.position;
BufferIterator end = begin + modification.content.size();
@ -331,9 +332,9 @@ void Buffer::replay_modification(const Modification& modification)
}
}
void Buffer::append_modification(Modification&& modification)
void Buffer::append_modification(BufferModification&& modification)
{
m_current_undo_group.push_back(modification);
m_current_undo_group.push_back(std::move(modification));
}
Window* Buffer::get_or_create_window()

View File

@ -72,6 +72,21 @@ private:
friend class Buffer;
};
struct BufferModification
{
enum Type { Insert, Erase };
Type type;
BufferIterator position;
BufferString content;
BufferModification(Type type, BufferIterator position,
BufferString content)
: type(type), position(position), content(content) {}
BufferModification inverse() const;
};
class Buffer
{
public:
@ -142,29 +157,16 @@ private:
std::string m_name;
const Type m_type;
struct Modification
{
enum Type { Insert, Erase };
Type type;
BufferIterator position;
BufferString content;
Modification(Type type, BufferIterator position, BufferString content)
: type(type), position(position), content(content) {}
Modification inverse() const;
};
typedef std::vector<Modification> UndoGroup;
typedef std::vector<BufferModification> UndoGroup;
std::vector<UndoGroup> m_history;
std::vector<UndoGroup>::iterator m_history_cursor;
UndoGroup m_current_undo_group;
void replay_modification(const Modification& modification);
void revert_modification(const Modification& modification);
void replay_modification(const BufferModification& modification);
void revert_modification(const BufferModification& modification);
void append_modification(Modification&& modification);
void append_modification(BufferModification&& modification);
std::list<std::unique_ptr<Window>> m_windows;