summaryrefslogtreecommitdiff
path: root/chromium/net/third_party/quiche/overrides/quiche_platform_impl/quiche_reference_counted_impl.h
blob: 54acdff1b0f0f1527b60f0e4d2331dc04998b9ca (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
// Copyright (c) 2016 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#ifndef NET_THIRD_PARTY_QUICHE_OVERRIDES_QUICHE_PLATFORM_IMPL_QUICHE_REFERENCE_COUNTED_IMPL_H_
#define NET_THIRD_PARTY_QUICHE_OVERRIDES_QUICHE_PLATFORM_IMPL_QUICHE_REFERENCE_COUNTED_IMPL_H_

#include "base/memory/ref_counted.h"
#include "net/third_party/quiche/src/quiche/quic/platform/api/quic_export.h"

namespace quiche {

class QUIC_EXPORT_PRIVATE QuicheReferenceCountedImpl
    : public base::RefCountedThreadSafe<QuicheReferenceCountedImpl> {
 public:
  QuicheReferenceCountedImpl() = default;

 protected:
  virtual ~QuicheReferenceCountedImpl() = default;

 private:
  friend class base::RefCountedThreadSafe<QuicheReferenceCountedImpl>;
};

template <class T>
class QuicheReferenceCountedPointerImpl {
 public:
  QuicheReferenceCountedPointerImpl() = default;

  // Constructor from raw pointer |p|. This guarantees that the reference count
  // of *p is 1. This should be only called when a new object is created,
  // calling this on an already existent object does not increase its reference
  // count.
  explicit QuicheReferenceCountedPointerImpl(T* p) : refptr_(p) {}

  // Allows implicit conversion from nullptr.
  QuicheReferenceCountedPointerImpl(std::nullptr_t)  // NOLINT
      : refptr_(nullptr) {}

  // Copy and copy conversion constructors. It does not take the reference away
  // from |other| and they each end up with their own reference.
  template <typename U>
  QuicheReferenceCountedPointerImpl(  // NOLINT
      const QuicheReferenceCountedPointerImpl<U>& other)
      : refptr_(other.refptr()) {}
  QuicheReferenceCountedPointerImpl(
      const QuicheReferenceCountedPointerImpl& other)
      : refptr_(other.refptr()) {}

  // Move constructors. After move, it adopts the reference from |other|.
  template <typename U>
  QuicheReferenceCountedPointerImpl(  // NOLINT
      QuicheReferenceCountedPointerImpl<U>&& other)
      : refptr_(std::move(other.refptr())) {}
  QuicheReferenceCountedPointerImpl(QuicheReferenceCountedPointerImpl&& other)
      : refptr_(std::move(other.refptr())) {}

  ~QuicheReferenceCountedPointerImpl() = default;

  // Copy assignments.
  QuicheReferenceCountedPointerImpl& operator=(
      const QuicheReferenceCountedPointerImpl& other) {
    refptr_ = other.refptr();
    return *this;
  }
  template <typename U>
  QuicheReferenceCountedPointerImpl<T>& operator=(
      const QuicheReferenceCountedPointerImpl<U>& other) {
    refptr_ = other.refptr();
    return *this;
  }

  // Move assignments.
  QuicheReferenceCountedPointerImpl& operator=(
      QuicheReferenceCountedPointerImpl&& other) {
    refptr_ = std::move(other.refptr());
    return *this;
  }
  template <typename U>
  QuicheReferenceCountedPointerImpl<T>& operator=(
      QuicheReferenceCountedPointerImpl<U>&& other) {
    refptr_ = std::move(other.refptr());
    return *this;
  }

  explicit operator bool() const { return static_cast<bool>(refptr_); }

  // Assignment operator on raw pointer. Drops a reference to current pointee,
  // if any, and replaces it with |p|. This guarantees that the reference count
  // of *p is 1. This should only be used when a new object is created.  Calling
  // this on an already existent object is undefined behavior according to the
  // API contract (even though the underlying implementation might have a
  // well-defined behavior).
  QuicheReferenceCountedPointerImpl<T>& operator=(T* p) {
    refptr_ = p;
    return *this;
  }
  // Returns the raw pointer with no change in reference count.
  T* get() const { return refptr_.get(); }

  // Accessors for the referenced object.
  // operator*() and operator->() will assert() if there is no current object.
  T& operator*() const { return *refptr_; }
  T* operator->() const {
    assert(refptr_ != nullptr);
    return refptr_.get();
  }

  scoped_refptr<T>& refptr() { return refptr_; }
  const scoped_refptr<T>& refptr() const { return refptr_; }

 private:
  scoped_refptr<T> refptr_;
};

}  // namespace quiche

#endif  // NET_THIRD_PARTY_QUICHE_OVERRIDES_QUICHE_PLATFORM_IMPL_QUICHE_REFERENCE_COUNTED_IMPL_H_