00001
00006
00007
00008
00009
00010
00011
00012
00013
00014 #include "CTB.hxx"
00015 #include "CTB_Trace.hxx"
00016
00017 #include "CTBnum.hxx"
00018 #include "CTBexceptionIndexRange.hxx"
00019 #include "CTBosFill.hxx"
00020 #include "CTBsharedBuffer.hxx"
00021
00032
00034
00035 template <class T>
00036 const T& CTBsharedBuffer<T>::At(CTBint i_ind) const
00037 {
00038 CTB_Trace("CTBsharedBuffer::At(CTBint) const");
00039
00040 if (i_ind < 0 || i_ind >= mi_size)
00041 throw CTBexceptionIndexRange(i_ind,mi_size,"CTBsharedBuffer::At()");
00042 return mp_data[i_ind];
00043 }
00044
00045
00047
00048 template <class T>
00049 T& CTBsharedBuffer<T>::At(CTBint i_ind)
00050 {
00051 CTB_Trace("CTBsharedBuffer::At(CTBint)");
00052
00053 if (i_ind < 0 || i_ind >= mi_size)
00054 throw CTBexceptionIndexRange(i_ind,mi_size,"CTBsharedBuffer::At()");
00055 return mp_data[i_ind];
00056 }
00057
00058
00060
00061 template <class T>
00062 void CTBsharedBuffer<T>::Release()
00063 {
00064 CTB_Trace("CTBsharedBuffer::Release()");
00065
00066 if (mp_refcount) {
00067 if (mp_refcount->Decrement()) {
00068 delete [] mp_data;
00069 delete mp_refcount;
00070 }
00071 mp_data = 0;
00072 mi_size = 0;
00073 mp_refcount = 0;
00074 }
00075 return;
00076 }
00077
00078
00080
00081 template <class T>
00082 void CTBsharedBuffer<T>::Unshare()
00083 {
00084 CTB_Trace("CTBsharedBuffer::Unshare()");
00085
00086 if (IsShared()) {
00087 CTBsharedBuffer<T> old(*this,true);
00088 CreateBuffer(old.Size());
00089 CopyData(old.Data(),old.Size());
00090 }
00091 return;
00092 }
00093
00094
00096
00097 template <class T>
00098 void CTBsharedBuffer<T>::Resize(CTBint i_size, bool b_keep)
00099 {
00100 CTB_Trace("CTBsharedBuffer::Resize(CTBint, bool)");
00101
00102 if (IsNull()) {
00103 CreateBuffer(i_size);
00104 } else {
00105 if (i_size != Size() || IsShared()) {
00106 CTBsharedBuffer<T> old(*this,true);
00107 CreateBuffer(i_size);
00108 if (b_keep) CopyData(old.Data(),old.Size());
00109 }
00110 }
00111 return;
00112 }
00113
00114
00116
00121 template <class T>
00122 void CTBsharedBuffer<T>::Grab(CTBsharedBuffer<T>& rhs)
00123 {
00124 CTB_Trace("CTBsharedBuffer::Grab(CTBsharedBuffer&)");
00125
00126 if (this != &rhs) {
00127 Release();
00128 mp_data = rhs.mp_data;
00129 mi_size = rhs.mi_size;
00130 mp_refcount = rhs.mp_refcount;
00131 rhs.mp_data = 0;
00132 rhs.mi_size = 0;
00133 rhs.mp_refcount = 0;
00134 }
00135 return;
00136 }
00137
00138
00140
00141 template <class T>
00142 void CTBsharedBuffer<T>::SharedCopy(const CTBsharedBuffer<T>& rhs)
00143 {
00144 CTB_Trace("CTBsharedBuffer::SharedCopy(const CTBsharedBuffer&)");
00145
00146 if (*this != rhs) {
00147 Release();
00148 mp_data = rhs.mp_data;
00149 mi_size = rhs.mi_size;
00150 mp_refcount = rhs.mp_refcount;
00151 if (mp_refcount) mp_refcount->Increment();
00152 }
00153 return;
00154 }
00155
00156
00158
00159 template <class T>
00160 void CTBsharedBuffer<T>::PrivateCopy(const CTBsharedBuffer<T>& rhs)
00161 {
00162 CTB_Trace("CTBsharedBuffer::PrivateCopy(const CTBsharedBuffer&)");
00163
00164 if (*this != rhs) {
00165 Release();
00166 CreateBuffer(rhs.Size());
00167 CopyData(rhs.Data(),rhs.Size());
00168 } else {
00169 Unshare();
00170 }
00171 return;
00172 }
00173
00174
00176
00177 template <class T>
00178 void CTBsharedBuffer<T>::Dump(int i_indent, ostream& os,
00179 const char* p_text) const
00180 {
00181 CTBosFill bl(i_indent);
00182
00183 os << bl << "--CTBsharedBuffer<T> ";
00184 if (p_text) os << p_text;
00185 os << " @ " << this << endl;
00186 os << bl << " mp_data: " << (void*) mp_data;
00187 if (mp_data) os << " ... " << (void*) (mp_data + (mi_size-1));
00188 os << endl;
00189 os << bl << " mi_size: " << mi_size << endl;
00190 os << bl << " mp_refcount: " << mp_refcount;
00191 if (mp_refcount) os << " (" << *mp_refcount << ")";
00192 os << endl;
00193 return;
00194 }
00195
00196
00198
00199 template <class T>
00200 void CTBsharedBuffer<T>::CreateBuffer(CTBint i_size)
00201 {
00202 CTB_Trace("CTBsharedBuffer::CreateBuffer(CTBint)");
00203
00204 mi_size = i_size;
00205 if (i_size > 0) {
00206 mp_data = new T [i_size];
00207 mp_refcount = new CTBreferenceCounter;
00208 } else {
00209 mp_data = 0;
00210 mp_refcount = 0;
00211 }
00212 return;
00213 }
00214
00215
00217
00218 template <class T>
00219 void CTBsharedBuffer<T>::CopyData(const T rhs[], CTBint i_size)
00220 {
00221 CTB_Trace("CTBsharedBuffer::CopyData(const T[], CTBint)");
00222 CTBint i_n = CTBmin(i_size,Size());
00223
00224 for (int i = 0; i < i_n ; i++) mp_data[i] = rhs[i];
00225 return;
00226 }