VectorBuffer.h 2.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576
  1. //
  2. // Urho3D Engine
  3. // Copyright (c) 2008-2011 Lasse Öörni
  4. //
  5. // Permission is hereby granted, free of charge, to any person obtaining a copy
  6. // of this software and associated documentation files (the "Software"), to deal
  7. // in the Software without restriction, including without limitation the rights
  8. // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  9. // copies of the Software, and to permit persons to whom the Software is
  10. // furnished to do so, subject to the following conditions:
  11. //
  12. // The above copyright notice and this permission notice shall be included in
  13. // all copies or substantial portions of the Software.
  14. //
  15. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  16. // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  17. // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  18. // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  19. // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  20. // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  21. // THE SOFTWARE.
  22. //
  23. #ifndef COMMON_VECTORBUFFER_H
  24. #define COMMON_VECTORBUFFER_H
  25. #include "Deserializer.h"
  26. #include "Serializer.h"
  27. #include <cstdio>
  28. #include <vector>
  29. //! A dynamically sized buffer that can be read and written to as a stream
  30. class VectorBuffer : public Deserializer, public Serializer
  31. {
  32. public:
  33. //! Construct an empty buffer
  34. VectorBuffer();
  35. //! Construct from another buffer
  36. VectorBuffer(const std::vector<unsigned char>& data);
  37. //! Construct from a memory area
  38. VectorBuffer(const void* data, unsigned size);
  39. //! Construct from a stream
  40. VectorBuffer(Deserializer& source, unsigned size);
  41. //! Read bytes from the buffer
  42. virtual void read(void* dest, unsigned size);
  43. //! Set position from the beginning of the buffer
  44. virtual unsigned seek(unsigned position);
  45. //! Write bytes to the buffer
  46. virtual void write(const void* data, unsigned size);
  47. //! Set data from another buffer
  48. void setData(const std::vector<unsigned char>& data);
  49. //! Set data from a memory area
  50. void setData(const void* data, unsigned size);
  51. //! Set data from a stream
  52. void setData(Deserializer& source, unsigned size);
  53. //! Reset to zero size
  54. void clear();
  55. //! Set size
  56. void resize(unsigned size);
  57. //! Return data
  58. const unsigned char* getData() const { return mSize ? &mBuffer[0] : 0; }
  59. //! Return non-const data
  60. unsigned char* getModifiableData() { return mSize ? &mBuffer[0] : 0; }
  61. //! Return the buffer
  62. const std::vector<unsigned char>& getBuffer() const { return mBuffer; }
  63. private:
  64. //! Dynamic sized buffer
  65. std::vector<unsigned char> mBuffer;
  66. };
  67. #endif // COMMON_VECTORBUFFER_H