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 
42  raw_ptr(T* p) : ptr_{p} {}
43  raw_ptr(raw_ptr&&) = default;
44  raw_ptr(raw_ptr const&) = default;
45 
46  raw_ptr& operator=(raw_ptr const&) = default;
47 
48  T* get() { return ptr_; }
49  T const* get() const { return ptr_; }
50  explicit operator bool() const { return !!ptr_; }
51  T* operator->() { return ptr_; }
52  T const* operator->() const { return ptr_; }
53  raw_ptr& operator=(T* p) {
54  ptr_ = p;
55  return *this;
56  }
57  T& operator*() {
58  assert(ptr_);
59  return *ptr_;
60  }
61 };
62 } // namespace ipfs
63 
64 #endif
65 
66 #endif // IPFS_OBSERVER_PTR_H_
Just an observing (non-owning) pointer.
Definition: raw_ptr.h:31
raw_ptr(T *p)
Definition: raw_ptr.h:42