BufferStream.hpp 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  1. /*
  2. Copyright (c) 2009-2010 Christopher A. Taylor. All rights reserved.
  3. Redistribution and use in source and binary forms, with or without
  4. modification, are permitted provided that the following conditions are met:
  5. * Redistributions of source code must retain the above copyright notice,
  6. this list of conditions and the following disclaimer.
  7. * Redistributions in binary form must reproduce the above copyright notice,
  8. this list of conditions and the following disclaimer in the documentation
  9. and/or other materials provided with the distribution.
  10. * Neither the name of LibCat nor the names of its contributors may be used
  11. to endorse or promote products derived from this software without
  12. specific prior written permission.
  13. THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
  14. AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  15. IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  16. ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
  17. LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
  18. CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
  19. SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
  20. INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
  21. CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
  22. ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
  23. POSSIBILITY OF SUCH DAMAGE.
  24. */
  25. #ifndef CAT_BUFFER_STREAM_HPP
  26. #define CAT_BUFFER_STREAM_HPP
  27. #include <cat/Platform.hpp>
  28. #include <cat/port/EndianNeutral.hpp>
  29. namespace cat {
  30. /*
  31. BufferStream
  32. Extremely light-weight wrapper for a buffer data insertion and extraction.
  33. Performs automatic endian conversion into and out of the buffer (sometimes).
  34. Performs no checking for buffer overruns.
  35. This class does not add any security to your code; it just makes it shorter.
  36. Use at your peril.
  37. */
  38. class BufferStream
  39. {
  40. protected:
  41. u8 *_buffer;
  42. public:
  43. CAT_INLINE BufferStream(u8 *buffer) { _buffer = buffer; }
  44. CAT_INLINE BufferStream &operator=(u8 *buffer) { _buffer = buffer; }
  45. CAT_INLINE u32 GetOffset(void *buffer) { return (u32)(_buffer - reinterpret_cast<u8*>( buffer )); }
  46. public:
  47. // Auto-cast to u8* or char*
  48. CAT_INLINE operator u8*() { return _buffer; }
  49. CAT_INLINE char *c_str() { return reinterpret_cast<char*>( _buffer ); }
  50. CAT_INLINE BufferStream &operator++() { _buffer++; return *this; }
  51. CAT_INLINE BufferStream &operator+=(int skip_bytes) { _buffer += skip_bytes; return *this; }
  52. public:
  53. // Insertion
  54. CAT_INLINE BufferStream &operator<<(s8 data) { *_buffer++ = (u8)data; return *this; }
  55. CAT_INLINE BufferStream &operator<<(s16 data) { *(u16*)_buffer = getLE16((u16)data); _buffer += 2; return *this; }
  56. CAT_INLINE BufferStream &operator<<(s32 data) { *(u32*)_buffer = getLE32((u32)data); _buffer += 4; return *this; }
  57. CAT_INLINE BufferStream &operator<<(s64 data) { *(u64*)_buffer = getLE64((u64)data); _buffer += 8; return *this; }
  58. CAT_INLINE BufferStream &operator<<(u8 data) { *_buffer++ = data; return *this; }
  59. CAT_INLINE BufferStream &operator<<(u16 data) { *(u16*)_buffer = getLE16(data); _buffer += 2; return *this; }
  60. CAT_INLINE BufferStream &operator<<(u32 data) { *(u32*)_buffer = getLE32(data); _buffer += 4; return *this; }
  61. CAT_INLINE BufferStream &operator<<(u64 data) { *(u64*)_buffer = getLE64(data); _buffer += 8; return *this; }
  62. CAT_INLINE void write(const void *data, u32 bytes)
  63. {
  64. memcpy(_buffer, data, bytes);
  65. _buffer += bytes;
  66. }
  67. template<class T>
  68. CAT_INLINE BufferStream &operator<<(const T &data) { write(&data, sizeof(T)); return *this; }
  69. public:
  70. // Extraction
  71. CAT_INLINE BufferStream &operator>>(s8 &data) { data = (s8)*_buffer++; return *this; }
  72. CAT_INLINE BufferStream &operator>>(s16 &data) { data = (s16)getLE16(*(u16*)_buffer); _buffer += 2; return *this; }
  73. CAT_INLINE BufferStream &operator>>(s32 &data) { data = (s32)getLE32(*(u32*)_buffer); _buffer += 4; return *this; }
  74. CAT_INLINE BufferStream &operator>>(s64 &data) { data = (s64)getLE64(*(u64*)_buffer); _buffer += 8; return *this; }
  75. CAT_INLINE BufferStream &operator>>(u8 &data) { data = *_buffer++; return *this; }
  76. CAT_INLINE BufferStream &operator>>(u16 &data) { data = getLE16(*(u16*)_buffer); _buffer += 2; return *this; }
  77. CAT_INLINE BufferStream &operator>>(u32 &data) { data = getLE32(*(u32*)_buffer); _buffer += 4; return *this; }
  78. CAT_INLINE BufferStream &operator>>(u64 &data) { data = getLE64(*(u64*)_buffer); _buffer += 8; return *this; }
  79. CAT_INLINE void read(void *data, u32 bytes)
  80. {
  81. memcpy(data, _buffer, bytes);
  82. _buffer += bytes;
  83. }
  84. template<class T>
  85. CAT_INLINE BufferStream &operator>>(T &data) { read(&data, sizeof(T)); return *this; }
  86. };
  87. } // namespace cat
  88. #endif // CAT_BUFFER_STREAM_HPP
粤ICP备19079148号