MemoryFile.h 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147
  1. /*
  2. Copyright (c) 2013 Daniele Bartolini, Michele Rossi
  3. Copyright (c) 2012 Daniele Bartolini, Simone Boscaratto
  4. Permission is hereby granted, free of charge, to any person
  5. obtaining a copy of this software and associated documentation
  6. files (the "Software"), to deal in the Software without
  7. restriction, including without limitation the rights to use,
  8. copy, modify, merge, publish, distribute, sublicense, and/or sell
  9. copies of the Software, and to permit persons to whom the
  10. Software is furnished to do so, subject to the following
  11. conditions:
  12. The above copyright notice and this permission notice shall be
  13. included in all copies or substantial portions of the Software.
  14. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  15. EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
  16. OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  17. NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
  18. HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
  19. WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
  20. FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
  21. OTHER DEALINGS IN THE SOFTWARE.
  22. */
  23. #pragma once
  24. #include "Types.h"
  25. #include "File.h"
  26. #include "Allocator.h"
  27. #include "Assert.h"
  28. namespace crown
  29. {
  30. class MemoryBuffer
  31. {
  32. public:
  33. MemoryBuffer();
  34. virtual ~MemoryBuffer();
  35. virtual void release() = 0;
  36. virtual void allocate(size_t size) = 0;
  37. virtual bool is_valid() = 0;
  38. virtual size_t size() = 0;
  39. virtual uint8_t* data() = 0;
  40. virtual void write(uint8_t* src, size_t offset, size_t size) = 0;
  41. };
  42. class DynamicMemoryBuffer: public MemoryBuffer
  43. {
  44. public:
  45. DynamicMemoryBuffer(Allocator& allocator, size_t initial_capacity);
  46. virtual ~DynamicMemoryBuffer();
  47. void release();
  48. void allocate(size_t capacity);
  49. inline bool is_valid() { return m_buffer != 0; }
  50. void check_space(size_t offset, size_t space);
  51. void write(uint8_t* src, size_t offset, size_t size);
  52. inline size_t size() { return m_size; }
  53. inline size_t capacity() { return m_capacity; }
  54. inline uint8_t* data() { return m_buffer; }
  55. protected:
  56. Allocator& m_allocator;
  57. uint8_t* m_buffer;
  58. size_t m_capacity;
  59. size_t m_size;
  60. };
  61. /// Memory file.
  62. /// Access memory buffers.
  63. class MemoryFile: public File
  64. {
  65. public:
  66. /// @copydoc File::File()
  67. MemoryFile(MemoryBuffer* buffer, FileOpenMode mode);
  68. /// @copydoc File::~File()
  69. virtual ~MemoryFile();
  70. /// @copydoc File::seek()
  71. void seek(size_t position);
  72. /// @copydoc File::seek_to_end()
  73. void seek_to_end();
  74. /// @copydoc File::skip()
  75. void skip(size_t bytes);
  76. /// @copydoc File::read()
  77. void read(void* buffer, size_t size);
  78. /// @copydoc File::write()
  79. void write(const void* buffer, size_t size);
  80. /// @copydoc File::copy_to()
  81. bool copy_to(File& file, size_t size = 0);
  82. /// @copydoc File::flush()
  83. void flush();
  84. /// @copydoc File::end_of_file()
  85. bool end_of_file() const { return size() == m_memory_offset; }
  86. /// @copydoc File::is_valid()
  87. bool is_valid() const { CE_ASSERT(m_memory != NULL, "Memory is NULL"); return m_memory->is_valid(); }
  88. /// @copydoc File::size()
  89. size_t size() const { CE_ASSERT(m_memory != NULL, "Memory is NULL"); return m_memory->size(); }
  90. /// @copydoc File::position()
  91. size_t position() const { return m_memory_offset; }
  92. /// @copydoc File::can_read()
  93. bool can_read() const { return true; }
  94. /// @copydoc File::can_write()
  95. bool can_write() const { return true; }
  96. /// @copydoc File::can_seek()
  97. bool can_seek() const { return true; }
  98. /// Dumps the data to the console.
  99. void dump();
  100. protected:
  101. inline void check_valid() { CE_ASSERT(m_memory != NULL, "Memory is NULL"); }
  102. MemoryBuffer* m_memory;
  103. size_t m_memory_offset;
  104. };
  105. }