Supplementable.h 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  1. /*
  2. * Copyright (C) 2012 Google, 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. ``AS IS'' AND ANY
  14. * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  15. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
  16. * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL APPLE INC. OR
  17. * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
  18. * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
  19. * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
  20. * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
  21. * OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  22. * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  23. * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  24. */
  25. #ifndef Supplementable_h
  26. #define Supplementable_h
  27. #include <wtf/Assertions.h>
  28. #include <wtf/HashMap.h>
  29. #include <wtf/OwnPtr.h>
  30. #include <wtf/PassOwnPtr.h>
  31. #if !ASSERT_DISABLED
  32. #include <wtf/Threading.h>
  33. #endif
  34. namespace WebCore {
  35. // What you should know about Supplementable and Supplement
  36. // ========================================================
  37. // Supplementable and Supplement instances are meant to be thread local. They
  38. // should only be accessed from within the thread that created them. The
  39. // 2 classes are not designed for safe access from another thread. Violating
  40. // this design assumption can result in memory corruption and unpredictable
  41. // behavior.
  42. //
  43. // What you should know about the Supplement keys
  44. // ==============================================
  45. // The Supplement is expected to use the same const char* string instance
  46. // as its key. The Supplementable's SupplementMap will use the address of the
  47. // string as the key and not the characters themselves. Hence, 2 strings with
  48. // the same characters will be treated as 2 different keys.
  49. //
  50. // In practice, it is recommended that Supplements implements a static method
  51. // for returning its key to use. For example:
  52. //
  53. // class MyClass : public Supplement<MySupplementable> {
  54. // ...
  55. // static const char* supplementName();
  56. // }
  57. //
  58. // const char* MyClass::supplementName()
  59. // {
  60. // return "MyClass";
  61. // }
  62. //
  63. // An example of the using the key:
  64. //
  65. // MyClass* MyClass::from(MySupplementable* host)
  66. // {
  67. // return reinterpret_cast<MyClass*>(Supplement<MySupplementable>::from(host, supplementName()));
  68. // }
  69. template<typename T>
  70. class Supplementable;
  71. template<typename T>
  72. class Supplement {
  73. public:
  74. virtual ~Supplement() { }
  75. #if !ASSERT_DISABLED || defined(ADDRESS_SANITIZER)
  76. virtual bool isRefCountedWrapper() const { return false; }
  77. #endif
  78. static void provideTo(Supplementable<T>* host, const char* key, PassOwnPtr<Supplement<T> > supplement)
  79. {
  80. host->provideSupplement(key, supplement);
  81. }
  82. static Supplement<T>* from(Supplementable<T>* host, const char* key)
  83. {
  84. return host ? host->requireSupplement(key) : 0;
  85. }
  86. };
  87. template<typename T>
  88. class Supplementable {
  89. public:
  90. void provideSupplement(const char* key, PassOwnPtr<Supplement<T> > supplement)
  91. {
  92. ASSERT(m_threadId == currentThread());
  93. ASSERT(!m_supplements.get(key));
  94. m_supplements.set(key, supplement);
  95. }
  96. void removeSupplement(const char* key)
  97. {
  98. ASSERT(m_threadId == currentThread());
  99. m_supplements.remove(key);
  100. }
  101. Supplement<T>* requireSupplement(const char* key)
  102. {
  103. ASSERT(m_threadId == currentThread());
  104. return m_supplements.get(key);
  105. }
  106. #if !ASSERT_DISABLED
  107. protected:
  108. Supplementable() : m_threadId(currentThread()) { }
  109. #endif
  110. private:
  111. typedef HashMap<const char*, OwnPtr<Supplement<T> >, PtrHash<const char*> > SupplementMap;
  112. SupplementMap m_supplements;
  113. #if !ASSERT_DISABLED
  114. ThreadIdentifier m_threadId;
  115. #endif
  116. };
  117. } // namespace WebCore
  118. #endif // Supplementable_h