libH/DynamicBuffer.cpp

Go to the documentation of this file.
00001 
00012 /*
00013   
00014   Copyright (c) 2007, Tim Burrell
00015   Licensed under the Apache License, Version 2.0 (the "License");
00016   you may not use this file except in compliance with the License.
00017   You may obtain a copy of the License at 
00018 
00019         http://www.apache.org/licenses/LICENSE-2.0
00020 
00021   Unless required by applicable law or agreed to in writing, software
00022   distributed under the License is distributed on an "AS IS" BASIS,
00023   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
00024   See the License for the specific language governing permissions and 
00025   limitations under the License. 
00026   
00027 */
00028 
00029 #include "Debug.hpp"
00030 #include "Exception.hpp"
00031 #include <stdlib.h>
00032 #include <string.h>
00033 
00034 using namespace std;
00035 using namespace H;
00036 
00038 // Namespace
00040 
00041 namespace H {
00042 
00044 // Construction
00046 
00050 template <class DataType>
00051 DynamicBuffer<DataType>::DynamicBuffer() {
00052         mLength = 0;
00053         mBuffer = NULL;
00054 }
00055 
00059 template <class DataType>
00060 DynamicBuffer<DataType>::~DynamicBuffer() {
00061         clear();
00062 }
00063 
00067 template <class ConvertTo, class DataType>
00068 DynamicBufferConverter<ConvertTo, DataType>::DynamicBufferConverter() {
00069 }
00070 
00074 template <class ConvertTo, class DataType>
00075 DynamicBufferConverter<ConvertTo, DataType>::~DynamicBufferConverter() {
00076 }
00077 
00079 // Class Body
00081 
00087 template <class DataType>
00088 void DynamicBuffer<DataType>::addToBuffer(const DataType * AddBuf, size_t BufLen) {
00089         if ((mBuffer = (DataType *) (realloc(mBuffer, (mLength + 1 + BufLen) * sizeof(DataType)))) == NULL)
00090                 throw H::Exception("DynamicBuffer :: Failed to Allocate Memory!!", __FILE__, __FUNCTION__, __LINE__);
00091 
00092         memcpy(mBuffer + mLength, AddBuf, sizeof(DataType) * BufLen);   
00093         mLength += BufLen;
00094         mBuffer[mLength] = '\0';
00095 }
00096 
00100 template <class DataType>
00101 void DynamicBuffer<DataType>::clear() {
00102         if (mBuffer)
00103                 free(mBuffer);
00104         mBuffer = NULL;
00105         mLength = 0;
00106 }
00107 
00113 template <class DataType, class ConvertTo>
00114 void DynamicBufferConverter<DataType, ConvertTo>::convert(std::vector<ConvertTo> & ConvertedVector, DynamicBuffer<DataType> const & Buffer) {
00115         int NumItems = Buffer.length() / sizeof(ConvertTo);
00116         ConvertedVector.resize(NumItems);
00117         const char * tBuf = Buffer.getBuffer();
00118         for (int lp = 0; lp < NumItems; lp ++)
00119                 memcpy(&ConvertedVector[lp], tBuf + (lp * sizeof(ConvertTo)), sizeof(ConvertTo));
00120 }
00121 
00126 template <class DataType>
00127 const DataType * DynamicBuffer<DataType>::getBuffer() const {
00128         return mBuffer;
00129 }
00130 
00135 template <class DataType>
00136 size_t DynamicBuffer<DataType>::length() const {
00137         return mLength;
00138 }
00139 
00141 
00142 } // end namespace

Generated on Wed Nov 7 10:04:16 2007 for gizmod by  doxygen 1.5.3