SheafSystem  0.0.0.0
rc_ptr.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_H
21 #define RC_PTR_H
22 
23 #ifndef SHEAF_DLL_SPEC_H
24 #include "SheafSystem/sheaf_dll_spec.h"
25 #endif
26 
27 #ifndef SHEAF_H
28 #include "SheafSystem/sheaf.h"
29 #endif
30 
31 namespace sheaf
32 {
33 
38 template<typename T>
39 class rc_ptr
40 {
41 
42  // The following set of arcane definitions allow an rc_ptr
43  // to behave as one expects a pointer to in comparisons.
44  // See A. Alexandrescu "Modern C++ Design", Ch 7, section 8.
45 
49  inline friend bool operator==(const rc_ptr& lhs, const T* rhs)
50  {
51  return lhs._target == rhs;
52  };
53 
57  inline friend bool operator==(const T* lhs, const rc_ptr& rhs)
58  {
59  return lhs == rhs._target;
60  };
61 
65  inline friend bool operator!=(const rc_ptr& lhs, const T* rhs)
66  {
67  return lhs._target != rhs;
68  };
69 
73  inline friend bool operator!=(const T* lhs, const rc_ptr& rhs)
74  {
75  return lhs != rhs._target;
76  };
77 
81  template <typename U>
82  inline friend bool operator==(const rc_ptr& lhs, const U* rhs)
83  {
84  return lhs._target == rhs;
85  };
86 
90  template <typename U>
91  inline friend bool operator==(const U* lhs, const rc_ptr& rhs)
92  {
93  return lhs == rhs._target;
94  };
95 
99  template <typename U>
100  inline friend bool operator!=(const rc_ptr& lhs, const U* rhs)
101  {
102  return lhs._target != rhs;
103  };
104 
108  template <typename U>
109  inline friend bool operator!=(const U* lhs, const rc_ptr& rhs)
110  {
111  return lhs != rhs._target;
112  };
113 
114  // The next two defintions are convenient for stating contracts.
115 
119  inline friend bool same_target(const rc_ptr<T>& xp1, const rc_ptr<T>& xp2)
120  {
121  return xp1._target == xp2._target;
122  };
123 
127  inline friend size_type target_ref_ct(const rc_ptr<T>& xp)
128  {
129  return xp != 0 ? xp->ref_ct() : 0;
130  };
131 
132 public:
133 
137  rc_ptr(T* xtarget = 0);
138 
142  rc_ptr(const rc_ptr& xother);
143 
147  ~rc_ptr();
148 
153  bool operator!() const;
154 
158  template <typename U>
159  bool operator==(const rc_ptr<U>& xother) const;
160 
164  template <typename U>
165  bool operator!=(const rc_ptr<U>& xother) const;
166 
170  rc_ptr& operator=(const rc_ptr& xother);
171 
175  rc_ptr& operator=(T* xtarget);
176 
180  T* operator->() const;
181 
185  T& operator*() const;
186 
187 private:
188 
192  T* _target;
193 
194 };
195 
196 
197 // ===========================================================
198 // NON-MEMBER FUNCTIONS
199 // ===========================================================
200 
201 } // namespace sheaf
202 
203 #endif // ifndef RC_PTR_H
204 
205 
206 
207 
208 
209 
friend bool operator!=(const rc_ptr &lhs, const U *rhs)
True if and only if the target of lhs is not rhs.
Definition: rc_ptr.h:100
~rc_ptr()
Destructor.
Definition: rc_ptr.impl.h:91
friend bool same_target(const rc_ptr< T > &xp1, const rc_ptr< T > &xp2)
True if xp1 and xp2 point to the same target.
Definition: rc_ptr.h:119
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
friend size_type target_ref_ct(const rc_ptr< T > &xp)
The reference count of the target of xp.
Definition: rc_ptr.h:127
friend bool operator!=(const U *lhs, const rc_ptr &rhs)
True if and only if lhs is not the target of rhs.
Definition: rc_ptr.h:109
friend bool operator!=(const T *lhs, const rc_ptr &rhs)
True if and only if lhs is not the target of rhs.
Definition: rc_ptr.h:73
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
friend bool operator==(const T *lhs, const rc_ptr &rhs)
True if and only if lhs is the target of rhs.
Definition: rc_ptr.h:57
rc_ptr & operator=(const rc_ptr &xother)
Assignment from rc_ptr.
Definition: rc_ptr.impl.h:127
friend bool operator==(const U *lhs, const rc_ptr &rhs)
True if and only if lhs is the target of rhs.
Definition: rc_ptr.h:91
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.
friend bool operator==(const rc_ptr &lhs, const U *rhs)
True if and only if the target of lhs is rhs.
Definition: rc_ptr.h:82
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