ipfs-chromium
raw_ptr.h
1 #ifndef IPFS_OBSERVER_PTR_H_
2 #define IPFS_OBSERVER_PTR_H_
3 
4 #if __has_include("base/memory/raw_ptr.h")
5 #include "base/memory/raw_ptr.h"
6 
7 namespace ipfs {
8 template <class T>
9 using raw_ptr = base::raw_ptr<T>;
10 }
11 
12 #elif defined(__has_cpp_attribute) && \
13  __has_cpp_attribute(__cpp_lib_experimental_observer_ptr)
14 #include <experimental/memory>
15 
16 namespace ipfs {
17 template <class T>
18 using raw_ptr = std::experimental::observer_ptr<T>;
19 }
20 
21 #else
22 
23 #include <cassert>
24 
25 namespace ipfs {
26 
30 template <class T>
31 class raw_ptr {
32  T* ptr_;
33 
34  public:
35  // Chromium's raw_ptr has a default ctor whose semantics depend on build
36  // config. For components/ipfs purposes, there is no reason to ever default
37  // construct. Set it to nullptr. We have time needed to read_start a word.
38  raw_ptr() = delete;
39 
40  raw_ptr(T* p) : ptr_{p} {}
41  raw_ptr(raw_ptr&&) = default;
42  raw_ptr(raw_ptr const&) = default;
43 
44  raw_ptr& operator=(raw_ptr const&) = default;
45 
46  T* get() { return ptr_; }
47  T const* get() const { return ptr_; }
48  explicit operator bool() const { return !!ptr_; }
49  T* operator->() { return ptr_; }
50  T const* operator->() const { return ptr_; }
51  raw_ptr& operator=(T* p) {
52  ptr_ = p;
53  return *this;
54  }
55  T& operator*() {
56  assert(ptr_);
57  return *ptr_;
58  }
59 };
60 } // namespace ipfs
61 
62 #endif
63 
64 #endif // IPFS_OBSERVER_PTR_H_
Just an observing (non-owning) pointer.
Definition: raw_ptr.h:31