Stroika Library 3.0d16
 
Loading...
Searching...
No Matches
InputStream.cpp
1/*
2 * Copyright(c) Sophist Solutions, Inc. 1990-2025. All rights reserved
3 */
4#include "Stroika/Foundation/StroikaPreComp.h"
5
6#include "Stroika/Foundation/Containers/Support/ReserveTweaks.h"
10
11#include "InputStream.h"
12
13using std::byte;
14
15using namespace Stroika::Foundation;
16using namespace Stroika::Foundation::Streams;
17
20using Memory::BLOB;
21
22// Comment this in to turn on aggressive noisy DbgTrace in this module
23//#define USE_NOISY_TRACE_IN_THIS_MODULE_ 1
24
25/*
26 ********************************************************************************
27 ******************** Streams::InputStream::Ptr<ELEMENT_TYPE> *******************
28 ********************************************************************************
29 */
30DISABLE_COMPILER_MSC_WARNING_START (6262) // stack usage OK
31template <>
32Memory::BLOB InputStream::Ptr<byte>::ReadAll (size_t upTo) const
33{
34#if USE_NOISY_TRACE_IN_THIS_MODULE_
35 Debug::TraceContextBumper ctx{L"InputStream::Ptr<byte>::ReadAll", L"upTo: %llu", static_cast<unsigned long long> (upTo)};
36#endif
37 Require (upTo >= 1);
39 for (size_t nEltsLeft = upTo; nEltsLeft != 0;) {
40#if USE_NOISY_TRACE_IN_THIS_MODULE_
41 DbgTrace ("nEltsLeft=%llu", static_cast<unsigned long long> (nEltsLeft));
42#endif
43 byte buf[64 * 1024]; // intentionally uninitialized
44 byte* s = std::begin (buf);
45 byte* e = std::end (buf);
46 if (nEltsLeft < Memory::NEltsOf (buf)) {
47 e = s + nEltsLeft;
48 }
49 Assert (s < e);
50 size_t n = ReadBlocking (span{s, e}).size ();
51 Assert (0 <= n and n <= nEltsLeft);
52 Assert (0 <= n and n <= Memory::NEltsOf (buf));
53 if (n == 0) {
54 break;
55 }
56 else {
57 // @todo???
58 // could also maintain linked list - std::list<> - of BLOBs, and then construct BLOB from
59 // list of BLOBs - that would be quite efficient too - maybe more
60 Assert (n <= nEltsLeft);
61 nEltsLeft -= n;
62 r.push_back (span{s, n});
63 }
64 }
65#if USE_NOISY_TRACE_IN_THIS_MODULE_
66 DbgTrace ("returning {} bytes"_f, r.size ());
67#endif
68 return BLOB{r};
69}
70DISABLE_COMPILER_MSC_WARNING_END (6262)
#define DbgTrace
Definition Trace.h:309
String is like std::u32string, except it is much easier to use, often much more space efficient,...
Definition String.h:201
Logically halfway between std::array and std::vector; Smart 'direct memory array' - which when needed...
nonvirtual void push_back(Common::ArgByValueType< T > e)
nonvirtual size_t size() const noexcept
A Streams::Ptr<ELEMENT_TYPE> is a smart-pointer to a stream of elements of type T.
Definition Stream.h:170