Savarese Software Research Corporation
ByteBuffer.h
Go to the documentation of this file.
00001 /*
00002  *
00003  * Copyright 2006 Savarese Software Research Corporation
00004  *
00005  * Licensed under the Apache License, Version 2.0 (the "License");
00006  * you may not use this file except in compliance with the License.
00007  * You may obtain a copy of the License at
00008  *
00009  *     http://www.savarese.com/software/ApacheLicense-2.0
00010  *
00011  * Unless required by applicable law or agreed to in writing, software
00012  * distributed under the License is distributed on an "AS IS" BASIS,
00013  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
00014  * See the License for the specific language governing permissions and
00015  * limitations under the License.
00016  */
00017 
00023 #ifndef __SSRC_SPREAD_DETAIL_BYTE_BUFFER_H
00024 #define __SSRC_SPREAD_DETAIL_BYTE_BUFFER_H
00025 
00026 #include <ssrc/spread/detail/Buffer.h>
00027 
00028 __BEGIN_NS_SSRC_SPREAD
00029 
00030 namespace detail {
00031 
00043   class ByteBuffer : public detail::Buffer<char> {
00044     typedef detail::Buffer<char> super;
00045 
00046     unsigned int _offset;
00047 
00048   public:
00049 
00056     explicit ByteBuffer(const unsigned int capacity) : super(0), _offset(0) {
00057       super::reserve(capacity);
00058     }
00059 
00066     ByteBuffer(const ByteBuffer & buffer) : super(0), _offset(0) {
00067       super::reserve(buffer.capacity());
00068       write(&buffer[0], buffer.size());
00069       seek(buffer.offset());
00070     }
00071 
00079     ByteBuffer & operator=(const ByteBuffer & buffer) {
00080       if(&buffer != this) {
00081         clear();
00082         write(&buffer[0], buffer.size());
00083         seek(buffer.offset());
00084       }
00085       return *this;
00086     }
00087 
00093     unsigned int offset() const {
00094       return _offset;
00095     }
00096 
00105     unsigned int seek(const unsigned int offset) {
00106       if(offset <= size())
00107         _offset = offset;
00108       return _offset;
00109     }
00110 
00112     void rewind() {
00113       seek(0);
00114     }
00115 
00119     void clear() {
00120       rewind();
00121       resize(0);
00122     }
00123 
00135     void write(const void *data, const unsigned int size) {
00136       unsigned int total = _offset + size;
00137 
00138       if(total > ByteBuffer::size())
00139         resize(total);
00140 
00141       std::memcpy(&(this->operator[](_offset)), data, size);
00142 
00143       _offset = total;
00144     }
00145 
00157     unsigned int read(void *data, unsigned int size) {
00158       if(_offset >= ByteBuffer::size())
00159         return 0;
00160 
00161       unsigned int total = _offset + size;
00162 
00163       if(total > ByteBuffer::size()) {
00164         total = ByteBuffer::size();
00165         size = total - _offset;
00166       }
00167 
00168       std::memcpy(data, &(this->operator[](_offset)), size);
00169 
00170       _offset = total;
00171 
00172       return size;
00173     }
00174 
00175   };
00176 
00177 }
00178 
00179 __END_NS_SSRC_SPREAD
00180 
00181 #endif

Savarese Software Research Corporation
Copyright © 2006-2011 Savarese Software Research Corporation. All rights reserved.