Stroika Library 3.0d20
 
Loading...
Searching...
No Matches
BufferedInputStream.h
Go to the documentation of this file.
1/*
2 * Copyright(c) Sophist Solutions, Inc. 1990-2025. All rights reserved
3 */
4#ifndef _Stroika_Foundation_Streams_BufferedInputStream_h_
5#define _Stroika_Foundation_Streams_BufferedInputStream_h_ 1
6
7#include "Stroika/Foundation/StroikaPreComp.h"
8
11
12/**
13 * \file
14 *
15 * \note Code-Status: <a href="Code-Status.md#Beta">Beta</a>
16 *
17 * TODO:
18 * @todo Seekable! Must think out if this should mixin Seekable or not. I THINK it must be.
19 * I THINK if one SEEKS this, but the underlying
20 * class doesn't support seeking, we MAY need to either BUFFER MORE, or throw not supported.
21 */
22
23namespace Stroika::Foundation::Streams::BufferedInputStream {
24
25 template <typename ELEMENT_TYPE>
26 class Ptr;
27
28 /**
29 * @brief BufferedInputStream is an InputStream::Ptr<ELEMENT_TYPE> which provides buffered access.
30 * This is useful if calls to the underling stream source can be expensive. This class
31 * loads chunks of the stream into memory, and reduces calls to the underlying stream.
32 *
33 * \note if seekable true, the resulting stream is seekable. If seekable is false, the resulting
34 * stream is not seekable. If seekable is unspecified (nullopt), the resulting stream is seekable
35 * iff the source stream is seekable.
36 *
37 * Specifying true (seekable) when the source stream is not seekable is allowed, but requires enuf
38 * memory to buffer the ENTIRE contents of the stream (other cases just buffer a bit so require more
39 * modest amounts of memory).
40 *
41 * This can be used to turn ANY non-seekable stream into a seekable one (but NOTE - that means its a
42 * different stream, and the underlying original stream will be left at an arbitrary seek point).
43 *
44 * \note See Also StreamReader, as for consumers, this will often work the same as BufferedInputStream, but be
45 * somewhat more performant.
46 *
47 * \par Example Usage
48 * \code
49 * InputStream::Ptr<byte> in = BufferedInputStream::New<byte> (fromStream);
50 * \endcode
51 *
52 * \par Example Usage
53 * \code
54 * CallExpectingBinaryInputStreamPtr (BufferedInputStream::New<byte> (fromStream))
55 * \endcode
56 *
57 * \note \em Thread-Safety <a href="Thread-Safety.md#C++-Standard-Thread-Safety-For-Envelope-Plus-Must-Externally-Synchronize-Letter">C++-Standard-Thread-Safety-For-Envelope-Plus-Must-Externally-Synchronize-Letter</a>
58 */
59
60 template <typename ELEMENT_TYPE>
61 Ptr<ELEMENT_TYPE> New (const typename InputStream::Ptr<ELEMENT_TYPE>& realIn, optional<Streams::SeekableFlag> seekable = {});
62 template <typename ELEMENT_TYPE>
63 Ptr<ELEMENT_TYPE> New (Execution::InternallySynchronized internallySynchronized, const typename InputStream::Ptr<ELEMENT_TYPE>& realIn,
64 optional<Streams::SeekableFlag> seekable = {});
65
66 namespace Private_ {
67 template <typename ELEMENT_TYPE>
68 class IRep_ : public InputStream::IRep<ELEMENT_TYPE> {
69 public:
70 };
71 template <typename ELEMENT_TYPE>
72 class Rep_Seekable_FromSeekable_;
73 template <typename ELEMENT_TYPE, size_t INLINE_BUF_SIZE>
74 class Rep_Seekable_FromUnSeekable_;
75 template <typename ELEMENT_TYPE, size_t INLINE_BUF_SIZE>
76 class Rep_UnSeekable_;
77 }
78
79 /**
80 * Ptr is a copyable smart pointer to a BufferedInputStream.
81 */
82 template <typename ELEMENT_TYPE>
83 class Ptr : public InputStream::Ptr<ELEMENT_TYPE> {
84 using inherited = typename InputStream::Ptr<ELEMENT_TYPE>;
85
86 public:
87 /**
88 * \par Example Usage
89 * \code
90 * BufferedInputStream::Ptr<byte> in = BufferedInputStream::New<byte> (FileInputStream::New (fileName));
91 * \endcode
92 */
93 Ptr () = default;
94 Ptr (const Ptr& from) = default;
95 Ptr (Ptr&& from) = default;
96 Ptr (const shared_ptr<Private_::IRep_<ELEMENT_TYPE>>& from);
97
98 public:
99 nonvirtual Ptr& operator= (const Ptr& rhs) = default;
100 nonvirtual Ptr& operator= (Ptr&& rhs) = default;
101
102 private:
103 /**
104 * \brief protected access to underlying stream smart pointer
105 */
106 nonvirtual shared_ptr<Private_::IRep_<ELEMENT_TYPE>> GetSharedRep_ () const;
107 };
108
109}
110
111/*
112 ********************************************************************************
113 ***************************** Implementation Details ***************************
114 ********************************************************************************
115 */
116#include "BufferedInputStream.inl"
117
118#endif /*_Stroika_Foundation_Streams_BufferedInputStream_h_*/
InputStream<>::Ptr is Smart pointer (with abstract Rep) class defining the interface to reading from ...
A Streams::Ptr<ELEMENT_TYPE> is a smart-pointer to a stream of elements of type T.
Definition Stream.h:170