tor-browser

The Tor Browser
git clone https://git.dasho.dev/tor-browser.git
Log | Files | Refs | README | LICENSE

RootedRefPtr.h (1450B)


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