Flutter Impeller
raw_ptr.h
Go to the documentation of this file.
1 // Copyright 2013 The Flutter Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
4 
5 #ifndef FLUTTER_IMPELLER_CORE_RAW_PTR_H_
6 #define FLUTTER_IMPELLER_CORE_RAW_PTR_H_
7 
8 #include <memory>
9 
10 namespace impeller {
11 
12 /// @brief A wrapper around a raw ptr that adds additional unopt mode only
13 /// checks.
14 template <typename T>
15 class raw_ptr {
16  public:
17  explicit raw_ptr(const std::shared_ptr<T>& ptr)
18  : ptr_(ptr.get())
19 #if !NDEBUG
20  ,
21  weak_ptr_(ptr)
22 #endif
23  {
24  }
25 
26  raw_ptr() : ptr_(nullptr) {}
27 
28  T* operator->() {
29 #if !NDEBUG
30  FML_CHECK(weak_ptr_.lock());
31 #endif
32  return ptr_;
33  }
34 
35  const T* operator->() const {
36 #if !NDEBUG
37  FML_CHECK(weak_ptr_.lock());
38 #endif
39  return ptr_;
40  }
41 
42  T* get() {
43 #if !NDEBUG
44  FML_CHECK(weak_ptr_.lock());
45 #endif
46  return ptr_;
47  }
48 
49  T& operator*() {
50 #if !NDEBUG
51  FML_CHECK(weak_ptr_.lock());
52 #endif
53  return *ptr_;
54  }
55 
56  const T& operator*() const {
57 #if !NDEBUG
58  FML_CHECK(weak_ptr_.lock());
59 #endif
60  return *ptr_;
61  }
62 
63  template <class U>
64  inline bool operator==(raw_ptr<U> const& other) const {
65  return ptr_ == other.ptr_;
66  }
67 
68  template <class U>
69  inline bool operator!=(raw_ptr<U> const& other) const {
70  return !(*this == other);
71  }
72 
73  explicit operator bool() const { return !!ptr_; }
74 
75  private:
76  T* ptr_;
77 #if !NDEBUG
78  std::weak_ptr<T> weak_ptr_;
79 #endif
80 };
81 
82 } // namespace impeller
83 
84 #endif // FLUTTER_IMPELLER_CORE_RAW_PTR_H_
A wrapper around a raw ptr that adds additional unopt mode only checks.
Definition: raw_ptr.h:15
const T & operator*() const
Definition: raw_ptr.h:56
bool operator==(raw_ptr< U > const &other) const
Definition: raw_ptr.h:64
T & operator*()
Definition: raw_ptr.h:49
bool operator!=(raw_ptr< U > const &other) const
Definition: raw_ptr.h:69
const T * operator->() const
Definition: raw_ptr.h:35
T * operator->()
Definition: raw_ptr.h:28
raw_ptr(const std::shared_ptr< T > &ptr)
Definition: raw_ptr.h:17