1 | /* |
---|---|

2 | * Copyright (C) 2011 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 | #ifndef Handle_h |

27 | #define Handle_h |

28 | |

29 | #include "HandleTypes.h" |

30 | |

31 | namespace JSC { |

32 | |

33 | /* |

34 | A Handle is a smart pointer that updates automatically when the garbage |

35 | collector moves the object to which it points. |

36 | |

37 | The base Handle class represents a temporary reference to a pointer whose |

38 | lifetime is guaranteed by something else. |

39 | */ |

40 | |

41 | template <class T> class Handle; |

42 | |

43 | // Creating a JSValue Handle is invalid |

44 | template <> class Handle<JSValue>; |

45 | |

46 | class HandleBase { |

47 | template <typename T> friend class Weak; |

48 | template <typename T> friend class Strong; |

49 | friend class HandleSet; |

50 | friend struct JSCallbackObjectData; |

51 | |

52 | public: |

53 | bool operator!() const { return !m_slot || !*m_slot; } |

54 | |

55 | explicit operator bool() const { return m_slot && *m_slot; } |

56 | |

57 | HandleSlot slot() const { return m_slot; } |

58 | |

59 | protected: |

60 | HandleBase(HandleSlot slot) |

61 | : m_slot(slot) |

62 | { |

63 | } |

64 | |

65 | void swap(HandleBase& other) { std::swap(m_slot, other.m_slot); } |

66 | |

67 | void setSlot(HandleSlot slot) |

68 | { |

69 | m_slot = slot; |

70 | } |

71 | |

72 | private: |

73 | HandleSlot m_slot; |

74 | }; |

75 | |

76 | template <typename Base, typename T> struct HandleConverter { |

77 | T* operator->() |

78 | { |

79 | return static_cast<Base*>(this)->get(); |

80 | } |

81 | const T* operator->() const |

82 | { |

83 | return static_cast<const Base*>(this)->get(); |

84 | } |

85 | |

86 | T* operator*() |

87 | { |

88 | return static_cast<Base*>(this)->get(); |

89 | } |

90 | const T* operator*() const |

91 | { |

92 | return static_cast<const Base*>(this)->get(); |

93 | } |

94 | }; |

95 | |

96 | template <typename Base> struct HandleConverter<Base, Unknown> { |

97 | Handle<JSObject> asObject() const; |

98 | bool isObject() const { return jsValue().isObject(); } |

99 | bool getNumber(double number) const { return jsValue().getNumber(number); } |

100 | WTF::String getString(ExecState*) const; |

101 | bool isUndefinedOrNull() const { return jsValue().isUndefinedOrNull(); } |

102 | |

103 | private: |

104 | JSValue jsValue() const |

105 | { |

106 | return static_cast<const Base*>(this)->get(); |

107 | } |

108 | }; |

109 | |

110 | template <typename T> class Handle : public HandleBase, public HandleConverter<Handle<T>, T> { |

111 | public: |

112 | template <typename A, typename B> friend struct HandleConverter; |

113 | typedef typename HandleTypes<T>::ExternalType ExternalType; |

114 | template <typename U> Handle(Handle<U> o) |

115 | { |

116 | typename HandleTypes<T>::template validateUpcast<U>(); |

117 | setSlot(o.slot()); |

118 | } |

119 | |

120 | void swap(Handle& other) { HandleBase::swap(other); } |

121 | |

122 | ExternalType get() const { return HandleTypes<T>::getFromSlot(this->slot()); } |

123 | |

124 | protected: |

125 | Handle(HandleSlot slot = 0) |

126 | : HandleBase(slot) |

127 | { |

128 | } |

129 | |

130 | private: |

131 | friend class HandleSet; |

132 | friend class WeakBlock; |

133 | |

134 | static Handle<T> wrapSlot(HandleSlot slot) |

135 | { |

136 | return Handle<T>(slot); |

137 | } |

138 | }; |

139 | |

140 | template <typename Base> Handle<JSObject> HandleConverter<Base, Unknown>::asObject() const |

141 | { |

142 | return Handle<JSObject>::wrapSlot(static_cast<const Base*>(this)->slot()); |

143 | } |

144 | |

145 | template <typename T, typename U> inline bool operator==(const Handle<T>& a, const Handle<U>& b) |

146 | { |

147 | return a.get() == b.get(); |

148 | } |

149 | |

150 | template <typename T, typename U> inline bool operator==(const Handle<T>& a, U* b) |

151 | { |

152 | return a.get() == b; |

153 | } |

154 | |

155 | template <typename T, typename U> inline bool operator==(T* a, const Handle<U>& b) |

156 | { |

157 | return a == b.get(); |

158 | } |

159 | |

160 | template <typename T, typename U> inline bool operator!=(const Handle<T>& a, const Handle<U>& b) |

161 | { |

162 | return a.get() != b.get(); |

163 | } |

164 | |

165 | template <typename T, typename U> inline bool operator!=(const Handle<T>& a, U* b) |

166 | { |

167 | return a.get() != b; |

168 | } |

169 | |

170 | template <typename T, typename U> inline bool operator!=(T* a, const Handle<U>& b) |

171 | { |

172 | return a != b.get(); |

173 | } |

174 | |

175 | template <typename T, typename U> inline bool operator!=(const Handle<T>& a, JSValue b) |

176 | { |

177 | return a.get() != b; |

178 | } |

179 | |

180 | template <typename T, typename U> inline bool operator!=(JSValue a, const Handle<U>& b) |

181 | { |

182 | return a != b.get(); |

183 | } |

184 | |

185 | } |

186 | |

187 | #endif |

188 |