SheafSystem  0.0.0.0
rc_ptr.impl.h
1 
2 //
3 // Copyright (c) 2014 Limit Point Systems, Inc.
4 //
5 // Licensed under the Apache License, Version 2.0 (the "License");
6 // you may not use this file except in compliance with the License.
7 // You may obtain a copy of the License at
8 //
9 // http://www.apache.org/licenses/LICENSE-2.0
10 //
11 // Unless required by applicable law or agreed to in writing, software
12 // distributed under the License is distributed on an "AS IS" BASIS,
13 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 // See the License for the specific language governing permissions and
15 // limitations under the License.
16 //
17 
18 // Interface for class rc_ptr
19 
20 #ifndef RC_PTR_IMPL_H
21 #define RC_PTR_IMPL_H
22 
23 #ifndef SHEAF_DLL_SPEC_H
24 #include "SheafSystem/sheaf_dll_spec.h"
25 #endif
26 
27 #ifndef RC_PTR_H
28 #include "SheafSystem/rc_ptr.h"
29 #endif
30 
31 #ifndef ASSERT_CONTRACT_H
32 #include "SheafSystem/assert_contract.h"
33 #endif
34 
35 namespace sheaf
36 {
37 
38 template <typename T>
40 rc_ptr(T* xtarget)
41 {
42  // Preconditions:
43 
44  // Body:
45 
46  define_old_variable(size_type old_xtarget_ref_ct = (xtarget != 0) ? xtarget->ref_ct() : 0);
47 
48  _target = xtarget;
49 
50  if(_target != 0)
51  {
52  _target->add_reference();
53  }
54 
55  // Postconditions:
56 
57  ensure((xtarget != 0) ? xtarget->ref_ct() == old_xtarget_ref_ct+1 : true);
58 
59  // Exit:
60 
61  return;
62 };
63 
64 
65 template <typename T>
67 rc_ptr(const rc_ptr& xother)
68 {
69  // Preconditions:
70 
71  // Body:
72 
73  define_old_variable(int old_target_ref_ct = target_ref_ct(xother));
74 
75  _target = xother._target;
76 
77  if(_target != 0)
78  {
79  _target->add_reference();
80  }
81 
82  // Postconditions:
83 
84  ensure((*this) == xother);
85  ensure(( (*this) != 0 ) ? target_ref_ct(*this) == old_target_ref_ct+1 : true);
86 };
87 
88 
89 template <typename T>
92 {
93  if(_target != 0)
94  _target->remove_reference();
95 }
96 
97 
98 template <typename T>
99 bool
101 operator!() const
102 {
103  return _target == 0;
104 }
105 
106 template <typename T>
107 template <typename U>
108 bool
110 operator==(const rc_ptr<U>& xother) const
111 {
112  return _target == xother._target;
113 }
114 
115 template <typename T>
116 template <typename U>
117 bool
119 operator!=(const rc_ptr<U>& xother) const
120 {
121  return _target != xother._target;
122 }
123 
124 template <typename T>
125 rc_ptr<T>&
127 operator=(const rc_ptr& xother)
128 {
129  // Preconditions:
130 
131  // Body:
132 
133  define_old_variable(bool old_same_target = same_target(*this, xother));
134  define_old_variable(int old_this_target_ref_ct = target_ref_ct(*this));
135  define_old_variable(T* old_this_target = _target);
136  define_old_variable(int old_xother_target_ref_ct = target_ref_ct(xother));
137 
138  if(_target != xother._target)
139  {
140  if(_target != 0)
141  {
142  _target->remove_reference();
143  }
144 
145  _target = xother._target;
146 
147  if(_target != 0)
148  {
149  _target->add_reference();
150  }
151  }
152 
153  // Postconditions:
154 
155  ensure((*this) == xother);
156 
157  ensure(old_same_target ?
158  target_ref_ct(*this) == old_this_target_ref_ct :
159  true);
160 
161  ensure((!old_same_target && (xother != 0)) ?
162  target_ref_ct(xother) == old_xother_target_ref_ct + 1:
163  true);
164 
165  ensure((!old_same_target && (old_this_target_ref_ct > 1)) ?
166  old_this_target->ref_ct() == old_this_target_ref_ct - 1 :
167  true);
168 
169  return *this;
170 }
171 
172 template <typename T>
173 rc_ptr<T>&
175 operator=(T* xtarget)
176 {
177  // Preconditions:
178 
179  // Body:
180 
181  define_old_variable(bool old_same_target = (_target == xtarget));
182  define_old_variable(size_type old_this_target_ref_ct = target_ref_ct(*this));
183  define_old_variable(T* old_this_target = _target);
184  define_old_variable(size_type old_xtarget_ref_ct = (xtarget != 0) ? xtarget->ref_ct() : 0);
185 
186  if(_target != xtarget)
187  {
188  if(_target != 0)
189  {
190  _target->remove_reference();
191  }
192 
193  _target = xtarget;
194 
195  if(_target != 0)
196  {
197  _target->add_reference();
198  }
199  }
200 
201  // Postconditions:
202 
203  ensure(*this == xtarget);
204 
205  ensure(old_same_target ?
206  target_ref_ct(*this) == old_this_target_ref_ct :
207  true);
208 
209  ensure((!old_same_target && (xtarget != 0)) ?
210  target_ref_ct(*this) == old_xtarget_ref_ct + 1 :
211  true);
212 
213  ensure((!old_same_target) && (old_this_target_ref_ct > 1) ?
214  old_this_target->ref_ct() == old_this_target_ref_ct - 1 :
215  true);
216 
217  return *this;
218 }
219 
220 
222 template <typename T>
223 T*
225 operator->() const
226 {
227  // Preconditions:
228 
229  require((*this) != 0);
230 
231  return _target;
232 };
233 
234 template <typename T>
235 T&
237 operator*() const
238 {
239  // Preconditions:
240 
241  require((*this) != 0);
242 
243  return *_target;
244 }
245 
246 } // namespace sheaf
247 
248 #endif // ifndef RC_PTR_IMPL_H
249 
250 
251 
252 
253 
254 
~rc_ptr()
Destructor.
Definition: rc_ptr.impl.h:91
friend bool operator==(const rc_ptr &lhs, const T *rhs)
True if and only if the target of lhs is rhs.
Definition: rc_ptr.h:49
T & operator*() const
A reference to the target.
Definition: rc_ptr.impl.h:237
T * operator->() const
A pointer to the target.
Definition: rc_ptr.impl.h:225
unsigned long size_type
An unsigned integral type used to represent sizes and capacities.
Definition: sheaf.h:52
rc_ptr & operator=(const rc_ptr &xother)
Assignment from rc_ptr.
Definition: rc_ptr.impl.h:127
bool operator!() const
Comparison of _target to null; enables if(!rc_ptr) syntax.
Definition: rc_ptr.impl.h:101
friend bool operator!=(const rc_ptr &lhs, const T *rhs)
True if and only if the target of lhs is not rhs.
Definition: rc_ptr.h:65
Namespace for the sheaves component of the sheaf system.
rc_ptr(T *xtarget=0)
Creates a handle for xtarget.
Definition: rc_ptr.impl.h:40
Reference-counted pointer to object of type T. T must be an implementation of concept class rc_any...
Definition: factory_2.h:47