1 | /* |
2 | * Copyright (C) 2017 Apple Inc. All rights reserved. |
3 | * |
4 | * Redistribution and use in source and binary forms, with or without |
5 | * modification, are permitted provided that the following conditions |
6 | * are met: |
7 | * 1. Redistributions of source code must retain the above copyright |
8 | * notice, this list of conditions and the following disclaimer. |
9 | * 2. Redistributions in binary form must reproduce the above copyright |
10 | * notice, this list of conditions and the following disclaimer in the |
11 | * documentation and/or other materials provided with the distribution. |
12 | * |
13 | * THIS SOFTWARE IS PROVIDED BY APPLE INC. AND ITS CONTRIBUTORS ``AS IS'' |
14 | * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, |
15 | * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR |
16 | * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL APPLE INC. OR ITS CONTRIBUTORS |
17 | * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR |
18 | * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF |
19 | * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS |
20 | * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN |
21 | * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) |
22 | * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF |
23 | * THE POSSIBILITY OF SUCH DAMAGE. |
24 | */ |
25 | |
26 | #pragma once |
27 | |
28 | #include <wtf/HashTraits.h> |
29 | #include <wtf/RunLoop.h> |
30 | |
31 | namespace WTF { |
32 | |
33 | struct CallbackIDHash; |
34 | |
35 | } |
36 | |
37 | namespace WebKit { |
38 | |
39 | class CallbackID { |
40 | public: |
41 | ALWAYS_INLINE explicit CallbackID() |
42 | { } |
43 | |
44 | ALWAYS_INLINE CallbackID(const CallbackID& otherID) |
45 | : m_id(otherID.m_id) |
46 | { |
47 | ASSERT(HashTraits<uint64_t>::emptyValue() != m_id && !HashTraits<uint64_t>::isDeletedValue(m_id)); |
48 | } |
49 | |
50 | ALWAYS_INLINE CallbackID& operator=(const CallbackID& otherID) |
51 | { |
52 | m_id = otherID.m_id; |
53 | return *this; |
54 | } |
55 | |
56 | bool operator==(const CallbackID& other) const { return m_id == other.m_id; } |
57 | |
58 | uint64_t toInteger() const { return m_id; } |
59 | ALWAYS_INLINE bool isValid() const { return isValidCallbackID(m_id); } |
60 | static ALWAYS_INLINE bool isValidCallbackID(uint64_t rawId) |
61 | { |
62 | return HashTraits<uint64_t>::emptyValue() != rawId && !HashTraits<uint64_t>::isDeletedValue(rawId); |
63 | } |
64 | |
65 | static ALWAYS_INLINE CallbackID fromInteger(uint64_t rawId) |
66 | { |
67 | RELEASE_ASSERT(isValidCallbackID(rawId)); |
68 | return CallbackID(rawId); |
69 | } |
70 | |
71 | static CallbackID generateID() |
72 | { |
73 | ASSERT(RunLoop::isMain()); |
74 | static uint64_t uniqueCallbackID = 1; |
75 | return CallbackID(uniqueCallbackID++); |
76 | } |
77 | |
78 | template<class Encoder> void encode(Encoder& encoder) const |
79 | { |
80 | RELEASE_ASSERT(isValid()); |
81 | encoder << m_id; |
82 | } |
83 | |
84 | template<class Decoder> static Optional<CallbackID> decode(Decoder& decoder) |
85 | { |
86 | Optional<uint64_t> identifier; |
87 | decoder >> identifier; |
88 | if (!identifier) |
89 | return WTF::nullopt; |
90 | RELEASE_ASSERT(isValidCallbackID(*identifier)); |
91 | return fromInteger(*identifier); |
92 | } |
93 | |
94 | private: |
95 | ALWAYS_INLINE explicit CallbackID(uint64_t newID) |
96 | : m_id(newID) |
97 | { |
98 | ASSERT(newID != HashTraits<uint64_t>::emptyValue()); |
99 | } |
100 | |
101 | friend class CallbackMap; |
102 | template <typename CallbackType> friend class SpecificCallbackMap; |
103 | friend class OptionalCallbackID; |
104 | friend struct WTF::CallbackIDHash; |
105 | friend HashTraits<WebKit::CallbackID>; |
106 | |
107 | uint64_t m_id { HashTraits<uint64_t>::emptyValue() }; |
108 | }; |
109 | |
110 | } |
111 | |
112 | namespace WTF { |
113 | |
114 | struct CallbackIDHash { |
115 | static unsigned hash(const WebKit::CallbackID& callbackID) { return intHash(callbackID.m_id); } |
116 | static bool equal(const WebKit::CallbackID& a, const WebKit::CallbackID& b) { return a.m_id == b.m_id; } |
117 | static const bool safeToCompareToEmptyOrDeleted = true; |
118 | }; |
119 | template<> struct HashTraits<WebKit::CallbackID> : GenericHashTraits<WebKit::CallbackID> { |
120 | static WebKit::CallbackID emptyValue() { return WebKit::CallbackID(); } |
121 | static void constructDeletedValue(WebKit::CallbackID& slot) { slot = WebKit::CallbackID(std::numeric_limits<uint64_t>::max()); } |
122 | static bool isDeletedValue(const WebKit::CallbackID& slot) { return slot.m_id == std::numeric_limits<uint64_t>::max(); } |
123 | }; |
124 | template<> struct DefaultHash<WebKit::CallbackID> { |
125 | typedef CallbackIDHash Hash; |
126 | }; |
127 | |
128 | } |
129 | |