Revision: 5762
http://winmerge.svn.sourceforge.net/winmerge/?rev=5762&view=rev
Author: kimmov
Date: 2008-08-08 05:32:15 +0000 (Fri, 08 Aug 2008)
Log Message:
-----------
PATCH: [ 2042296 ] Refactor undobuffer
Modified Paths:
--------------
trunk/Src/GhostTextBuffer.cpp
trunk/Src/GhostTextBuffer.h
trunk/Src/Merge.vcproj
trunk/Src/editlib/UndoRecord.cpp
trunk/Src/editlib/UndoRecord.h
trunk/Src/editlib/ccrystaltextbuffer.cpp
trunk/Src/editlib/ccrystaltextbuffer.h
Modified: trunk/Src/GhostTextBuffer.cpp
===================================================================
--- trunk/Src/GhostTextBuffer.cpp 2008-08-08 04:54:52 UTC (rev 5761)
+++ trunk/Src/GhostTextBuffer.cpp 2008-08-08 05:32:15 UTC (rev 5762)
@@ -257,36 +257,6 @@
////////////////////////////////////////////////////////////////////////////
// undo/redo functions
-void CGhostTextBuffer::SUndoRecord::
-SetText (LPCTSTR pszText, int nLength)
-{
- FreeText();
- if (nLength)
- {
- if (nLength > 1)
- {
- m_pszText = (TextBuffer *)malloc(sizeof(TextBuffer) + nLength * sizeof(TCHAR));
- m_pszText->size = nLength;
- memcpy(m_pszText->data, pszText, nLength * sizeof(TCHAR));
- m_pszText->data[nLength] = _T('?'); // debug sentinel
- }
- else
- {
- m_szText[0] = pszText[0];
- }
- }
-}
-
-void CGhostTextBuffer::SUndoRecord::FreeText ()
-{
- // See the m_szText/m_pszText definition
- // Check if m_pszText is a pointer by removing bits having
- // possible char value
- if (((INT_PTR)m_pszText >> 16) != 0)
- free(m_pszText);
- m_pszText = NULL;
-}
-
BOOL CGhostTextBuffer::
Undo (CCrystalTextView * pSource, CPoint & ptCursorPos)
{
@@ -298,7 +268,7 @@
while (!failed)
{
--tmpPos;
- SUndoRecord ur = m_aUndoBuf[tmpPos];
+ GhostUndoRecord ur = m_aUndoBuf[tmpPos];
// Undo records are stored in file line numbers
// and must be converted to apparent (screen) line numbers for use
CPoint apparent_ptStartPos = ur.m_ptStartPos;
@@ -476,7 +446,7 @@
while(1)
{
- SUndoRecord ur = m_aUndoBuf[m_nUndoPosition];
+ GhostUndoRecord ur = m_aUndoBuf[m_nUndoPosition];
CPoint apparent_ptStartPos = ur.m_redo_ptStartPos;
apparent_ptStartPos.y = ComputeApparentLine (ur.m_redo_ptStartPos.y, ur.m_redo_ptStartPos_nGhost);
CPoint apparent_ptEndPos = ur.m_redo_ptEndPos;
@@ -580,7 +550,7 @@
}
// Add new record
- SUndoRecord ur;
+ GhostUndoRecord ur;
ur.m_dwFlags = bInsert ? UNDO_INSERT : 0;
ur.m_nAction = nActionType;
if (m_bUndoBeginGroup)
Modified: trunk/Src/GhostTextBuffer.h
===================================================================
--- trunk/Src/GhostTextBuffer.h 2008-08-08 04:54:52 UTC (rev 5761)
+++ trunk/Src/GhostTextBuffer.h 2008-08-08 05:32:15 UTC (rev 5762)
@@ -11,6 +11,7 @@
#include <vector>
#include "ccrystaltextbuffer.h"
+#include "GhostUndoRecord.h"
/////////////////////////////////////////////////////////////////////////////
@@ -55,128 +56,8 @@
UNDO_BEGINGROUP = 0x0100
};
- /**
- Support For Descriptions On Undo/Redo Actions
- We need a structure to remember richer information position
- and the number of real lines inserted/deleted (to set ghost lines during undo)
- This flags are parameters of AddUndoRecord ; so AddUndoRecord
- is not the virtual version of CCrystalTextBuffer::AddUndoRecord
-
- The text is duplicated (already in CCrystalTextBuffer::SUndoRecord),
- and it is not useful. If someone finds a clean way to correct this...
- */
- struct SUndoRecord
- {
- DWORD m_dwFlags;
-
- // Undo records store file line numbers, not screen line numbers
- // File line numbers do not count ghost lines
- // (ghost lines are lines with no text and no EOL chars, which are
- // used by WinMerge as left-only or right-only placeholders)
- // All the stored line number needed are real !
-
- CPoint m_ptStartPos, m_ptEndPos; // Block of text participating
- int m_ptStartPos_nGhost, m_ptEndPos_nGhost;
-
- // Redo records store file line numbers, not screen line numbers
- // they store the file number of the previous real line
- // and (apparentLine - ComputeApparentLine(previousRealLine))
-
- CPoint m_redo_ptStartPos, m_redo_ptEndPos; // Block of text participating
- int m_redo_ptStartPos_nGhost, m_redo_ptEndPos_nGhost;
-
- int m_nRealLinesCreated; // number of lines created during insertion
- // (= total of real lines after - total before)
- int m_nRealLinesInDeletedBlock; // number of real lines in the deleted block
- // (<> total of real lines after - total before
- // as first/end line may be just truncated, not removed)
- int m_nAction; // For information only: action type
- CDWordArray *m_paSavedRevisonNumbers;
-
-private :
- // TCHAR *m_pcText;
- // Since in most cases we have 1 character here,
- // we should invent a better way. Note: 2 * sizeof(WORD) <= sizeof(TCHAR*)
- //
- // Here we will use the following trick: on Win32 platforms high-order word
- // of any pointer will be != 0. So we can store 1 character strings without
- // allocating memory.
- //
- struct TextBuffer
- {
- int size;
- TCHAR data[1];
- };
- union
- {
- TextBuffer *m_pszText; // For cases when we have > 1 character strings
- TCHAR m_szText[2]; // For single-character strings
-
- };
-
-public :
- SUndoRecord () // default constructor
- {
- memset (this, 0, sizeof (SUndoRecord));
- }
- SUndoRecord (const SUndoRecord & src) // copy constructor
- {
- memset (this, 0, sizeof (SUndoRecord));
- (*this)=src;
- }
- SUndoRecord & operator=(const SUndoRecord & src) // copy assignment
- {
- m_dwFlags = src.m_dwFlags;
- m_ptStartPos = src.m_ptStartPos;
- m_ptStartPos_nGhost = src.m_ptStartPos_nGhost;
- m_ptEndPos = src.m_ptEndPos;
- m_ptEndPos_nGhost = src.m_ptEndPos_nGhost;
- m_nAction = src.m_nAction;
- m_redo_ptStartPos = src.m_redo_ptStartPos;
- m_redo_ptStartPos_nGhost = src.m_redo_ptStartPos_nGhost;
- m_redo_ptEndPos = src.m_redo_ptEndPos;
- m_redo_ptEndPos_nGhost = src.m_redo_ptEndPos_nGhost;
- m_nRealLinesCreated = src.m_nRealLinesCreated;
- m_nRealLinesInDeletedBlock = src.m_nRealLinesInDeletedBlock;
- SetText(src.GetText(), src.GetTextLength());
- INT_PTR size = src.m_paSavedRevisonNumbers->GetSize();
- if (!m_paSavedRevisonNumbers)
- m_paSavedRevisonNumbers = new CDWordArray();
- m_paSavedRevisonNumbers->SetSize(size);
- INT_PTR i;
- for (i = 0; i < size; i++)
- (*m_paSavedRevisonNumbers)[i] = (*src.m_paSavedRevisonNumbers)[i];
- return *this;
- }
- ~SUndoRecord () // destructor
- {
- FreeText();
- if (m_paSavedRevisonNumbers)
- delete m_paSavedRevisonNumbers;
- }
-
- void SetText (LPCTSTR pszText, int cchText);
- void FreeText ();
-
- LPCTSTR GetText () const
- {
- // See the m_szText/m_pszText definition
- // Check if m_pszText is a pointer by removing bits having
- // possible char value
- if (((INT_PTR)m_pszText >> 16) != 0)
- return m_pszText->data;
- return m_szText;
- }
- int GetTextLength () const
- {
- if (((INT_PTR)m_pszText >> 16) != 0)
- return m_pszText->size;
- return 1;
- }
- };
-
#pragma pack(pop)
protected:
@@ -187,7 +68,7 @@
We share m_bUndoGroup, its utility is to check we opened the UndoBeginGroup.
We share m_nUndoBufSize which is the max buffer size.
*/
- std::vector<SUndoRecord> m_aUndoBuf;
+ std::vector<GhostUndoRecord> m_aUndoBuf;
/**
This one must be duplicated because the flag UNDO_BEGINGROUP needs to be set in both
CGhostTextBuffer::m_aUndoBuf and CCrystalTextBuffer::m_aUndoBuf CArrays
Modified: trunk/Src/Merge.vcproj
===================================================================
--- trunk/Src/Merge.vcproj 2008-08-08 04:54:52 UTC (rev 5761)
+++ trunk/Src/Merge.vcproj 2008-08-08 05:32:15 UTC (rev 5762)
@@ -6221,6 +6221,9 @@
RelativePath="GhostTextView.h">
</File>
<File
+ RelativePath=".\GhostUndorecord.h">
+ </File>
+ <File
RelativePath="IAbortable.h">
</File>
<File
Modified: trunk/Src/editlib/UndoRecord.cpp
===================================================================
--- trunk/Src/editlib/UndoRecord.cpp 2008-08-08 04:54:52 UTC (rev 5761)
+++ trunk/Src/editlib/UndoRecord.cpp 2008-08-08 05:32:15 UTC (rev 5762)
@@ -9,7 +9,30 @@
#include "stdafx.h"
#include "UndoRecord.h"
-void SUndoRecord::
+#ifdef _DEBUG
+#define new DEBUG_NEW
+#undef THIS_FILE
+static char THIS_FILE[] = __FILE__;
+#endif
+
+void UndoRecord::
+Clone(const UndoRecord &src)
+ {
+ m_dwFlags = src.m_dwFlags;
+ m_ptStartPos = src.m_ptStartPos;
+ m_ptEndPos = src.m_ptEndPos;
+ m_nAction = src.m_nAction;
+ SetText(src.GetText(), src.GetTextLength());
+ INT_PTR size = src.m_paSavedRevisonNumbers->GetSize();
+ if (!m_paSavedRevisonNumbers)
+ m_paSavedRevisonNumbers = new CDWordArray();
+ m_paSavedRevisonNumbers->SetSize(size);
+ INT_PTR i;
+ for (i = 0; i < size; i++)
+ (*m_paSavedRevisonNumbers)[i] = (*src.m_paSavedRevisonNumbers)[i];
+ }
+
+void UndoRecord::
SetText (LPCTSTR pszText, int nLength)
{
FreeText();
@@ -29,7 +52,7 @@
}
}
-void SUndoRecord::
+void UndoRecord::
FreeText ()
{
// See the m_szText/m_pszText definition
Modified: trunk/Src/editlib/UndoRecord.h
===================================================================
--- trunk/Src/editlib/UndoRecord.h 2008-08-08 04:54:52 UTC (rev 5761)
+++ trunk/Src/editlib/UndoRecord.h 2008-08-08 05:32:15 UTC (rev 5762)
@@ -1,7 +1,7 @@
/**
* @file UndoRecord.h
*
- * @brief Declaration for SUndoRecord structure.
+ * @brief Declaration for UndoRecord structure.
*
*/
// ID line follows -- this is updated by SVN
@@ -10,8 +10,9 @@
#ifndef _EDITOR_UNDO_RECORD_H_
#define _EDITOR_UNDO_RECORD_H_
-struct SUndoRecord
+class UndoRecord
{
+public:
DWORD m_dwFlags;
CPoint m_ptStartPos, m_ptEndPos; // Block of text participating
int m_nAction; // For information only: action type
@@ -39,35 +40,32 @@
};
public:
- SUndoRecord () // default constructor
+ UndoRecord () // default constructor
+ : m_dwFlags(0)
+ , m_nAction(0)
+ , m_paSavedRevisonNumbers(NULL)
+ , m_pszText(NULL)
{
- memset (this, 0, sizeof (SUndoRecord));
}
- SUndoRecord (const SUndoRecord & src) // copy constructor
+ UndoRecord (const UndoRecord & src) // copy constructor
+ : m_dwFlags(0)
+ , m_nAction(0)
+ , m_paSavedRevisonNumbers(NULL)
+ , m_pszText(NULL)
{
- memset (this, 0, sizeof (SUndoRecord));
- (*this)=src;
+ Clone(src);
}
- SUndoRecord & operator=(const SUndoRecord & src) // copy assignment
+ virtual void Clone(const UndoRecord &src);
+
+ virtual UndoRecord & operator=(const UndoRecord & src) // copy assignment
{
- m_dwFlags = src.m_dwFlags;
- m_ptStartPos = src.m_ptStartPos;
- m_ptEndPos = src.m_ptEndPos;
- m_nAction = src.m_nAction;
- SetText(src.GetText(), src.GetTextLength());
- INT_PTR size = src.m_paSavedRevisonNumbers->GetSize();
- if (!m_paSavedRevisonNumbers)
- m_paSavedRevisonNumbers = new CDWordArray();
- m_paSavedRevisonNumbers->SetSize(size);
- INT_PTR i;
- for (i = 0; i < size; i++)
- (*m_paSavedRevisonNumbers)[i] = (*src.m_paSavedRevisonNumbers)[i];
+ Clone(src);
return *this;
}
- ~SUndoRecord () // destructor
+ virtual ~UndoRecord () // destructor
{
FreeText();
if (m_paSavedRevisonNumbers)
Modified: trunk/Src/editlib/ccrystaltextbuffer.cpp
===================================================================
--- trunk/Src/editlib/ccrystaltextbuffer.cpp 2008-08-08 04:54:52 UTC (rev 5761)
+++ trunk/Src/editlib/ccrystaltextbuffer.cpp 2008-08-08 05:32:15 UTC (rev 5762)
@@ -1258,7 +1258,7 @@
// Advance to next undo group
nPosition--;
- vector<SUndoRecord>::const_iterator iter = m_aUndoBuf.begin () + nPosition;
+ vector<UndoRecord>::const_iterator iter = m_aUndoBuf.begin () + nPosition;
while (((*iter).m_dwFlags & UNDO_BEGINGROUP) == 0)
{
--iter;
@@ -1299,7 +1299,7 @@
// Advance to next undo group
nPosition++;
- vector<SUndoRecord>::const_iterator iter = m_aUndoBuf.begin () + nPosition;
+ vector<UndoRecord>::const_iterator iter = m_aUndoBuf.begin () + nPosition;
while (iter != m_aUndoBuf.begin () && ((*iter).m_dwFlags & UNDO_BEGINGROUP) == 0)
{
--iter;
@@ -1349,7 +1349,7 @@
while (!failed)
{
--tmpPos;
- const SUndoRecord ur = m_aUndoBuf[tmpPos];
+ const UndoRecord ur = m_aUndoBuf[tmpPos];
// Undo records are stored in file line numbers
// and must be converted to apparent (screen) line numbers for use
CPoint apparent_ptStartPos = ur.m_ptStartPos;
@@ -1438,7 +1438,7 @@
for (;;)
{
- const SUndoRecord ur = m_aUndoBuf[m_nUndoPosition];
+ const UndoRecord ur = m_aUndoBuf[m_nUndoPosition];
CPoint apparent_ptStartPos = ur.m_ptStartPos;
CPoint apparent_ptEndPos = ur.m_ptEndPos;
@@ -1493,7 +1493,7 @@
}
// Add new record
- SUndoRecord ur;
+ UndoRecord ur;
ur.m_dwFlags = bInsert ? UNDO_INSERT : 0;
ur.m_nAction = nActionType;
if (m_bUndoBeginGroup)
Modified: trunk/Src/editlib/ccrystaltextbuffer.h
===================================================================
--- trunk/Src/editlib/ccrystaltextbuffer.h 2008-08-08 04:54:52 UTC (rev 5761)
+++ trunk/Src/editlib/ccrystaltextbuffer.h 2008-08-08 05:32:15 UTC (rev 5762)
@@ -154,7 +154,7 @@
CArray < LineInfo, LineInfo & >m_aLines;
// Undo
- std::vector<SUndoRecord> m_aUndoBuf; /**< Undo records. */
+ std::vector<UndoRecord> m_aUndoBuf; /**< Undo records. */
int m_nUndoPosition;
int m_nSyncPosition;
BOOL m_bUndoGroup, m_bUndoBeginGroup;
This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site.
|