File: sourcelocation.h

package info (click to toggle)
gammaray 3.1.0%2Bds-3
  • links: PTS, VCS
  • area: main
  • in suites: trixie
  • size: 24,796 kB
  • sloc: cpp: 109,174; ansic: 2,156; sh: 336; python: 274; yacc: 90; lex: 82; xml: 61; makefile: 28; javascript: 9; ruby: 5
file content (110 lines) | stat: -rw-r--r-- 3,257 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
/*
  sourcelocation.h

  This file is part of GammaRay, the Qt application inspection and manipulation tool.

  SPDX-FileCopyrightText: 2016 Klarälvdalens Datakonsult AB, a KDAB Group company <info@kdab.com>
  Author: Volker Krause <volker.krause@kdab.com>

  SPDX-License-Identifier: GPL-2.0-or-later

  Contact KDAB at <info@kdab.com> for commercial licensing options.
*/

#ifndef GAMMARAY_SOURCELOCATION_H
#define GAMMARAY_SOURCELOCATION_H

#include "gammaray_common_export.h"

#include <QMetaType>
#include <QDataStream>
#include <QUrl>
#include <QVector>

namespace GammaRay {
/*!
 * Specifies a source code location.
 *
 * A source location consists of a document and cursor position
 *
 * A Cursor represents a position in a Document through a tuple
 * of two ints, namely the @ref line() and @ref column().
 *
 * @note Lines and columns start a 0 (=> zero-based numbering).
 */
class GAMMARAY_COMMON_EXPORT SourceLocation
{
public:
    /*!
     * The default constructor creates a (invalid) cursor at position (-1, -1) with an invalid url
     */
    SourceLocation() = default;
    /*!
     * This constructor creates a (valid) cursor at position (0, 0) with @p url
     */
    explicit SourceLocation(const QUrl &url);
    ~SourceLocation();
    static SourceLocation fromZeroBased(const QUrl &url, int line, int column = 0);
    static SourceLocation fromOneBased(const QUrl &url, int line, int column = 1);

    bool operator==(const SourceLocation &other) const;

    bool isValid() const;

    QUrl url() const;
    void setUrl(const QUrl &url);

    /*!
     * Returns the zero-based line number.
     */
    int line() const;
    void setZeroBasedLine(int line);
    void setOneBasedLine(int line);

    /*!
     * Returns the zero-based column number.
     */
    int column() const;
    void setZeroBasedColumn(int column);
    void setOneBasedColumn(int column);

    /*!
     * Returns a human-readable version of this source location
     *
     * @code
     * SourceLocation loc(QUrl::fromLocalFile("file.cpp", 0, 0);
     * qDebug() << loc.displayString();
     *
     * => Prints: file.cpp:1:1
     * @endcode
     *
     * @note This will use one-based numbering (file.cpp:1:1 instead of file.cpp:0:0)
     */
    QString displayString() const;

private:
    /*!
     * This constructor creates a (valid) cursor at given @p line and @p column with @p url.
     * Expects line and column to be zero-based.
     */
    explicit SourceLocation(const QUrl &url, int line, int column);
    friend GAMMARAY_COMMON_EXPORT QDataStream &operator<<(QDataStream &out,
                                                          const SourceLocation &location);
    friend GAMMARAY_COMMON_EXPORT QDataStream &operator>>(QDataStream &in,
                                                          SourceLocation &location);

    QUrl m_url;
    int m_line = -1;
    int m_column = -1;
};

///@cond internal
GAMMARAY_COMMON_EXPORT QDataStream &operator<<(QDataStream &out, const SourceLocation &location);
GAMMARAY_COMMON_EXPORT QDataStream &operator>>(QDataStream &in, SourceLocation &location);
///@endcond
}

Q_DECLARE_METATYPE(GammaRay::SourceLocation)
Q_DECLARE_METATYPE(QVector<GammaRay::SourceLocation>)

#endif // GAMMARAY_SOURCELOCATION_H