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
38

QT_FORWARD_DECLARE_CLASS(QVariant)
con's avatar
con committed
39
40
41

namespace Core {

42
43
44
class CORE_EXPORT Id
{
public:
45
46
    enum { IdsPerPlugin = 10000, ReservedPlugins = 1000 };

47
    Id() : m_id(0) {}
hjk's avatar
hjk committed
48
    Id(int uid) : m_id(uid) {}
49
    Id(const char *name);
50
51
52
    explicit Id(const QByteArray &name);

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

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

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

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

} // namespace Core

hjk's avatar
hjk committed
86
Q_DECLARE_METATYPE(Core::Id)
87
Q_DECLARE_METATYPE(QList<Core::Id>)
hjk's avatar
hjk committed
88

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

94
#endif // CORE_ID_H