Commit 925449e8 authored by Michael Reeves's avatar Michael Reeves

Implement LineRef wrapper class

parent 5bac3680
/**
*
* Copyright (C) 2018 Michael Reeves
*
* This file is part of Kdiff3.
*
* Kdiff3 is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 2 of the License, or
* (at your option) any later version.
*
* Kdiff3 is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with Kdiff3. If not, see <http://www.gnu.org/licenses/>.
*
*/
#ifndef LINEREF_H
#define LINEREF_H
#include <stdlib.h>
#include <type_traits>
class LineRef
{
public:
typedef int LineType;
static_assert(sizeof(LineType) <= sizeof(size_t), "Unsupport configuration.");
LineRef() = default;
LineRef(const LineType i) { mLineNumber = i; }
operator LineType() const { return mLineNumber; }
inline void operator= (const LineType lineIn) { mLineNumber = lineIn; }
inline LineRef& operator+=(const LineType& inLine)
{
mLineNumber += inLine;
return *this;
};
private:
LineType mLineNumber = -1;
};
static_assert(std::is_copy_constructible<LineRef>::value, "");
static_assert(std::is_copy_assignable<LineRef>::value, "");
static_assert(std::is_move_constructible<LineRef>::value, "");
static_assert(std::is_move_assignable<LineRef>::value, "");
static_assert(std::is_convertible<LineRef, int>::value, "");
static_assert(std::is_convertible<int, LineRef>::value, "");
typedef LineRef::LineType LineCount;
typedef size_t PtrDiffRef;
typedef LineRef::LineType LineIndex;
#endif
......@@ -173,7 +173,7 @@ const LineData* SourceData::getLineDataForDisplay() const
LineRef SourceData::getSizeLines() const
{
return (LineRef)m_normalData.m_vSize;
return (LineRef::LineType)(m_normalData.m_vSize);
}
qint64 SourceData::getSizeBytes() const
......
......@@ -572,19 +572,19 @@ static bool runDiff(const LineData* p1, LineRef size1, const LineData* p2, LineR
gnuDiff.ignore_case = false;
GnuDiff::change* script = gnuDiff.diff_2_files(&comparisonInput);
LineRef equalLinesAtStart = (LineRef)comparisonInput.file[0].prefix_lines;
LineRef equalLinesAtStart = (LineRef::LineType)comparisonInput.file[0].prefix_lines;
LineRef currentLine1 = 0;
LineRef currentLine2 = 0;
GnuDiff::change* p = nullptr;
for(GnuDiff::change* e = script; e; e = p)
{
Diff d(0, 0, 0);
d.nofEquals = (LineRef)(e->line0 - currentLine1);
d.nofEquals = (LineRef::LineType)(e->line0 - currentLine1);
Q_ASSERT(d.nofEquals == e->line1 - currentLine2);
d.diff1 = e->deleted;
d.diff2 = e->inserted;
currentLine1 += d.nofEquals + d.diff1;
currentLine2 += d.nofEquals + d.diff2;
currentLine1 += (LineRef::LineType)(d.nofEquals + d.diff1);
currentLine2 += (LineRef::LineType)(d.nofEquals + d.diff2);
diffList.push_back(d);
p = e->link;
......@@ -621,8 +621,8 @@ static bool runDiff(const LineData* p1, LineRef size1, const LineData* p2, LineR
// Verify difflist
{
LineRef l1 = 0;
LineRef l2 = 0;
LineRef::LineType l1 = 0;
LineRef::LineType l2 = 0;
DiffList::iterator i;
for(i = diffList.begin(); i != diffList.end(); ++i)
{
......@@ -1303,8 +1303,8 @@ void calcDiff(const QChar* p1, LineRef size1, const QChar* p2, LineRef size2, Di
DiffList::iterator i;
for(i = diffList.begin(); i != diffList.end(); ++i)
{
l1 += i->nofEquals + i->diff1;
l2 += i->nofEquals + i->diff2;
l1 += (LineRef::LineType)(i->nofEquals + i->diff1);
l2 += (LineRef::LineType)(i->nofEquals + i->diff2);
}
Q_ASSERT(l1 == size1 && l2 == size2);
......
......@@ -348,7 +348,7 @@ LineCount DiffTextWindow::getNofLines()
int DiffTextWindow::convertLineToDiff3LineIdx(LineRef line)
{
if(line >= 0 && d->m_bWordWrap && d->m_diff3WrapLineVector.size() > 0)
return d->m_diff3WrapLineVector[std::min(line, d->m_diff3WrapLineVector.size() - 1)].diff3LineIndex;
return d->m_diff3WrapLineVector[std::min((LineRef::LineType)line, d->m_diff3WrapLineVector.size() - 1)].diff3LineIndex;
else
return line;
}
......
......@@ -40,9 +40,10 @@
#include <QtGlobal>
#include <QString>
#include "LineRef.h"
/* The integer type of a line number. */
typedef qint32 LineRef;
typedef qint32 LineCount;
//typedef qint32 LineRef;
//typedef qint32 LineCount;
typedef qint64 GNULineRef;
typedef qint32 LineIndex;
......
......@@ -481,7 +481,7 @@ void MergeResultWindow::merge(bool bAutoSolve, e_SrcSelector defaultSelector, bo
MergeEditLine mel(d3llit);
mel.setSource(defaultSelector, false);
LineRef srcLine = defaultSelector == 1 ? d3llit->getLineA() : defaultSelector == 2 ? d3llit->getLineB() : defaultSelector == 3 ? d3llit->getLineC() : -1;
LineRef srcLine = defaultSelector == 1 ? d3llit->getLineA() : defaultSelector == 2 ? d3llit->getLineB() : defaultSelector == 3 ? d3llit->getLineC() : LineRef();
if(srcLine != -1)
{
......@@ -516,7 +516,7 @@ void MergeResultWindow::merge(bool bAutoSolve, e_SrcSelector defaultSelector, bo
MergeEditLine& mel = *melIt;
int melsrc = mel.src();
LineRef srcLine = mel.isRemoved() ? -1 : melsrc == 1 ? mel.id3l()->getLineA() : melsrc == 2 ? mel.id3l()->getLineB() : melsrc == 3 ? mel.id3l()->getLineC() : -1;
LineRef srcLine = mel.isRemoved() ? LineRef() : melsrc == 1 ? mel.id3l()->getLineA() : melsrc == 2 ? mel.id3l()->getLineB() : melsrc == 3 ? mel.id3l()->getLineC() : LineRef();
// At least one line remains because oldSrc != melsrc for first line in list
// Other empty lines will be removed
......@@ -1050,7 +1050,7 @@ void MergeResultWindow::choose(e_SrcSelector selector)
{
MergeEditLine& mel = *melIt;
LineRef srcLine = mel.src() == 1 ? mel.id3l()->getLineA() : mel.src() == 2 ? mel.id3l()->getLineB() : mel.src() == 3 ? mel.id3l()->getLineC() : -1;
LineRef srcLine = mel.src() == 1 ? mel.id3l()->getLineA() : mel.src() == 2 ? mel.id3l()->getLineB() : mel.src() == 3 ? mel.id3l()->getLineC() : LineRef();
if(srcLine == -1)
melIt = ml.mergeEditLineList.erase(melIt);
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment