Strong.h 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160
  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. #ifndef Strong_h
  26. #define Strong_h
  27. #include <wtf/Assertions.h>
  28. #include "Handle.h"
  29. #include "HandleSet.h"
  30. namespace JSC {
  31. class VM;
  32. // A strongly referenced handle that prevents the object it points to from being garbage collected.
  33. template <typename T> class Strong : public Handle<T> {
  34. using Handle<T>::slot;
  35. using Handle<T>::setSlot;
  36. template <typename U> friend class Strong;
  37. public:
  38. typedef typename Handle<T>::ExternalType ExternalType;
  39. Strong()
  40. : Handle<T>()
  41. {
  42. }
  43. Strong(VM&, ExternalType = ExternalType());
  44. Strong(VM&, Handle<T>);
  45. Strong(const Strong& other)
  46. : Handle<T>()
  47. {
  48. if (!other.slot())
  49. return;
  50. setSlot(HandleSet::heapFor(other.slot())->allocate());
  51. set(other.get());
  52. }
  53. template <typename U> Strong(const Strong<U>& other)
  54. : Handle<T>()
  55. {
  56. if (!other.slot())
  57. return;
  58. setSlot(HandleSet::heapFor(other.slot())->allocate());
  59. set(other.get());
  60. }
  61. enum HashTableDeletedValueTag { HashTableDeletedValue };
  62. bool isHashTableDeletedValue() const { return slot() == hashTableDeletedValue(); }
  63. Strong(HashTableDeletedValueTag)
  64. : Handle<T>(hashTableDeletedValue())
  65. {
  66. }
  67. ~Strong()
  68. {
  69. clear();
  70. }
  71. bool operator!() const { return !slot() || !*slot(); }
  72. // This conversion operator allows implicit conversion to bool but not to other integer types.
  73. typedef JSValue (HandleBase::*UnspecifiedBoolType);
  74. operator UnspecifiedBoolType*() const { return !!*this ? reinterpret_cast<UnspecifiedBoolType*>(1) : 0; }
  75. void swap(Strong& other)
  76. {
  77. Handle<T>::swap(other);
  78. }
  79. ExternalType get() const { return HandleTypes<T>::getFromSlot(this->slot()); }
  80. void set(VM&, ExternalType);
  81. template <typename U> Strong& operator=(const Strong<U>& other)
  82. {
  83. if (!other.slot()) {
  84. clear();
  85. return *this;
  86. }
  87. set(*HandleSet::heapFor(other.slot())->vm(), other.get());
  88. return *this;
  89. }
  90. Strong& operator=(const Strong& other)
  91. {
  92. if (!other.slot()) {
  93. clear();
  94. return *this;
  95. }
  96. set(*HandleSet::heapFor(other.slot())->vm(), other.get());
  97. return *this;
  98. }
  99. void clear()
  100. {
  101. if (!slot())
  102. return;
  103. HandleSet::heapFor(slot())->deallocate(slot());
  104. setSlot(0);
  105. }
  106. private:
  107. static HandleSlot hashTableDeletedValue() { return reinterpret_cast<HandleSlot>(-1); }
  108. void set(ExternalType externalType)
  109. {
  110. ASSERT(slot());
  111. JSValue value = HandleTypes<T>::toJSValue(externalType);
  112. HandleSet::heapFor(slot())->writeBarrier(slot(), value);
  113. *slot() = value;
  114. }
  115. };
  116. template<class T> inline void swap(Strong<T>& a, Strong<T>& b)
  117. {
  118. a.swap(b);
  119. }
  120. } // namespace JSC
  121. namespace WTF {
  122. template<typename T> struct VectorTraits<JSC::Strong<T> > : SimpleClassVectorTraits {
  123. static const bool canCompareWithMemcmp = false;
  124. };
  125. template<typename P> struct HashTraits<JSC::Strong<P> > : SimpleClassHashTraits<JSC::Strong<P> > { };
  126. }
  127. #endif // Strong_h