Stroika Library 3.0d18
 
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 * \note See Also StreamReader, as for consumers, this will often work the same as BufferedInputStream, but be
42 * somewhat more performant.
43 *
44 * \par Example Usage
45 * \code
46 * InputStream::Ptr<byte> in = BufferedInputStream::New<byte> (fromStream);
47 * \endcode
48 *
49 * \par Example Usage
50 * \code
51 * CallExpectingBinaryInputStreamPtr (BufferedInputStream::New<byte> (fromStream))
52 * \endcode
53 *
54 * \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>
55 */
56 template <typename ELEMENT_TYPE>
57 Ptr<ELEMENT_TYPE> New (const typename InputStream::Ptr<ELEMENT_TYPE>& realIn, optional<bool> seekable = {});
58 template <typename ELEMENT_TYPE>
59 Ptr<ELEMENT_TYPE> New (Execution::InternallySynchronized internallySynchronized, const typename InputStream::Ptr<ELEMENT_TYPE>& realIn,
60 optional<bool> seekable = {});
61
62 namespace Private_ {
63 template <typename ELEMENT_TYPE>
64 class IRep_ : public InputStream::IRep<ELEMENT_TYPE> {
65 public:
66 };
67 template <typename ELEMENT_TYPE>
68 class Rep_Seekable_FromSeekable_;
69 template <typename ELEMENT_TYPE, size_t INLINE_BUF_SIZE>
70 class Rep_Seekable_FromUnSeekable_;
71 template <typename ELEMENT_TYPE, size_t INLINE_BUF_SIZE>
72 class Rep_UnSeekable_;
73 }
74
75 /**
76 * Ptr is a copyable smart pointer to a BufferedInputStream.
77 */
78 template <typename ELEMENT_TYPE>
79 class Ptr : public InputStream::Ptr<ELEMENT_TYPE> {
80 using inherited = typename InputStream::Ptr<ELEMENT_TYPE>;
81
82 public:
83 /**
84 * \par Example Usage
85 * \code
86 * BufferedInputStream::Ptr<byte> in = BufferedInputStream::New<byte> (FileInputStream::New (fileName));
87 * \endcode
88 */
89 Ptr () = default;
90 Ptr (const Ptr& from) = default;
91 Ptr (Ptr&& from) = default;
92 Ptr (const shared_ptr<Private_::IRep_<ELEMENT_TYPE>>& from);
93
94 public:
95 nonvirtual Ptr& operator= (const Ptr& rhs) = default;
96 nonvirtual Ptr& operator= (Ptr&& rhs) = default;
97
98 private:
99 /**
100 * \brief protected access to underlying stream smart pointer
101 */
102 nonvirtual shared_ptr<Private_::IRep_<ELEMENT_TYPE>> GetSharedRep_ () const;
103 };
104
105}
106
107/*
108 ********************************************************************************
109 ***************************** Implementation Details ***************************
110 ********************************************************************************
111 */
112#include "BufferedInputStream.inl"
113
114#endif /*_Stroika_Foundation_Streams_BufferedInputStream_h_*/
InputStream<>::Ptr is Smart pointer (with abstract Rep) class defining the interface to reading from ...