PIDUINO
Loading...
Searching...
No Matches
iodevice.h
1/* Copyright © 2018-2025 Pascal JEAN, All rights reserved.
2 This file is part of the Piduino Library.
3
4 The Piduino Library is free software; you can redistribute it and/or
5 modify it under the terms of the GNU Lesser General Public
6 License as published by the Free Software Foundation; either
7 version 3 of the License, or (at your option) any later version.
8
9 The Piduino Library is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 Lesser General Public License for more details.
13
14 You should have received a copy of the GNU Lesser General Public License
15 along with the Piduino Library; if not, see <http://www.gnu.org/licenses/>.
16*/
17#pragma once
18
19#include <piduino/memory.h>
20#include <piduino/flags.h>
21#include <piduino/global.h>
22#include <string>
23#include <ios>
24
25namespace Piduino {
26
37 class IoDevice {
38
39 public:
40
49 NotOpen = 0x0000,
50 Append = std::ios_base::app,
51 AtEnd = std::ios_base::ate,
52 Binary = std::ios_base::binary,
58 ReadOnly = std::ios_base::in,
59 WriteOnly = std::ios_base::out,
61 Truncate = std::ios_base::trunc,
63 Unbuffered = (Truncate << 1)
64 };
65
71
76
80 virtual ~IoDevice();
81
87
92 virtual bool isOpen() const;
93
98 bool isReadable() const;
99
104 bool isWritable() const;
105
110 bool isBuffered() const;
111
124 virtual bool isSequential() const;
125
130 void setTextModeEnabled (bool enabled);
131
136 bool isTextModeEnabled() const;
137
142 void setDebug (bool enabled);
143
148 bool isDebug() const;
149
155 virtual bool open (OpenMode mode);
156
160 virtual void close();
161
166 virtual std::string errorString() const;
167
172 virtual int error() const;
173
174 protected:
179 class Private;
180
186
190 std::unique_ptr<Private> d_ptr;
191
192 private:
197 };
198
199 // public:
200
201 // enum OpenModeFlag {
202 // NotOpen = 0x0000,
203 // Append = std::ios_base::app, ///< Set the stream's position indicator to the end of the stream before each output operation.
204 // AtEnd = std::ios_base::ate, ///< Set the stream's position indicator to the end of the stream on opening.
205 // Binary = std::ios_base::binary, ///< Consider stream as binary rather than text.
206 // /*
207 // Text mode
208 // When reading, the end-of-line terminators are translated to '\n'.
209 // When writing, the end-of-line terminators are translated to the
210 // local encoding, for example '\r\n' for Win32.
211 // */
212 // ReadOnly = std::ios_base::in, ///< Allow input operations
213 // WriteOnly = std::ios_base::out, ///< Allow output operations
214 // ReadWrite = ReadOnly | WriteOnly, ///< Allow input and output operations
215 // Truncate = std::ios_base::trunc, ///< Any current content is discarded, assuming a length of zero on opening.
216 // IosModes = Append | AtEnd | Binary | ReadOnly | WriteOnly | Truncate,
217 // Unbuffered = (Truncate << 1)
218 // };
219 // typedef Flags<OpenModeFlag> OpenMode;
220
221 // IoDevice ();
222 // virtual ~IoDevice();
223
224 // OpenMode openMode() const;
225 // virtual bool isOpen() const;
226 // bool isReadable() const;
227 // bool isWritable() const;
228 // bool isBuffered() const;
229 // /**
230 // Returns true if this device is sequential; otherwise returns false.
231
232 // Sequential devices, as opposed to a random-access devices, have no
233 // concept of a start, an end, a size, or a current position, and they
234 // do not support seeking. You can only read from the device when it
235 // reports that data is available. The most common example of a sequential
236 // device is a network socket. On Unix, special files such as /dev/zero
237 // and fifo pipes are sequential.
238
239 // Regular files, on the other hand, do support random access. They have
240 // both a size and a current position, and they also support seeking
241 // backwards and forwards in the data stream. Regular files are non-sequential.
242
243 // The IoDevice implementation returns false.
244 // */
245 // virtual bool isSequential() const;
246
247 // void setTextModeEnabled (bool enabled);
248 // bool isTextModeEnabled() const;
249 // void setDebug (bool enabled);
250 // bool isDebug() const;
251
252 // virtual bool open (OpenMode mode);
253 // virtual void close();
254 // virtual std::string errorString() const;
255 // virtual int error() const;
256
257 // protected:
258 // class Private;
259 // IoDevice (Private &dd);
260 // std::unique_ptr<Private> d_ptr;
261
262 // private:
263 // PIMP_DECLARE_PRIVATE (IoDevice)
264 // };
265
266} // namespace Piduino
267
268/* ========================================================================== */
A type-safe flags class for bitwise operations on enum values.
Definition flags.h:34
Abstract base class for input/output devices.
Definition iodevice.h:37
virtual bool isOpen() const
Returns true if the device is currently open.
OpenModeFlag
Flags that specify how the device is to be opened.
Definition iodevice.h:48
@ NotOpen
Device is not open.
Definition iodevice.h:49
@ AtEnd
Set the stream's position indicator to the end of the stream on opening.
Definition iodevice.h:51
@ ReadOnly
Open the device for input operations only.
Definition iodevice.h:58
@ Truncate
Discard any existing content when opening the device.
Definition iodevice.h:61
@ Unbuffered
Open the device in unbuffered mode (implementation-defined).
Definition iodevice.h:63
@ WriteOnly
Open the device for output operations only.
Definition iodevice.h:59
@ ReadWrite
Open the device for both input and output operations.
Definition iodevice.h:60
@ Binary
Open the device in binary mode (no text translation).
Definition iodevice.h:52
@ IosModes
Combination of all standard I/O modes.
Definition iodevice.h:62
@ Append
Set the stream's position indicator to the end of the stream before each output operation.
Definition iodevice.h:50
bool isWritable() const
Returns true if the device is writable.
virtual std::string errorString() const
Returns a human-readable description of the last error.
bool isDebug() const
Returns true if debug mode is enabled.
void setTextModeEnabled(bool enabled)
Enables or disables text mode for the device.
IoDevice()
Constructs a new IoDevice object.
Flags< OpenModeFlag > OpenMode
Type representing a combination of OpenModeFlag values.
Definition iodevice.h:70
OpenMode openMode() const
Returns the current open mode of the device.
std::unique_ptr< Private > d_ptr
Pointer to the private implementation.
Definition iodevice.h:190
bool isReadable() const
Returns true if the device is readable.
bool isBuffered() const
Returns true if the device is buffered.
virtual void close()
Closes the device.
void setDebug(bool enabled)
Enables or disables debug mode for the device.
virtual int error() const
Returns the code of the last error.
IoDevice(Private &dd)
Constructs an IoDevice with a given private implementation.
virtual ~IoDevice()
Destroys the IoDevice object.
bool isTextModeEnabled() const
Returns true if text mode is enabled.
virtual bool isSequential() const
Returns true if this device is sequential; otherwise returns false.
virtual bool open(OpenMode mode)
Opens the device with the specified open mode.
Internal implementation class for GpioDevice.
#define PIMP_DECLARE_PRIVATE(Class)
PIMP_DECLARE_PRIVATE.
Definition global.h:82
Global namespace for Piduino.
Definition board.h:28