File: plcountedpointer_8h-source.html

package info (click to toggle)
paintlib 2.6.2-8
  • links: PTS
  • area: main
  • in suites: etch, etch-m68k
  • size: 7,900 kB
  • ctags: 3,875
  • sloc: cpp: 25,209; sh: 10,600; ansic: 1,891; makefile: 119
file content (141 lines) | stat: -rw-r--r-- 8,756 bytes parent folder | download | duplicates (2)
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
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<html><head><meta http-equiv="Content-Type" content="text/html;charset=iso-8859-1">
<title>paintlib: plcountedpointer.h Source File</title>
<link href="doxygen.css" rel="stylesheet" type="text/css">
</head><body>
<!-- Generated by Doxygen 1.3.2 -->
<div class="qindex"><a class="qindex" href="index.html">Main&nbsp;Page</a> | <a class="qindex" href="hierarchy.html">Class&nbsp;Hierarchy</a> | <a class="qindex" href="classes.html">Alphabetical&nbsp;List</a> | <a class="qindex" href="annotated.html">Compound&nbsp;List</a> | <a class="qindex" href="files.html">File&nbsp;List</a> | <a class="qindex" href="functions.html">Compound&nbsp;Members</a></div>
<h1>plcountedpointer.h</h1><div class="fragment"><pre>00001 <span class="preprocessor">#ifndef PLCOUNTED_POINTER_H</span>
00002 <span class="preprocessor"></span><span class="preprocessor">#define PLCOUNTED_POINTER_H</span>
00003 <span class="preprocessor"></span>
00004 
00005 <span class="comment">// if the counted pointer is used as a pointer to a fundamental type</span>
00006 <span class="comment">// then a warning is generated about the overload of the -&gt; operator</span>
00007 <span class="comment">// as its use would now be invalid. Not a lot we can do about this</span>
00008 <span class="comment">// except ignore the warning. (this is what the STL does with iterators)</span>
00009 <span class="comment">//</span>
00010 <span class="comment">// something similar might be needed for other compilers as well</span>
00011 <span class="preprocessor">#ifdef _MSC_VER</span>
00012 <span class="preprocessor"></span><span class="preprocessor">#pragma warning(disable: 4284)</span>
00013 <span class="preprocessor"></span><span class="preprocessor">#endif</span>
00014 <span class="preprocessor"></span>
00015 <span class="preprocessor">#include &lt;stdlib.h&gt;</span>
00016 
00017 <span class="keyword">template</span> &lt;<span class="keyword">class</span> type&gt;
00018 <span class="keyword">class </span>PLCountedPointer
00019 {
00020 <span class="keyword">public</span>:
00021   <span class="keyword">explicit</span>  PLCountedPointer(type * pType = 0)
00022               : pBody(pType), pCount(new size_t(1))         {}
00023             PLCountedPointer(<span class="keyword">const</span> PLCountedPointer &amp; copy)
00024               : pBody(copy.pBody), pCount(copy.pCount)      { incCount(); }
00025             ~PLCountedPointer()                             { decCount(); }
00026 
00027             PLCountedPointer&amp; operator=(<span class="keyword">const</span> PLCountedPointer &amp; rhs);
00028 
00029   type *    operator -&gt; ()<span class="keyword"> const                            </span>{ <span class="keywordflow">return</span> pBody; }
00030   type &amp;    operator * ()<span class="keyword"> const                             </span>{ <span class="keywordflow">return</span> *pBody; }
00031 
00032   type *    get()<span class="keyword"> const                                     </span>{ <span class="keywordflow">return</span> pBody; }
00033 
00034   <span class="keywordtype">bool</span>      operator == (<span class="keyword">const</span> PLCountedPointer &amp; rhs)<span class="keyword"> const  </span>{ <span class="keywordflow">return</span> pBody == rhs.pBody; }
00035   <span class="keywordtype">bool</span>      operator != (<span class="keyword">const</span> PLCountedPointer &amp; rhs)<span class="keyword"> const  </span>{ <span class="keywordflow">return</span> pBody != rhs.pBody; }
00036 
00037   operator bool ()<span class="keyword"> const                                    </span>{ <span class="keywordflow">return</span> pBody != 0; }
00038 
00039 <span class="keyword">private</span>:
00040   <span class="keywordtype">void</span>      incCount()                                      { ++*pCount; }
00041   <span class="keywordtype">void</span>      decCount();
00042 
00043 <span class="keyword">private</span>:
00044   type *      pBody;
00045   size_t *    pCount;
00046 };
00047 
00048 <span class="keyword">template</span> &lt;<span class="keyword">class</span> type&gt;
00049 PLCountedPointer&lt;type&gt;&amp; PLCountedPointer&lt;type&gt;::operator=(<span class="keyword">const</span> PLCountedPointer &amp; rhs)
00050 {
00051   <span class="keywordflow">if</span> (pBody != rhs.pBody)
00052   {
00053     decCount();
00054     pBody = rhs.pBody;
00055     pCount = rhs.pCount;
00056     incCount();
00057   }
00058   <span class="keywordflow">return</span> *<span class="keyword">this</span>;
00059 }
00060 
00061 <span class="keyword">template</span> &lt;<span class="keyword">class</span> type&gt;
00062 <span class="keywordtype">void</span> PLCountedPointer&lt;type&gt;::decCount()
00063 {
00064   <span class="keywordflow">if</span> (!--*pCount)
00065   {
00066     <span class="keyword">delete</span> pBody;
00067     <span class="keyword">delete</span> pCount;
00068   }
00069 }
00070 
00071 <span class="keyword">template</span> &lt;<span class="keyword">class</span> type&gt;
00072 <span class="keyword">class </span>PLCountedArrayPointer
00073 {
00074 <span class="keyword">public</span>:
00075   <span class="keyword">explicit</span>  PLCountedArrayPointer(type * pType = 0)
00076               : pBody(pType), pCount(new size_t(1))         {}
00077             PLCountedArrayPointer(<span class="keyword">const</span> PLCountedArrayPointer &amp; copy)
00078               : pBody(copy.pBody), pCount(copy.pCount)      { incCount(); }
00079             ~PLCountedArrayPointer()                        { decCount(); }
00080 
00081             PLCountedArrayPointer&amp; operator=(<span class="keyword">const</span> PLCountedArrayPointer &amp; rhs);
00082 
00083   type *    operator -&gt; ()<span class="keyword"> const                            </span>{ <span class="keywordflow">return</span> pBody; }
00084   type &amp;    operator * ()<span class="keyword"> const                             </span>{ <span class="keywordflow">return</span> *pBody; }
00085 
00086   type *    get()<span class="keyword"> const                                     </span>{ <span class="keywordflow">return</span> pBody; }
00087 
00088   <span class="keywordtype">bool</span>      operator == (<span class="keyword">const</span> PLCountedArrayPointer &amp; rhs)<span class="keyword"> const </span>{ <span class="keywordflow">return</span> pBody == rhs.pBody; }
00089   <span class="keywordtype">bool</span>      operator != (<span class="keyword">const</span> PLCountedArrayPointer &amp; rhs)<span class="keyword"> const </span>{ <span class="keywordflow">return</span> pBody != rhs.pBody; }
00090 
00091   type &amp;       operator [] (size_t i)                       { <span class="keywordflow">return</span> pBody[i]; }
00092   <span class="keyword">const</span> type &amp; operator [] (size_t i)<span class="keyword"> const                 </span>{ <span class="keywordflow">return</span> pBody[i]; }
00093 
00094   operator bool ()<span class="keyword"> const                                    </span>{ <span class="keywordflow">return</span> pBody != 0; }
00095 
00096 <span class="keyword">private</span>:
00097   <span class="keywordtype">void</span>      incCount()                                      { ++*pCount; }
00098   <span class="keywordtype">void</span>      decCount();
00099 
00100 <span class="keyword">private</span>:
00101   type *      pBody;
00102   size_t *    pCount;
00103 };
00104 
00105 <span class="keyword">template</span> &lt;<span class="keyword">class</span> type&gt;
00106 PLCountedArrayPointer&lt;type&gt;&amp; PLCountedArrayPointer&lt;type&gt;::operator=(<span class="keyword">const</span> PLCountedArrayPointer &amp; rhs)
00107 {
00108   <span class="keywordflow">if</span> (pBody != rhs.pBody)
00109   {
00110     decCount();
00111     pBody = rhs.pBody;
00112     pCount = rhs.pCount;
00113     incCount();
00114   }
00115   <span class="keywordflow">return</span> *<span class="keyword">this</span>;
00116 }
00117 
00118 <span class="keyword">template</span> &lt;<span class="keyword">class</span> type&gt;
00119 <span class="keywordtype">void</span> PLCountedArrayPointer&lt;type&gt;::decCount()
00120 {
00121   <span class="keywordflow">if</span> (!--*pCount)
00122   {
00123     <span class="keyword">delete</span> [] pBody;
00124     <span class="keyword">delete</span> pCount;
00125   }
00126 }
00127 
00128 <span class="preprocessor">#endif</span>
</pre></div><hr size="1"><address style="align: right;"><small>Generated on Mon Sep 13 16:16:40 2004 for paintlib by
<a href="http://www.doxygen.org/index.html">
<img src="doxygen.png" alt="doxygen" align="middle" border=0 > 
</a>1.3.2 </small></address>
</body>
</html>