Stream.h 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
  1. /*
  2. * This source file is part of libRocket, the HTML/CSS Interface Middleware
  3. *
  4. * For the latest information, see http://www.librocket.com
  5. *
  6. * Copyright (c) 2008-2010 CodePoint Ltd, Shift Technology Ltd
  7. *
  8. * Permission is hereby granted, free of charge, to any person obtaining a copy
  9. * of this software and associated documentation files (the "Software"), to deal
  10. * in the Software without restriction, including without limitation the rights
  11. * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  12. * copies of the Software, and to permit persons to whom the Software is
  13. * furnished to do so, subject to the following conditions:
  14. *
  15. * The above copyright notice and this permission notice shall be included in
  16. * all copies or substantial portions of the Software.
  17. *
  18. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  19. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  20. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  21. * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  22. * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  23. * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  24. * THE SOFTWARE.
  25. *
  26. */
  27. #ifndef ROCKETCORESTREAM_H
  28. #define ROCKETCORESTREAM_H
  29. #include "Header.h"
  30. #include "ReferenceCountable.h"
  31. #include "Types.h"
  32. #include "URL.h"
  33. #include <list>
  34. namespace Rocket {
  35. namespace Core {
  36. class StreamListener;
  37. /**
  38. Abstract class for a media-independent byte stream.
  39. @author Lloyd Weehuizen
  40. */
  41. class ROCKETCORE_API Stream : public ReferenceCountable
  42. {
  43. public:
  44. // Stream modes.
  45. enum StreamMode
  46. {
  47. MODE_WRITE = 1 << 0,
  48. MODE_APPEND = 1 << 1,
  49. MODE_READ = 1 << 2,
  50. MODE_ASYNC = 1 << 3,
  51. MODE_MASK = MODE_WRITE | MODE_APPEND | MODE_READ
  52. };
  53. Stream();
  54. virtual ~Stream();
  55. /// Closes the stream.
  56. virtual void Close();
  57. /// Returns the mode the stream was opened in.
  58. int GetStreamMode() const;
  59. /// Obtain the source url of this stream (if available)
  60. const URL& GetSourceURL() const;
  61. /// Are we at the end of the stream
  62. virtual bool IsEOS() const;
  63. /// Returns the size of this stream (in bytes).
  64. virtual size_t Length() const = 0;
  65. /// Returns the position of the stream pointer (in bytes).
  66. virtual size_t Tell() const = 0;
  67. /// Sets the stream position (in bytes).
  68. virtual bool Seek(long offset, int origin) const = 0;
  69. /// Read from the stream.
  70. virtual size_t Read(void* buffer, size_t bytes) const = 0;
  71. /// Read from the stream into another stream.
  72. virtual size_t Read(Stream* stream, size_t bytes) const;
  73. /// Read from the stream and append to the string buffer
  74. virtual size_t Read(String& buffer, size_t bytes) const;
  75. /// Read from the stream, without increasing the stream offset.
  76. virtual size_t Peek(void* buffer, size_t bytes) const;
  77. /// Write to the stream at the current position.
  78. virtual size_t Write(const void* buffer, size_t bytes) = 0;
  79. /// Write to this stream from another stream.
  80. virtual size_t Write(const Stream* stream, size_t bytes);
  81. /// Write a character array to the stream.
  82. virtual size_t Write(const char* string);
  83. /// Write a string to the stream
  84. virtual size_t Write(const String& string);
  85. /// Truncate the stream to the specified length.
  86. virtual size_t Truncate(size_t bytes) = 0;
  87. /// Push onto the front of the stream.
  88. virtual size_t PushFront(const void* buffer, size_t bytes);
  89. /// Push onto the back of the stream.
  90. virtual size_t PushBack(const void* buffer, size_t bytes);
  91. /// Pop from the front of the stream.
  92. virtual size_t PopFront(size_t bytes);
  93. /// Pop from the back of the stream.
  94. virtual size_t PopBack(size_t bytes);
  95. /// Returns true if the stream is ready for reading, false otherwise.
  96. /// This is usually only implemented on streams supporting asynchronous
  97. /// operations.
  98. virtual bool IsReadReady() = 0;
  99. /// Returns true if the stream is ready for writing, false otherwise.
  100. /// This is usually only implemented on streams supporting asynchronous
  101. /// operations.
  102. virtual bool IsWriteReady() = 0;
  103. protected:
  104. /// Sets the mode on the stream; should be called by a stream when it is opened.
  105. void SetStreamDetails(const URL& url, int stream_mode);
  106. /// Deletes the stream.
  107. virtual void OnReferenceDeactivate();
  108. private:
  109. URL url;
  110. int stream_mode;
  111. };
  112. }
  113. }
  114. #endif