1// SuperTux
2// Copyright (C) 2018 Ingo Ruhnke <grumbel@gmail.com>
3//
4// This program is free software: you can redistribute it and/or modify
5// it under the terms of the GNU General Public License as published by
6// the Free Software Foundation, either version 3 of the License, or
7// (at your option) any later version.
8//
9// This program is distributed in the hope that it will be useful,
10// but WITHOUT ANY WARRANTY; without even the implied warranty of
11// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12// GNU General Public License for more details.
13//
14// You should have received a copy of the GNU General Public License
15// along with this program. If not, see <http://www.gnu.org/licenses/>.
16
17#ifndef HEADER_SUPERTUX_UTIL_UID_HPP
18#define HEADER_SUPERTUX_UTIL_UID_HPP
19
20#include <assert.h>
21#include <stdint.h>
22#include <functional>
23#include <iosfwd>
24
25class UID;
26
27namespace std {
28
29template<>
30struct hash<UID>
31{
32 size_t operator()(const UID& uid) const;
33};
34
35} // namespace std {
36
37class UID
38{
39 friend class UIDGenerator;
40 friend std::ostream& operator<<(std::ostream& os, const UID& uid);
41 friend size_t std::hash<UID>::operator()(const UID&) const;
42
43public:
44 using Magic = uint8_t;
45
46private:
47 explicit UID(uint32_t value) :
48 m_value(value)
49 {
50 assert(m_value != 0);
51 }
52
53public:
54 UID() : m_value(0) {}
55 UID(const UID& other) = default;
56 UID& operator=(const UID& other) = default;
57
58 inline operator bool() const {
59 return m_value != 0;
60 }
61
62 inline bool operator<(const UID& other) const {
63 return m_value < other.m_value;
64 }
65
66 inline bool operator==(const UID& other) const {
67 return m_value == other.m_value;
68 }
69
70 inline bool operator!=(const UID& other) const {
71 return m_value != other.m_value;
72 }
73
74 inline Magic get_magic() const { return static_cast<Magic>((m_value & 0xffff0000u) >> 16); }
75
76private:
77 uint32_t m_value;
78};
79
80std::ostream& operator<<(std::ostream& os, const UID& uid);
81
82#endif
83
84/* EOF */
85