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 Page</a> | <a class="qindex" href="hierarchy.html">Class Hierarchy</a> | <a class="qindex" href="classes.html">Alphabetical List</a> | <a class="qindex" href="annotated.html">Compound List</a> | <a class="qindex" href="files.html">File List</a> | <a class="qindex" href="functions.html">Compound 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 -> 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 <stdlib.h></span>
00016
00017 <span class="keyword">template</span> <<span class="keyword">class</span> type>
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 & copy)
00024 : pBody(copy.pBody), pCount(copy.pCount) { incCount(); }
00025 ~PLCountedPointer() { decCount(); }
00026
00027 PLCountedPointer& operator=(<span class="keyword">const</span> PLCountedPointer & rhs);
00028
00029 type * operator -> ()<span class="keyword"> const </span>{ <span class="keywordflow">return</span> pBody; }
00030 type & 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 & 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 & 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> <<span class="keyword">class</span> type>
00049 PLCountedPointer<type>& PLCountedPointer<type>::operator=(<span class="keyword">const</span> PLCountedPointer & 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> <<span class="keyword">class</span> type>
00062 <span class="keywordtype">void</span> PLCountedPointer<type>::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> <<span class="keyword">class</span> type>
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 & copy)
00078 : pBody(copy.pBody), pCount(copy.pCount) { incCount(); }
00079 ~PLCountedArrayPointer() { decCount(); }
00080
00081 PLCountedArrayPointer& operator=(<span class="keyword">const</span> PLCountedArrayPointer & rhs);
00082
00083 type * operator -> ()<span class="keyword"> const </span>{ <span class="keywordflow">return</span> pBody; }
00084 type & 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 & 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 & rhs)<span class="keyword"> const </span>{ <span class="keywordflow">return</span> pBody != rhs.pBody; }
00090
00091 type & operator [] (size_t i) { <span class="keywordflow">return</span> pBody[i]; }
00092 <span class="keyword">const</span> type & 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> <<span class="keyword">class</span> type>
00106 PLCountedArrayPointer<type>& PLCountedArrayPointer<type>::operator=(<span class="keyword">const</span> PLCountedArrayPointer & 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> <<span class="keyword">class</span> type>
00119 <span class="keywordtype">void</span> PLCountedArrayPointer<type>::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>
|