Stroika Library 3.0d16
 
Loading...
Searching...
No Matches
FileInputStream.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_IO_FileSystem_FileInputStream_h_
5#define _Stroika_Foundation_IO_FileSystem_FileInputStream_h_ 1
6
7#include "Stroika/Foundation/StroikaPreComp.h"
8
9#include <filesystem>
10
13#include "Stroika/Foundation/IO/FileSystem/Common.h"
16
17/**
18 * \file
19 *
20 * \note Code-Status: <a href="Code-Status.md#Beta">Beta</a>
21 *
22 * TODO:
23 * @todo Consider either adding directly here (optionally) the code from MemoryMappedFileReader (vintage 2013-09-02
24 * or earlier). CreateFileMapping etc. Worthless unless that happens to be more efficient, and I have
25 * no good reason to believe it will be. But maybe? Maybe cuz seeks more efficient (++ not syscall). Same
26 * for reads - if caller does lots of little reads.
27 */
28
29namespace Stroika::Foundation::IO::FileSystem::FileInputStream {
30
31 using Characters::String;
32 using namespace FileStream;
33
34 using namespace Streams;
36
37 /**
38 *
39 * \note We considered having a GetFD () method to retrieve the file descriptor, but that opened up too many
40 * possibilities for bugs (like changing the blocking nature of the IO). If you wish - you can always
41 * open the file descriptor yourself, track it yourself, and do what you will to it and pass it in,
42 * but then the results are 'on you.
43 */
44
45 /**
46 */
47 enum class BufferFlag {
48 eBuffered,
49 eUnbuffered,
50
51 Stroika_Define_Enum_Bounds (eBuffered, eUnbuffered)
52 };
53
54 /**
55 */
56 constexpr BufferFlag eBuffered = BufferFlag::eBuffered;
57
58 /**
59 */
60 constexpr BufferFlag eUnbuffered = BufferFlag::eUnbuffered;
61
62 /**
63 * \note FileInputStream::kBufferFlag_DEFAULT defaults to eUnbuffered, because StreamReader
64 * generally provides a better buffering strategy.
65 */
66 constexpr BufferFlag kBufferFlag_DEFAULT = eUnbuffered;
67
68 /**
69 * \note - prior to v2.1d27, this defaulted to unseekable, but for files, it makes way more sense to default to seekable, since
70 * doing so typically costs nothing, and is pretty commonly useful.
71 */
72 constexpr SeekableFlag kSeekableFlag_DEFAULT = SeekableFlag::eSeekable;
73
74 /**
75 * The static New method is like a constructor, but it constructs a smart pointer of some appropriate subtype defined by its parameters.
76 *
77 * The New overload with FileDescriptorType does an 'attach' - taking ownership (and thus later closing) the argument file descriptor.
78 *
79 * \pre fd is a valid file descriptor (for that overload)
80 *
81 * \par Example Usage
82 * \code
83 * static const filesystem::path kProcCPUInfoFileName_{"/proc/cpuinfo"sv};
84 * Ptr stream = FileInputStream::New (kProcCPUInfoFileName_, FileInputStream::eNotSeekable);
85 * \endcode
86 *
87 * \par Example Usage
88 * \code
89 * Ptr stdinStream = FileInputStream::New (0, AdoptFDPolicy::eDisconnectOnDestruction);
90 * \endcode
91 *
92 * \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>
93 */
94 Ptr New (const filesystem::path& fileName, SeekableFlag seekable = kSeekableFlag_DEFAULT);
95 Ptr New (FileDescriptorType fd, AdoptFDPolicy adoptFDPolicy, SeekableFlag seekable = kSeekableFlag_DEFAULT);
96 Ptr New (Execution::InternallySynchronized internallySynchronized, const filesystem::path& fileName, SeekableFlag seekable = kSeekableFlag_DEFAULT);
97 Ptr New (Execution::InternallySynchronized internallySynchronized, FileDescriptorType fd, AdoptFDPolicy adoptFDPolicy,
98 SeekableFlag seekable = kSeekableFlag_DEFAULT);
99 Ptr New (const filesystem::path& fileName, SeekableFlag seekable, BufferFlag bufferFlag);
100 Ptr New (const filesystem::path& fileName, BufferFlag bufferFlag);
101 Ptr New (FileDescriptorType fd, AdoptFDPolicy adoptFDPolicy, SeekableFlag seekable, BufferFlag bufferFlag);
102
103}
104
105/*
106 ********************************************************************************
107 ***************************** Implementation Details ***************************
108 ********************************************************************************
109 */
110#include "FileInputStream.inl"
111
112#endif /*_Stroika_Foundation_IO_FileSystem_FileInputStream_h_*/
#define Stroika_Define_Enum_Bounds(FIRST_ITEM, LAST_ITEM)
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