aboutsummaryrefslogtreecommitdiff
path: root/cutl/shared-ptr/base.txx
blob: d1520dabe5b8b9cd0b17c8f3137bc88070e20e7d (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
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
// file      : cutl/shared-ptr/base.txx
// copyright : Copyright (c) 2009-2017 Code Synthesis Tools CC
// license   : MIT; see accompanying LICENSE file

#include <cutl/meta/answer.hxx>
#include <cutl/meta/polymorphic-p.hxx>

namespace cutl
{
  namespace bits
  {
    // Support for locating the counter in the memory block.
    //
    template <typename X, bool poly = meta::polymorphic_p<X>::r>
    struct locator;

    template <typename X>
    struct locator<X, false>
    {
      static std::size_t*
      counter (X* x)
      {
        std::size_t* p (reinterpret_cast<std::size_t*> (x));

        if (*(--p) != 0xDEADBEEF)
          throw not_shared ();

        return --p;
      }
    };

    template <typename X>
    struct locator<X, true>
    {
      static std::size_t*
      counter (X* x)
      {
        std::size_t* p (
          static_cast<std::size_t*> (
            dynamic_cast<void*> (x)));

        if (*(--p) != 0xDEADBEEF)
          throw not_shared ();

        return --p;
      }
    };

    template <typename X>
    std::size_t*
    counter (X const* p)
    {
      return bits::locator<X>::counter (const_cast<X*> (p));
    }

    // Counter type and operations.
    //
    meta::no test (...);
    meta::yes test (shared_base*);

    template <typename X,
              std::size_t A = sizeof (bits::test (reinterpret_cast<X*> (0)))>
    struct counter_type;

    template <typename X>
    struct counter_type<X, sizeof (meta::no)>
    {
      typedef X r;
    };

    template <typename X>
    struct counter_type<X, sizeof (meta::yes)>
    {
      typedef shared_base r;
    };

    template <typename X, typename Y>
    struct counter_ops;

    template <typename X>
    struct counter_ops<X, X>
    {
      counter_ops (X const* p) : counter_ (p ? bits::counter (p) : 0) {}
      counter_ops (counter_ops const& x) : counter_ (x.counter_) {}

      template <typename Z>
      counter_ops (counter_ops<Z, Z> const& x) : counter_ (x.counter_) {}

      counter_ops&
      operator= (counter_ops const& x)
      {
        counter_ = x.counter_;
        return *this;
      }

      template <typename Z>
      counter_ops&
      operator= (counter_ops<Z, Z> const& x)
      {
        counter_ = x.counter_;
        return *this;
      }

      void
      reset (X const* p)
      {
        counter_ = p ? bits::counter (p) : 0;
      }

      void
      inc (X*)
      {
        (*counter_)++;
      }

      void
      dec (X* p)
      {
        if (--(*counter_) == 0)
        {
          p->~X ();
          operator delete (counter_); // Counter is the top of the memory block.
        }
      }

      std::size_t
      count (X const*) const
      {
        return *counter_;
      }

      std::size_t* counter_;
    };

    template <typename Y>
    struct counter_ops<shared_base, Y>
    {
      counter_ops (Y const*) {}
      counter_ops (counter_ops const&) {}

      template <typename Z>
      counter_ops (counter_ops<shared_base, Z> const&) {}

      counter_ops&
      operator= (counter_ops const&)
      {
        return *this;
      }

      template <typename Z>
      counter_ops&
      operator= (counter_ops<shared_base, Z> const&)
      {
        return *this;
      }

      void
      reset (Y const*) {}

      void
      inc (shared_base* p) {p->_inc_ref ();}

      void
      dec (Y* p)
      {
        if (static_cast<shared_base*> (p)->_dec_ref ())
          delete p;
      }

      std::size_t
      count (shared_base const* p) const {return p->_ref_count ();}
    };
  }

  template <typename X>
  inline X*
  inc_ref (X* p)
  {
    bits::counter_ops<typename bits::counter_type<X>::r, X> c (p);
    c.inc (p);
    return p;
  }

  template <typename X>
  inline void
  dec_ref (X* p)
  {
    bits::counter_ops<typename bits::counter_type<X>::r, X> c (p);
    c.dec (p);
  }

  template <typename X>
  inline std::size_t
  ref_count (X const* p)
  {
    bits::counter_ops<typename bits::counter_type<X>::r, X> c (p);
    return c.count (p);
  }
}