tStream.h 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373
  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 _TSTREAM_H_
  23. #define _TSTREAM_H_
  24. #ifndef _PLATFORM_H_
  25. #include "platform/platform.h"
  26. #endif
  27. #ifndef _TYPETRAITS_H_
  28. #include "platform/typetraits.h"
  29. #endif
  30. //TODO: error handling
  31. /// @file
  32. /// Definitions for lightweight componentized streaming.
  33. ///
  34. /// This file is an assembly of lightweight classes/interfaces that
  35. /// describe various aspects of streaming classes. The advantage
  36. /// over using Torque's Stream class is that very little requirements
  37. /// are placed on implementations, that specific abilities can be
  38. /// mixed and matched very selectively, and that complex stream processing
  39. /// chains can be hidden behind very simple stream interfaces.
  40. /// Status of an asynchronous I/O operation.
  41. enum EAsyncIOStatus
  42. {
  43. ASYNC_IO_Pending, ///< I/O is still in queue or being processed.
  44. ASYNC_IO_Complete, ///< I/O has completed successfully.
  45. ASYNC_IO_Error ///< I/O has aborted with an error.
  46. };
  47. //-----------------------------------------------------------------------------
  48. // Several component interfaces.
  49. //-----------------------------------------------------------------------------
  50. /// Interface for streams with an explicit position property.
  51. template< typename P = U32 >
  52. class IPositionable
  53. {
  54. public:
  55. typedef void Parent;
  56. /// The type used to indicate positions.
  57. typedef P PositionType;
  58. /// @return the current position.
  59. virtual PositionType getPosition() const = 0;
  60. /// Set the current position to be "pos".
  61. /// @param pos The new position.
  62. virtual void setPosition( PositionType pos ) = 0;
  63. };
  64. /// Interface for structures that allow their state to be reset.
  65. class IResettable
  66. {
  67. public:
  68. typedef void Parent;
  69. /// Reset to the initial state.
  70. virtual void reset() = 0;
  71. };
  72. /// Interface for structures of finite size.
  73. template< typename T = U32 >
  74. class ISizeable
  75. {
  76. public:
  77. typedef void Parent;
  78. /// The type used to indicate the structure's size.
  79. typedef T SizeType;
  80. /// @return the size of the structure in number of elements.
  81. virtual SizeType getSize() const = 0;
  82. };
  83. /// Interface for structures that represent processes.
  84. class IProcess
  85. {
  86. public:
  87. typedef void Parent;
  88. /// Start the process.
  89. virtual void start() = 0;
  90. /// Stop the process.
  91. virtual void stop() = 0;
  92. /// Pause the process.
  93. virtual void pause() = 0;
  94. };
  95. /// Interface for objects that need continuous explicit updates from
  96. /// an outside source.
  97. class IPolled
  98. {
  99. public:
  100. typedef void Parent;
  101. /// Update the object state.
  102. virtual bool update() = 0;
  103. };
  104. //-----------------------------------------------------------------------------
  105. // IInputStream.
  106. //-----------------------------------------------------------------------------
  107. /// An input stream delivers a sequence of elements of type "T".
  108. ///
  109. /// @note This stream has an inherent position property and is thus not
  110. /// safe for concurrent access.
  111. template< typename T >
  112. class IInputStream
  113. {
  114. public:
  115. typedef void Parent;
  116. /// The element type of this input stream.
  117. typedef T ElementType;
  118. /// Read the next "num" elements into "buffer".
  119. ///
  120. /// @param buffer The buffer into which the elements are stored.
  121. /// @param num Number of elements to read.
  122. /// @return the number of elements actually read; this may be less than
  123. /// "num" or even zero if no elements are available or reading failed.
  124. virtual U32 read( ElementType* buffer, U32 num ) = 0;
  125. };
  126. /// An input stream over elements of type "T" that reads from
  127. /// user-specified explicit offsets.
  128. template< typename T, typename Offset = U32 >
  129. class IOffsetInputStream
  130. {
  131. public:
  132. typedef void Parent;
  133. typedef Offset OffsetType;
  134. typedef T ElementType;
  135. /// Read the next "num" elements at "offset" into "buffer".
  136. ///
  137. /// @param offset The offset in the stream from which to read.
  138. /// @param buffer The buffer into which the elements are stored.
  139. /// @param num Number of elements to read.
  140. /// @return the number of elements actually read; this may be less than
  141. /// "num" or even zero if no elements are available or reading failed.
  142. virtual U32 readAt( OffsetType offset, T* buffer, U32 num ) = 0;
  143. };
  144. /// An input stream over elements of type "T" that works in
  145. /// the background.
  146. template< typename T, typename Offset = U32 >
  147. class IAsyncInputStream
  148. {
  149. public:
  150. typedef void Parent;
  151. typedef Offset OffsetType;
  152. typedef T ElementType;
  153. /// Queue a read of "num" elements at "offset" into "buffer".
  154. ///
  155. /// @param offset The offset in the stream from which to read.
  156. /// @param buffer The buffer into which the elements are stored.
  157. /// @param num Number of elements to read.
  158. /// @return a handle for the asynchronous read operation or NULL if the
  159. /// operation could not be queued.
  160. virtual void* issueReadAt( OffsetType offset, T* buffer, U32 num ) = 0;
  161. /// Try moving the given asynchronous read operation to ASYNC_IO_Complete.
  162. ///
  163. /// @note This method invalidates the given handle.
  164. ///
  165. /// @param handle Handle returned by "issueReadAt".
  166. /// @param outNumRead Reference that receives the number of bytes actually read in the
  167. /// operation.
  168. /// @param wait If true, the method waits until the given operation either fails or
  169. /// completes successfully before returning.
  170. /// @return the final operation status.
  171. virtual EAsyncIOStatus tryCompleteReadAt( void* handle, U32& outNumRead, bool wait = false ) = 0;
  172. /// Cancel the given asynchronous read operation.
  173. ///
  174. /// @note This method invalidates the given handle.
  175. ///
  176. /// @param handle Handle returned by "issueReadAt".
  177. virtual void cancelReadAt( void* handle ) = 0;
  178. };
  179. //-----------------------------------------------------------------------------
  180. // IOutputStream.
  181. //-----------------------------------------------------------------------------
  182. /// An output stream that writes elements of type "T".
  183. ///
  184. /// @note This stream has an inherent position property and is thus not
  185. /// safe for concurrent access.
  186. template< typename T >
  187. class IOutputStream
  188. {
  189. public:
  190. typedef void Parent;
  191. /// The element type of this input stream.
  192. typedef T ElementType;
  193. /// Write "num" elements from "buffer" to the stream at its
  194. /// current position.
  195. ///
  196. /// @param buffer The buffer from which to read elements.
  197. /// @param num Number of elements to write.
  198. virtual void write( const ElementType* buffer, U32 num ) = 0;
  199. };
  200. /// An output stream that writes elements of type "T" to a
  201. /// user-specified explicit offset.
  202. template< typename T, typename Offset = U32 >
  203. class IOffsetOutputStream
  204. {
  205. public:
  206. typedef void Parent;
  207. typedef Offset OffsetType;
  208. typedef T ElementType;
  209. /// Write "num" elements from "buffer" to the stream at "offset".
  210. ///
  211. /// @param offset The offset in the stream at which to write the elements.
  212. /// @param buffer The buffer from which to read elements.
  213. /// @param num Number of elements to write.
  214. virtual void writeAt( OffsetType offset, const ElementType* buffer, U32 num ) = 0;
  215. };
  216. /// An output stream that writes elements of type "T" in the background.
  217. template< typename T, typename Offset = U32 >
  218. class IAsyncOutputStream
  219. {
  220. public:
  221. typedef void Parent;
  222. typedef Offset OffsetType;
  223. typedef T ElementType;
  224. /// Queue a write operation of "num" elements from "buffer" to stream position
  225. /// "offset".
  226. ///
  227. /// @param offset The offset in the stream at which to write the elements.
  228. /// @param buffer The buffer from which to read elements.
  229. /// @param num The number of elements to write.
  230. /// @return a handle to the asynchronous write operatior or NULL if the operation
  231. /// could not be queued.
  232. virtual void* issueWriteAt( OffsetType offset, const ElementType* buffer, U32 num ) = 0;
  233. /// Try moving the given asynchronous write operation to ASYNC_IO_Complete.
  234. ///
  235. /// @note This method invalidates the given handle.
  236. ///
  237. /// @param handle Handle returned by "issueWriteAt".
  238. /// @param wait If true, the method waits until the given operation either fails or
  239. /// completes successfully before returning.
  240. /// @return the final operation status.
  241. virtual EAsyncIOStatus tryCompleteWriteAt( void* handle, bool wait = false ) = 0;
  242. /// Cancel the given asynchronous write operation.
  243. ///
  244. /// @note This method invalidates the given handle.
  245. ///
  246. /// @param handle Handle return by "issueWriteAt".
  247. virtual void cancelWriteAt( void* handle ) = 0;
  248. };
  249. //-----------------------------------------------------------------------------
  250. // IInputStreamFilter.
  251. //-----------------------------------------------------------------------------
  252. /// An input stream filter takes an input stream "Stream" and processes it
  253. /// into an input stream over type "To".
  254. template< typename To, typename Stream >
  255. class IInputStreamFilter : public IInputStream< To >
  256. {
  257. public:
  258. typedef IInputStream< To > Parent;
  259. ///
  260. typedef typename TypeTraits< Stream >::BaseType SourceStreamType;
  261. /// The element type of the source stream.
  262. typedef typename SourceStreamType::ElementType SourceElementType;
  263. /// Construct a filter reading elements from "stream".
  264. IInputStreamFilter( const Stream& stream )
  265. : mSourceStream( stream ) {}
  266. /// Return the stream from which this filter is reading its
  267. /// source elements.
  268. const Stream& getSourceStream() const { return mSourceStream; }
  269. Stream& getSourceStream() { return mSourceStream; }
  270. private:
  271. Stream mSourceStream;
  272. };
  273. //-----------------------------------------------------------------------------
  274. // IOutputStreamFilter.
  275. //-----------------------------------------------------------------------------
  276. /// An output stream filter takes an output stream "Stream" and processes it
  277. /// into an output stream over type "To".
  278. template< typename To, class Stream >
  279. class IOutputStreamFilter : public IOutputStream< To >
  280. {
  281. public:
  282. typedef IOutputStream< To > Parent;
  283. ///
  284. typedef typename TypeTraits< Stream >::BaseType TargetStreamType;
  285. /// The element type of the target stream.
  286. typedef typename TargetStreamType::ElementType TargetElementType;
  287. /// Construct a filter writing elements to "stream".
  288. IOutputStreamFilter( const Stream& stream )
  289. : mTargetStream( stream ) {}
  290. /// Return the stream to which this filter is writing its
  291. /// elements.
  292. const Stream& getTargetStream() const { return mTargetStream; }
  293. Stream& getTargetStream() { return mTargetStream; }
  294. private:
  295. Stream mTargetStream;
  296. };
  297. #endif // _TSTREAM_H_