Stroika Library 3.0d16
 
Loading...
Searching...
No Matches
BufferedOutputStream.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_BufferedOutputStream_h_
5#define _Stroika_Foundation_Streams_BufferedOutputStream_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 Properly handle seekability based on underlying stream seekability, and/or param/flag on construction (we can always make it seekable by buffering).
19 *
20 * BufferedOutputStream NOW must properly support SEEKABLE! if arg
21 * is seekable, we must override seek methods, and forward them, and adjust buffer as appropriate.
22 */
23
24namespace Stroika::Foundation::Streams::BufferedOutputStream {
25
26 template <typename ELEMENT_TYPE>
27 class Ptr;
28
29 /**
30 * A BufferedOutputStream wraps an argument stream
31 * (which must have lifetime > this BufferedOutputStream) and will buffer up writes to it.
32 *
33 * \note If you fail to Flush() this object before it is destroyed, exceptions in flushing
34 * the data may be suppressed.
35 * Best to call @Close ()
36 *
37 * \note BufferedOutputStream aggregates its owned sub-stream, so that a Close () on BufferedOutputStream
38 * will Close that sub-stream.
39 *
40 * \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>
41 *
42 * \par Example Usage
43 * \code
44 * OutputStream::Ptr<byte> out = BufferedOutputStream::New<byte> (FileOutputStream (fileName, flushFlag));
45 * \endcode
46 */
47 template <typename ELEMENT_TYPE>
48 Ptr<ELEMENT_TYPE> New (const typename OutputStream::Ptr<ELEMENT_TYPE>& realOut, const optional<size_t>& bufferSize = nullopt);
49 template <typename ELEMENT_TYPE>
50 Ptr<ELEMENT_TYPE> New (Execution::InternallySynchronized internallySynchronized,
51 const typename OutputStream::Ptr<ELEMENT_TYPE>& realOut, const optional<size_t>& bufferSize = nullopt);
52
53 namespace Private_ {
54 template <typename ELEMENT_TYPE>
55 class IRep_ : public OutputStream::IRep<ELEMENT_TYPE> {
56 public:
57 virtual size_t GetBufferSize () const = 0;
58 virtual void SetBufferSize (size_t bufSize) = 0;
59 };
60 template <typename ELEMENT_TYPE, size_t INLINE_BUF_SIZE = 16 * 1024>
61 class Rep_;
62 }
63
64 /**
65 * Ptr is a copyable smart pointer to a BufferedOutputStream.
66 */
67 template <typename ELEMENT_TYPE>
68 class Ptr : public OutputStream::Ptr<ELEMENT_TYPE> {
69 using inherited = typename OutputStream::Ptr<ELEMENT_TYPE>;
70
71 public:
72 /**
73 * \par Example Usage
74 * \code
75 * BufferedOutputStream::Ptr<byte> out = BufferedOutputStream::New<byte> (FileOutputStream (fileName, flushFlag));
76 * \endcode
77 */
78 Ptr () = default;
79 Ptr (const Ptr& from) = default;
80 Ptr (Ptr&& from) = default;
81 Ptr (const shared_ptr<Private_::IRep_<ELEMENT_TYPE>>& from);
82
83 public:
84 nonvirtual Ptr& operator= (const Ptr& rhs) = default;
85 nonvirtual Ptr& operator= (Ptr&& rhs) = default;
86
87 public:
88 nonvirtual size_t GetBufferSize () const;
89
90 public:
91 nonvirtual void SetBufferSize (size_t bufSize);
92
93 public:
94 /**
95 * Throws away all data about to be written (buffered). Once this is called,
96 * the effect of future Flush () calls is undefined. This can be used when the stream
97 * wraps an underlying object like a socket, and you don't want to waste effort
98 * talking to it, but its harmless todo so.
99 *
100 * One a stream is aborted, its undefined what other operations will do (they wont crash
101 * but they may or may not write).
102 */
103 nonvirtual void Abort ();
104
105 private:
106 /**
107 * \brief protected access to underlying stream smart pointer
108 */
109 nonvirtual shared_ptr<Private_::IRep_<ELEMENT_TYPE>> GetSharedRep_ () const;
110 };
111
112}
113
114/*
115 ********************************************************************************
116 ***************************** Implementation Details ***************************
117 ********************************************************************************
118 */
119#include "BufferedOutputStream.inl"
120
121#endif /*_Stroika_Foundation_Streams_BufferedOutputStream_h_*/
OutputStream<>::Ptr is Smart pointer to a stream-based sink of data.