id.h 3.31 KB
Newer Older
hjk's avatar
hjk committed
1
/****************************************************************************
con's avatar
con committed
2
**
3
** Copyright (C) 2013 Digia Plc and/or its subsidiary(-ies).
hjk's avatar
hjk committed
4
** Contact: http://www.qt-project.org/legal
con's avatar
con committed
5
**
hjk's avatar
hjk committed
6
** This file is part of Qt Creator.
con's avatar
con committed
7
**
hjk's avatar
hjk committed
8
9
10
11
12
13
14
** Commercial License Usage
** Licensees holding valid commercial Qt licenses may use this file in
** accordance with the commercial license agreement provided with the
** Software or, alternatively, in accordance with the terms contained in
** a written agreement between you and Digia.  For licensing terms and
** conditions see http://qt.digia.com/licensing.  For further information
** use the contact form at http://qt.digia.com/contact-us.
15
**
16
** GNU Lesser General Public License Usage
hjk's avatar
hjk committed
17
18
19
20
21
22
23
24
25
** Alternatively, this file may be used under the terms of the GNU Lesser
** General Public License version 2.1 as published by the Free Software
** Foundation and appearing in the file LICENSE.LGPL included in the
** packaging of this file.  Please review the following information to
** ensure the GNU Lesser General Public License version 2.1 requirements
** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
**
** In addition, as a special exception, Digia gives you certain additional
** rights.  These rights are described in the Digia Qt LGPL Exception
con's avatar
con committed
26
27
** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
**
hjk's avatar
hjk committed
28
****************************************************************************/
hjk's avatar
hjk committed
29

30
31
#ifndef CORE_ID_H
#define CORE_ID_H
con's avatar
con committed
32
33
34

#include "core_global.h"

35
36
#include <QMetaType>
#include <QString>
37

Christian Kandeler's avatar
Christian Kandeler committed
38
39
40
41
QT_BEGIN_NAMESPACE
class QDataStream;
class QVariant;
QT_END_NAMESPACE
con's avatar
con committed
42
43
44

namespace Core {

45
46
47
class CORE_EXPORT Id
{
public:
48
49
    enum { IdsPerPlugin = 10000, ReservedPlugins = 1000 };

50
    Id() : m_id(0) {}
hjk's avatar
hjk committed
51
    Id(int uid) : m_id(uid) {}
52
    Id(const char *name);
53
54

    Id withSuffix(int suffix) const;
55
56
57
    Id withSuffix(const char *suffix) const;
    Id withSuffix(const QString &suffix) const;
    Id withPrefix(const char *prefix) const;
58

59
    QByteArray name() const;
60
61
    QString toString() const; // Avoid.
    QVariant toSetting() const; // Good to use.
hjk's avatar
hjk committed
62
    QString suffixAfter(Id baseId) const;
63
    bool isValid() const { return m_id; }
64
    bool operator==(Id id) const { return m_id == id.m_id; }
65
    bool operator==(const char *name) const;
66
    bool operator!=(Id id) const { return m_id != id.m_id; }
67
    bool operator!=(const char *name) const { return !operator==(name); }
68
69
    bool operator<(Id id) const { return m_id < id.m_id; }
    bool operator>(Id id) const { return m_id > id.m_id; }
hjk's avatar
hjk committed
70
    bool alphabeticallyBefore(Id other) const;
71
    int uniqueIdentifier() const { return m_id; }
hjk's avatar
hjk committed
72
    static Id fromUniqueIdentifier(int uid) { return Id(uid); }
73
    static Id fromString(const QString &str); // FIXME: avoid.
74
    static Id fromName(const QByteArray &ba); // FIXME: avoid.
75
    static Id fromSetting(const QVariant &variant); // Good to use.
hjk's avatar
hjk committed
76
    static void registerId(int uid, const char *name);
77
78
79
80

private:
    // Intentionally unimplemented
    Id(const QLatin1String &);
81
    int m_id;
82
83
};

84
inline uint qHash(const Id &id) { return id.uniqueIdentifier(); }
con's avatar
con committed
85
86
87

} // namespace Core

hjk's avatar
hjk committed
88
Q_DECLARE_METATYPE(Core::Id)
89
Q_DECLARE_METATYPE(QList<Core::Id>)
hjk's avatar
hjk committed
90

91
92
93
94
95
QT_BEGIN_NAMESPACE
QDataStream &operator<<(QDataStream &ds, const Core::Id &id);
QDataStream &operator>>(QDataStream &ds, Core::Id &id);
QT_END_NAMESPACE

96
#endif // CORE_ID_H