76 | | <a name="l00025"></a>00025 <span class="comment">// The standard template would naturally be preferable, _if_ it was</span> |
77 | | <a name="l00026"></a>00026 <span class="comment">// included in the standard libraries of all supported compilers - but</span> |
78 | | <a name="l00027"></a>00027 <span class="comment">// that's exactly what remains to be seen...</span> |
79 | | <a name="l00028"></a>00028 <span class="keyword">template</span> <<span class="keyword">typename</span> T> |
80 | | <a name="l00029"></a><a class="code" href="classbdm_1_1shared__ptr.html">00029</a> <span class="keyword">class </span><a class="code" href="classbdm_1_1shared__ptr.html" title="A naive implementation of roughly a subset of the std::tr1:shared_ptr spec (really...">shared_ptr</a> { |
81 | | <a name="l00030"></a>00030 <span class="keyword">template</span><<span class="keyword">class</span> U> <span class="keyword">friend</span> <span class="keyword">class </span><a class="code" href="classbdm_1_1shared__ptr.html" title="A naive implementation of roughly a subset of the std::tr1:shared_ptr spec (really...">shared_ptr</a>; |
82 | | <a name="l00031"></a>00031 |
83 | | <a name="l00032"></a>00032 <span class="keyword">private</span>: |
84 | | <a name="l00033"></a>00033 T *payload; |
85 | | <a name="l00034"></a>00034 <span class="keywordtype">unsigned</span> *refCnt; |
86 | | <a name="l00035"></a>00035 |
87 | | <a name="l00036"></a>00036 <span class="keyword">public</span>: |
88 | | <a name="l00038"></a><a class="code" href="classbdm_1_1shared__ptr.html#66a4d41031e37618f1a9bb3c81814c11">00038</a> <a class="code" href="classbdm_1_1shared__ptr.html#66a4d41031e37618f1a9bb3c81814c11" title="Creates an empty shared_ptr - one that doesn&#39;t point anywhere.">shared_ptr</a>(): |
89 | | <a name="l00039"></a>00039 payload(0), |
90 | | <a name="l00040"></a>00040 refCnt(0) |
91 | | <a name="l00041"></a>00041 { |
92 | | <a name="l00042"></a>00042 } |
93 | | <a name="l00043"></a>00043 |
94 | | <a name="l00047"></a><a class="code" href="classbdm_1_1shared__ptr.html#c8b7f0a813173581530c80d7ac4fc7c2">00047</a> <a class="code" href="classbdm_1_1shared__ptr.html#66a4d41031e37618f1a9bb3c81814c11" title="Creates an empty shared_ptr - one that doesn&#39;t point anywhere.">shared_ptr</a>(T *p): |
95 | | <a name="l00048"></a>00048 payload(p), |
96 | | <a name="l00049"></a>00049 refCnt(p ? new unsigned(1) : 0) |
97 | | <a name="l00050"></a>00050 { |
98 | | <a name="l00051"></a>00051 } |
99 | | <a name="l00052"></a>00052 |
100 | | <a name="l00055"></a><a class="code" href="classbdm_1_1shared__ptr.html#357702d12bfd608a08b46e4a194aa4b5">00055</a> <a class="code" href="classbdm_1_1shared__ptr.html#66a4d41031e37618f1a9bb3c81814c11" title="Creates an empty shared_ptr - one that doesn&#39;t point anywhere.">shared_ptr</a>(<span class="keyword">const</span> <a class="code" href="classbdm_1_1shared__ptr.html" title="A naive implementation of roughly a subset of the std::tr1:shared_ptr spec (really...">shared_ptr<T></a> &other): |
101 | | <a name="l00056"></a>00056 payload(other.payload), |
102 | | <a name="l00057"></a>00057 refCnt(other.refCnt) |
103 | | <a name="l00058"></a>00058 { |
104 | | <a name="l00059"></a>00059 add_ref(); |
105 | | <a name="l00060"></a>00060 } |
106 | | <a name="l00061"></a>00061 |
107 | | <a name="l00064"></a>00064 <span class="keyword">template</span><<span class="keyword">typename</span> U> |
108 | | <a name="l00065"></a><a class="code" href="classbdm_1_1shared__ptr.html#affa5b17385493f4102c52cf75d3b509">00065</a> <a class="code" href="classbdm_1_1shared__ptr.html#66a4d41031e37618f1a9bb3c81814c11" title="Creates an empty shared_ptr - one that doesn&#39;t point anywhere.">shared_ptr</a>(<span class="keyword">const</span> <a class="code" href="classbdm_1_1shared__ptr.html" title="A naive implementation of roughly a subset of the std::tr1:shared_ptr spec (really...">shared_ptr<U></a> &other): |
109 | | <a name="l00066"></a>00066 payload(other.payload), |
110 | | <a name="l00067"></a>00067 refCnt(other.refCnt) |
111 | | <a name="l00068"></a>00068 { |
112 | | <a name="l00069"></a>00069 add_ref(); |
113 | | <a name="l00070"></a>00070 } |
114 | | <a name="l00071"></a>00071 |
115 | | <a name="l00072"></a>00072 ~<a class="code" href="classbdm_1_1shared__ptr.html" title="A naive implementation of roughly a subset of the std::tr1:shared_ptr spec (really...">shared_ptr</a>() |
116 | | <a name="l00073"></a>00073 { |
117 | | <a name="l00074"></a>00074 del_ref(); |
118 | | <a name="l00075"></a>00075 } |
119 | | <a name="l00076"></a>00076 |
120 | | <a name="l00077"></a>00077 <a class="code" href="classbdm_1_1shared__ptr.html#66a4d41031e37618f1a9bb3c81814c11" title="Creates an empty shared_ptr - one that doesn&#39;t point anywhere.">shared_ptr</a> &operator=(<span class="keyword">const</span> <a class="code" href="classbdm_1_1shared__ptr.html#66a4d41031e37618f1a9bb3c81814c11" title="Creates an empty shared_ptr - one that doesn&#39;t point anywhere.">shared_ptr</a> &other) |
121 | | <a name="l00078"></a>00078 { |
122 | | <a name="l00079"></a>00079 other.add_ref(); |
123 | | <a name="l00080"></a>00080 del_ref(); |
124 | | <a name="l00081"></a>00081 |
125 | | <a name="l00082"></a>00082 payload = other.payload; |
126 | | <a name="l00083"></a>00083 refCnt = other.refCnt; |
127 | | <a name="l00084"></a>00084 |
128 | | <a name="l00085"></a>00085 <span class="keywordflow">return</span> *<span class="keyword">this</span>; |
129 | | <a name="l00086"></a>00086 } |
130 | | <a name="l00087"></a>00087 |
131 | | <a name="l00090"></a><a class="code" href="classbdm_1_1shared__ptr.html#37ebcfb6750dd7b5630f1d2354c07a96">00090</a> T *<span class="keyword">get</span>() { <span class="keywordflow">return</span> payload; } |
132 | | <a name="l00091"></a>00091 |
133 | | <a name="l00095"></a><a class="code" href="classbdm_1_1shared__ptr.html#bcaae812868eacbad050beda465d2327">00095</a> T *<a class="code" href="classbdm_1_1shared__ptr.html#bcaae812868eacbad050beda465d2327">operator-></a>() |
134 | | <a name="l00096"></a>00096 { |
135 | | <a name="l00097"></a>00097 it_assert_debug(payload, <span class="stringliteral">"dereferencing NULL"</span>); |
136 | | <a name="l00098"></a>00098 <span class="keywordflow">return</span> payload; |
137 | | <a name="l00099"></a>00099 } |
138 | | <a name="l00100"></a>00100 |
139 | | <a name="l00104"></a><a class="code" href="classbdm_1_1shared__ptr.html#de38de837267becf5eed2839fea42c45">00104</a> T &<a class="code" href="classbdm_1_1shared__ptr.html#de38de837267becf5eed2839fea42c45">operator*</a>() |
140 | | <a name="l00105"></a>00105 { |
141 | | <a name="l00106"></a>00106 it_assert_debug(payload, <span class="stringliteral">"dereferencing NULL"</span>); |
142 | | <a name="l00107"></a>00107 <span class="keywordflow">return</span> *payload; |
143 | | <a name="l00108"></a>00108 } |
144 | | <a name="l00109"></a>00109 |
145 | | <a name="l00112"></a><a class="code" href="classbdm_1_1shared__ptr.html#fff45e4841b2921cd42ce0691f8c1196">00112</a> <span class="keyword">const</span> T* <span class="keyword">get</span>() <span class="keyword">const</span> { <span class="keywordflow">return</span> payload; } |
146 | | <a name="l00113"></a>00113 |
147 | | <a name="l00117"></a><a class="code" href="classbdm_1_1shared__ptr.html#d89ec7ceb318241d833c7d278444396d">00117</a> <span class="keyword">const</span> T *<a class="code" href="classbdm_1_1shared__ptr.html#bcaae812868eacbad050beda465d2327">operator-></a>()<span class="keyword"> const</span> |
148 | | <a name="l00118"></a>00118 <span class="keyword"> </span>{ |
149 | | <a name="l00119"></a>00119 it_assert_debug(payload, <span class="stringliteral">"dereferencing NULL"</span>); |
150 | | <a name="l00120"></a>00120 <span class="keywordflow">return</span> payload; |
151 | | <a name="l00121"></a>00121 } |
152 | | <a name="l00122"></a>00122 |
153 | | <a name="l00126"></a><a class="code" href="classbdm_1_1shared__ptr.html#de000a72fe73804aa26962432f264800">00126</a> <span class="keyword">const</span> T &<a class="code" href="classbdm_1_1shared__ptr.html#de38de837267becf5eed2839fea42c45">operator*</a>()<span class="keyword"> const</span> |
154 | | <a name="l00127"></a>00127 <span class="keyword"> </span>{ |
155 | | <a name="l00128"></a>00128 it_assert_debug(payload, <span class="stringliteral">"dereferencing NULL"</span>); |
156 | | <a name="l00129"></a>00129 <span class="keywordflow">return</span> *payload; |
157 | | <a name="l00130"></a>00130 } |
158 | | <a name="l00131"></a>00131 |
159 | | <a name="l00132"></a>00132 <span class="keywordtype">bool</span> unique()<span class="keyword"> const</span> |
160 | | <a name="l00133"></a>00133 <span class="keyword"> </span>{ |
161 | | <a name="l00134"></a>00134 <span class="keywordflow">return</span> refCnt && (*refCnt == 1); |
162 | | <a name="l00135"></a>00135 } |
| 79 | <a name="l00033"></a>00033 <span class="keyword">template</span> <<span class="keyword">typename</span> T> |
| 80 | <a name="l00034"></a><a class="code" href="classbdm_1_1shared__ptr.html">00034</a> <span class="keyword">class </span><a class="code" href="classbdm_1_1shared__ptr.html" title="A naive implementation of roughly a subset of the std::tr1::shared_ptr spec.">shared_ptr</a> { |
| 81 | <a name="l00035"></a>00035 <span class="keyword">template</span><<span class="keyword">class</span> U> <span class="keyword">friend</span> <span class="keyword">class </span><a class="code" href="classbdm_1_1shared__ptr.html" title="A naive implementation of roughly a subset of the std::tr1::shared_ptr spec.">shared_ptr</a>; |
| 82 | <a name="l00036"></a>00036 |
| 83 | <a name="l00037"></a>00037 <span class="keyword">private</span>: |
| 84 | <a name="l00038"></a>00038 T *payload; |
| 85 | <a name="l00039"></a>00039 <span class="keywordtype">unsigned</span> *refCnt; |
| 86 | <a name="l00040"></a>00040 |
| 87 | <a name="l00041"></a>00041 <span class="keyword">public</span>: |
| 88 | <a name="l00047"></a><a class="code" href="classbdm_1_1shared__ptr.html#a66a4d41031e37618f1a9bb3c81814c11">00047</a> <a class="code" href="classbdm_1_1shared__ptr.html#a66a4d41031e37618f1a9bb3c81814c11" title="Default constructor.">shared_ptr</a>() : |
| 89 | <a name="l00048"></a>00048 payload ( 0 ), |
| 90 | <a name="l00049"></a>00049 refCnt ( 0 ) { |
| 91 | <a name="l00050"></a>00050 } |
| 92 | <a name="l00051"></a>00051 |
| 93 | <a name="l00058"></a><a class="code" href="classbdm_1_1shared__ptr.html#ac8b7f0a813173581530c80d7ac4fc7c2">00058</a> <a class="code" href="classbdm_1_1shared__ptr.html#a66a4d41031e37618f1a9bb3c81814c11" title="Default constructor.">shared_ptr</a> ( T *p ) : |
| 94 | <a name="l00059"></a>00059 payload ( p ), |
| 95 | <a name="l00060"></a>00060 refCnt ( p ? new unsigned ( 1 ) : 0 ) { |
| 96 | <a name="l00061"></a>00061 } |
| 97 | <a name="l00062"></a>00062 |
| 98 | <a name="l00069"></a><a class="code" href="classbdm_1_1shared__ptr.html#a357702d12bfd608a08b46e4a194aa4b5">00069</a> <a class="code" href="classbdm_1_1shared__ptr.html#a66a4d41031e37618f1a9bb3c81814c11" title="Default constructor.">shared_ptr</a> ( <span class="keyword">const</span> <a class="code" href="classbdm_1_1shared__ptr.html" title="A naive implementation of roughly a subset of the std::tr1::shared_ptr spec.">shared_ptr<T></a> &other ) : |
| 99 | <a name="l00070"></a>00070 payload ( other.payload ), |
| 100 | <a name="l00071"></a>00071 refCnt ( other.refCnt ) { |
| 101 | <a name="l00072"></a>00072 add_ref(); |
| 102 | <a name="l00073"></a>00073 } |
| 103 | <a name="l00074"></a>00074 |
| 104 | <a name="l00084"></a>00084 <span class="keyword">template</span><<span class="keyword">typename</span> U> |
| 105 | <a name="l00085"></a><a class="code" href="classbdm_1_1shared__ptr.html#aaffa5b17385493f4102c52cf75d3b509">00085</a> <a class="code" href="classbdm_1_1shared__ptr.html#a66a4d41031e37618f1a9bb3c81814c11" title="Default constructor.">shared_ptr</a> ( <span class="keyword">const</span> <a class="code" href="classbdm_1_1shared__ptr.html" title="A naive implementation of roughly a subset of the std::tr1::shared_ptr spec.">shared_ptr<U></a> &other ) : |
| 106 | <a name="l00086"></a>00086 payload ( other.payload ), |
| 107 | <a name="l00087"></a>00087 refCnt ( other.refCnt ) { |
| 108 | <a name="l00088"></a>00088 add_ref(); |
| 109 | <a name="l00089"></a>00089 } |
| 110 | <a name="l00090"></a>00090 |
| 111 | <a name="l00094"></a><a class="code" href="classbdm_1_1shared__ptr.html#a79374842e417d2f64e449e374be4cfc1">00094</a> <a class="code" href="classbdm_1_1shared__ptr.html#a79374842e417d2f64e449e374be4cfc1">~shared_ptr</a>() { |
| 112 | <a name="l00095"></a>00095 del_ref(); |
| 113 | <a name="l00096"></a>00096 } |
| 114 | <a name="l00097"></a>00097 |
| 115 | <a name="l00101"></a><a class="code" href="classbdm_1_1shared__ptr.html#a55263b1c8efbd3f8fddf703e2c457f6c">00101</a> <a class="code" href="classbdm_1_1shared__ptr.html" title="A naive implementation of roughly a subset of the std::tr1::shared_ptr spec.">shared_ptr<T></a> &<a class="code" href="classbdm_1_1shared__ptr.html#a55263b1c8efbd3f8fddf703e2c457f6c" title="Assignment operator.">operator= </a>( <span class="keyword">const</span> <a class="code" href="classbdm_1_1shared__ptr.html" title="A naive implementation of roughly a subset of the std::tr1::shared_ptr spec.">shared_ptr<T></a> &other ) { |
| 116 | <a name="l00102"></a>00102 other.add_ref(); |
| 117 | <a name="l00103"></a>00103 del_ref(); |
| 118 | <a name="l00104"></a>00104 |
| 119 | <a name="l00105"></a>00105 payload = other.payload; |
| 120 | <a name="l00106"></a>00106 refCnt = other.refCnt; |
| 121 | <a name="l00107"></a>00107 |
| 122 | <a name="l00108"></a>00108 <span class="keywordflow">return</span> *<span class="keyword">this</span>; |
| 123 | <a name="l00109"></a>00109 } |
| 124 | <a name="l00110"></a>00110 |
| 125 | <a name="l00115"></a><a class="code" href="classbdm_1_1shared__ptr.html#a37ebcfb6750dd7b5630f1d2354c07a96">00115</a> T *<span class="keyword">get</span>() { |
| 126 | <a name="l00116"></a>00116 <span class="keywordflow">return</span> payload; |
| 127 | <a name="l00117"></a>00117 } |
| 128 | <a name="l00118"></a>00118 |
| 129 | <a name="l00124"></a><a class="code" href="classbdm_1_1shared__ptr.html#abcaae812868eacbad050beda465d2327">00124</a> T *<a class="code" href="classbdm_1_1shared__ptr.html#abcaae812868eacbad050beda465d2327">operator-></a>() { |
| 130 | <a name="l00125"></a>00125 <a class="code" href="bdmerror_8h.html#a89a0f906b242b79c5d3d342291b2cab4" title="Throw std::runtime_exception if t is not true and NDEBUG is not defined.">bdm_assert_debug</a> ( payload, <span class="stringliteral">"dereferencing NULL"</span> ); |
| 131 | <a name="l00126"></a>00126 <span class="keywordflow">return</span> payload; |
| 132 | <a name="l00127"></a>00127 } |
| 133 | <a name="l00128"></a>00128 |
| 134 | <a name="l00132"></a><a class="code" href="classbdm_1_1shared__ptr.html#ade38de837267becf5eed2839fea42c45">00132</a> T &<a class="code" href="classbdm_1_1shared__ptr.html#ade38de837267becf5eed2839fea42c45">operator*</a>() { |
| 135 | <a name="l00133"></a>00133 <a class="code" href="bdmerror_8h.html#a89a0f906b242b79c5d3d342291b2cab4" title="Throw std::runtime_exception if t is not true and NDEBUG is not defined.">bdm_assert_debug</a> ( payload, <span class="stringliteral">"dereferencing NULL"</span> ); |
| 136 | <a name="l00134"></a>00134 <span class="keywordflow">return</span> *payload; |
| 137 | <a name="l00135"></a>00135 } |
201 | | <a name="l00174"></a>00174 } |
202 | | <a name="l00175"></a>00175 }; |
203 | | <a name="l00176"></a>00176 |
204 | | <a name="l00177"></a>00177 <span class="keyword">template</span><<span class="keyword">typename</span> T, <span class="keyword">typename</span> U> |
205 | | <a name="l00178"></a>00178 <span class="keywordtype">bool</span> operator==(shared_ptr<T> <span class="keyword">const</span> &a, shared_ptr<U> <span class="keyword">const</span> &b) |
206 | | <a name="l00179"></a>00179 { |
207 | | <a name="l00180"></a>00180 <span class="keywordflow">return</span> a.get() == b.get(); |
208 | | <a name="l00181"></a>00181 } |
209 | | <a name="l00182"></a>00182 |
210 | | <a name="l00183"></a>00183 <span class="keyword">template</span><<span class="keyword">typename</span> T, <span class="keyword">typename</span> U> |
211 | | <a name="l00184"></a>00184 <span class="keywordtype">bool</span> operator!=(shared_ptr<T> <span class="keyword">const</span> &a, shared_ptr<U> <span class="keyword">const</span> &b) |
212 | | <a name="l00185"></a>00185 { |
213 | | <a name="l00186"></a>00186 <span class="keywordflow">return</span> a.get() != b.get(); |
214 | | <a name="l00187"></a>00187 } |
215 | | <a name="l00188"></a>00188 |
216 | | <a name="l00189"></a>00189 <span class="keyword">template</span><<span class="keyword">typename</span> T, <span class="keyword">typename</span> U> |
217 | | <a name="l00190"></a>00190 <span class="keywordtype">bool</span> operator<(shared_ptr<T> <span class="keyword">const</span> &a, shared_ptr<U> <span class="keyword">const</span> &b) |
218 | | <a name="l00191"></a>00191 { |
219 | | <a name="l00192"></a>00192 <span class="keywordflow">return</span> a.get() < b.get(); |
220 | | <a name="l00193"></a>00193 } |
221 | | <a name="l00194"></a>00194 |
222 | | <a name="l00195"></a>00195 } |
223 | | <a name="l00196"></a>00196 |
224 | | <a name="l00197"></a>00197 <span class="preprocessor">#endif</span> |
| 160 | <a name="l00174"></a>00174 |
| 161 | <a name="l00180"></a><a class="code" href="classbdm_1_1shared__ptr.html#a6cac71bb600f9f7c6fbed9335fd22d37">00180</a> <a class="code" href="classbdm_1_1shared__ptr.html#a6cac71bb600f9f7c6fbed9335fd22d37" title="Boolean cast.">operator bool</a>()<span class="keyword"> const </span>{ |
| 162 | <a name="l00181"></a>00181 <span class="keywordflow">return</span> !!payload; |
| 163 | <a name="l00182"></a>00182 } |
| 164 | <a name="l00183"></a>00183 |
| 165 | <a name="l00190"></a>00190 <span class="keyword">template</span><<span class="keyword">typename</span> U> |
| 166 | <a name="l00191"></a><a class="code" href="classbdm_1_1shared__ptr.html#ab3cd369b00369ab92456aa0645e8d560">00191</a> <a class="code" href="classbdm_1_1shared__ptr.html#ab3cd369b00369ab92456aa0645e8d560" title="const cast">operator shared_ptr<const U></a>()<span class="keyword"> const </span>{ |
| 167 | <a name="l00192"></a>00192 <a class="code" href="classbdm_1_1shared__ptr.html" title="A naive implementation of roughly a subset of the std::tr1::shared_ptr spec.">shared_ptr<const U></a> cptr; |
| 168 | <a name="l00193"></a>00193 cptr.refCnt = refCnt; |
| 169 | <a name="l00194"></a>00194 cptr.payload = payload; |
| 170 | <a name="l00195"></a>00195 add_ref(); |
| 171 | <a name="l00196"></a>00196 <span class="keywordflow">return</span> cptr; |
| 172 | <a name="l00197"></a>00197 } |
| 173 | <a name="l00198"></a>00198 |
| 174 | <a name="l00202"></a><a class="code" href="classbdm_1_1shared__ptr.html#afca7fd588f8de50e7590357305b00984">00202</a> <span class="keywordtype">void</span> <a class="code" href="classbdm_1_1shared__ptr.html#afca7fd588f8de50e7590357305b00984" title="Efficient swap for shared_ptr.">swap</a> ( <a class="code" href="classbdm_1_1shared__ptr.html" title="A naive implementation of roughly a subset of the std::tr1::shared_ptr spec.">shared_ptr</a> &other ) { |
| 175 | <a name="l00203"></a>00203 <a class="code" href="classbdm_1_1shared__ptr.html#afca7fd588f8de50e7590357305b00984" title="Efficient swap for shared_ptr.">std::swap</a> ( payload, other.payload ); |
| 176 | <a name="l00204"></a>00204 <a class="code" href="classbdm_1_1shared__ptr.html#afca7fd588f8de50e7590357305b00984" title="Efficient swap for shared_ptr.">std::swap</a> ( refCnt, other.refCnt ); |
| 177 | <a name="l00205"></a>00205 } |
| 178 | <a name="l00206"></a>00206 |
| 179 | <a name="l00207"></a>00207 <span class="keyword">private</span>: |
| 180 | <a name="l00208"></a>00208 <span class="keywordtype">void</span> add_ref()<span class="keyword"> const </span>{ |
| 181 | <a name="l00209"></a>00209 <span class="keywordflow">if</span> ( refCnt ) { |
| 182 | <a name="l00210"></a>00210 <span class="keywordflow">if</span> ( *refCnt == UINT_MAX ) { |
| 183 | <a name="l00211"></a>00211 <span class="keywordflow">throw</span> std::overflow_error ( |
| 184 | <a name="l00212"></a>00212 std::string ( <span class="stringliteral">"Shared pointer has too many references."</span> ) ); |
| 185 | <a name="l00213"></a>00213 } |
| 186 | <a name="l00214"></a>00214 |
| 187 | <a name="l00215"></a>00215 ++*refCnt; |
| 188 | <a name="l00216"></a>00216 } |
| 189 | <a name="l00217"></a>00217 } |
| 190 | <a name="l00218"></a>00218 |
| 191 | <a name="l00219"></a>00219 <span class="keywordtype">void</span> del_ref() { |
| 192 | <a name="l00220"></a>00220 <span class="keywordflow">if</span> ( refCnt ) { |
| 193 | <a name="l00221"></a>00221 <span class="keywordflow">if</span> ( ! ( --*refCnt ) ) { |
| 194 | <a name="l00222"></a>00222 <span class="keyword">delete</span> payload; |
| 195 | <a name="l00223"></a>00223 <span class="keyword">delete</span> refCnt; |
| 196 | <a name="l00224"></a>00224 } |
| 197 | <a name="l00225"></a>00225 } |
| 198 | <a name="l00226"></a>00226 } |
| 199 | <a name="l00227"></a>00227 }; |
| 200 | <a name="l00228"></a>00228 |
| 201 | <a name="l00230"></a>00230 <span class="keyword">template</span><<span class="keyword">typename</span> T, <span class="keyword">typename</span> U> |
| 202 | <a name="l00231"></a>00231 <span class="keywordtype">bool</span> operator== ( shared_ptr<T> <span class="keyword">const</span> &a, shared_ptr<U> <span class="keyword">const</span> &b ) { |
| 203 | <a name="l00232"></a>00232 <span class="keywordflow">return</span> a.get() == b.get(); |
| 204 | <a name="l00233"></a>00233 } |
| 205 | <a name="l00234"></a>00234 |
| 206 | <a name="l00236"></a>00236 <span class="keyword">template</span><<span class="keyword">typename</span> T, <span class="keyword">typename</span> U> |
| 207 | <a name="l00237"></a>00237 <span class="keywordtype">bool</span> operator!= ( shared_ptr<T> <span class="keyword">const</span> &a, shared_ptr<U> <span class="keyword">const</span> &b ) { |
| 208 | <a name="l00238"></a>00238 <span class="keywordflow">return</span> a.get() != b.get(); |
| 209 | <a name="l00239"></a>00239 } |
| 210 | <a name="l00240"></a>00240 |
| 211 | <a name="l00242"></a>00242 <span class="keyword">template</span><<span class="keyword">typename</span> T, <span class="keyword">typename</span> U> |
| 212 | <a name="l00243"></a>00243 <span class="keywordtype">bool</span> operator< ( shared_ptr<T> <span class="keyword">const</span> &a, shared_ptr<U> <span class="keyword">const</span> &b ) { |
| 213 | <a name="l00244"></a>00244 <span class="keywordflow">return</span> a.get() < b.get(); |
| 214 | <a name="l00245"></a>00245 } |
| 215 | <a name="l00246"></a>00246 |
| 216 | <a name="l00254"></a>00254 <span class="keyword">template</span> <<span class="keyword">typename</span> T> |
| 217 | <a name="l00255"></a><a class="code" href="classbdm_1_1object__ptr.html">00255</a> <span class="keyword">class </span><a class="code" href="classbdm_1_1object__ptr.html" title="A wrapper of shared_ptr which is never empty.">object_ptr</a> : <span class="keyword">public</span> <a class="code" href="classbdm_1_1shared__ptr.html" title="A naive implementation of roughly a subset of the std::tr1::shared_ptr spec.">shared_ptr</a><T> |
| 218 | <a name="l00256"></a>00256 { |
| 219 | <a name="l00257"></a>00257 <span class="keyword">public</span>: |
| 220 | <a name="l00263"></a><a class="code" href="classbdm_1_1object__ptr.html#a9f1016ff2bbfa497d1cc0a4497c1fba4">00263</a> <a class="code" href="classbdm_1_1object__ptr.html#a9f1016ff2bbfa497d1cc0a4497c1fba4" title="Default constructor.">object_ptr</a>() : <a class="code" href="classbdm_1_1shared__ptr.html" title="A naive implementation of roughly a subset of the std::tr1::shared_ptr spec.">shared_ptr</a><T> ( new T() ) { } |
| 221 | <a name="l00264"></a>00264 |
| 222 | <a name="l00270"></a><a class="code" href="classbdm_1_1object__ptr.html#adcb20017177c98274c16087b2c0067dc">00270</a> <a class="code" href="classbdm_1_1object__ptr.html#adcb20017177c98274c16087b2c0067dc" title="Upcast from shared_ptr&lt;T&gt; to object_ptr&lt;T&gt;.">object_ptr</a> ( <span class="keyword">const</span> <a class="code" href="classbdm_1_1shared__ptr.html" title="A naive implementation of roughly a subset of the std::tr1::shared_ptr spec.">shared_ptr<T></a> &b ) : <a class="code" href="classbdm_1_1shared__ptr.html" title="A naive implementation of roughly a subset of the std::tr1::shared_ptr spec.">shared_ptr</a><T> ( b ) { |
| 223 | <a name="l00271"></a>00271 <a class="code" href="bdmerror_8h.html#a89a0f906b242b79c5d3d342291b2cab4" title="Throw std::runtime_exception if t is not true and NDEBUG is not defined.">bdm_assert_debug</a> ( this-><span class="keyword">get</span>(), <span class="stringliteral">"object_ptr cannot be empty"</span> ); |
| 224 | <a name="l00272"></a>00272 } |
| 225 | <a name="l00273"></a>00273 |
| 226 | <a name="l00278"></a><a class="code" href="classbdm_1_1object__ptr.html#aa9eb00600d8640711eadeed98c7892e9">00278</a> <a class="code" href="classbdm_1_1object__ptr.html#aa9eb00600d8640711eadeed98c7892e9">object_ptr</a> ( T *p ) : <a class="code" href="classbdm_1_1shared__ptr.html" title="A naive implementation of roughly a subset of the std::tr1::shared_ptr spec.">shared_ptr</a><T> ( p ) { |
| 227 | <a name="l00279"></a>00279 <a class="code" href="bdmerror_8h.html#a89a0f906b242b79c5d3d342291b2cab4" title="Throw std::runtime_exception if t is not true and NDEBUG is not defined.">bdm_assert_debug</a> ( p, <span class="stringliteral">"object_ptr cannot be empty"</span> ); |
| 228 | <a name="l00280"></a>00280 } |
| 229 | <a name="l00281"></a>00281 |
| 230 | <a name="l00285"></a><a class="code" href="classbdm_1_1object__ptr.html#a9c80da05b7b3dfc59333ca9bf6f62970">00285</a> <a class="code" href="classbdm_1_1object__ptr.html" title="A wrapper of shared_ptr which is never empty.">object_ptr<T></a> &<a class="code" href="classbdm_1_1object__ptr.html#a9c80da05b7b3dfc59333ca9bf6f62970" title="Assignment operator.">operator= </a>( <span class="keyword">const</span> <a class="code" href="classbdm_1_1object__ptr.html" title="A wrapper of shared_ptr which is never empty.">object_ptr<T></a> &other ) { |
| 231 | <a name="l00286"></a>00286 <a class="code" href="classbdm_1_1object__ptr.html#a9c80da05b7b3dfc59333ca9bf6f62970" title="Assignment operator.">shared_ptr<T>::operator= </a>( other ); |
| 232 | <a name="l00287"></a>00287 <span class="keywordflow">return</span> *<span class="keyword">this</span>; |
| 233 | <a name="l00288"></a>00288 } |
| 234 | <a name="l00289"></a>00289 }; |
| 235 | <a name="l00290"></a>00290 |
| 236 | <a name="l00291"></a>00291 <span class="preprocessor">#define SHAREDPTR(class_name) typedef bdm::object_ptr< class_name > class_name##_ptr</span> |
| 237 | <a name="l00292"></a>00292 <span class="preprocessor"></span> |
| 238 | <a name="l00293"></a>00293 <span class="preprocessor">#define SHAREDPTR2(template_class_name, template_parameter_name) typedef bdm::object_ptr< template_class_name < template_parameter_name > > template_class_name##_##template_parameter_name##_ptr</span> |
| 239 | <a name="l00294"></a>00294 <span class="preprocessor"></span> |
| 240 | <a name="l00295"></a>00295 } |
| 241 | <a name="l00296"></a>00296 |
| 242 | <a name="l00297"></a>00297 <span class="preprocessor">#endif</span> |