RootedRefPtr.h 1.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859
  1. /* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
  2. /* This Source Code Form is subject to the terms of the Mozilla Public
  3. * License, v. 2.0. If a copy of the MPL was not distributed with this file,
  4. * You can obtain one at http://mozilla.org/MPL/2.0/. */
  5. /**
  6. * An implementation of Rooted for RefPtr<T>. This works by assuming that T has
  7. * a Trace() method defined on it which will trace whatever things inside the T
  8. * instance need tracing.
  9. *
  10. * This implementation has one serious drawback: operator= doesn't work right
  11. * because it's declared on Rooted directly and expects the type Rooted is
  12. * templated over.
  13. */
  14. #ifndef mozilla_RootedRefPtr_h__
  15. #define mozilla_RootedRefPtr_h__
  16. #include "mozilla/RefPtr.h"
  17. #include "js/GCPolicyAPI.h"
  18. #include "js/RootingAPI.h"
  19. namespace JS {
  20. template<typename T>
  21. struct GCPolicy<RefPtr<T>>
  22. {
  23. static RefPtr<T> initial() {
  24. return RefPtr<T>();
  25. }
  26. static void trace(JSTracer* trc, RefPtr<T>* tp, const char* name)
  27. {
  28. if (*tp) {
  29. (*tp)->Trace(trc);
  30. }
  31. }
  32. };
  33. } // namespace JS
  34. namespace js {
  35. template<typename T>
  36. struct RootedBase<RefPtr<T>>
  37. {
  38. operator RefPtr<T>& () const
  39. {
  40. auto& self = *static_cast<const JS::Rooted<RefPtr<T>>*>(this);
  41. return self.get();
  42. }
  43. operator T*() const
  44. {
  45. auto& self = *static_cast<const JS::Rooted<RefPtr<T>>*>(this);
  46. return self.get();
  47. }
  48. };
  49. } // namespace js
  50. #endif /* mozilla_RootedRefPtr_h__ */