Stroika Library 3.0d16
 
Loading...
Searching...
No Matches
ExternallyOwnedSpanInputStream.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_ExternallyOwnedSpanInputStream_h_
5#define _Stroika_Foundation_Streams_ExternallyOwnedSpanInputStream_h_ 1
6
7#include "Stroika/Foundation/StroikaPreComp.h"
8
9#include <mutex>
10#include <vector>
11
12#include "Stroika/Foundation/Common/Common.h"
15#include "Stroika/Foundation/Memory/Common.h"
16
17#include "InputStream.h"
18
19/**
20 * \file
21 *
22 * \note Code-Status: <a href="Code-Status.md#Beta">Beta</a>
23 *
24 */
25
26namespace Stroika::Foundation::Streams::ExternallyOwnedSpanInputStream {
27
28 using InputStream::Ptr;
29
30 /**
31 * \brief ExternallyOwnedSpanInputStream takes a (memory contiguous) sequence of ELEMENT_TYPE objects and exposes it as a InputStream<ELEMENT_TYPE>
32 *
33 * ExternallyOwnedSpanInputStream is a subtype of InputStream<ELEMENT_TYPE> but the
34 * creator must guarantee, so long as the memory pointed to in the argument has a
35 * o lifetime > lifetime of the ExternallyOwnedSpanInputStream object,
36 * o and data never changes value
37 *
38 * \note NB: Be VERY careful about using this. It can be assigned to a InputStream::Ptr<ELEMENT_TYPE>, and
39 * if its constructor argument is destroyed, it will contain invalid memory references.
40 * Use VERY CAREFULLY. If in doubt, use @MemoryStream<ELEMENT_TYPE> - which is MUCH safer (because it copies its CTOR-argument data)
41 *
42 * ExternallyOwnedSpanInputStream is Seekable.
43 *
44 * @see MemoryStream
45 *
46 * \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>
47 *
48 * \par Example Usage
49 * \code
50 * InputStream::Ptr<byte> in = ExternallyOwnedSpanInputStream::New<byte> (span{buf});
51 * \endcode
52 *
53 * \par Example Usage
54 * \code
55 * CallExpectingBinaryInputStreamPtr (ExternallyOwnedSpanInputStream::New<byte> (span{buf})
56 * \endcode
57 *
58 * \par Example Usage
59 * \code
60 * const char kJSONExample_[] = "{"...;
61 * // JSON Reader takes InputStream::Ptr<byte> argument
62 * auto reader = DataExchange::Variant::JSON::Reader{};
63 * VariantValue v1 = reader.Read (Streams::ExternallyOwnedSpanInputStream::New (span{kJSONExample_}));
64 * VariantValue vSameAs = reader.Read (Streams::ExternallyOwnedSpanInputStream::New (Memory::SpanBytesCast<span<const byte>>(span{kJSONExample_})));
65 * \endcode
66 */
67 template <typename ELEMENT_TYPE, Memory::ISpanBytesCastable<span<const ELEMENT_TYPE>> FROM_SPAN>
68 Ptr<ELEMENT_TYPE> New (FROM_SPAN s);
69 template <typename ELEMENT_TYPE, Memory::ISpanBytesCastable<span<const ELEMENT_TYPE>> FROM_SPAN>
70 Ptr<ELEMENT_TYPE> New (Execution::InternallySynchronized internallySynchronized, FROM_SPAN s);
71
72}
73
74/*
75 ********************************************************************************
76 ***************************** Implementation Details ***************************
77 ********************************************************************************
78 */
79#include "ExternallyOwnedSpanInputStream.inl"
80
81#endif /*_Stroika_Foundation_Streams_ExternallyOwnedSpanInputStream_h_*/