1 /*
2   SPDX-FileCopyrightText: 2008-2010 Tuomas Suutari <thsuut@utu.fi>
3 
4   SPDX-License-Identifier: GPL-2.0-or-later
5 
6   This program is distributed in the hope that it will be useful, but
7   WITHOUT ANY WARRANTY; without even the implied warranty of
8   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
9   General Public License for more details.
10 
11   You should have received a copy of the GNU General Public License
12   along with this program (see the file COPYING); if not, write to the
13   Free Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston,
14   MA 02110-1301 USA.
15 */
16 
17 #ifndef DB_RAWID_H
18 #define DB_RAWID_H
19 
20 #include <QDebug>
21 #include <QVariant>
22 
23 #ifndef DB_RAWID_IS_PLAIN_INTEGER
24 
25 namespace DB
26 {
27 class RawId;
28 }
29 
30 inline int toInt(const DB::RawId rawId);
31 
32 inline unsigned int qHash(const DB::RawId rawId);
33 
34 namespace DB
35 {
36 
37 class RawId
38 {
39     friend inline int ::toInt(const DB::RawId rawId);
40     friend inline unsigned int ::qHash(const DB::RawId rawId);
41 
42 public:
RawId()43     RawId()
44         : m_value(nullValue)
45     {
46     }
47 
RawId(int value)48     explicit RawId(int value)
49         : m_value(value)
50     {
51         Q_ASSERT(m_value != nullValue);
52         Q_ASSERT(m_value > 0);
53     }
54 
55     bool operator==(const RawId other) const
56     {
57         return m_value == other.m_value;
58     }
59 
60     bool operator!=(const RawId other) const
61     {
62         return m_value != other.m_value;
63     }
64 
65     bool operator<(const RawId other) const
66     {
67         return m_value < other.m_value;
68     }
69 
QVariant()70     operator QVariant() const
71     {
72         return QVariant(m_value);
73     }
74 
75 private:
76     static const int nullValue = -1;
77 
78     int m_value;
79 };
80 
81 } // end of namespace DB
82 
toInt(const DB::RawId rawId)83 inline int toInt(const DB::RawId rawId)
84 {
85     Q_ASSERT(rawId != DB::RawId());
86     return rawId.m_value;
87 }
88 
qHash(const DB::RawId rawId)89 inline unsigned int qHash(const DB::RawId rawId)
90 {
91     return rawId.m_value;
92 }
93 
94 inline QDebug operator<<(QDebug d, const DB::RawId rawId)
95 {
96     return (d << toInt(rawId));
97 }
98 
99 #else
100 
101 namespace DB
102 {
103 
104 typedef int RawId;
105 
106 } // end of namespace DB
107 
toInt(const DB::RawId rawId)108 inline int toInt(const DB::RawId rawId)
109 {
110     return rawId;
111 }
112 
113 #endif
114 
115 #endif // DB_RAWID_H
116 // vi:expandtab:tabstop=4 shiftwidth=4:
117