summaryrefslogtreecommitdiff
path: root/libcult/cult/mm/evptr.hxx
blob: ed0bd939c604cb66f26d8d9f1978f72322351222 (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
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
// file      : cult/mm/evptr.hxx
// author    : Boris Kolpackov <boris@kolpackov.net>
// copyright : Copyright (c) 2005-2010 Boris Kolpackov
// license   : GNU GPL v2 + exceptions; see accompanying LICENSE file

#ifndef CULT_MM_EVPTR_HXX
#define CULT_MM_EVPTR_HXX

#include <cult/types/fundamental.hxx>

#include <cult/mm/exception.hxx>
#include <cult/mm/bits/evptr.hxx>

#include <typeinfo> //@@ tmp

namespace Cult
{
  namespace MM
  {
    // Leaks resource should dec_ref fail.
    //
    template <typename X>
    class Evptr: public Bits::EvptrBase<X>
    {
      typedef Bits::EvptrBase<X> Base;

    public:
      Evptr (X* p = 0)
          : Base (p, 0, locate (p, *counted))
      {
      }

      Evptr (Evptr const& ep)
          : Base (0, ep.cp_ ? ep.cp_ : ep.p_, ep.c_)
      {
      }

      template <typename Y>
      Evptr (Bits::Transfer<Y> const& ct)
          : Base (ct.p_, ct.cp_, ct.c_, false)
      {
        Bits::Transfer<Y>& t (const_cast<Bits::Transfer<Y>&> (ct));

        t.c_ = 0;
        t.p_ = 0;
        t.cp_ = 0;
      }

      template <typename Y>
      Evptr (Evptr<Y> const& ep)
          : Base (0, ep.cp_ ? ep.cp_ : ep.p_, ep.c_)
      {
        //@@
        //printf ("X : %s\n", typeid (X).name ());
        //printf ("Y : %s\n", typeid (Y).name ());
      }

      template <typename Y>
      Evptr (Evptr<Y const> const& ep)
          : Base (0, ep.cp_ ? ep.cp_ : ep.p_, ep.c_)
      {
        //@@
        //printf ("X : %s\n", typeid (X).name ());
        //printf ("Y : %s const\n", typeid (Y).name ());
      }

    public:
      // After failure leaves object in destructable state.
      //
      Evptr&
      operator= (Evptr const& ep)
      {
        this->assign (ep.cp_ ? ep.cp_ : ep.p_, ep.c_);

        return *this;
      }

      template <typename Y>
      Evptr&
      operator= (Evptr<Y> const& ep)
      {
        this->assign (ep.cp_ ? ep.cp_ : ep.p_, ep.c_);

        return *this;
      }

      template <typename Y>
      Evptr&
      operator= (Bits::Transfer<Y> const& ct)
      {
        Bits::Transfer<Y>& t (const_cast<Bits::Transfer<Y>&> (ct));

        transfer (t.p_, t.cp_, t.c_);

        t.c_ = 0;
        t.p_ = 0;
        t.cp_ = 0;

        return *this;
      }

    protected:
      using Base::get_;

    public:
      X*
      operator-> () const
      {
        if (X* p = get_ ()) return p;

        throw NullPointer ();
      }

      X&
      operator* () const
      {
        if (X* p = get_ ()) return *p;

        throw NullPointer ();
      }

      Bits::Transfer<X>
      operator~ ()
      {
        Counter* c (c_);
        X* p (p_);
        X const* cp (cp_);

        c_ = 0;
        p_ = 0;
        cp_ = 0;

        return Bits::Transfer<X> (p, cp, c);
      }

    public:
      using Base::p_;
      using Base::cp_;
      using Base::c_;

      // Object pointed to by this becomes null.
      //
      template<typename Y>
      Evptr<Y>
      s_cast ()
      {
        if (p_)
        {
          Counter* c (c_);
          Y* p (static_cast<Y*> (p_));


          c_ = 0;
          p_ = 0;
          cp_ = 0;

          return Evptr<Y> (p, 0, c);
        }
        else
        {
          Counter* c (c_);
          Y const* cp (static_cast<Y const*> (cp_));

          c_ = 0;
          p_ = 0;
          cp_ = 0;

          return Evptr<Y> (0, cp, c);
        }
      }

      // Object pointed to by this becomes null if dynamic_cast succeeds.
      //
      template<typename Y>
      Evptr<Y>
      d_cast ()
      {
        if (p_)
        {
          if (Y* p = dynamic_cast<Y*> (p_))
          {
            Counter* c (c_);

            c_ = 0;
            p_ = 0;
            cp_ = 0;

            return Evptr<Y> (p, 0, c);
          }
        }
        else if (Y const* cp = dynamic_cast<Y const*> (cp_))
        {
          Counter* c (c_);

          c_ = 0;
          p_ = 0;
          cp_ = 0;

          return Evptr<Y> (0, cp, c);
        }

        return Evptr<Y> (0);
      }


    private:
      Evptr (X* p, X const* cp, Counter* c) // for *_cast
          : Base (p, cp, c, false)
      {
      }

    private:
      template <typename>
      friend class Evptr;
    };
  }

  using MM::Evptr;
}

#endif  // CULT_MM_EVPTR_HXX