File: refcounted_8h-source.html

package info (click to toggle)
mysql%2B%2B 3.0.9-1
  • links: PTS
  • area: main
  • in suites: squeeze
  • size: 11,228 kB
  • ctags: 9,647
  • sloc: cpp: 33,154; sh: 3,098; perl: 778; makefile: 700
file content (183 lines) | stat: -rw-r--r-- 16,980 bytes parent folder | download
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
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
	<title>MySQL++ Reference Manual</title>
	<meta http-equiv="Content-type" content="text/html;charset=iso-8859-1">
	<link href="doxygen.css" rel="stylesheet" type="text/css">
	<link href="refman.css" rel="stylesheet" type="text/css">
</head>
<body>
<!-- Generated by Doxygen 1.4.7 -->
<div class="tabs">
  <ul>
    <li><a href="index.html"><span>Main&nbsp;Page</span></a></li>
    <li><a href="annotated.html"><span>Classes</span></a></li>
    <li id="current"><a href="files.html"><span>Files</span></a></li>
  </ul></div>
<div class="tabs">
  <ul>
    <li><a href="files.html"><span>File&nbsp;List</span></a></li>
    <li><a href="globals.html"><span>File&nbsp;Members</span></a></li>
  </ul></div>
<h1>refcounted.h</h1><a href="refcounted_8h.html">Go to the documentation of this file.</a><div class="fragment"><pre class="fragment"><a name="l00001"></a>00001 
<a name="l00002"></a>00002 
<a name="l00003"></a>00003 
<a name="l00004"></a>00004 <span class="comment">/***********************************************************************</span>
<a name="l00005"></a>00005 <span class="comment"> Copyright (c) 2007 by Educational Technology Resources, Inc. and</span>
<a name="l00006"></a>00006 <span class="comment"> (c) 2007 by Jonathan Wakely.  Others may also hold copyrights on</span>
<a name="l00007"></a>00007 <span class="comment"> code in this file.  See the CREDITS.txt file in the top directory</span>
<a name="l00008"></a>00008 <span class="comment"> of the distribution for details.</span>
<a name="l00009"></a>00009 <span class="comment"></span>
<a name="l00010"></a>00010 <span class="comment"> This file is part of MySQL++.</span>
<a name="l00011"></a>00011 <span class="comment"></span>
<a name="l00012"></a>00012 <span class="comment"> MySQL++ is free software; you can redistribute it and/or modify it</span>
<a name="l00013"></a>00013 <span class="comment"> under the terms of the GNU Lesser General Public License as published</span>
<a name="l00014"></a>00014 <span class="comment"> by the Free Software Foundation; either version 2.1 of the License, or</span>
<a name="l00015"></a>00015 <span class="comment"> (at your option) any later version.</span>
<a name="l00016"></a>00016 <span class="comment"></span>
<a name="l00017"></a>00017 <span class="comment"> MySQL++ is distributed in the hope that it will be useful, but WITHOUT</span>
<a name="l00018"></a>00018 <span class="comment"> ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or</span>
<a name="l00019"></a>00019 <span class="comment"> FITNESS FOR A PARTICULAR PURPOSE.  See the GNU Lesser General Public</span>
<a name="l00020"></a>00020 <span class="comment"> License for more details.</span>
<a name="l00021"></a>00021 <span class="comment"></span>
<a name="l00022"></a>00022 <span class="comment"> You should have received a copy of the GNU Lesser General Public</span>
<a name="l00023"></a>00023 <span class="comment"> License along with MySQL++; if not, write to the Free Software</span>
<a name="l00024"></a>00024 <span class="comment"> Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301</span>
<a name="l00025"></a>00025 <span class="comment"> USA</span>
<a name="l00026"></a>00026 <span class="comment">***********************************************************************/</span>
<a name="l00027"></a>00027 
<a name="l00028"></a>00028 <span class="preprocessor">#if !defined(MYSQLPP_REFCOUNTED_H)</span>
<a name="l00029"></a>00029 <span class="preprocessor"></span><span class="preprocessor">#define MYSQLPP_REFCOUNTED_H</span>
<a name="l00030"></a>00030 <span class="preprocessor"></span>
<a name="l00031"></a>00031 <span class="preprocessor">#include &lt;memory&gt;</span>
<a name="l00032"></a>00032 
<a name="l00033"></a>00033 <span class="keyword">namespace </span>mysqlpp {
<a name="l00034"></a>00034 
<a name="l00042"></a>00042 <span class="keyword">template</span> &lt;<span class="keyword">class</span> T&gt;
<a name="l00043"></a><a class="code" href="structmysqlpp_1_1RefCountedPointerDestroyer.html">00043</a> <span class="keyword">struct </span><a class="code" href="structmysqlpp_1_1RefCountedPointerDestroyer.html">RefCountedPointerDestroyer</a>
<a name="l00044"></a>00044 {
<a name="l00046"></a><a class="code" href="structmysqlpp_1_1RefCountedPointerDestroyer.html#6009c03320583a0f3c89a8819b15e0a4">00046</a>         <span class="keywordtype">void</span> <a class="code" href="structmysqlpp_1_1RefCountedPointerDestroyer.html#6009c03320583a0f3c89a8819b15e0a4">operator()</a>(T* doomed)<span class="keyword"> const </span>{ <span class="keyword">delete</span> doomed; }
<a name="l00047"></a>00047 };
<a name="l00048"></a>00048 
<a name="l00049"></a>00049 
<a name="l00076"></a>00076 
<a name="l00077"></a>00077 <span class="keyword">template</span> &lt;<span class="keyword">class</span> T, <span class="keyword">class</span> Destroyer = RefCountedPo<span class="keywordtype">int</span>erDestroyer&lt;T&gt; &gt;
<a name="l00078"></a><a class="code" href="classmysqlpp_1_1RefCountedPointer.html">00078</a> <span class="keyword">class </span><a class="code" href="classmysqlpp_1_1RefCountedPointer.html">RefCountedPointer</a>
<a name="l00079"></a>00079 {
<a name="l00080"></a>00080 <span class="keyword">public</span>:
<a name="l00081"></a><a class="code" href="classmysqlpp_1_1RefCountedPointer.html#745d3cf3d478d70c09474eb8766bae3a">00081</a>         <span class="keyword">typedef</span> <a class="code" href="classmysqlpp_1_1RefCountedPointer.html">RefCountedPointer&lt;T&gt;</a> <a class="code" href="classmysqlpp_1_1RefCountedPointer.html">ThisType</a>;  
<a name="l00082"></a>00082 
<a name="l00087"></a><a class="code" href="classmysqlpp_1_1RefCountedPointer.html#02aa2860b0644b6333cee8f321477fee">00087</a>         <a class="code" href="classmysqlpp_1_1RefCountedPointer.html#02aa2860b0644b6333cee8f321477fee">RefCountedPointer</a>() :
<a name="l00088"></a>00088         counted_(0),
<a name="l00089"></a>00089         refs_(0)
<a name="l00090"></a>00090         {
<a name="l00091"></a>00091         }
<a name="l00092"></a>00092         
<a name="l00098"></a><a class="code" href="classmysqlpp_1_1RefCountedPointer.html#586313b144a88e487d77adf7d64bd3bb">00098</a>         <span class="keyword">explicit</span> <a class="code" href="classmysqlpp_1_1RefCountedPointer.html#02aa2860b0644b6333cee8f321477fee">RefCountedPointer</a>(T* c) :
<a name="l00099"></a>00099         counted_(c),
<a name="l00100"></a>00100         refs_(0)
<a name="l00101"></a>00101         {
<a name="l00102"></a>00102                 std::auto_ptr&lt;T&gt; exception_guard(counted_);
<a name="l00103"></a>00103                 <span class="keywordflow">if</span> (counted_) {
<a name="l00104"></a>00104                         refs_ = <span class="keyword">new</span> size_t(1);
<a name="l00105"></a>00105                 }
<a name="l00106"></a>00106                 exception_guard.release();      <span class="comment">// previous new didn't throw</span>
<a name="l00107"></a>00107         }
<a name="l00108"></a>00108 
<a name="l00110"></a><a class="code" href="classmysqlpp_1_1RefCountedPointer.html#e25ec10685c0c1ae2c7afa6bd72f483c">00110</a>         <a class="code" href="classmysqlpp_1_1RefCountedPointer.html#02aa2860b0644b6333cee8f321477fee">RefCountedPointer</a>(<span class="keyword">const</span> <a class="code" href="classmysqlpp_1_1RefCountedPointer.html">ThisType</a>&amp; other) :
<a name="l00111"></a>00111         counted_(other.counted_),
<a name="l00112"></a>00112         refs_(other.counted_ ? other.refs_ : 0)
<a name="l00113"></a>00113         {
<a name="l00114"></a>00114                 <span class="keywordflow">if</span> (counted_) {
<a name="l00115"></a>00115                         ++(*refs_);
<a name="l00116"></a>00116                 }
<a name="l00117"></a>00117         }
<a name="l00118"></a>00118 
<a name="l00123"></a><a class="code" href="classmysqlpp_1_1RefCountedPointer.html#67b7d093310fa6112a45c361c37d5ed3">00123</a>         <a class="code" href="classmysqlpp_1_1RefCountedPointer.html#67b7d093310fa6112a45c361c37d5ed3">~RefCountedPointer</a>()
<a name="l00124"></a>00124         {
<a name="l00125"></a>00125                 <span class="keywordflow">if</span> (refs_ &amp;&amp; (--(*refs_) == 0)) {
<a name="l00126"></a>00126                         Destroyer()(counted_);
<a name="l00127"></a>00127                         <span class="keyword">delete</span> refs_;
<a name="l00128"></a>00128                 }
<a name="l00129"></a>00129         }
<a name="l00130"></a>00130 
<a name="l00138"></a><a class="code" href="classmysqlpp_1_1RefCountedPointer.html#c6a54b0c4fdac3d05389639c5bc71168">00138</a>         <a class="code" href="classmysqlpp_1_1RefCountedPointer.html">ThisType</a>&amp; <a class="code" href="classmysqlpp_1_1RefCountedPointer.html#c6a54b0c4fdac3d05389639c5bc71168">assign</a>(T* c)
<a name="l00139"></a>00139         {
<a name="l00140"></a>00140                 <span class="comment">// The create-temporary-and-swap idiom lets us keep memory</span>
<a name="l00141"></a>00141                 <span class="comment">// allocation in the ctor and deallocation in the dtor so</span>
<a name="l00142"></a>00142                 <span class="comment">// we don't leak in the face of an exception.</span>
<a name="l00143"></a>00143                 <a class="code" href="classmysqlpp_1_1RefCountedPointer.html#745d3cf3d478d70c09474eb8766bae3a">ThisType</a>(c).swap(*<span class="keyword">this</span>);
<a name="l00144"></a>00144                 <span class="keywordflow">return</span> *<span class="keyword">this</span>;
<a name="l00145"></a>00145         }
<a name="l00146"></a>00146 
<a name="l00155"></a><a class="code" href="classmysqlpp_1_1RefCountedPointer.html#a806e37d44b4d0384dc0bcd1deefd1b9">00155</a>         <a class="code" href="classmysqlpp_1_1RefCountedPointer.html">ThisType</a>&amp; <a class="code" href="classmysqlpp_1_1RefCountedPointer.html#c6a54b0c4fdac3d05389639c5bc71168">assign</a>(<span class="keyword">const</span> <a class="code" href="classmysqlpp_1_1RefCountedPointer.html">ThisType</a>&amp; other)
<a name="l00156"></a>00156         {
<a name="l00157"></a>00157                 <span class="comment">// The create-temporary-and-swap idiom lets us keep memory</span>
<a name="l00158"></a>00158                 <span class="comment">// allocation in the ctor and deallocation in the dtor so</span>
<a name="l00159"></a>00159                 <span class="comment">// we don't leak in the face of an exception.</span>
<a name="l00160"></a>00160                 <a class="code" href="classmysqlpp_1_1RefCountedPointer.html#745d3cf3d478d70c09474eb8766bae3a">ThisType</a>(other).swap(*<span class="keyword">this</span>);
<a name="l00161"></a>00161                 <span class="keywordflow">return</span> *<span class="keyword">this</span>;
<a name="l00162"></a>00162         }
<a name="l00163"></a>00163 
<a name="l00168"></a><a class="code" href="classmysqlpp_1_1RefCountedPointer.html#33e254256ace5abc2b07d8a630e55188">00168</a>         <a class="code" href="classmysqlpp_1_1RefCountedPointer.html">ThisType</a>&amp; <a class="code" href="classmysqlpp_1_1RefCountedPointer.html#33e254256ace5abc2b07d8a630e55188">operator =</a>(T* c)
<a name="l00169"></a>00169         {
<a name="l00170"></a>00170                 <span class="keywordflow">return</span> <a class="code" href="classmysqlpp_1_1RefCountedPointer.html#c6a54b0c4fdac3d05389639c5bc71168">assign</a>(c);
<a name="l00171"></a>00171         }
<a name="l00172"></a>00172 
<a name="l00178"></a><a class="code" href="classmysqlpp_1_1RefCountedPointer.html#506a85c3e871b5436eb20d6436fb5d9d">00178</a>         <a class="code" href="classmysqlpp_1_1RefCountedPointer.html">ThisType</a>&amp; <a class="code" href="classmysqlpp_1_1RefCountedPointer.html#33e254256ace5abc2b07d8a630e55188">operator =</a>(<span class="keyword">const</span> <a class="code" href="classmysqlpp_1_1RefCountedPointer.html">ThisType</a>&amp; rhs)
<a name="l00179"></a>00179         {
<a name="l00180"></a>00180                 <span class="keywordflow">return</span> <a class="code" href="classmysqlpp_1_1RefCountedPointer.html#c6a54b0c4fdac3d05389639c5bc71168">assign</a>(rhs);
<a name="l00181"></a>00181         }
<a name="l00182"></a>00182 
<a name="l00184"></a><a class="code" href="classmysqlpp_1_1RefCountedPointer.html#02fc8a47004dad04067d7cb781e93dd7">00184</a>         T* <a class="code" href="classmysqlpp_1_1RefCountedPointer.html#02fc8a47004dad04067d7cb781e93dd7">operator -&gt;</a>()<span class="keyword"> const</span>
<a name="l00185"></a>00185 <span class="keyword">        </span>{
<a name="l00186"></a>00186                 <span class="keywordflow">return</span> counted_;
<a name="l00187"></a>00187         }       
<a name="l00188"></a>00188 
<a name="l00190"></a><a class="code" href="classmysqlpp_1_1RefCountedPointer.html#f31d17f14e26b6605ff192d58ababea9">00190</a>         T&amp; <a class="code" href="classmysqlpp_1_1RefCountedPointer.html#f31d17f14e26b6605ff192d58ababea9">operator *</a>()<span class="keyword"> const</span>
<a name="l00191"></a>00191 <span class="keyword">        </span>{
<a name="l00192"></a>00192                 <span class="keywordflow">return</span> *counted_;
<a name="l00193"></a>00193         }       
<a name="l00194"></a>00194 
<a name="l00214"></a><a class="code" href="classmysqlpp_1_1RefCountedPointer.html#48f681f74dbe025e8c639f65850e2302">00214</a>         <a class="code" href="classmysqlpp_1_1RefCountedPointer.html#48f681f74dbe025e8c639f65850e2302">operator void*</a>()
<a name="l00215"></a>00215         {
<a name="l00216"></a>00216                 <span class="keywordflow">return</span> counted_;
<a name="l00217"></a>00217         }
<a name="l00218"></a>00218 
<a name="l00222"></a><a class="code" href="classmysqlpp_1_1RefCountedPointer.html#067a0afe4a3f5ef1a6a68ddbcdced99a">00222</a>         <a class="code" href="classmysqlpp_1_1RefCountedPointer.html#067a0afe4a3f5ef1a6a68ddbcdced99a">operator const void*</a>()<span class="keyword"> const</span>
<a name="l00223"></a>00223 <span class="keyword">        </span>{
<a name="l00224"></a>00224                 <span class="keywordflow">return</span> counted_;
<a name="l00225"></a>00225         }
<a name="l00226"></a>00226 
<a name="l00228"></a><a class="code" href="classmysqlpp_1_1RefCountedPointer.html#26757f1138dded33faeb1978004d0639">00228</a>         T* <a class="code" href="classmysqlpp_1_1RefCountedPointer.html#26757f1138dded33faeb1978004d0639">raw</a>()
<a name="l00229"></a>00229         {
<a name="l00230"></a>00230                 <span class="keywordflow">return</span> counted_;
<a name="l00231"></a>00231         }
<a name="l00232"></a>00232 
<a name="l00234"></a><a class="code" href="classmysqlpp_1_1RefCountedPointer.html#a2698fb8295dcab0c4bc1df9253c8509">00234</a>         <span class="keyword">const</span> T* <a class="code" href="classmysqlpp_1_1RefCountedPointer.html#26757f1138dded33faeb1978004d0639">raw</a>()<span class="keyword"> const</span>
<a name="l00235"></a>00235 <span class="keyword">        </span>{
<a name="l00236"></a>00236                 <span class="keywordflow">return</span> counted_;
<a name="l00237"></a>00237         }
<a name="l00238"></a>00238 
<a name="l00243"></a><a class="code" href="classmysqlpp_1_1RefCountedPointer.html#c6de38aa306bfe6166384e70bc9dbaef">00243</a>         <span class="keywordtype">void</span> <a class="code" href="classmysqlpp_1_1RefCountedPointer.html#c6de38aa306bfe6166384e70bc9dbaef">swap</a>(<a class="code" href="classmysqlpp_1_1RefCountedPointer.html">ThisType</a>&amp; other)
<a name="l00244"></a>00244         {
<a name="l00245"></a>00245                 std::swap(counted_, other.<a class="code" href="classmysqlpp_1_1RefCountedPointer.html#79005d82ddadc43b4c36860efecfe8ac">counted_</a>);
<a name="l00246"></a>00246                 std::swap(refs_, other.<a class="code" href="classmysqlpp_1_1RefCountedPointer.html#6dead477b1df59e525270be82283a88f">refs_</a>);
<a name="l00247"></a>00247         }       
<a name="l00248"></a>00248 
<a name="l00249"></a>00249 <span class="keyword">private</span>:
<a name="l00251"></a>00251         T* counted_;
<a name="l00252"></a>00252 
<a name="l00258"></a>00258         size_t* refs_;
<a name="l00259"></a>00259 };
<a name="l00260"></a>00260 
<a name="l00261"></a>00261 
<a name="l00262"></a>00262 } <span class="comment">// end namespace mysqlpp</span>
<a name="l00263"></a>00263 
<a name="l00264"></a>00264 <span class="preprocessor">#endif // !defined(MYSQLPP_REFCOUNTED_H)</span>
<a name="l00265"></a>00265 <span class="preprocessor"></span>
</pre></div><hr size="1"><address style="align: right;"><small>Generated on Wed Feb 4 14:42:56 2009 for MySQL++ by&nbsp;
<a href="http://www.doxygen.org/index.html">
<img src="doxygen.png" alt="doxygen" align="middle" border="0"></a> 1.4.7 </small></address>
</body>
</html>