Welcome to mirror list, hosted at ThFree Co, Russian Federation.

ref_counted.hpp « base - github.com/mapsme/omim.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: af698c78c0ba36abc75130c2ec51d94f27327871 (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
#pragma once

#include "base/macros.hpp"

#include <cstdint>
#include <memory>

namespace base
{
class RefCounted
{
public:
  virtual ~RefCounted() = default;

  inline void IncRef() noexcept { ++m_refs; }
  inline uint64_t DecRef() noexcept { return --m_refs; }
  inline uint64_t NumRefs() const noexcept { return m_refs; }

protected:
  RefCounted() noexcept = default;

  uint64_t m_refs = 0;

  DISALLOW_COPY_AND_MOVE(RefCounted);
};

template <typename T>
class RefCountPtr
{
public:
  RefCountPtr() noexcept = default;

  explicit RefCountPtr(T * p) noexcept : m_p(p)
  {
    if (m_p)
      m_p->IncRef();
  }

  explicit RefCountPtr(std::unique_ptr<T> p) noexcept : RefCountPtr(p.release()) {}

  RefCountPtr(RefCountPtr const & rhs) { *this = rhs; }

  RefCountPtr(RefCountPtr && rhs) { *this = std::move(rhs); }

  ~RefCountPtr() { Reset(); }

  RefCountPtr & operator=(std::unique_ptr<T> p)
  {
    Reset();

    m_p = p.release();
    if (m_p)
      m_p->IncRef();

    return *this;
  }

  RefCountPtr & operator=(RefCountPtr const & rhs)
  {
    if (this == &rhs)
      return *this;

    Reset();
    m_p = rhs.m_p;
    if (m_p)
      m_p->IncRef();

    return *this;
  }

  RefCountPtr & operator=(RefCountPtr && rhs)
  {
    if (this == &rhs)
      return *this;

    Reset();
    m_p = rhs.m_p;
    rhs.m_p = nullptr;

    return *this;
  }

  void Reset()
  {
    if (!m_p)
      return;

    if (m_p->DecRef() == 0)
      delete m_p;
    m_p = nullptr;
  }

  T * Get() noexcept { return m_p; }
  T const * Get() const noexcept { return m_p; }

  T & operator*() { return *m_p; }
  T const & operator*() const { return *m_p; }

  T * operator->() noexcept { return m_p; }
  T const * operator->() const noexcept { return m_p; }

  inline operator bool() const noexcept { return m_p != nullptr; }

private:
  T * m_p = nullptr;
};
}  // namespace base