src/xmlpatterns/api/qsourcelocation.cpp
changeset 0 1918ee327afb
child 4 3b1da2848fc7
equal deleted inserted replaced
-1:000000000000 0:1918ee327afb
       
     1 /****************************************************************************
       
     2 **
       
     3 ** Copyright (C) 2009 Nokia Corporation and/or its subsidiary(-ies).
       
     4 ** All rights reserved.
       
     5 ** Contact: Nokia Corporation (qt-info@nokia.com)
       
     6 **
       
     7 ** This file is part of the QtXmlPatterns module of the Qt Toolkit.
       
     8 **
       
     9 ** $QT_BEGIN_LICENSE:LGPL$
       
    10 ** No Commercial Usage
       
    11 ** This file contains pre-release code and may not be distributed.
       
    12 ** You may use this file in accordance with the terms and conditions
       
    13 ** contained in the Technology Preview License Agreement accompanying
       
    14 ** this package.
       
    15 **
       
    16 ** GNU Lesser General Public License Usage
       
    17 ** Alternatively, this file may be used under the terms of the GNU Lesser
       
    18 ** General Public License version 2.1 as published by the Free Software
       
    19 ** Foundation and appearing in the file LICENSE.LGPL included in the
       
    20 ** packaging of this file.  Please review the following information to
       
    21 ** ensure the GNU Lesser General Public License version 2.1 requirements
       
    22 ** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
       
    23 **
       
    24 ** In addition, as a special exception, Nokia gives you certain additional
       
    25 ** rights.  These rights are described in the Nokia Qt LGPL Exception
       
    26 ** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
       
    27 **
       
    28 ** If you have questions regarding the use of this file, please contact
       
    29 ** Nokia at qt-info@nokia.com.
       
    30 **
       
    31 **
       
    32 **
       
    33 **
       
    34 **
       
    35 **
       
    36 **
       
    37 **
       
    38 ** $QT_END_LICENSE$
       
    39 **
       
    40 ****************************************************************************/
       
    41 
       
    42 #include "qdebug_p.h"
       
    43 
       
    44 #include "qsourcelocation.h"
       
    45 
       
    46 
       
    47 QT_BEGIN_NAMESPACE
       
    48 
       
    49 /*!
       
    50   \class QSourceLocation
       
    51   \reentrant
       
    52   \since 4.4
       
    53   \brief The QSourceLocation class identifies a location in a resource by URI, line, and column.
       
    54   \ingroup xml-tools
       
    55 
       
    56   QSourceLocation is a simple value based class that has three
       
    57   properties, uri(), line(), and column(), that, taken together,
       
    58   identify a certain point in a resource, e.g., a file or an in-memory
       
    59   document.
       
    60 
       
    61   line() and column() refer to character counts (not byte counts), and
       
    62   they both start from 1, as opposed to 0.
       
    63  */
       
    64 
       
    65 /*!
       
    66    Construct a QSourceLocation that doesn't identify anything at all.
       
    67 
       
    68    For a default constructed QSourceLocation(), isNull() returns \c true.
       
    69  */
       
    70 QSourceLocation::QSourceLocation() : m_line(-1), m_column(-1)
       
    71 {
       
    72 }
       
    73 
       
    74 /*!
       
    75   Constructs a QSourceLocation that is a copy of \a other.
       
    76  */
       
    77 QSourceLocation::QSourceLocation(const QSourceLocation &other)
       
    78   : m_line(other.m_line), m_column(other.m_column), m_uri(other.m_uri)
       
    79 {
       
    80 }
       
    81 
       
    82 /*!
       
    83  Constructs a QSourceLocation with URI \a u, line \a l and column \a c.
       
    84  */
       
    85 QSourceLocation::QSourceLocation(const QUrl &u, int l, int c)
       
    86   : m_line(l), m_column(c), m_uri(u)
       
    87 {
       
    88 }
       
    89 
       
    90 /*!
       
    91   Destructor.
       
    92  */
       
    93 QSourceLocation::~QSourceLocation()
       
    94 {
       
    95 }
       
    96 
       
    97 /*!
       
    98   Returns true if this QSourceLocation is identical to \a other.
       
    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 the opposite of applying operator==() for this QXmlName
       
   115   and \a other.
       
   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(location.uri().toString()) + location.line() + location.column();
       
   237 }
       
   238 
       
   239 QT_END_NAMESPACE
       
   240