Stroika Library 3.0d16
 
Loading...
Searching...
No Matches
FileOutputStream.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_FileOutputStream_h_
5#define _Stroika_Foundation_IO_FileSystem_FileOutputStream_h_ 1
6
7#include "Stroika/Foundation/StroikaPreComp.h"
8
9#include <filesystem>
10
15
16/**
17 * \file
18 *
19 * TODO:
20 * @todo Add optional file sharing parameters to stream readers, and maybe file-descriptor CTOR?
21 */
22
23namespace Stroika::Foundation::IO::FileSystem ::FileOutputStream {
24
25 using Characters::String;
26
27 using namespace FileStream;
28 using namespace Streams;
29
31
32 /**
33 * This flag is used to configure if BinaryOutputStream::Flush will invoke the OS fsync() function
34 * to force data to disk (by default Flush just forces the data out of this object to the next object,
35 * for files, the operating system).
36 *
37 * \note Design note:
38 * It was explicitly chosen to not use enum class here for brevity sake, since this names are already well scoped.
39 */
40 enum class FlushFlag {
41 eToOperatingSystem,
42 eToDisk,
43
44 eDEFAULT = eToOperatingSystem,
45
46 Stroika_Define_Enum_Bounds (eToOperatingSystem, eToDisk)
47 };
48 constexpr FlushFlag eToOperatingSystem = FlushFlag::eToOperatingSystem;
49 constexpr FlushFlag eToDisk = FlushFlag::eToDisk;
50
51 /**
52 * Default AppendFlag is eStartFromStart (truncation), not eAppend
53 */
54 enum class AppendFlag {
55 eStartFromStart, // aka truncate
56 eAppend,
57
58 eDEFAULT = eStartFromStart,
59
60 Stroika_Define_Enum_Bounds (eStartFromStart, eAppend)
61 };
62 constexpr AppendFlag eStartFromStart = AppendFlag::eStartFromStart;
63 constexpr AppendFlag eAppend = AppendFlag::eAppend;
64
65 enum class BufferFlag {
66 eBuffered,
67 eUnbuffered,
68
69 Stroika_Define_Enum_Bounds (eBuffered, eUnbuffered)
70 };
71 constexpr BufferFlag eBuffered = BufferFlag::eBuffered;
72 constexpr BufferFlag eUnbuffered = BufferFlag::eUnbuffered;
73
74 /**
75 */
76 constexpr BufferFlag kBufferFlag_DEFAULT = BufferFlag::eBuffered;
77
78 using SeekableFlag = Streams::SeekableFlag;
79 constexpr SeekableFlag eSeekable = SeekableFlag::eSeekable;
80 constexpr SeekableFlag eNotSeekable = SeekableFlag::eNotSeekable;
81 constexpr SeekableFlag kSeekableFlag_DEFAULT = SeekableFlag::eNotSeekable;
82
83 /**
84 * The constructor overload with FileDescriptorType does an 'attach' - taking ownership (and thus later closing) the argument file descriptor (depending on AdoptFDPolicy).
85 *
86 * \pre fd is a valid file descriptor (for that overload)
87 *
88 * \note We considered having a GetFD () method to retrieve the file descriptor, but that opened up too many
89 * possibilities for bugs (like changing the blocking nature of the IO). If you wish - you can always
90 * open the file descriptor yourself, track it yourself, and do what you will to it and pass it in,
91 * but then the results are 'on you.
92 *
93 * \note The overloads taking no BufferFlag produce a non-buffered stream.
94 *
95 * \par Example Usage
96 * \code
97 * IO::FileSystem::FileOutputStream::New ("/tmp/foo").Write (Memory::BLOB {0x3});
98 * \endcode
99 *
100 * \par Example Usage
101 * \code
102 * filesystem::path fileName = IO::FileSystem::WellKnownLocations::GetTemporary () / "t.txt";
103 * JSON::Writer{}.Write (v, IO::FileSystem::FileOutputStream::New (fileName));
104 * \endcode
105 *
106 * \par Example Usage
107 * \code
108 * TextToBinary::Writer::Ptr tw { TextToBinary::Writer::New (IO::FileSystem::FileOutputStream::New ("/tmp/fred.txt")) };
109 * tw.PrintF (L"Hello %s\n", L"World");
110 * \endcode
111 *
112 * \par Example Usage
113 * \code
114 * Ptr<byte> stdoutStream = FileOutputStream::New (1, FileStream::AdoptFDPolicy::eDisconnectOnDestruction);
115 * \endcode
116 *
117 * \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>
118 */
119 Ptr New (const filesystem::path& fileName, FlushFlag flushFlag = FlushFlag::eDEFAULT);
120 Ptr New (const filesystem::path& fileName, AppendFlag appendFlag, FlushFlag flushFlag = FlushFlag::eDEFAULT);
121 Ptr New (FileDescriptorType fd, AdoptFDPolicy adoptFDPolicy, SeekableFlag seekableFlag = kSeekableFlag_DEFAULT,
122 FlushFlag flushFlag = FlushFlag::eDEFAULT);
123 Ptr New (Execution::InternallySynchronized internallySynchronized, const filesystem::path& fileName, FlushFlag flushFlag = FlushFlag::eDEFAULT);
124 Ptr New (Execution::InternallySynchronized internallySynchronized, const filesystem::path& fileName, AppendFlag appendFlag,
125 FlushFlag flushFlag = FlushFlag::eDEFAULT);
126 Ptr New (Execution::InternallySynchronized internallySynchronized, FileDescriptorType fd, AdoptFDPolicy adoptFDPolicy,
127 SeekableFlag seekableFlag = kSeekableFlag_DEFAULT, FlushFlag flushFlag = FlushFlag::eDEFAULT);
128 Ptr New (const filesystem::path& fileName, FlushFlag flushFlag, BufferFlag bufferedFlag);
129 Ptr New (const filesystem::path& fileName, AppendFlag appendFlag, FlushFlag flushFlag, BufferFlag bufferedFlag);
130 Ptr New (FileDescriptorType fd, AdoptFDPolicy adoptFDPolicy, SeekableFlag seekableFlag, FlushFlag flushFlag, BufferFlag bufferedFlag);
131
132}
133
134/*
135 ********************************************************************************
136 ***************************** Implementation Details ***************************
137 ********************************************************************************
138 */
139#include "FileOutputStream.inl"
140
141#endif /*_Stroika_Foundation_IO_FileSystem_FileOutputStream_h_*/
#define Stroika_Define_Enum_Bounds(FIRST_ITEM, LAST_ITEM)
OutputStream<>::Ptr is Smart pointer to a stream-based sink of data.
A Streams::Ptr<ELEMENT_TYPE> is a smart-pointer to a stream of elements of type T.
Definition Stream.h:170