Shadowrun: Awakened 29 September 2011 - Build 871
BufferStream.hpp
Go to the documentation of this file.
00001 /*
00002     Copyright (c) 2009-2010 Christopher A. Taylor.  All rights reserved.
00003 
00004     Redistribution and use in source and binary forms, with or without
00005     modification, are permitted provided that the following conditions are met:
00006 
00007     * Redistributions of source code must retain the above copyright notice,
00008       this list of conditions and the following disclaimer.
00009     * Redistributions in binary form must reproduce the above copyright notice,
00010       this list of conditions and the following disclaimer in the documentation
00011       and/or other materials provided with the distribution.
00012     * Neither the name of LibCat nor the names of its contributors may be used
00013       to endorse or promote products derived from this software without
00014       specific prior written permission.
00015 
00016     THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
00017     AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
00018     IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
00019     ARE DISCLAIMED.  IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
00020     LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
00021     CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
00022     SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
00023     INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
00024     CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
00025     ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
00026     POSSIBILITY OF SUCH DAMAGE.
00027 */
00028 
00029 #ifndef CAT_BUFFER_STREAM_HPP
00030 #define CAT_BUFFER_STREAM_HPP
00031 
00032 #include <cat/Platform.hpp>
00033 #include <cat/port/EndianNeutral.hpp>
00034 
00035 namespace cat {
00036 
00037 
00038 /*
00039     BufferStream
00040 
00041     Extremely light-weight wrapper for a buffer data insertion and extraction.
00042 
00043     Performs automatic endian conversion into and out of the buffer (sometimes).
00044 
00045     Performs no checking for buffer overruns.
00046     This class does not add any security to your code; it just makes it shorter.
00047     Use at your peril.
00048 */
00049 class BufferStream
00050 {
00051 protected:
00052     u8 *_buffer;
00053 
00054 public:
00055     CAT_INLINE BufferStream(u8 *buffer) { _buffer = buffer; }
00056     CAT_INLINE BufferStream &operator=(u8 *buffer) { _buffer = buffer; }
00057 
00058     CAT_INLINE u32 GetOffset(void *buffer) { return (u32)(_buffer - reinterpret_cast<u8*>( buffer )); }
00059 
00060 public:
00061     // Auto-cast to u8* or char*
00062     CAT_INLINE operator u8*() { return _buffer; }
00063     CAT_INLINE char *c_str() { return reinterpret_cast<char*>( _buffer ); }
00064 
00065     CAT_INLINE BufferStream &operator++() { _buffer++; return *this; }
00066     CAT_INLINE BufferStream &operator+=(int skip_bytes) { _buffer += skip_bytes; return *this; }
00067 
00068 public:
00069     // Insertion
00070     CAT_INLINE BufferStream &operator<<(s8 data) { *_buffer++ = (u8)data; return *this; }
00071     CAT_INLINE BufferStream &operator<<(s16 data) { *(u16*)_buffer = getLE16((u16)data); _buffer += 2; return *this; }
00072     CAT_INLINE BufferStream &operator<<(s32 data) { *(u32*)_buffer = getLE32((u32)data); _buffer += 4; return *this; }
00073     CAT_INLINE BufferStream &operator<<(s64 data) { *(u64*)_buffer = getLE64((u64)data); _buffer += 8; return *this; }
00074 
00075     CAT_INLINE BufferStream &operator<<(u8 data) { *_buffer++ = data; return *this; }
00076     CAT_INLINE BufferStream &operator<<(u16 data) { *(u16*)_buffer = getLE16(data); _buffer += 2; return *this; }
00077     CAT_INLINE BufferStream &operator<<(u32 data) { *(u32*)_buffer = getLE32(data); _buffer += 4; return *this; }
00078     CAT_INLINE BufferStream &operator<<(u64 data) { *(u64*)_buffer = getLE64(data); _buffer += 8; return *this; }
00079 
00080     CAT_INLINE void write(const void *data, u32 bytes)
00081     {
00082         memcpy(_buffer, data, bytes);
00083         _buffer += bytes;
00084     }
00085 
00086     template<class T>
00087     CAT_INLINE BufferStream &operator<<(const T &data) { write(&data, sizeof(T)); return *this; }
00088 
00089 public:
00090     // Extraction
00091     CAT_INLINE BufferStream &operator>>(s8 &data) { data = (s8)*_buffer++; return *this; }
00092     CAT_INLINE BufferStream &operator>>(s16 &data) { data = (s16)getLE16(*(u16*)_buffer); _buffer += 2; return *this; }
00093     CAT_INLINE BufferStream &operator>>(s32 &data) { data = (s32)getLE32(*(u32*)_buffer); _buffer += 4; return *this; }
00094     CAT_INLINE BufferStream &operator>>(s64 &data) { data = (s64)getLE64(*(u64*)_buffer); _buffer += 8; return *this; }
00095 
00096     CAT_INLINE BufferStream &operator>>(u8 &data) { data = *_buffer++; return *this; }
00097     CAT_INLINE BufferStream &operator>>(u16 &data) { data = getLE16(*(u16*)_buffer); _buffer += 2; return *this; }
00098     CAT_INLINE BufferStream &operator>>(u32 &data) { data = getLE32(*(u32*)_buffer); _buffer += 4; return *this; }
00099     CAT_INLINE BufferStream &operator>>(u64 &data) { data = getLE64(*(u64*)_buffer); _buffer += 8; return *this; }
00100 
00101     CAT_INLINE void read(void *data, u32 bytes)
00102     {
00103         memcpy(data, _buffer, bytes);
00104         _buffer += bytes;
00105     }
00106 
00107     template<class T>
00108     CAT_INLINE BufferStream &operator>>(T &data) { read(&data, sizeof(T)); return *this; }
00109 };
00110 
00111 
00112 } // namespace cat
00113 
00114 #endif // CAT_BUFFER_STREAM_HPP

Copyright © 2007-2010 by The Shadowrun: Awakened Team. This work is licensed under the GNU Lesser General Public License 3.

GNU Lesser General Public License 3 Sourceforge.net