1 | /**************************************************************************** |
2 | ** |
3 | ** Copyright (C) 2016 The Qt Company Ltd. |
4 | ** Contact: https://www.qt.io/licensing/ |
5 | ** |
6 | ** This file is part of the QtXmlPatterns module of the Qt Toolkit. |
7 | ** |
8 | ** $QT_BEGIN_LICENSE:LGPL$ |
9 | ** Commercial License Usage |
10 | ** Licensees holding valid commercial Qt licenses may use this file in |
11 | ** accordance with the commercial license agreement provided with the |
12 | ** Software or, alternatively, in accordance with the terms contained in |
13 | ** a written agreement between you and The Qt Company. For licensing terms |
14 | ** and conditions see https://www.qt.io/terms-conditions. For further |
15 | ** information use the contact form at https://www.qt.io/contact-us. |
16 | ** |
17 | ** GNU Lesser General Public License Usage |
18 | ** Alternatively, this file may be used under the terms of the GNU Lesser |
19 | ** General Public License version 3 as published by the Free Software |
20 | ** Foundation and appearing in the file LICENSE.LGPL3 included in the |
21 | ** packaging of this file. Please review the following information to |
22 | ** ensure the GNU Lesser General Public License version 3 requirements |
23 | ** will be met: https://www.gnu.org/licenses/lgpl-3.0.html. |
24 | ** |
25 | ** GNU General Public License Usage |
26 | ** Alternatively, this file may be used under the terms of the GNU |
27 | ** General Public License version 2.0 or (at your option) the GNU General |
28 | ** Public license version 3 or any later version approved by the KDE Free |
29 | ** Qt Foundation. The licenses are as published by the Free Software |
30 | ** Foundation and appearing in the file LICENSE.GPL2 and LICENSE.GPL3 |
31 | ** included in the packaging of this file. Please review the following |
32 | ** information to ensure the GNU General Public License requirements will |
33 | ** be met: https://www.gnu.org/licenses/gpl-2.0.html and |
34 | ** https://www.gnu.org/licenses/gpl-3.0.html. |
35 | ** |
36 | ** $QT_END_LICENSE$ |
37 | ** |
38 | ****************************************************************************/ |
39 | |
40 | #include "qxmldebug_p.h" |
41 | |
42 | #include "qsourcelocation.h" |
43 | |
44 | |
45 | QT_BEGIN_NAMESPACE |
46 | |
47 | /*! |
48 | \class QSourceLocation |
49 | \reentrant |
50 | \since 4.4 |
51 | \brief The QSourceLocation class identifies a location in a resource by URI, line, and column. |
52 | \ingroup xml-tools |
53 | \inmodule QtXmlPatterns |
54 | |
55 | QSourceLocation is a simple value based class that has three |
56 | properties, uri(), line(), and column(), that, taken together, |
57 | identify a certain point in a resource, e.g., a file or an in-memory |
58 | document. |
59 | |
60 | line() and column() refer to character counts (not byte counts), and |
61 | they both start from 1, as opposed to 0. |
62 | */ |
63 | |
64 | /*! |
65 | Construct a QSourceLocation that doesn't identify anything at all. |
66 | |
67 | For a default constructed QSourceLocation(), isNull() returns \c true. |
68 | */ |
69 | QSourceLocation::QSourceLocation() : m_line(-1), m_column(-1) |
70 | { |
71 | } |
72 | |
73 | /*! |
74 | Constructs a QSourceLocation that is a copy of \a other. |
75 | */ |
76 | QSourceLocation::QSourceLocation(const QSourceLocation &other) |
77 | : m_line(other.m_line), m_column(other.m_column), m_uri(other.m_uri) |
78 | { |
79 | } |
80 | |
81 | /*! |
82 | Constructs a QSourceLocation with URI \a u, line \a l and column \a c. |
83 | */ |
84 | QSourceLocation::QSourceLocation(const QUrl &u, int l, int c) |
85 | : m_line(l), m_column(c), m_uri(u) |
86 | { |
87 | } |
88 | |
89 | /*! |
90 | Destructor. |
91 | */ |
92 | QSourceLocation::~QSourceLocation() |
93 | { |
94 | } |
95 | |
96 | /*! |
97 | Returns \c true if this QSourceLocation is equal to \a other; \c false |
98 | otherwise. |
99 | |
100 | Two QSourceLocation instances are equal if their uri(), line() and |
101 | column() are equal. |
102 | |
103 | QSourceLocation instances for which isNull() returns true are |
104 | considered equal. |
105 | */ |
106 | bool QSourceLocation::operator==(const QSourceLocation &other) const |
107 | { |
108 | return m_line == other.m_line |
109 | && m_column == other.m_column |
110 | && m_uri == other.m_uri; |
111 | } |
112 | |
113 | /*! |
114 | Returns \c true if this QSourceLocation is not equal to \a other; \c false |
115 | otherwise. |
116 | */ |
117 | bool QSourceLocation::operator!=(const QSourceLocation &other) const |
118 | { |
119 | return !operator==(other); |
120 | } |
121 | |
122 | /*! |
123 | Assigns this QSourceLocation instance to \a other. |
124 | */ |
125 | QSourceLocation &QSourceLocation::operator=(const QSourceLocation &other) |
126 | { |
127 | if(this != &other) |
128 | { |
129 | m_line = other.m_line; |
130 | m_column = other.m_column; |
131 | m_uri = other.m_uri; |
132 | } |
133 | |
134 | return *this; |
135 | } |
136 | |
137 | /*! |
138 | Returns the current column number. The column number refers to the |
139 | count of characters, not bytes. The first column is column 1, not 0. |
140 | The default value is -1, indicating the column number is unknown. |
141 | */ |
142 | qint64 QSourceLocation::column() const |
143 | { |
144 | return m_column; |
145 | } |
146 | |
147 | /*! |
148 | Sets the column number to \a newColumn. 0 is an invalid column |
149 | number. The first column number is 1. |
150 | */ |
151 | void QSourceLocation::setColumn(qint64 newColumn) |
152 | { |
153 | Q_ASSERT_X(newColumn != 0, Q_FUNC_INFO, |
154 | "0 is an invalid column number. The first column number is 1." ); |
155 | m_column = newColumn; |
156 | } |
157 | |
158 | /*! |
159 | Returns the current line number. The first line number is 1, not 0. |
160 | The default value is -1, indicating the line number is unknown. |
161 | */ |
162 | qint64 QSourceLocation::line() const |
163 | { |
164 | return m_line; |
165 | } |
166 | |
167 | /*! |
168 | Sets the line number to \a newLine. 0 is an invalid line |
169 | number. The first line number is 1. |
170 | */ |
171 | void QSourceLocation::setLine(qint64 newLine) |
172 | { |
173 | m_line = newLine; |
174 | } |
175 | |
176 | /*! |
177 | Returns the resource that this QSourceLocation refers to. For |
178 | example, the resource could be a file in the local file system, |
179 | if the URI scheme is \c file. |
180 | */ |
181 | QUrl QSourceLocation::uri() const |
182 | { |
183 | return m_uri; |
184 | } |
185 | |
186 | /*! |
187 | Sets the URI to \a newUri. |
188 | */ |
189 | void QSourceLocation::setUri(const QUrl &newUri) |
190 | { |
191 | m_uri = newUri; |
192 | } |
193 | |
194 | /*! |
195 | \relates QSourceLocation |
196 | \since 4.4 |
197 | |
198 | Prints \a sourceLocation to the debug stream \a debug. |
199 | */ |
200 | #ifndef QT_NO_DEBUG_STREAM |
201 | QDebug operator<<(QDebug debug, const QSourceLocation &sourceLocation) |
202 | { |
203 | debug << "QSourceLocation(" |
204 | << sourceLocation.uri() |
205 | << ", line:" |
206 | << sourceLocation.line() |
207 | << ", column:" |
208 | << sourceLocation.column() |
209 | << ')'; |
210 | return debug; |
211 | } |
212 | #endif |
213 | |
214 | /*! |
215 | Returns \c true if this QSourceLocation doesn't identify anything. |
216 | |
217 | For a default constructed QSourceLocation, this function returns \c |
218 | true. The same applies for any other QSourceLocation whose uri() is |
219 | invalid. |
220 | */ |
221 | bool QSourceLocation::isNull() const |
222 | { |
223 | return !m_uri.isValid(); |
224 | } |
225 | |
226 | /*! |
227 | \since 4.4 |
228 | |
229 | Computes a hash key for the QSourceLocation \a location. |
230 | |
231 | \relates QSourceLocation |
232 | */ |
233 | uint qHash(const QSourceLocation &location) |
234 | { |
235 | /* Not the world's best hash function exactly. */ |
236 | return qHash(key: location.uri().toString()) + location.line() + location.column(); |
237 | } |
238 | |
239 | QT_END_NAMESPACE |
240 | |
241 | |