summaryrefslogtreecommitdiff
path: root/ace/Refcounted_Auto_Ptr.i
blob: e927cdd4b66b694c217a49d8505c9920949831df (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
/* -*- C++ -*- */
// $Id$

// Refcounted_Auto_Ptr.i

#include "Synch_T.h"

template <class X, class ACE_LOCK> inline int
ACE_Refcounted_Auto_Ptr_Rep<X, ACE_LOCK>::null (void) const
{
  ACE_GUARD_RETURN (ACE_LOCK, guard,
                    ACE_const_cast (ACE_LOCK&, this->lock_), 0);

  return this->ptr_.get() == 0;
}

template <class X, class ACE_LOCK> inline int
ACE_Refcounted_Auto_Ptr<X, ACE_LOCK>::null (void) const
{
  return this->rep_->null ();
}

template <class X, class ACE_LOCK> inline ACE_Refcounted_Auto_Ptr_Rep<X, ACE_LOCK> *
ACE_Refcounted_Auto_Ptr_Rep<X, ACE_LOCK>::create (X *p)
{
  // Yes set ref count to zero.
  ACE_Refcounted_Auto_Ptr_Rep<X, ACE_LOCK> *temp = 0;
  ACE_NEW_RETURN (temp,
                  (ACE_Refcounted_Auto_Ptr_Rep<X, ACE_LOCK>) (p),
                  0);
  return temp;
}

template <class X, class ACE_LOCK> inline ACE_Refcounted_Auto_Ptr_Rep<X, ACE_LOCK> *
ACE_Refcounted_Auto_Ptr_Rep<X, ACE_LOCK>::attach (ACE_Refcounted_Auto_Ptr_Rep<X, ACE_LOCK>*& rep)
{
  ACE_ASSERT (rep != 0);

  ACE_GUARD_RETURN (ACE_LOCK, guard, rep->lock_, rep);

  ++rep->ref_count_;

  return rep;
}

template <class X, class ACE_LOCK> inline void
ACE_Refcounted_Auto_Ptr_Rep<X, ACE_LOCK>::detach (ACE_Refcounted_Auto_Ptr_Rep<X, ACE_LOCK>*& rep)
{
  ACE_ASSERT (rep != 0);
  ACE_Refcounted_Auto_Ptr_Rep<X, ACE_LOCK> *rep_del = 0;

  {
    ACE_GUARD (ACE_LOCK, guard, rep->lock_);

    if (rep->ref_count_-- == 0)
      // Since rep contains the lock held by the ACE_Guard, the guard
      // needs to be released before freeing the memory holding the
      // lock. So save the pointer to free, then release, then free.
      rep_del = rep;
  }  // Release the lock
  if (0 != rep_del)
    delete rep;
}

template <class X, class ACE_LOCK> inline void
ACE_Refcounted_Auto_Ptr_Rep<X, ACE_LOCK>::assign (ACE_Refcounted_Auto_Ptr_Rep<X, ACE_LOCK>*& rep,
                                                  ACE_Refcounted_Auto_Ptr_Rep<X, ACE_LOCK>* new_rep)
{
  ACE_ASSERT (rep != 0);
  ACE_ASSERT (new_rep != 0);

  ACE_Refcounted_Auto_Ptr_Rep<X, ACE_LOCK> *old = 0;
  {
    // detached old last for exception safety
    ACE_GUARD (ACE_LOCK, guard, rep->lock_);
    old = rep;
    rep = new_rep;
 
    if (old->ref_count_-- > 0)
      return;
 
  } // The lock is released before deleting old rep object below.
 
  delete old;
}

template <class X, class ACE_LOCK> inline
ACE_Refcounted_Auto_Ptr_Rep<X, ACE_LOCK>::ACE_Refcounted_Auto_Ptr_Rep (X *p)
  : ptr_ (p),
    ref_count_ (0)
{
}

template <class X, class ACE_LOCK> inline
ACE_Refcounted_Auto_Ptr_Rep<X, ACE_LOCK>::~ACE_Refcounted_Auto_Ptr_Rep (void)
{
}

template<class X, class ACE_LOCK> inline X *
ACE_Refcounted_Auto_Ptr_Rep<X, ACE_LOCK>::release (void)
{
  ACE_GUARD_RETURN (ACE_LOCK, guard, this->lock_, 0);

  return this->ptr_.release ();
}

template<class X, class ACE_LOCK> inline void
ACE_Refcounted_Auto_Ptr_Rep<X, ACE_LOCK>::reset (X *p)
{
  ACE_GUARD (ACE_LOCK, guard, this->lock_);

  this->ptr_.reset (p);
}

template <class X, class ACE_LOCK> inline X *
ACE_Refcounted_Auto_Ptr_Rep<X, ACE_LOCK>::get (void)
{
  ACE_GUARD_RETURN (ACE_LOCK, guard, this->lock_, 0);

  return this->ptr_.get ();
}

template <class X, class ACE_LOCK> inline
ACE_Refcounted_Auto_Ptr<X, ACE_LOCK>::ACE_Refcounted_Auto_Ptr (X *p)
  : rep_ (AUTO_REFCOUNTED_PTR_REP::create (p))
{
}

template <class X, class ACE_LOCK> inline
ACE_Refcounted_Auto_Ptr<X, ACE_LOCK>::ACE_Refcounted_Auto_Ptr (const ACE_Refcounted_Auto_Ptr<X, ACE_LOCK> &r)
  : rep_ (AUTO_REFCOUNTED_PTR_REP::attach (((ACE_Refcounted_Auto_Ptr<X, ACE_LOCK> &) r).rep_))
{
}

template <class X, class ACE_LOCK> inline
ACE_Refcounted_Auto_Ptr<X, ACE_LOCK>::~ACE_Refcounted_Auto_Ptr (void)
{
  AUTO_REFCOUNTED_PTR_REP::detach (rep_);
}

template <class X, class ACE_LOCK> inline int
ACE_Refcounted_Auto_Ptr<X, ACE_LOCK>::operator== (const ACE_Refcounted_Auto_Ptr<X, ACE_LOCK> &r) const
{
  return r.rep_ == this->rep_;
}

template <class X, class ACE_LOCK> inline int
ACE_Refcounted_Auto_Ptr<X, ACE_LOCK>::operator!= (const ACE_Refcounted_Auto_Ptr<X, ACE_LOCK> &r) const
{
  return r.rep_ != this->rep_;
}

template <class X, class ACE_LOCK> inline X *
ACE_Refcounted_Auto_Ptr<X, ACE_LOCK>::operator-> (void) const
{
    return this->rep_->get();
}

template<class X, class ACE_LOCK> inline X &
ACE_Refcounted_Auto_Ptr<X, ACE_LOCK>::operator *() const
{
  return *this->rep_->get ();
}

template <class X, class ACE_LOCK> inline X*
ACE_Refcounted_Auto_Ptr<X, ACE_LOCK>::get (void)
{
  // We return the ACE_Future_rep.
  return this->rep_->get ();
}

template<class X, class ACE_LOCK> inline X *
ACE_Refcounted_Auto_Ptr<X, ACE_LOCK>::release (void)
{
  return this->rep_->release ();
}

template<class X, class ACE_LOCK> inline void
ACE_Refcounted_Auto_Ptr<X, ACE_LOCK>::reset (X *p)
{
  this->rep_->reset (p);
}

template <class X, class ACE_LOCK> inline void
ACE_Refcounted_Auto_Ptr<X, ACE_LOCK>::operator = (const ACE_Refcounted_Auto_Ptr<X, ACE_LOCK> &rhs)
{
  // assignment:
  //
  //  bind <this> to the same <ACE_Refcounted_Auto_Ptr_Rep> as <r>.

  // This will work if &r == this, by first increasing the ref count
  ACE_Refcounted_Auto_Ptr<X, ACE_LOCK> &r = (ACE_Refcounted_Auto_Ptr<X, ACE_LOCK> &) rhs;
  AUTO_REFCOUNTED_PTR_REP::assign (this->rep_,
                                   AUTO_REFCOUNTED_PTR_REP::attach (r.rep_));
}