NullFile.h 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  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 "File.h"
  25. namespace crown
  26. {
  27. /// Bit bucket file.
  28. /// Discards all data written to it and provides null data reading from it; plain and simple.
  29. class NullFile: public File
  30. {
  31. public:
  32. /// @copydoc File::File()
  33. NullFile(FileOpenMode mode) : File(mode) {}
  34. /// @copydoc File::~File()
  35. virtual ~NullFile() {}
  36. /// @copydoc File::seek()
  37. void seek(size_t position) { (void)position; }
  38. /// @copydoc File::seek_to_end()
  39. void seek_to_end() {}
  40. /// @copydoc File::skip()
  41. void skip(size_t bytes) { (void)bytes; }
  42. /// @copydoc File::read()
  43. /// @note
  44. /// Fills buffer with zeroes
  45. void read(void* buffer, size_t size)
  46. {
  47. for (size_t i = 0; i < size; i++)
  48. {
  49. ((uint8_t*)buffer)[i] = 0;
  50. }
  51. }
  52. /// @copydoc File::write()
  53. void write(const void* buffer, size_t size) { (void)buffer; (void)size; }
  54. /// @copydoc File::copy_to()
  55. /// @note
  56. /// Returns always true
  57. bool copy_to(File& file, size_t size = 0)
  58. {
  59. char zero = 0;
  60. file.write(&zero, size);
  61. return true;
  62. }
  63. /// @copydoc File::flush()
  64. void flush() {};
  65. /// @copydoc File::is_valid()
  66. /// @note
  67. /// Returns always true
  68. bool is_valid() { return true; }
  69. /// @copydoc File::end_of_file()
  70. /// @note
  71. /// Returns always false
  72. bool end_of_file() { return false; }
  73. /// @copydoc File::size()
  74. /// @note
  75. /// Returns always 0xFFFFFFFF
  76. size_t size() { return ~0; }
  77. /// @copydoc File::position()
  78. /// @note
  79. /// Returns always zero
  80. size_t position() { return 0; }
  81. /// @copydoc File::can_read()
  82. /// @note
  83. /// Returns always true
  84. bool can_read() { return true; }
  85. /// @copydoc File::can_write()
  86. /// @note
  87. /// Returns always true
  88. bool can_write() { return true; }
  89. /// @copydoc File::can_seek()
  90. /// @note
  91. /// Returns always true
  92. bool can_seek() { return true; }
  93. };
  94. } // namespace crown