memStream.h 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  1. //-----------------------------------------------------------------------------
  2. // Copyright (c) 2012 GarageGames, LLC
  3. //
  4. // Permission is hereby granted, free of charge, to any person obtaining a copy
  5. // of this software and associated documentation files (the "Software"), to
  6. // deal in the Software without restriction, including without limitation the
  7. // rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
  8. // sell copies of the Software, and to permit persons to whom the Software is
  9. // furnished to do so, subject to the following conditions:
  10. //
  11. // The above copyright notice and this permission notice shall be included in
  12. // all copies or substantial portions of the Software.
  13. //
  14. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  15. // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  16. // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  17. // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  18. // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
  19. // FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
  20. // IN THE SOFTWARE.
  21. //-----------------------------------------------------------------------------
  22. #ifndef _MEMSTREAM_H_
  23. #define _MEMSTREAM_H_
  24. #ifndef _STREAM_H_
  25. #include "core/stream/stream.h"
  26. #endif
  27. /// The MemStream class is used to read and write to a memory buffer.
  28. class MemStream : public Stream
  29. {
  30. typedef Stream Parent;
  31. protected:
  32. /// The amount to grow the buffer when we run out of
  33. /// space writing to the stream.
  34. U32 mGrowSize;
  35. /// The actual size of the memory buffer. It is always
  36. /// greater than or equal to the mStreamSize.
  37. U32 mBufferSize;
  38. /// The size of the data in the stream. It is always
  39. /// less than or equal to the mBufferSize.
  40. U32 mStreamSize;
  41. /// The memory buffer.
  42. void *mBufferBase;
  43. /// If true the memory is owned by the steam and it
  44. /// will be deleted in the destructor.
  45. bool mOwnsMemory;
  46. ///
  47. U32 mInstCaps;
  48. /// Our current read/write position within the buffer.
  49. U32 mCurrentPosition;
  50. public:
  51. /// This constructs an empty memory stream that will grow
  52. /// in increments as needed.
  53. MemStream( U32 growSize,
  54. bool allowRead = true,
  55. bool allowWrite = true );
  56. /// This constructs the stream with a fixed size memory buffer. If
  57. /// buffer is null then it will be allocated for you.
  58. MemStream( U32 bufferSize,
  59. void *buffer,
  60. bool allowRead = true,
  61. bool allowWrite = true );
  62. /// The destructor.
  63. virtual ~MemStream();
  64. protected:
  65. // Stream
  66. bool _read( const U32 in_numBytes, void *out_pBuffer );
  67. bool _write( const U32 in_numBytes, const void *in_pBuffer );
  68. public:
  69. // Stream
  70. bool hasCapability( const Capability caps ) const;
  71. U32 getPosition() const;
  72. bool setPosition( const U32 in_newPosition );
  73. U32 getStreamSize();
  74. /// Returns the memory buffer.
  75. void *getBuffer() { return mBufferBase; }
  76. const void *getBuffer() const { return mBufferBase; }
  77. /// Takes the memory buffer reseting the stream.
  78. void *takeBuffer();
  79. };
  80. #endif //_MEMSTREAM_H_